1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file contains database functionality specifically designed for packages (mods) to utilize. |
||
5 | * |
||
6 | * Simple Machines Forum (SMF) |
||
7 | * |
||
8 | * @package SMF |
||
9 | * @author Simple Machines https://www.simplemachines.org |
||
10 | * @copyright 2020 Simple Machines and individual contributors |
||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1 RC2 |
||
14 | */ |
||
15 | |||
16 | if (!defined('SMF')) |
||
17 | die('No direct access...'); |
||
18 | |||
19 | /** |
||
20 | * Add the file functions to the $smcFunc array. |
||
21 | */ |
||
22 | function db_packages_init() |
||
23 | { |
||
24 | global $smcFunc, $reservedTables, $db_package_log, $db_prefix; |
||
25 | |||
26 | if (!isset($smcFunc['db_create_table']) || $smcFunc['db_create_table'] != 'smf_db_create_table') |
||
27 | { |
||
28 | $smcFunc += array( |
||
29 | 'db_add_column' => 'smf_db_add_column', |
||
30 | 'db_add_index' => 'smf_db_add_index', |
||
31 | 'db_calculate_type' => 'smf_db_calculate_type', |
||
32 | 'db_change_column' => 'smf_db_change_column', |
||
33 | 'db_create_table' => 'smf_db_create_table', |
||
34 | 'db_drop_table' => 'smf_db_drop_table', |
||
35 | 'db_table_structure' => 'smf_db_table_structure', |
||
36 | 'db_list_columns' => 'smf_db_list_columns', |
||
37 | 'db_list_indexes' => 'smf_db_list_indexes', |
||
38 | 'db_remove_column' => 'smf_db_remove_column', |
||
39 | 'db_remove_index' => 'smf_db_remove_index', |
||
40 | ); |
||
41 | $db_package_log = array(); |
||
42 | } |
||
43 | |||
44 | // We setup an array of SMF tables we can't do auto-remove on - in case a mod writer cocks it up! |
||
45 | $reservedTables = array( |
||
46 | 'admin_info_files', 'approval_queue', 'attachments', |
||
47 | 'background_tasks', 'ban_groups', 'ban_items', 'board_permissions', |
||
48 | 'board_permissions_view', 'boards', 'calendar', 'calendar_holidays', |
||
49 | 'categories', 'custom_fields', 'group_moderators', 'log_actions', |
||
50 | 'log_activity', 'log_banned', 'log_boards', 'log_comments', |
||
51 | 'log_digest', 'log_errors', 'log_floodcontrol', 'log_group_requests', |
||
52 | 'log_mark_read', 'log_member_notices', 'log_notify', 'log_online', |
||
53 | 'log_packages', 'log_polls', 'log_reported', 'log_reported_comments', |
||
54 | 'log_scheduled_tasks', 'log_search_messages', 'log_search_results', |
||
55 | 'log_search_subjects', 'log_search_topics', 'log_spider_hits', |
||
56 | 'log_spider_stats', 'log_subscribed', 'log_topics', 'mail_queue', |
||
57 | 'member_logins', 'membergroups', 'members', 'mentions', |
||
58 | 'message_icons', 'messages', 'moderator_groups', 'moderators', |
||
59 | 'package_servers', 'permission_profiles', 'permissions', |
||
60 | 'personal_messages', 'pm_labeled_messages', 'pm_labels', |
||
61 | 'pm_recipients', 'pm_rules', 'poll_choices', 'polls', 'qanda', |
||
62 | 'scheduled_tasks', 'sessions', 'settings', 'smiley_files', 'smileys', |
||
63 | 'spiders', 'subscriptions', 'themes', 'topics', 'user_alerts', |
||
64 | 'user_alerts_prefs', 'user_drafts', 'user_likes', |
||
65 | ); |
||
66 | foreach ($reservedTables as $k => $table_name) |
||
67 | $reservedTables[$k] = strtolower($db_prefix . $table_name); |
||
68 | |||
69 | // We in turn may need the extra stuff. |
||
70 | db_extend('extra'); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * This function can be used to create a table without worrying about schema |
||
75 | * compatibilities across supported database systems. |
||
76 | * - If the table exists will, by default, do nothing. |
||
77 | * - Builds table with columns as passed to it - at least one column must be sent. |
||
78 | * The columns array should have one sub-array for each column - these sub arrays contain: |
||
79 | * 'name' = Column name |
||
80 | * 'type' = Type of column - values from (smallint, mediumint, int, text, varchar, char, tinytext, mediumtext, largetext) |
||
81 | * 'size' => Size of column (If applicable) - for example 255 for a large varchar, 10 for an int etc. |
||
82 | * If not set SMF will pick a size. |
||
83 | * - 'default' = Default value - do not set if no default required. |
||
84 | * - 'null' => Can it be null (true or false) - if not set default will be false. |
||
85 | * - 'auto' => Set to true to make it an auto incrementing column. Set to a numerical value to set from what |
||
86 | * it should begin counting. |
||
87 | * - Adds indexes as specified within indexes parameter. Each index should be a member of $indexes. Values are: |
||
88 | * - 'name' => Index name (If left empty SMF will generate). |
||
89 | * - 'type' => Type of index. Choose from 'primary', 'unique' or 'index'. If not set will default to 'index'. |
||
90 | * - 'columns' => Array containing columns that form part of key - in the order the index is to be created. |
||
91 | * - parameters: (None yet) |
||
92 | * - if_exists values: |
||
93 | * - 'ignore' will do nothing if the table exists. (And will return true) |
||
94 | * - 'overwrite' will drop any existing table of the same name. |
||
95 | * - 'error' will return false if the table already exists. |
||
96 | * - 'update' will update the table if the table already exists (no change of ai field and only colums with the same name keep the data) |
||
97 | * |
||
98 | * @param string $table_name The name of the table to create |
||
99 | * @param array $columns An array of column info in the specified format |
||
100 | * @param array $indexes An array of index info in the specified format |
||
101 | * @param array $parameters Currently not used |
||
102 | * @param string $if_exists What to do if the table exists. |
||
103 | * @param string $error |
||
104 | */ |
||
105 | function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal') |
||
106 | { |
||
107 | global $reservedTables, $smcFunc, $db_package_log, $db_prefix; |
||
108 | |||
109 | $db_trans = false; |
||
110 | $old_table_exists = false; |
||
111 | |||
112 | // Strip out the table name, we might not need it in some cases |
||
113 | $real_prefix = preg_match('~^("?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; |
||
114 | |||
115 | // With or without the database name, the fullname looks like this. |
||
116 | $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); |
||
117 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
118 | |||
119 | // First - no way do we touch SMF tables. |
||
120 | if (in_array(strtolower($table_name), $reservedTables)) |
||
121 | return false; |
||
122 | |||
123 | // Log that we'll want to remove this on uninstall. |
||
124 | $db_package_log[] = array('remove_table', $table_name); |
||
125 | |||
126 | // This... my friends... is a function in a half - let's start by checking if the table exists! |
||
127 | $tables = $smcFunc['db_list_tables'](); |
||
128 | if (in_array($full_table_name, $tables)) |
||
129 | { |
||
130 | // This is a sad day... drop the table? If not, return false (error) by default. |
||
131 | if ($if_exists == 'overwrite') |
||
132 | $smcFunc['db_drop_table']($table_name); |
||
133 | elseif ($if_exists == 'update') |
||
134 | { |
||
135 | $smcFunc['db_drop_table']($table_name . '_old'); |
||
136 | $smcFunc['db_transaction']('begin'); |
||
137 | $db_trans = true; |
||
138 | $smcFunc['db_query']('', ' |
||
139 | ALTER TABLE ' . $table_name . ' RENAME TO ' . $table_name . '_old', |
||
140 | array( |
||
141 | 'security_override' => true, |
||
142 | ) |
||
143 | ); |
||
144 | $old_table_exists = true; |
||
145 | } |
||
146 | else |
||
147 | return $if_exists == 'ignore'; |
||
148 | } |
||
149 | |||
150 | // If we've got this far - good news - no table exists. We can build our own! |
||
151 | if (!$db_trans) |
||
152 | $smcFunc['db_transaction']('begin'); |
||
153 | $table_query = 'CREATE TABLE ' . $table_name . "\n" . '('; |
||
154 | foreach ($columns as $column) |
||
155 | { |
||
156 | // If we have an auto increment do it! |
||
157 | if (!empty($column['auto'])) |
||
158 | { |
||
159 | if (!$old_table_exists) |
||
160 | $smcFunc['db_query']('', ' |
||
161 | DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
||
162 | array( |
||
163 | 'security_override' => true, |
||
164 | ) |
||
165 | ); |
||
166 | |||
167 | if (!$old_table_exists) |
||
168 | $smcFunc['db_query']('', ' |
||
169 | CREATE SEQUENCE ' . $table_name . '_seq', |
||
170 | array( |
||
171 | 'security_override' => true, |
||
172 | ) |
||
173 | ); |
||
174 | $default = 'default nextval(\'' . $table_name . '_seq\')'; |
||
175 | } |
||
176 | elseif (isset($column['default']) && $column['default'] !== null) |
||
177 | $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\''; |
||
178 | else |
||
179 | $default = ''; |
||
180 | |||
181 | // Sort out the size... |
||
182 | $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null; |
||
183 | list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']); |
||
184 | if ($size !== null) |
||
185 | $type = $type . '(' . $size . ')'; |
||
186 | |||
187 | // Now just put it together! |
||
188 | $table_query .= "\n\t\"" . $column['name'] . '" ' . $type . ' ' . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default . ','; |
||
189 | } |
||
190 | |||
191 | // Loop through the indexes a sec... |
||
192 | $index_queries = array(); |
||
193 | foreach ($indexes as $index) |
||
194 | { |
||
195 | $columns = implode(',', $index['columns']); |
||
196 | |||
197 | // Primary goes in the table... |
||
198 | if (isset($index['type']) && $index['type'] == 'primary') |
||
199 | $table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),'; |
||
200 | else |
||
201 | { |
||
202 | if (empty($index['name'])) |
||
203 | $index['name'] = implode('_', $index['columns']); |
||
204 | $index_queries[] = 'CREATE ' . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : '') . ' INDEX ' . $table_name . '_' . $index['name'] . ' ON ' . $table_name . ' (' . $columns . ')'; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | // No trailing commas! |
||
209 | if (substr($table_query, -1) == ',') |
||
210 | $table_query = substr($table_query, 0, -1); |
||
211 | |||
212 | $table_query .= ')'; |
||
213 | |||
214 | // Create the table! |
||
215 | $smcFunc['db_query']('', $table_query, |
||
216 | array( |
||
217 | 'security_override' => true, |
||
218 | ) |
||
219 | ); |
||
220 | |||
221 | // Fill the old data |
||
222 | if ($old_table_exists) |
||
223 | { |
||
224 | $same_col = array(); |
||
225 | |||
226 | $request = $smcFunc['db_query']('', ' |
||
227 | SELECT count(*), column_name |
||
228 | FROM information_schema.columns |
||
229 | WHERE table_name in ({string:table1},{string:table2}) AND table_schema = {string:schema} |
||
230 | GROUP BY column_name |
||
231 | HAVING count(*) > 1', |
||
232 | array( |
||
233 | 'table1' => $table_name, |
||
234 | 'table2' => $table_name . '_old', |
||
235 | 'schema' => 'public', |
||
236 | ) |
||
237 | ); |
||
238 | |||
239 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
240 | { |
||
241 | $same_col[] = $row['column_name']; |
||
242 | } |
||
243 | |||
244 | $smcFunc['db_query']('', ' |
||
245 | INSERT INTO ' . $table_name . '(' |
||
246 | . implode($same_col, ',') . |
||
0 ignored issues
–
show
|
|||
247 | ') |
||
248 | SELECT ' . implode($same_col, ',') . ' |
||
249 | FROM ' . $table_name . '_old', |
||
250 | array() |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | // And the indexes... |
||
255 | foreach ($index_queries as $query) |
||
256 | $smcFunc['db_query']('', $query, |
||
257 | array( |
||
258 | 'security_override' => true, |
||
259 | ) |
||
260 | ); |
||
261 | |||
262 | // Go, go power rangers! |
||
263 | $smcFunc['db_transaction']('commit'); |
||
264 | |||
265 | if ($old_table_exists) |
||
266 | $smcFunc['db_drop_table']($table_name . '_old'); |
||
267 | |||
268 | return true; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Drop a table and its associated sequences. |
||
273 | * |
||
274 | * @param string $table_name The name of the table to drop |
||
275 | * @param array $parameters Not used at the moment |
||
276 | * @param string $error |
||
277 | * @return boolean Whether or not the operation was successful |
||
278 | */ |
||
279 | function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal') |
||
280 | { |
||
281 | global $reservedTables, $smcFunc, $db_prefix; |
||
282 | |||
283 | // After stripping away the database name, this is what's left. |
||
284 | $real_prefix = preg_match('~^("?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; |
||
285 | |||
286 | // Get some aliases. |
||
287 | $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); |
||
288 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
289 | |||
290 | // God no - dropping one of these = bad. |
||
291 | if (in_array(strtolower($table_name), $reservedTables)) |
||
292 | return false; |
||
293 | |||
294 | // Does it exist? |
||
295 | if (in_array($full_table_name, $smcFunc['db_list_tables']())) |
||
296 | { |
||
297 | // We can then drop the table. |
||
298 | $smcFunc['db_transaction']('begin'); |
||
299 | |||
300 | // the table |
||
301 | $table_query = 'DROP TABLE ' . $table_name; |
||
302 | |||
303 | // and the assosciated sequence, if any |
||
304 | $sequence_query = 'DROP SEQUENCE IF EXISTS ' . $table_name . '_seq'; |
||
305 | |||
306 | // drop them |
||
307 | $smcFunc['db_query']('', |
||
308 | $table_query, |
||
309 | array( |
||
310 | 'security_override' => true, |
||
311 | ) |
||
312 | ); |
||
313 | $smcFunc['db_query']('', |
||
314 | $sequence_query, |
||
315 | array( |
||
316 | 'security_override' => true, |
||
317 | ) |
||
318 | ); |
||
319 | |||
320 | $smcFunc['db_transaction']('commit'); |
||
321 | |||
322 | return true; |
||
323 | } |
||
324 | |||
325 | // Otherwise do 'nout. |
||
326 | return false; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * This function adds a column. |
||
331 | * |
||
332 | * @param string $table_name The name of the table to add the column to |
||
333 | * @param array $column_info An array of column info (see {@link smf_db_create_table()}) |
||
334 | * @param array $parameters Not used? |
||
335 | * @param string $if_exists What to do if the column exists. If 'update', column is updated. |
||
336 | * @param string $error |
||
337 | * @return boolean Whether or not the operation was successful |
||
338 | */ |
||
339 | function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') |
||
340 | { |
||
341 | global $smcFunc, $db_package_log, $db_prefix; |
||
342 | |||
343 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
344 | |||
345 | // Log that we will want to uninstall this! |
||
346 | $db_package_log[] = array('remove_column', $table_name, $column_info['name']); |
||
347 | |||
348 | // Does it exist - if so don't add it again! |
||
349 | $columns = $smcFunc['db_list_columns']($table_name, false); |
||
350 | foreach ($columns as $column) |
||
351 | if ($column == $column_info['name']) |
||
352 | { |
||
353 | // If we're going to overwrite then use change column. |
||
354 | if ($if_exists == 'update') |
||
355 | return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info); |
||
356 | else |
||
357 | return false; |
||
358 | } |
||
359 | |||
360 | // Get the specifics... |
||
361 | $column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null; |
||
362 | list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']); |
||
363 | if ($size !== null) |
||
364 | $type = $type . '(' . $size . ')'; |
||
365 | |||
366 | // Now add the thing! |
||
367 | $query = ' |
||
368 | ALTER TABLE ' . $table_name . ' |
||
369 | ADD COLUMN ' . $column_info['name'] . ' ' . $type; |
||
370 | $smcFunc['db_query']('', $query, |
||
371 | array( |
||
372 | 'security_override' => true, |
||
373 | ) |
||
374 | ); |
||
375 | |||
376 | // If there's more attributes they need to be done via a change on PostgreSQL. |
||
377 | unset($column_info['type'], $column_info['size']); |
||
378 | |||
379 | if (count($column_info) != 1) |
||
380 | return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info); |
||
381 | else |
||
382 | return true; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Removes a column. |
||
387 | * |
||
388 | * @param string $table_name The name of the table to drop the column from |
||
389 | * @param string $column_name The name of the column to drop |
||
390 | * @param array $parameters Not used? |
||
391 | * @param string $error |
||
392 | * @return boolean Whether or not the operation was successful |
||
393 | */ |
||
394 | function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal') |
||
395 | { |
||
396 | global $smcFunc, $db_prefix; |
||
397 | |||
398 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
399 | |||
400 | // Does it exist? |
||
401 | $columns = $smcFunc['db_list_columns']($table_name, true); |
||
402 | foreach ($columns as $column) |
||
403 | if ($column['name'] == $column_name) |
||
404 | { |
||
405 | // If there is an auto we need remove it! |
||
406 | if ($column['auto']) |
||
407 | $smcFunc['db_query']('', ' |
||
408 | DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
||
409 | array( |
||
410 | 'security_override' => true, |
||
411 | ) |
||
412 | ); |
||
413 | |||
414 | $smcFunc['db_query']('', ' |
||
415 | ALTER TABLE ' . $table_name . ' |
||
416 | DROP COLUMN ' . $column_name, |
||
417 | array( |
||
418 | 'security_override' => true, |
||
419 | ) |
||
420 | ); |
||
421 | |||
422 | return true; |
||
423 | } |
||
424 | |||
425 | // If here we didn't have to work - joy! |
||
426 | return false; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Change a column. |
||
431 | * |
||
432 | * @param string $table_name The name of the table this column is in |
||
433 | * @param string $old_column The name of the column we want to change |
||
434 | * @param array $column_info An array of info about the "new" column definition (see {@link smf_db_create_table()}) |
||
435 | * @return bool |
||
436 | */ |
||
437 | function smf_db_change_column($table_name, $old_column, $column_info) |
||
438 | { |
||
439 | global $smcFunc, $db_prefix; |
||
440 | |||
441 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
442 | |||
443 | // Check it does exist! |
||
444 | $columns = $smcFunc['db_list_columns']($table_name, true); |
||
445 | $old_info = null; |
||
446 | foreach ($columns as $column) |
||
447 | if ($column['name'] == $old_column) |
||
448 | $old_info = $column; |
||
449 | |||
450 | // Nothing? |
||
451 | if ($old_info == null) |
||
452 | return false; |
||
453 | |||
454 | // Now we check each bit individually and ALTER as required. |
||
455 | if (isset($column_info['name']) && $column_info['name'] != $old_column) |
||
456 | { |
||
457 | $smcFunc['db_query']('', ' |
||
458 | ALTER TABLE ' . $table_name . ' |
||
459 | RENAME COLUMN ' . $old_column . ' TO ' . $column_info['name'], |
||
460 | array( |
||
461 | 'security_override' => true, |
||
462 | ) |
||
463 | ); |
||
464 | } |
||
465 | // Different default? |
||
466 | if (isset($column_info['default']) && $column_info['default'] != $old_info['default']) |
||
467 | { |
||
468 | $action = $column_info['default'] !== null ? 'SET DEFAULT \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'' : 'DROP DEFAULT'; |
||
469 | $smcFunc['db_query']('', ' |
||
470 | ALTER TABLE ' . $table_name . ' |
||
471 | ALTER COLUMN ' . $column_info['name'] . ' ' . $action, |
||
472 | array( |
||
473 | 'security_override' => true, |
||
474 | ) |
||
475 | ); |
||
476 | } |
||
477 | // Is it null - or otherwise? |
||
478 | if (isset($column_info['null']) && $column_info['null'] != $old_info['null']) |
||
479 | { |
||
480 | $action = $column_info['null'] ? 'DROP' : 'SET'; |
||
481 | $smcFunc['db_transaction']('begin'); |
||
482 | if (!$column_info['null']) |
||
483 | { |
||
484 | // We have to set it to something if we are making it NOT NULL. And we must comply with the current column format. |
||
485 | $setTo = isset($column_info['default']) ? $column_info['default'] : (strpos($old_info['type'], 'int') !== false ? 0 : ''); |
||
486 | $smcFunc['db_query']('', ' |
||
487 | UPDATE ' . $table_name . ' |
||
488 | SET ' . $column_info['name'] . ' = \'' . $setTo . '\' |
||
489 | WHERE ' . $column_info['name'] . ' IS NULL', |
||
490 | array( |
||
491 | 'security_override' => true, |
||
492 | ) |
||
493 | ); |
||
494 | } |
||
495 | $smcFunc['db_query']('', ' |
||
496 | ALTER TABLE ' . $table_name . ' |
||
497 | ALTER COLUMN ' . $column_info['name'] . ' ' . $action . ' NOT NULL', |
||
498 | array( |
||
499 | 'security_override' => true, |
||
500 | ) |
||
501 | ); |
||
502 | $smcFunc['db_transaction']('commit'); |
||
503 | } |
||
504 | // What about a change in type? |
||
505 | if (isset($column_info['type']) && ($column_info['type'] != $old_info['type'] || (isset($column_info['size']) && $column_info['size'] != $old_info['size']))) |
||
506 | { |
||
507 | $column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null; |
||
508 | list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']); |
||
509 | if ($size !== null) |
||
510 | $type = $type . '(' . $size . ')'; |
||
511 | |||
512 | // The alter is a pain. |
||
513 | $smcFunc['db_transaction']('begin'); |
||
514 | $smcFunc['db_query']('', ' |
||
515 | ALTER TABLE ' . $table_name . ' |
||
516 | ADD COLUMN ' . $column_info['name'] . '_tempxx ' . $type, |
||
517 | array( |
||
518 | 'security_override' => true, |
||
519 | ) |
||
520 | ); |
||
521 | $smcFunc['db_query']('', ' |
||
522 | UPDATE ' . $table_name . ' |
||
523 | SET ' . $column_info['name'] . '_tempxx = CAST(' . $column_info['name'] . ' AS ' . $type . ')', |
||
524 | array( |
||
525 | 'security_override' => true, |
||
526 | ) |
||
527 | ); |
||
528 | $smcFunc['db_query']('', ' |
||
529 | ALTER TABLE ' . $table_name . ' |
||
530 | DROP COLUMN ' . $column_info['name'], |
||
531 | array( |
||
532 | 'security_override' => true, |
||
533 | ) |
||
534 | ); |
||
535 | $smcFunc['db_query']('', ' |
||
536 | ALTER TABLE ' . $table_name . ' |
||
537 | RENAME COLUMN ' . $column_info['name'] . '_tempxx TO ' . $column_info['name'], |
||
538 | array( |
||
539 | 'security_override' => true, |
||
540 | ) |
||
541 | ); |
||
542 | $smcFunc['db_transaction']('commit'); |
||
543 | } |
||
544 | // Finally - auto increment?! |
||
545 | if (isset($column_info['auto']) && $column_info['auto'] != $old_info['auto']) |
||
546 | { |
||
547 | // Are we removing an old one? |
||
548 | if ($old_info['auto']) |
||
549 | { |
||
550 | // Alter the table first - then drop the sequence. |
||
551 | $smcFunc['db_query']('', ' |
||
552 | ALTER TABLE ' . $table_name . ' |
||
553 | ALTER COLUMN ' . $column_info['name'] . ' SET DEFAULT \'0\'', |
||
554 | array( |
||
555 | 'security_override' => true, |
||
556 | ) |
||
557 | ); |
||
558 | $smcFunc['db_query']('', ' |
||
559 | DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
||
560 | array( |
||
561 | 'security_override' => true, |
||
562 | ) |
||
563 | ); |
||
564 | } |
||
565 | // Otherwise add it! |
||
566 | else |
||
567 | { |
||
568 | $smcFunc['db_query']('', ' |
||
569 | DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
||
570 | array( |
||
571 | 'security_override' => true, |
||
572 | ) |
||
573 | ); |
||
574 | |||
575 | $smcFunc['db_query']('', ' |
||
576 | CREATE SEQUENCE ' . $table_name . '_seq', |
||
577 | array( |
||
578 | 'security_override' => true, |
||
579 | ) |
||
580 | ); |
||
581 | $smcFunc['db_query']('', ' |
||
582 | ALTER TABLE ' . $table_name . ' |
||
583 | ALTER COLUMN ' . $column_info['name'] . ' SET DEFAULT nextval(\'' . $table_name . '_seq\')', |
||
584 | array( |
||
585 | 'security_override' => true, |
||
586 | ) |
||
587 | ); |
||
588 | } |
||
589 | } |
||
590 | |||
591 | return true; |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * Add an index. |
||
596 | * |
||
597 | * @param string $table_name The name of the table to add the index to |
||
598 | * @param array $index_info An array of index info (see {@link smf_db_create_table()}) |
||
599 | * @param array $parameters Not used? |
||
600 | * @param string $if_exists What to do if the index exists. If 'update', the definition will be updated. |
||
601 | * @param string $error |
||
602 | * @return boolean Whether or not the operation was successful |
||
603 | */ |
||
604 | function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') |
||
605 | { |
||
606 | global $smcFunc, $db_package_log, $db_prefix; |
||
607 | |||
608 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
609 | |||
610 | // No columns = no index. |
||
611 | if (empty($index_info['columns'])) |
||
612 | return false; |
||
613 | $columns = implode(',', $index_info['columns']); |
||
614 | |||
615 | // No name - make it up! |
||
616 | if (empty($index_info['name'])) |
||
617 | { |
||
618 | // No need for primary. |
||
619 | if (isset($index_info['type']) && $index_info['type'] == 'primary') |
||
620 | $index_info['name'] = ''; |
||
621 | else |
||
622 | $index_info['name'] = $table_name . implode('_', $index_info['columns']); |
||
623 | } |
||
624 | else |
||
625 | $index_info['name'] = $table_name . $index_info['name']; |
||
626 | |||
627 | // Log that we are going to want to remove this! |
||
628 | $db_package_log[] = array('remove_index', $table_name, $index_info['name']); |
||
629 | |||
630 | // Let's get all our indexes. |
||
631 | $indexes = $smcFunc['db_list_indexes']($table_name, true); |
||
632 | // Do we already have it? |
||
633 | foreach ($indexes as $index) |
||
634 | { |
||
635 | if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary')) |
||
636 | { |
||
637 | // If we want to overwrite simply remove the current one then continue. |
||
638 | if ($if_exists != 'update' || $index['type'] == 'primary') |
||
639 | return false; |
||
640 | else |
||
641 | $smcFunc['db_remove_index']($table_name, $index_info['name']); |
||
642 | } |
||
643 | } |
||
644 | |||
645 | // If we're here we know we don't have the index - so just add it. |
||
646 | if (!empty($index_info['type']) && $index_info['type'] == 'primary') |
||
647 | { |
||
648 | $smcFunc['db_query']('', ' |
||
649 | ALTER TABLE ' . $table_name . ' |
||
650 | ADD PRIMARY KEY (' . $columns . ')', |
||
651 | array( |
||
652 | 'security_override' => true, |
||
653 | ) |
||
654 | ); |
||
655 | } |
||
656 | else |
||
657 | { |
||
658 | $smcFunc['db_query']('', ' |
||
659 | CREATE ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : '') . ' INDEX ' . $index_info['name'] . ' ON ' . $table_name . ' (' . $columns . ')', |
||
660 | array( |
||
661 | 'security_override' => true, |
||
662 | ) |
||
663 | ); |
||
664 | } |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * Remove an index. |
||
669 | * |
||
670 | * @param string $table_name The name of the table to remove the index from |
||
671 | * @param string $index_name The name of the index to remove |
||
672 | * @param array $parameters Not used? |
||
673 | * @param string $error |
||
674 | * @return boolean Whether or not the operation was successful |
||
675 | */ |
||
676 | function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal') |
||
677 | { |
||
678 | global $smcFunc, $db_prefix; |
||
679 | |||
680 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
681 | |||
682 | // Better exist! |
||
683 | $indexes = $smcFunc['db_list_indexes']($table_name, true); |
||
684 | if ($index_name != 'primary') |
||
685 | $index_name = $table_name . '_' . $index_name; |
||
686 | |||
687 | foreach ($indexes as $index) |
||
688 | { |
||
689 | // If the name is primary we want the primary key! |
||
690 | if ($index['type'] == 'primary' && $index_name == 'primary') |
||
691 | { |
||
692 | // Dropping primary key is odd... |
||
693 | $smcFunc['db_query']('', ' |
||
694 | ALTER TABLE ' . $table_name . ' |
||
695 | DROP CONSTRAINT ' . $index['name'], |
||
696 | array( |
||
697 | 'security_override' => true, |
||
698 | ) |
||
699 | ); |
||
700 | |||
701 | return true; |
||
702 | } |
||
703 | if ($index['name'] == $index_name) |
||
704 | { |
||
705 | // Drop the bugger... |
||
706 | $smcFunc['db_query']('', ' |
||
707 | DROP INDEX ' . $index_name, |
||
708 | array( |
||
709 | 'security_override' => true, |
||
710 | ) |
||
711 | ); |
||
712 | |||
713 | return true; |
||
714 | } |
||
715 | } |
||
716 | |||
717 | // Not to be found ;( |
||
718 | return false; |
||
719 | } |
||
720 | |||
721 | /** |
||
722 | * Get the schema formatted name for a type. |
||
723 | * |
||
724 | * @param string $type_name The data type (int, varchar, smallint, etc.) |
||
725 | * @param int $type_size The size (8, 255, etc.) |
||
726 | * @param boolean $reverse If true, returns specific types for a generic type |
||
727 | * @return array An array containing the appropriate type and size for this DB type |
||
728 | */ |
||
729 | function smf_db_calculate_type($type_name, $type_size = null, $reverse = false) |
||
730 | { |
||
731 | // Let's be sure it's lowercase MySQL likes both, others no. |
||
732 | $type_name = strtolower($type_name); |
||
733 | // Generic => Specific. |
||
734 | if (!$reverse) |
||
735 | { |
||
736 | $types = array( |
||
737 | 'varchar' => 'character varying', |
||
738 | 'char' => 'character', |
||
739 | 'mediumint' => 'int', |
||
740 | 'tinyint' => 'smallint', |
||
741 | 'tinytext' => 'character varying', |
||
742 | 'mediumtext' => 'text', |
||
743 | 'largetext' => 'text', |
||
744 | 'inet' => 'inet', |
||
745 | 'time' => 'time without time zone', |
||
746 | 'datetime' => 'timestamp without time zone', |
||
747 | 'timestamp' => 'timestamp without time zone', |
||
748 | ); |
||
749 | } |
||
750 | else |
||
751 | { |
||
752 | $types = array( |
||
753 | 'character varying' => 'varchar', |
||
754 | 'character' => 'char', |
||
755 | 'integer' => 'int', |
||
756 | 'inet' => 'inet', |
||
757 | 'time without time zone' => 'time', |
||
758 | 'timestamp without time zone' => 'datetime', |
||
759 | 'numeric' => 'decimal', |
||
760 | ); |
||
761 | } |
||
762 | |||
763 | // Got it? Change it! |
||
764 | if (isset($types[$type_name])) |
||
765 | { |
||
766 | if ($type_name == 'tinytext') |
||
767 | $type_size = 255; |
||
768 | $type_name = $types[$type_name]; |
||
769 | } |
||
770 | |||
771 | // Only char fields got size |
||
772 | if (strpos($type_name, 'char') === false) |
||
773 | $type_size = null; |
||
774 | |||
775 | return array($type_name, $type_size); |
||
776 | } |
||
777 | |||
778 | /** |
||
779 | * Get table structure. |
||
780 | * |
||
781 | * @param string $table_name The name of the table |
||
782 | * @return array An array of table structure - the name, the column info from {@link smf_db_list_columns()} and the index info from {@link smf_db_list_indexes()} |
||
783 | */ |
||
784 | function smf_db_table_structure($table_name) |
||
785 | { |
||
786 | global $smcFunc, $db_prefix; |
||
787 | |||
788 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
789 | |||
790 | return array( |
||
791 | 'name' => $table_name, |
||
792 | 'columns' => $smcFunc['db_list_columns']($table_name, true), |
||
793 | 'indexes' => $smcFunc['db_list_indexes']($table_name, true), |
||
794 | ); |
||
795 | } |
||
796 | |||
797 | /** |
||
798 | * Return column information for a table. |
||
799 | * |
||
800 | * @param string $table_name The name of the table to get column info for |
||
801 | * @param bool $detail Whether or not to return detailed info. If true, returns the column info. If false, just returns the column names. |
||
802 | * @param array $parameters Not used? |
||
803 | * @return array An array of column names or detailed column info, depending on $detail |
||
804 | */ |
||
805 | function smf_db_list_columns($table_name, $detail = false, $parameters = array()) |
||
806 | { |
||
807 | global $smcFunc, $db_prefix; |
||
808 | |||
809 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
810 | |||
811 | $result = $smcFunc['db_query']('', ' |
||
812 | SELECT column_name, column_default, is_nullable, data_type, character_maximum_length |
||
813 | FROM information_schema.columns |
||
814 | WHERE table_schema = {string:schema_public} |
||
815 | AND table_name = {string:table_name} |
||
816 | ORDER BY ordinal_position', |
||
817 | array( |
||
818 | 'schema_public' => 'public', |
||
819 | 'table_name' => $table_name, |
||
820 | ) |
||
821 | ); |
||
822 | $columns = array(); |
||
823 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
824 | { |
||
825 | if (!$detail) |
||
826 | { |
||
827 | $columns[] = $row['column_name']; |
||
828 | } |
||
829 | else |
||
830 | { |
||
831 | $auto = false; |
||
832 | // What is the default? |
||
833 | if (preg_match('~nextval\(\'(.+?)\'(.+?)*\)~i', $row['column_default'], $matches) != 0) |
||
834 | { |
||
835 | $default = null; |
||
836 | $auto = true; |
||
837 | } |
||
838 | elseif (trim($row['column_default']) != '') |
||
839 | $default = strpos($row['column_default'], '::') === false ? $row['column_default'] : substr($row['column_default'], 0, strpos($row['column_default'], '::')); |
||
840 | else |
||
841 | $default = null; |
||
842 | |||
843 | // Make the type generic. |
||
844 | list ($type, $size) = $smcFunc['db_calculate_type']($row['data_type'], $row['character_maximum_length'], true); |
||
845 | |||
846 | $columns[$row['column_name']] = array( |
||
847 | 'name' => $row['column_name'], |
||
848 | 'null' => $row['is_nullable'] ? true : false, |
||
849 | 'default' => $default, |
||
850 | 'type' => $type, |
||
851 | 'size' => $size, |
||
852 | 'auto' => $auto, |
||
853 | ); |
||
854 | } |
||
855 | } |
||
856 | $smcFunc['db_free_result']($result); |
||
857 | |||
858 | return $columns; |
||
859 | } |
||
860 | |||
861 | /** |
||
862 | * Get index information. |
||
863 | * |
||
864 | * @param string $table_name The name of the table to get indexes for |
||
865 | * @param bool $detail Whether or not to return detailed info. |
||
866 | * @param array $parameters Not used? |
||
867 | * @return array An array of index names or a detailed array of index info, depending on $detail |
||
868 | */ |
||
869 | function smf_db_list_indexes($table_name, $detail = false, $parameters = array()) |
||
870 | { |
||
871 | global $smcFunc, $db_prefix; |
||
872 | |||
873 | $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
||
874 | |||
875 | $result = $smcFunc['db_query']('', ' |
||
876 | SELECT CASE WHEN i.indisprimary THEN 1 ELSE 0 END AS is_primary, |
||
877 | CASE WHEN i.indisunique THEN 1 ELSE 0 END AS is_unique, |
||
878 | c2.relname AS name, |
||
879 | pg_get_indexdef(i.indexrelid) AS inddef |
||
880 | FROM pg_class AS c, pg_class AS c2, pg_index AS i |
||
881 | WHERE c.relname = {string:table_name} |
||
882 | AND c.oid = i.indrelid |
||
883 | AND i.indexrelid = c2.oid', |
||
884 | array( |
||
885 | 'table_name' => $table_name, |
||
886 | ) |
||
887 | ); |
||
888 | $indexes = array(); |
||
889 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
890 | { |
||
891 | // Try get the columns that make it up. |
||
892 | if (preg_match('~\(([^\)]+?)\)~i', $row['inddef'], $matches) == 0) |
||
893 | continue; |
||
894 | |||
895 | $columns = explode(',', $matches[1]); |
||
896 | |||
897 | if (empty($columns)) |
||
898 | continue; |
||
899 | |||
900 | foreach ($columns as $k => $v) |
||
901 | $columns[$k] = trim($v); |
||
902 | |||
903 | // Fix up the name to be consistent cross databases |
||
904 | if (substr($row['name'], -5) == '_pkey' && $row['is_primary'] == 1) |
||
905 | $row['name'] = 'PRIMARY'; |
||
906 | else |
||
907 | $row['name'] = str_replace($table_name . '_', '', $row['name']); |
||
908 | |||
909 | if (!$detail) |
||
910 | $indexes[] = $row['name']; |
||
911 | else |
||
912 | { |
||
913 | $indexes[$row['name']] = array( |
||
914 | 'name' => $row['name'], |
||
915 | 'type' => $row['is_primary'] ? 'primary' : ($row['is_unique'] ? 'unique' : 'index'), |
||
916 | 'columns' => $columns, |
||
917 | ); |
||
918 | } |
||
919 | } |
||
920 | $smcFunc['db_free_result']($result); |
||
921 | |||
922 | return $indexes; |
||
923 | } |
||
924 | |||
925 | ?> |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.