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 2022 Simple Machines and individual contributors |
11
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
12
|
|
|
* |
13
|
|
|
* @version 2.1.0 |
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
|
|
|
* - 'not_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
|
|
|
// backward compatibility |
188
|
|
|
if (isset($column['null'])) |
189
|
|
|
$column['not_null'] != $column['null']; |
190
|
|
|
|
191
|
|
|
// Now just put it together! |
192
|
|
|
$table_query .= "\n\t\"" . $column['name'] . '" ' . $type . ' ' . (!empty($column['not_null']) ? 'NOT NULL' : '') . ' ' . $default . ','; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Loop through the indexes a sec... |
196
|
|
|
$index_queries = array(); |
197
|
|
|
foreach ($indexes as $index) |
198
|
|
|
{ |
199
|
|
|
$columns = implode(',', $index['columns']); |
200
|
|
|
|
201
|
|
|
// Primary goes in the table... |
202
|
|
|
if (isset($index['type']) && $index['type'] == 'primary') |
203
|
|
|
$table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),'; |
204
|
|
|
else |
205
|
|
|
{ |
206
|
|
|
if (empty($index['name'])) |
207
|
|
|
$index['name'] = implode('_', $index['columns']); |
208
|
|
|
$index_queries[] = 'CREATE ' . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : '') . ' INDEX ' . $table_name . '_' . $index['name'] . ' ON ' . $table_name . ' (' . $columns . ')'; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// No trailing commas! |
213
|
|
|
if (substr($table_query, -1) == ',') |
214
|
|
|
$table_query = substr($table_query, 0, -1); |
215
|
|
|
|
216
|
|
|
$table_query .= ')'; |
217
|
|
|
|
218
|
|
|
// Create the table! |
219
|
|
|
$smcFunc['db_query']('', $table_query, |
220
|
|
|
array( |
221
|
|
|
'security_override' => true, |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
// Fill the old data |
226
|
|
|
if ($old_table_exists) |
227
|
|
|
{ |
228
|
|
|
$same_col = array(); |
229
|
|
|
|
230
|
|
|
$request = $smcFunc['db_query']('', ' |
231
|
|
|
SELECT count(*), column_name |
232
|
|
|
FROM information_schema.columns |
233
|
|
|
WHERE table_name in ({string:table1},{string:table2}) AND table_schema = {string:schema} |
234
|
|
|
GROUP BY column_name |
235
|
|
|
HAVING count(*) > 1', |
236
|
|
|
array( |
237
|
|
|
'table1' => $table_name, |
238
|
|
|
'table2' => $table_name . '_old', |
239
|
|
|
'schema' => 'public', |
240
|
|
|
) |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
244
|
|
|
{ |
245
|
|
|
$same_col[] = $row['column_name']; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$smcFunc['db_query']('', ' |
249
|
|
|
INSERT INTO ' . $table_name . '(' |
250
|
|
|
. implode(',', $same_col) . |
251
|
|
|
') |
252
|
|
|
SELECT ' . implode(',', $same_col) . ' |
253
|
|
|
FROM ' . $table_name . '_old', |
254
|
|
|
array() |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// And the indexes... |
259
|
|
|
foreach ($index_queries as $query) |
260
|
|
|
$smcFunc['db_query']('', $query, |
261
|
|
|
array( |
262
|
|
|
'security_override' => true, |
263
|
|
|
) |
264
|
|
|
); |
265
|
|
|
|
266
|
|
|
// Go, go power rangers! |
267
|
|
|
$smcFunc['db_transaction']('commit'); |
268
|
|
|
|
269
|
|
|
if ($old_table_exists) |
270
|
|
|
$smcFunc['db_drop_table']($table_name . '_old'); |
271
|
|
|
|
272
|
|
|
return true; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Drop a table and its associated sequences. |
277
|
|
|
* |
278
|
|
|
* @param string $table_name The name of the table to drop |
279
|
|
|
* @param array $parameters Not used at the moment |
280
|
|
|
* @param string $error |
281
|
|
|
* @return boolean Whether or not the operation was successful |
282
|
|
|
*/ |
283
|
|
|
function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal') |
284
|
|
|
{ |
285
|
|
|
global $reservedTables, $smcFunc, $db_prefix; |
286
|
|
|
|
287
|
|
|
// After stripping away the database name, this is what's left. |
288
|
|
|
$real_prefix = preg_match('~^("?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; |
289
|
|
|
|
290
|
|
|
// Get some aliases. |
291
|
|
|
$full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); |
292
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
293
|
|
|
|
294
|
|
|
// God no - dropping one of these = bad. |
295
|
|
|
if (in_array(strtolower($table_name), $reservedTables)) |
296
|
|
|
return false; |
297
|
|
|
|
298
|
|
|
// Does it exist? |
299
|
|
|
if (in_array($full_table_name, $smcFunc['db_list_tables']())) |
300
|
|
|
{ |
301
|
|
|
// We can then drop the table. |
302
|
|
|
$smcFunc['db_transaction']('begin'); |
303
|
|
|
|
304
|
|
|
// the table |
305
|
|
|
$table_query = 'DROP TABLE ' . $table_name; |
306
|
|
|
|
307
|
|
|
// and the assosciated sequence, if any |
308
|
|
|
$sequence_query = 'DROP SEQUENCE IF EXISTS ' . $table_name . '_seq'; |
309
|
|
|
|
310
|
|
|
// drop them |
311
|
|
|
$smcFunc['db_query']('', |
312
|
|
|
$table_query, |
313
|
|
|
array( |
314
|
|
|
'security_override' => true, |
315
|
|
|
) |
316
|
|
|
); |
317
|
|
|
$smcFunc['db_query']('', |
318
|
|
|
$sequence_query, |
319
|
|
|
array( |
320
|
|
|
'security_override' => true, |
321
|
|
|
) |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
$smcFunc['db_transaction']('commit'); |
325
|
|
|
|
326
|
|
|
return true; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Otherwise do 'nout. |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* This function adds a column. |
335
|
|
|
* |
336
|
|
|
* @param string $table_name The name of the table to add the column to |
337
|
|
|
* @param array $column_info An array of column info (see {@link smf_db_create_table()}) |
338
|
|
|
* @param array $parameters Not used? |
339
|
|
|
* @param string $if_exists What to do if the column exists. If 'update', column is updated. |
340
|
|
|
* @param string $error |
341
|
|
|
* @return boolean Whether or not the operation was successful |
342
|
|
|
*/ |
343
|
|
|
function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') |
344
|
|
|
{ |
345
|
|
|
global $smcFunc, $db_package_log, $db_prefix; |
346
|
|
|
|
347
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
348
|
|
|
|
349
|
|
|
// Log that we will want to uninstall this! |
350
|
|
|
$db_package_log[] = array('remove_column', $table_name, $column_info['name']); |
351
|
|
|
|
352
|
|
|
// Does it exist - if so don't add it again! |
353
|
|
|
$columns = $smcFunc['db_list_columns']($table_name, false); |
354
|
|
|
foreach ($columns as $column) |
355
|
|
|
if ($column == $column_info['name']) |
356
|
|
|
{ |
357
|
|
|
// If we're going to overwrite then use change column. |
358
|
|
|
if ($if_exists == 'update') |
359
|
|
|
return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info); |
360
|
|
|
else |
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Get the specifics... |
365
|
|
|
$column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null; |
366
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']); |
367
|
|
|
if ($size !== null) |
368
|
|
|
$type = $type . '(' . $size . ')'; |
369
|
|
|
|
370
|
|
|
// Now add the thing! |
371
|
|
|
$query = ' |
372
|
|
|
ALTER TABLE ' . $table_name . ' |
373
|
|
|
ADD COLUMN ' . $column_info['name'] . ' ' . $type; |
374
|
|
|
$smcFunc['db_query']('', $query, |
375
|
|
|
array( |
376
|
|
|
'security_override' => true, |
377
|
|
|
) |
378
|
|
|
); |
379
|
|
|
|
380
|
|
|
// If there's more attributes they need to be done via a change on PostgreSQL. |
381
|
|
|
unset($column_info['type'], $column_info['size']); |
382
|
|
|
|
383
|
|
|
if (count($column_info) != 1) |
384
|
|
|
return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info); |
385
|
|
|
else |
386
|
|
|
return true; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Removes a column. |
391
|
|
|
* |
392
|
|
|
* @param string $table_name The name of the table to drop the column from |
393
|
|
|
* @param string $column_name The name of the column to drop |
394
|
|
|
* @param array $parameters Not used? |
395
|
|
|
* @param string $error |
396
|
|
|
* @return boolean Whether or not the operation was successful |
397
|
|
|
*/ |
398
|
|
|
function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal') |
399
|
|
|
{ |
400
|
|
|
global $smcFunc, $db_prefix; |
401
|
|
|
|
402
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
403
|
|
|
|
404
|
|
|
// Does it exist? |
405
|
|
|
$columns = $smcFunc['db_list_columns']($table_name, true); |
406
|
|
|
foreach ($columns as $column) |
407
|
|
|
if ($column['name'] == $column_name) |
408
|
|
|
{ |
409
|
|
|
// If there is an auto we need remove it! |
410
|
|
|
if ($column['auto']) |
411
|
|
|
$smcFunc['db_query']('', ' |
412
|
|
|
DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
413
|
|
|
array( |
414
|
|
|
'security_override' => true, |
415
|
|
|
) |
416
|
|
|
); |
417
|
|
|
|
418
|
|
|
$smcFunc['db_query']('', ' |
419
|
|
|
ALTER TABLE ' . $table_name . ' |
420
|
|
|
DROP COLUMN ' . $column_name, |
421
|
|
|
array( |
422
|
|
|
'security_override' => true, |
423
|
|
|
) |
424
|
|
|
); |
425
|
|
|
|
426
|
|
|
return true; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
// If here we didn't have to work - joy! |
430
|
|
|
return false; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Change a column. |
435
|
|
|
* |
436
|
|
|
* @param string $table_name The name of the table this column is in |
437
|
|
|
* @param string $old_column The name of the column we want to change |
438
|
|
|
* @param array $column_info An array of info about the "new" column definition (see {@link smf_db_create_table()}) |
439
|
|
|
* @return bool |
440
|
|
|
*/ |
441
|
|
|
function smf_db_change_column($table_name, $old_column, $column_info) |
442
|
|
|
{ |
443
|
|
|
global $smcFunc, $db_prefix; |
444
|
|
|
|
445
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
446
|
|
|
|
447
|
|
|
// backward compatibility |
448
|
|
|
if (isset($column_info['null'])) |
449
|
|
|
$column_info['not_null'] = !$column_info['null']; |
450
|
|
|
|
451
|
|
|
// Check it does exist! |
452
|
|
|
$columns = $smcFunc['db_list_columns']($table_name, true); |
453
|
|
|
$old_info = null; |
454
|
|
|
foreach ($columns as $column) |
455
|
|
|
if ($column['name'] == $old_column) |
456
|
|
|
$old_info = $column; |
457
|
|
|
|
458
|
|
|
// Nothing? |
459
|
|
|
if ($old_info == null) |
460
|
|
|
return false; |
461
|
|
|
|
462
|
|
|
// Now we check each bit individually and ALTER as required. |
463
|
|
|
if (isset($column_info['name']) && $column_info['name'] != $old_column) |
464
|
|
|
{ |
465
|
|
|
$smcFunc['db_query']('', ' |
466
|
|
|
ALTER TABLE ' . $table_name . ' |
467
|
|
|
RENAME COLUMN ' . $old_column . ' TO ' . $column_info['name'], |
468
|
|
|
array( |
469
|
|
|
'security_override' => true, |
470
|
|
|
) |
471
|
|
|
); |
472
|
|
|
} |
473
|
|
|
// Different default? |
474
|
|
|
if (isset($column_info['default']) && $column_info['default'] != $old_info['default']) |
475
|
|
|
{ |
476
|
|
|
$action = $column_info['default'] !== null ? 'SET DEFAULT \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'' : 'DROP DEFAULT'; |
477
|
|
|
$smcFunc['db_query']('', ' |
478
|
|
|
ALTER TABLE ' . $table_name . ' |
479
|
|
|
ALTER COLUMN ' . $column_info['name'] . ' ' . $action, |
480
|
|
|
array( |
481
|
|
|
'security_override' => true, |
482
|
|
|
) |
483
|
|
|
); |
484
|
|
|
} |
485
|
|
|
// Is it null - or otherwise? |
486
|
|
|
if (isset($column_info['not_null']) && $column_info['not_null'] != $old_info['not_null']) |
487
|
|
|
{ |
488
|
|
|
$action = $column_info['not_null'] ? 'SET' : 'DROP'; |
489
|
|
|
$smcFunc['db_transaction']('begin'); |
490
|
|
|
if ($column_info['not_null']) |
491
|
|
|
{ |
492
|
|
|
// We have to set it to something if we are making it NOT NULL. And we must comply with the current column format. |
493
|
|
|
$setTo = isset($column_info['default']) ? $column_info['default'] : (strpos($old_info['type'], 'int') !== false ? 0 : ''); |
494
|
|
|
$smcFunc['db_query']('', ' |
495
|
|
|
UPDATE ' . $table_name . ' |
496
|
|
|
SET ' . $column_info['name'] . ' = \'' . $setTo . '\' |
497
|
|
|
WHERE ' . $column_info['name'] . ' IS NULL', |
498
|
|
|
array( |
499
|
|
|
'security_override' => true, |
500
|
|
|
) |
501
|
|
|
); |
502
|
|
|
} |
503
|
|
|
$smcFunc['db_query']('', ' |
504
|
|
|
ALTER TABLE ' . $table_name . ' |
505
|
|
|
ALTER COLUMN ' . $column_info['name'] . ' ' . $action . ' NOT NULL', |
506
|
|
|
array( |
507
|
|
|
'security_override' => true, |
508
|
|
|
) |
509
|
|
|
); |
510
|
|
|
$smcFunc['db_transaction']('commit'); |
511
|
|
|
} |
512
|
|
|
// What about a change in type? |
513
|
|
|
if (isset($column_info['type']) && ($column_info['type'] != $old_info['type'] || (isset($column_info['size']) && $column_info['size'] != $old_info['size']))) |
514
|
|
|
{ |
515
|
|
|
$column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null; |
516
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']); |
517
|
|
|
if ($size !== null) |
518
|
|
|
$type = $type . '(' . $size . ')'; |
519
|
|
|
|
520
|
|
|
// The alter is a pain. |
521
|
|
|
$smcFunc['db_transaction']('begin'); |
522
|
|
|
$smcFunc['db_query']('', ' |
523
|
|
|
ALTER TABLE ' . $table_name . ' |
524
|
|
|
ADD COLUMN ' . $column_info['name'] . '_tempxx ' . $type, |
525
|
|
|
array( |
526
|
|
|
'security_override' => true, |
527
|
|
|
) |
528
|
|
|
); |
529
|
|
|
$smcFunc['db_query']('', ' |
530
|
|
|
UPDATE ' . $table_name . ' |
531
|
|
|
SET ' . $column_info['name'] . '_tempxx = CAST(' . $column_info['name'] . ' AS ' . $type . ')', |
532
|
|
|
array( |
533
|
|
|
'security_override' => true, |
534
|
|
|
) |
535
|
|
|
); |
536
|
|
|
$smcFunc['db_query']('', ' |
537
|
|
|
ALTER TABLE ' . $table_name . ' |
538
|
|
|
DROP COLUMN ' . $column_info['name'], |
539
|
|
|
array( |
540
|
|
|
'security_override' => true, |
541
|
|
|
) |
542
|
|
|
); |
543
|
|
|
$smcFunc['db_query']('', ' |
544
|
|
|
ALTER TABLE ' . $table_name . ' |
545
|
|
|
RENAME COLUMN ' . $column_info['name'] . '_tempxx TO ' . $column_info['name'], |
546
|
|
|
array( |
547
|
|
|
'security_override' => true, |
548
|
|
|
) |
549
|
|
|
); |
550
|
|
|
$smcFunc['db_transaction']('commit'); |
551
|
|
|
} |
552
|
|
|
// Finally - auto increment?! |
553
|
|
|
if (isset($column_info['auto']) && $column_info['auto'] != $old_info['auto']) |
554
|
|
|
{ |
555
|
|
|
// Are we removing an old one? |
556
|
|
|
if ($old_info['auto']) |
557
|
|
|
{ |
558
|
|
|
// Alter the table first - then drop the sequence. |
559
|
|
|
$smcFunc['db_query']('', ' |
560
|
|
|
ALTER TABLE ' . $table_name . ' |
561
|
|
|
ALTER COLUMN ' . $column_info['name'] . ' SET DEFAULT \'0\'', |
562
|
|
|
array( |
563
|
|
|
'security_override' => true, |
564
|
|
|
) |
565
|
|
|
); |
566
|
|
|
$smcFunc['db_query']('', ' |
567
|
|
|
DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
568
|
|
|
array( |
569
|
|
|
'security_override' => true, |
570
|
|
|
) |
571
|
|
|
); |
572
|
|
|
} |
573
|
|
|
// Otherwise add it! |
574
|
|
|
else |
575
|
|
|
{ |
576
|
|
|
$smcFunc['db_query']('', ' |
577
|
|
|
DROP SEQUENCE IF EXISTS ' . $table_name . '_seq', |
578
|
|
|
array( |
579
|
|
|
'security_override' => true, |
580
|
|
|
) |
581
|
|
|
); |
582
|
|
|
|
583
|
|
|
$smcFunc['db_query']('', ' |
584
|
|
|
CREATE SEQUENCE ' . $table_name . '_seq', |
585
|
|
|
array( |
586
|
|
|
'security_override' => true, |
587
|
|
|
) |
588
|
|
|
); |
589
|
|
|
$smcFunc['db_query']('', ' |
590
|
|
|
ALTER TABLE ' . $table_name . ' |
591
|
|
|
ALTER COLUMN ' . $column_info['name'] . ' SET DEFAULT nextval(\'' . $table_name . '_seq\')', |
592
|
|
|
array( |
593
|
|
|
'security_override' => true, |
594
|
|
|
) |
595
|
|
|
); |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
return true; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Add an index. |
604
|
|
|
* |
605
|
|
|
* @param string $table_name The name of the table to add the index to |
606
|
|
|
* @param array $index_info An array of index info (see {@link smf_db_create_table()}) |
607
|
|
|
* @param array $parameters Not used? |
608
|
|
|
* @param string $if_exists What to do if the index exists. If 'update', the definition will be updated. |
609
|
|
|
* @param string $error |
610
|
|
|
* @return boolean Whether or not the operation was successful |
611
|
|
|
*/ |
612
|
|
|
function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') |
613
|
|
|
{ |
614
|
|
|
global $smcFunc, $db_package_log, $db_prefix; |
615
|
|
|
|
616
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
617
|
|
|
|
618
|
|
|
// No columns = no index. |
619
|
|
|
if (empty($index_info['columns'])) |
620
|
|
|
return false; |
621
|
|
|
$columns = implode(',', $index_info['columns']); |
622
|
|
|
|
623
|
|
|
// No name - make it up! |
624
|
|
|
if (empty($index_info['name'])) |
625
|
|
|
{ |
626
|
|
|
// No need for primary. |
627
|
|
|
if (isset($index_info['type']) && $index_info['type'] == 'primary') |
628
|
|
|
$index_info['name'] = ''; |
629
|
|
|
else |
630
|
|
|
$index_info['name'] = $table_name . implode('_', $index_info['columns']); |
631
|
|
|
} |
632
|
|
|
else |
633
|
|
|
$index_info['name'] = $table_name . $index_info['name']; |
634
|
|
|
|
635
|
|
|
// Log that we are going to want to remove this! |
636
|
|
|
$db_package_log[] = array('remove_index', $table_name, $index_info['name']); |
637
|
|
|
|
638
|
|
|
// Let's get all our indexes. |
639
|
|
|
$indexes = $smcFunc['db_list_indexes']($table_name, true); |
640
|
|
|
// Do we already have it? |
641
|
|
|
foreach ($indexes as $index) |
642
|
|
|
{ |
643
|
|
|
if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary')) |
644
|
|
|
{ |
645
|
|
|
// If we want to overwrite simply remove the current one then continue. |
646
|
|
|
if ($if_exists != 'update' || $index['type'] == 'primary') |
647
|
|
|
return false; |
648
|
|
|
else |
649
|
|
|
$smcFunc['db_remove_index']($table_name, $index_info['name']); |
650
|
|
|
} |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
// If we're here we know we don't have the index - so just add it. |
654
|
|
|
if (!empty($index_info['type']) && $index_info['type'] == 'primary') |
655
|
|
|
{ |
656
|
|
|
$smcFunc['db_query']('', ' |
657
|
|
|
ALTER TABLE ' . $table_name . ' |
658
|
|
|
ADD PRIMARY KEY (' . $columns . ')', |
659
|
|
|
array( |
660
|
|
|
'security_override' => true, |
661
|
|
|
) |
662
|
|
|
); |
663
|
|
|
} |
664
|
|
|
else |
665
|
|
|
{ |
666
|
|
|
$smcFunc['db_query']('', ' |
667
|
|
|
CREATE ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : '') . ' INDEX ' . $index_info['name'] . ' ON ' . $table_name . ' (' . $columns . ')', |
668
|
|
|
array( |
669
|
|
|
'security_override' => true, |
670
|
|
|
) |
671
|
|
|
); |
672
|
|
|
} |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Remove an index. |
677
|
|
|
* |
678
|
|
|
* @param string $table_name The name of the table to remove the index from |
679
|
|
|
* @param string $index_name The name of the index to remove |
680
|
|
|
* @param array $parameters Not used? |
681
|
|
|
* @param string $error |
682
|
|
|
* @return boolean Whether or not the operation was successful |
683
|
|
|
*/ |
684
|
|
|
function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal') |
685
|
|
|
{ |
686
|
|
|
global $smcFunc, $db_prefix; |
687
|
|
|
|
688
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
689
|
|
|
|
690
|
|
|
// Better exist! |
691
|
|
|
$indexes = $smcFunc['db_list_indexes']($table_name, true); |
692
|
|
|
if ($index_name != 'primary') |
693
|
|
|
$index_name = $table_name . '_' . $index_name; |
694
|
|
|
|
695
|
|
|
foreach ($indexes as $index) |
696
|
|
|
{ |
697
|
|
|
// If the name is primary we want the primary key! |
698
|
|
|
if ($index['type'] == 'primary' && $index_name == 'primary') |
699
|
|
|
{ |
700
|
|
|
// Dropping primary key is odd... |
701
|
|
|
$smcFunc['db_query']('', ' |
702
|
|
|
ALTER TABLE ' . $table_name . ' |
703
|
|
|
DROP CONSTRAINT ' . $index['name'], |
704
|
|
|
array( |
705
|
|
|
'security_override' => true, |
706
|
|
|
) |
707
|
|
|
); |
708
|
|
|
|
709
|
|
|
return true; |
710
|
|
|
} |
711
|
|
|
if ($index['name'] == $index_name) |
712
|
|
|
{ |
713
|
|
|
// Drop the bugger... |
714
|
|
|
$smcFunc['db_query']('', ' |
715
|
|
|
DROP INDEX ' . $index_name, |
716
|
|
|
array( |
717
|
|
|
'security_override' => true, |
718
|
|
|
) |
719
|
|
|
); |
720
|
|
|
|
721
|
|
|
return true; |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
// Not to be found ;( |
726
|
|
|
return false; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Get the schema formatted name for a type. |
731
|
|
|
* |
732
|
|
|
* @param string $type_name The data type (int, varchar, smallint, etc.) |
733
|
|
|
* @param int $type_size The size (8, 255, etc.) |
734
|
|
|
* @param boolean $reverse If true, returns specific types for a generic type |
735
|
|
|
* @return array An array containing the appropriate type and size for this DB type |
736
|
|
|
*/ |
737
|
|
|
function smf_db_calculate_type($type_name, $type_size = null, $reverse = false) |
738
|
|
|
{ |
739
|
|
|
// Let's be sure it's lowercase MySQL likes both, others no. |
740
|
|
|
$type_name = strtolower($type_name); |
741
|
|
|
// Generic => Specific. |
742
|
|
|
if (!$reverse) |
743
|
|
|
{ |
744
|
|
|
$types = array( |
745
|
|
|
'varchar' => 'character varying', |
746
|
|
|
'char' => 'character', |
747
|
|
|
'mediumint' => 'int', |
748
|
|
|
'tinyint' => 'smallint', |
749
|
|
|
'tinytext' => 'character varying', |
750
|
|
|
'mediumtext' => 'text', |
751
|
|
|
'largetext' => 'text', |
752
|
|
|
'inet' => 'inet', |
753
|
|
|
'time' => 'time without time zone', |
754
|
|
|
'datetime' => 'timestamp without time zone', |
755
|
|
|
'timestamp' => 'timestamp without time zone', |
756
|
|
|
); |
757
|
|
|
} |
758
|
|
|
else |
759
|
|
|
{ |
760
|
|
|
$types = array( |
761
|
|
|
'character varying' => 'varchar', |
762
|
|
|
'character' => 'char', |
763
|
|
|
'integer' => 'int', |
764
|
|
|
'inet' => 'inet', |
765
|
|
|
'time without time zone' => 'time', |
766
|
|
|
'timestamp without time zone' => 'datetime', |
767
|
|
|
'numeric' => 'decimal', |
768
|
|
|
); |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
// Got it? Change it! |
772
|
|
|
if (isset($types[$type_name])) |
773
|
|
|
{ |
774
|
|
|
if ($type_name == 'tinytext') |
775
|
|
|
$type_size = 255; |
776
|
|
|
$type_name = $types[$type_name]; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
// Only char fields got size |
780
|
|
|
if (strpos($type_name, 'char') === false) |
781
|
|
|
$type_size = null; |
782
|
|
|
|
783
|
|
|
return array($type_name, $type_size); |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Get table structure. |
788
|
|
|
* |
789
|
|
|
* @param string $table_name The name of the table |
790
|
|
|
* @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()} |
791
|
|
|
*/ |
792
|
|
|
function smf_db_table_structure($table_name) |
793
|
|
|
{ |
794
|
|
|
global $smcFunc, $db_prefix; |
795
|
|
|
|
796
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
797
|
|
|
|
798
|
|
|
return array( |
799
|
|
|
'name' => $table_name, |
800
|
|
|
'columns' => $smcFunc['db_list_columns']($table_name, true), |
801
|
|
|
'indexes' => $smcFunc['db_list_indexes']($table_name, true), |
802
|
|
|
); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* Return column information for a table. |
807
|
|
|
* |
808
|
|
|
* @param string $table_name The name of the table to get column info for |
809
|
|
|
* @param bool $detail Whether or not to return detailed info. If true, returns the column info. If false, just returns the column names. |
810
|
|
|
* @param array $parameters Not used? |
811
|
|
|
* @return array An array of column names or detailed column info, depending on $detail |
812
|
|
|
*/ |
813
|
|
|
function smf_db_list_columns($table_name, $detail = false, $parameters = array()) |
814
|
|
|
{ |
815
|
|
|
global $smcFunc, $db_prefix; |
816
|
|
|
|
817
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
818
|
|
|
|
819
|
|
|
$result = $smcFunc['db_query']('', ' |
820
|
|
|
SELECT column_name, column_default, is_nullable, data_type, character_maximum_length |
821
|
|
|
FROM information_schema.columns |
822
|
|
|
WHERE table_schema = {string:schema_public} |
823
|
|
|
AND table_name = {string:table_name} |
824
|
|
|
ORDER BY ordinal_position', |
825
|
|
|
array( |
826
|
|
|
'schema_public' => 'public', |
827
|
|
|
'table_name' => $table_name, |
828
|
|
|
) |
829
|
|
|
); |
830
|
|
|
$columns = array(); |
831
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($result)) |
832
|
|
|
{ |
833
|
|
|
if (!$detail) |
834
|
|
|
{ |
835
|
|
|
$columns[] = $row['column_name']; |
836
|
|
|
} |
837
|
|
|
else |
838
|
|
|
{ |
839
|
|
|
$auto = false; |
840
|
|
|
// What is the default? |
841
|
|
|
if (preg_match('~nextval\(\'(.+?)\'(.+?)*\)~i', $row['column_default'], $matches) != 0) |
842
|
|
|
{ |
843
|
|
|
$default = null; |
844
|
|
|
$auto = true; |
845
|
|
|
} |
846
|
|
|
elseif (trim($row['column_default']) != '') |
847
|
|
|
$default = strpos($row['column_default'], '::') === false ? $row['column_default'] : substr($row['column_default'], 0, strpos($row['column_default'], '::')); |
848
|
|
|
else |
849
|
|
|
$default = null; |
850
|
|
|
|
851
|
|
|
// Make the type generic. |
852
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($row['data_type'], $row['character_maximum_length'], true); |
853
|
|
|
|
854
|
|
|
$columns[$row['column_name']] = array( |
855
|
|
|
'name' => $row['column_name'], |
856
|
|
|
'not_null' => !($row['is_nullable'] ? true : false), |
857
|
|
|
'null' => $row['is_nullable'] ? true : false, |
858
|
|
|
'default' => $default, |
859
|
|
|
'type' => $type, |
860
|
|
|
'size' => $size, |
861
|
|
|
'auto' => $auto, |
862
|
|
|
); |
863
|
|
|
} |
864
|
|
|
} |
865
|
|
|
$smcFunc['db_free_result']($result); |
866
|
|
|
|
867
|
|
|
return $columns; |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
/** |
871
|
|
|
* Get index information. |
872
|
|
|
* |
873
|
|
|
* @param string $table_name The name of the table to get indexes for |
874
|
|
|
* @param bool $detail Whether or not to return detailed info. |
875
|
|
|
* @param array $parameters Not used? |
876
|
|
|
* @return array An array of index names or a detailed array of index info, depending on $detail |
877
|
|
|
*/ |
878
|
|
|
function smf_db_list_indexes($table_name, $detail = false, $parameters = array()) |
879
|
|
|
{ |
880
|
|
|
global $smcFunc, $db_prefix; |
881
|
|
|
|
882
|
|
|
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
883
|
|
|
|
884
|
|
|
$result = $smcFunc['db_query']('', ' |
885
|
|
|
SELECT CASE WHEN i.indisprimary THEN 1 ELSE 0 END AS is_primary, |
886
|
|
|
CASE WHEN i.indisunique THEN 1 ELSE 0 END AS is_unique, |
887
|
|
|
c2.relname AS name, |
888
|
|
|
pg_get_indexdef(i.indexrelid) AS inddef |
889
|
|
|
FROM pg_class AS c, pg_class AS c2, pg_index AS i |
890
|
|
|
WHERE c.relname = {string:table_name} |
891
|
|
|
AND c.oid = i.indrelid |
892
|
|
|
AND i.indexrelid = c2.oid', |
893
|
|
|
array( |
894
|
|
|
'table_name' => $table_name, |
895
|
|
|
) |
896
|
|
|
); |
897
|
|
|
$indexes = array(); |
898
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($result)) |
899
|
|
|
{ |
900
|
|
|
// Try get the columns that make it up. |
901
|
|
|
if (preg_match('~\(([^\)]+?)\)~i', $row['inddef'], $matches) == 0) |
902
|
|
|
continue; |
903
|
|
|
|
904
|
|
|
$columns = explode(',', $matches[1]); |
905
|
|
|
|
906
|
|
|
if (empty($columns)) |
907
|
|
|
continue; |
908
|
|
|
|
909
|
|
|
foreach ($columns as $k => $v) |
910
|
|
|
$columns[$k] = trim($v); |
911
|
|
|
|
912
|
|
|
// Fix up the name to be consistent cross databases |
913
|
|
|
if (substr($row['name'], -5) == '_pkey' && $row['is_primary'] == 1) |
914
|
|
|
$row['name'] = 'PRIMARY'; |
915
|
|
|
else |
916
|
|
|
$row['name'] = str_replace($table_name . '_', '', $row['name']); |
917
|
|
|
|
918
|
|
|
if (!$detail) |
919
|
|
|
$indexes[] = $row['name']; |
920
|
|
|
else |
921
|
|
|
{ |
922
|
|
|
$indexes[$row['name']] = array( |
923
|
|
|
'name' => $row['name'], |
924
|
|
|
'type' => $row['is_primary'] ? 'primary' : ($row['is_unique'] ? 'unique' : 'index'), |
925
|
|
|
'columns' => $columns, |
926
|
|
|
); |
927
|
|
|
} |
928
|
|
|
} |
929
|
|
|
$smcFunc['db_free_result']($result); |
930
|
|
|
|
931
|
|
|
return $indexes; |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
?> |