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 2025 Simple Machines and individual contributors |
11
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
12
|
|
|
* |
13
|
|
|
* @version 2.1.5 |
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 Extra parameters. Currently only 'engine', the desired MySQL storage engine, is used. |
102
|
|
|
* @param string $if_exists What to do if the table exists. |
103
|
|
|
* @param string $error |
104
|
|
|
* @return boolean Whether or not the operation was successful |
105
|
|
|
*/ |
106
|
|
|
function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal') |
107
|
|
|
{ |
108
|
|
|
global $reservedTables, $smcFunc, $db_package_log, $db_prefix, $db_character_set, $db_name; |
109
|
|
|
|
110
|
|
|
static $engines = array(); |
111
|
|
|
|
112
|
|
|
$old_table_exists = false; |
113
|
|
|
|
114
|
|
|
// Strip out the table name, we might not need it in some cases |
115
|
|
|
$real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; |
116
|
|
|
$database = !empty($match[2]) ? $match[2] : $db_name; |
117
|
|
|
|
118
|
|
|
// With or without the database name, the fullname looks like this. |
119
|
|
|
$full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); |
120
|
|
|
// Do not overwrite $table_name, this causes issues if we pass it onto a helper function. |
121
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
122
|
|
|
|
123
|
|
|
// First - no way do we touch SMF tables. |
124
|
|
|
if (in_array(strtolower($short_table_name), $reservedTables)) |
125
|
|
|
return false; |
126
|
|
|
|
127
|
|
|
// Log that we'll want to remove this on uninstall. |
128
|
|
|
$db_package_log[] = array('remove_table', $short_table_name); |
129
|
|
|
|
130
|
|
|
// Slightly easier on MySQL than the others... |
131
|
|
|
$tables = $smcFunc['db_list_tables']($database); |
132
|
|
|
|
133
|
|
|
if (in_array($full_table_name, $tables)) |
134
|
|
|
{ |
135
|
|
|
// This is a sad day... drop the table? If not, return false (error) by default. |
136
|
|
|
if ($if_exists == 'overwrite') |
137
|
|
|
$smcFunc['db_drop_table']($table_name); |
138
|
|
|
elseif ($if_exists == 'update') |
139
|
|
|
{ |
140
|
|
|
$smcFunc['db_transaction']('begin'); |
141
|
|
|
$db_trans = true; |
|
|
|
|
142
|
|
|
$smcFunc['db_drop_table']($short_table_name . '_old'); |
143
|
|
|
$smcFunc['db_query']('', ' |
144
|
|
|
RENAME TABLE ' . $short_table_name . ' TO ' . $short_table_name . '_old', |
145
|
|
|
array( |
146
|
|
|
'security_override' => true, |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
$old_table_exists = true; |
150
|
|
|
} |
151
|
|
|
else |
152
|
|
|
return $if_exists == 'ignore'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Righty - let's do the damn thing! |
156
|
|
|
$table_query = 'CREATE TABLE ' . $short_table_name . "\n" . '('; |
157
|
|
|
foreach ($columns as $column) |
158
|
|
|
$table_query .= "\n\t" . smf_db_create_query_column($column) . ','; |
159
|
|
|
|
160
|
|
|
// Loop through the indexes next... |
161
|
|
|
foreach ($indexes as $index) |
162
|
|
|
{ |
163
|
|
|
// MySQL If its a text column, we need to add a size. |
164
|
|
|
foreach ($index['columns'] as &$c) |
165
|
|
|
{ |
166
|
|
|
$c = trim($c); |
167
|
|
|
|
168
|
|
|
// If a size was already specified, we won't be able to match it anyways. |
169
|
|
|
$key = array_search($c, array_column($columns, 'name')); |
170
|
|
|
$columns[$key]['size'] = isset($columns[$key]['size']) && is_numeric($columns[$key]['size']) ? $columns[$key]['size'] : null; |
171
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($columns[$key]['type'], $columns[$key]['size']); |
172
|
|
|
if ( |
173
|
|
|
$key === false |
174
|
|
|
|| !isset($columns[$key]) |
175
|
|
|
|| !in_array($columns[$key]['type'], array('text', 'mediumtext', 'largetext', 'varchar', 'char')) |
176
|
|
|
|| ( |
177
|
|
|
isset($size) |
178
|
|
|
&& $size <= 191 |
179
|
|
|
) |
180
|
|
|
) |
181
|
|
|
continue; |
182
|
|
|
|
183
|
|
|
$c .= '(191)'; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$idx_columns = implode(',', $index['columns']); |
187
|
|
|
|
188
|
|
|
// Is it the primary? |
189
|
|
|
if (isset($index['type']) && $index['type'] == 'primary') |
190
|
|
|
$table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),'; |
191
|
|
|
else |
192
|
|
|
{ |
193
|
|
|
if (empty($index['name'])) |
194
|
|
|
$index['name'] = trim(implode('_', preg_replace('~(\(\d+\))~', '', $index['columns']))); |
195
|
|
|
|
196
|
|
|
$table_query .= "\n\t" . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : 'KEY') . ' ' . $index['name'] . ' (' . $idx_columns . '),'; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// No trailing commas! |
201
|
|
|
if (substr($table_query, -1) == ',') |
202
|
|
|
$table_query = substr($table_query, 0, -1); |
203
|
|
|
|
204
|
|
|
// Which engine do we want here? |
205
|
|
|
if (empty($engines)) |
206
|
|
|
{ |
207
|
|
|
// Figure out which engines we have |
208
|
|
|
$get_engines = $smcFunc['db_query']('', 'SHOW ENGINES', array()); |
209
|
|
|
|
210
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($get_engines)) |
211
|
|
|
{ |
212
|
|
|
if ($row['Support'] == 'YES' || $row['Support'] == 'DEFAULT') |
213
|
|
|
$engines[] = $row['Engine']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$smcFunc['db_free_result']($get_engines); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// If we don't have this engine, or didn't specify one, default to InnoDB or MyISAM |
220
|
|
|
// depending on which one is available |
221
|
|
|
if (!isset($parameters['engine']) || !in_array($parameters['engine'], $engines)) |
222
|
|
|
{ |
223
|
|
|
$parameters['engine'] = in_array('InnoDB', $engines) ? 'InnoDB' : 'MyISAM'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$table_query .= ') ENGINE=' . $parameters['engine']; |
227
|
|
|
if (!empty($db_character_set) && $db_character_set == 'utf8') |
228
|
|
|
$table_query .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci'; |
229
|
|
|
|
230
|
|
|
// Create the table! |
231
|
|
|
$smcFunc['db_query']('', $table_query, |
232
|
|
|
array( |
233
|
|
|
'security_override' => true, |
234
|
|
|
) |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
// Fill the old data |
238
|
|
|
if ($old_table_exists) |
239
|
|
|
{ |
240
|
|
|
$same_col = array(); |
241
|
|
|
|
242
|
|
|
$request = $smcFunc['db_query']('', ' |
243
|
|
|
SELECT count(*), column_name |
244
|
|
|
FROM information_schema.columns |
245
|
|
|
WHERE table_name in ({string:table1},{string:table2}) AND table_schema = {string:schema} |
246
|
|
|
GROUP BY column_name |
247
|
|
|
HAVING count(*) > 1', |
248
|
|
|
array( |
249
|
|
|
'table1' => $short_table_name, |
250
|
|
|
'table2' => $short_table_name . '_old', |
251
|
|
|
'schema' => $db_name, |
252
|
|
|
) |
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
256
|
|
|
{ |
257
|
|
|
$row = array_change_key_case($row, CASE_LOWER); |
258
|
|
|
$same_col[] = $row['column_name']; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$smcFunc['db_query']('', ' |
262
|
|
|
INSERT INTO ' . $short_table_name . '(' |
263
|
|
|
. implode(',', $same_col) . |
264
|
|
|
') |
265
|
|
|
SELECT ' . implode(',', $same_col) . ' |
266
|
|
|
FROM ' . $short_table_name . '_old', |
267
|
|
|
array() |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$smcFunc['db_drop_table']($short_table_name . '_old'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return true; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Drop a table. |
278
|
|
|
* |
279
|
|
|
* @param string $table_name The name of the table to drop |
280
|
|
|
* @param array $parameters Not used at the moment |
281
|
|
|
* @param string $error |
282
|
|
|
* @return boolean Whether or not the operation was successful |
283
|
|
|
*/ |
284
|
|
|
function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal') |
285
|
|
|
{ |
286
|
|
|
global $reservedTables, $smcFunc, $db_prefix, $db_name; |
287
|
|
|
|
288
|
|
|
// After stripping away the database name, this is what's left. |
289
|
|
|
$real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; |
290
|
|
|
$database = !empty($match[2]) ? $match[2] : $db_name; |
291
|
|
|
|
292
|
|
|
// Get some aliases. |
293
|
|
|
$full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); |
294
|
|
|
// Do not overwrite $table_name, this causes issues if we pass it onto a helper function. |
295
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
296
|
|
|
|
297
|
|
|
// God no - dropping one of these = bad. |
298
|
|
|
if (in_array(strtolower($short_table_name), $reservedTables)) |
299
|
|
|
return false; |
300
|
|
|
|
301
|
|
|
// Does it exist? |
302
|
|
|
$tables = $smcFunc['db_list_tables']($database); |
303
|
|
|
if (in_array($full_table_name, $tables)) |
304
|
|
|
{ |
305
|
|
|
$query = 'DROP TABLE ' . $short_table_name; |
306
|
|
|
$smcFunc['db_query']('', |
307
|
|
|
$query, |
308
|
|
|
array( |
309
|
|
|
'security_override' => true, |
310
|
|
|
) |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
// Otherwise do 'nout. |
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* This function adds a column. |
322
|
|
|
* |
323
|
|
|
* @param string $table_name The name of the table to add the column to |
324
|
|
|
* @param array $column_info An array of column info ({@see smf_db_create_table}) |
325
|
|
|
* @param array $parameters Not used? |
326
|
|
|
* @param string $if_exists What to do if the column exists. If 'update', column is updated. |
327
|
|
|
* @param string $error |
328
|
|
|
* @return boolean Whether or not the operation was successful |
329
|
|
|
*/ |
330
|
|
|
function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') |
331
|
|
|
{ |
332
|
|
|
global $smcFunc, $db_package_log, $db_prefix; |
333
|
|
|
|
334
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
335
|
|
|
$column_info = array_change_key_case($column_info); |
336
|
|
|
|
337
|
|
|
// Log that we will want to uninstall this! |
338
|
|
|
$db_package_log[] = array('remove_column', $short_table_name, $column_info['name']); |
339
|
|
|
|
340
|
|
|
// Does it exist - if so don't add it again! |
341
|
|
|
$columns = $smcFunc['db_list_columns']($table_name, false); |
342
|
|
|
foreach ($columns as $column) |
343
|
|
|
if ($column == $column_info['name']) |
344
|
|
|
{ |
345
|
|
|
// If we're going to overwrite then use change column. |
346
|
|
|
if ($if_exists == 'update') |
347
|
|
|
return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info); |
348
|
|
|
else |
349
|
|
|
return false; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// Get the specifics... |
353
|
|
|
$column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null; |
354
|
|
|
|
355
|
|
|
// Now add the thing! |
356
|
|
|
$query = ' |
357
|
|
|
ALTER TABLE ' . $short_table_name . ' |
358
|
|
|
ADD ' . smf_db_create_query_column($column_info) . (empty($column_info['auto']) ? '' : ' primary key' |
359
|
|
|
); |
360
|
|
|
$smcFunc['db_query']('', $query, |
361
|
|
|
array( |
362
|
|
|
'security_override' => true, |
363
|
|
|
) |
364
|
|
|
); |
365
|
|
|
|
366
|
|
|
return true; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Removes a column. |
371
|
|
|
* |
372
|
|
|
* @param string $table_name The name of the table to drop the column from |
373
|
|
|
* @param string $column_name The name of the column to drop |
374
|
|
|
* @param array $parameters Not used? |
375
|
|
|
* @param string $error |
376
|
|
|
* @return boolean Whether or not the operation was successful |
377
|
|
|
*/ |
378
|
|
|
function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal') |
379
|
|
|
{ |
380
|
|
|
global $smcFunc, $db_prefix; |
381
|
|
|
|
382
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
383
|
|
|
|
384
|
|
|
// Does it exist? |
385
|
|
|
$columns = $smcFunc['db_list_columns']($table_name, true); |
386
|
|
|
|
387
|
|
|
foreach ($columns as $column) |
388
|
|
|
if ($column['name'] == $column_name) |
389
|
|
|
{ |
390
|
|
|
$smcFunc['db_query']('', ' |
391
|
|
|
ALTER TABLE ' . $short_table_name . ' |
392
|
|
|
DROP COLUMN ' . $column_name, |
393
|
|
|
array( |
394
|
|
|
'security_override' => true, |
395
|
|
|
) |
396
|
|
|
); |
397
|
|
|
|
398
|
|
|
return true; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
// If here we didn't have to work - joy! |
402
|
|
|
return false; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Change a column. You only need to specify the column attributes that are changing. |
407
|
|
|
* |
408
|
|
|
* @param string $table_name The name of the table this column is in |
409
|
|
|
* @param string $old_column The name of the column we want to change |
410
|
|
|
* @param array $column_info An array of info about the "new" column definition (see {@link smf_db_create_table()}) |
411
|
|
|
* Note that $column_info also supports two additional parameters that only make sense when changing columns: |
412
|
|
|
* - drop_default - to drop a default that was previously specified |
413
|
|
|
* @return bool |
414
|
|
|
*/ |
415
|
|
|
function smf_db_change_column($table_name, $old_column, $column_info) |
416
|
|
|
{ |
417
|
|
|
global $smcFunc, $db_prefix; |
418
|
|
|
|
419
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
420
|
|
|
$column_info = array_change_key_case($column_info); |
421
|
|
|
|
422
|
|
|
// Check it does exist! |
423
|
|
|
$columns = $smcFunc['db_list_columns']($table_name, true); |
424
|
|
|
$old_info = null; |
425
|
|
|
foreach ($columns as $column) |
426
|
|
|
if ($column['name'] == $old_column) |
427
|
|
|
$old_info = $column; |
428
|
|
|
|
429
|
|
|
// Nothing? |
430
|
|
|
if ($old_info == null) |
431
|
|
|
return false; |
432
|
|
|
|
433
|
|
|
// backward compatibility |
434
|
|
|
if (isset($column_info['null']) && !isset($column_info['not_null'])) |
435
|
|
|
$column_info['not_null'] = !$column_info['null']; |
436
|
|
|
|
437
|
|
|
// Get the right bits. |
438
|
|
|
if (isset($column_info['drop_default']) && !empty($column_info['drop_default'])) |
439
|
|
|
$column_info['drop_default'] = true; |
440
|
|
|
else |
441
|
|
|
$column_info['drop_default'] = false; |
442
|
|
|
if (!isset($column_info['name'])) |
443
|
|
|
$column_info['name'] = $old_column; |
444
|
|
|
if (!array_key_exists('default', $column_info) && array_key_exists('default', $old_info) && empty($column_info['drop_default'])) |
445
|
|
|
$column_info['default'] = $old_info['default']; |
446
|
|
|
if (!isset($column_info['not_null'])) |
447
|
|
|
$column_info['not_null'] = $old_info['not_null']; |
448
|
|
|
if (!isset($column_info['auto'])) |
449
|
|
|
$column_info['auto'] = $old_info['auto']; |
450
|
|
|
if (!isset($column_info['type'])) |
451
|
|
|
$column_info['type'] = $old_info['type']; |
452
|
|
|
if (!isset($column_info['size']) || !is_numeric($column_info['size'])) |
453
|
|
|
$column_info['size'] = $old_info['size']; |
454
|
|
|
if (!isset($column_info['unsigned']) || !in_array($column_info['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint'))) |
455
|
|
|
$column_info['unsigned'] = ''; |
456
|
|
|
|
457
|
|
|
// If truly unspecified, make that clear, otherwise, might be confused with NULL... |
458
|
|
|
// (Unspecified = no default whatsoever = column is not nullable with a value of null...) |
459
|
|
|
if (($column_info['not_null'] === true) && !$column_info['drop_default'] && array_key_exists('default', $column_info) && is_null($column_info['default'])) |
460
|
|
|
unset($column_info['default']); |
461
|
|
|
|
462
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']); |
463
|
|
|
|
464
|
|
|
// Allow for unsigned integers (mysql only) |
465
|
|
|
$unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : ''; |
466
|
|
|
|
467
|
|
|
// If you need to drop the default, that needs it's own thing... |
468
|
|
|
// Must be done first, in case the default type is inconsistent with the other changes. |
469
|
|
|
if ($column_info['drop_default']) |
470
|
|
|
{ |
471
|
|
|
$smcFunc['db_query']('', ' |
472
|
|
|
ALTER TABLE ' . $short_table_name . ' |
473
|
|
|
ALTER COLUMN `' . $old_column . '` DROP DEFAULT', |
474
|
|
|
array( |
475
|
|
|
'security_override' => true, |
476
|
|
|
) |
477
|
|
|
); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
// Set the default clause. |
481
|
|
|
$default_clause = ''; |
482
|
|
|
if (!$column_info['drop_default'] && array_key_exists('default', $column_info)) |
483
|
|
|
{ |
484
|
|
|
if (is_null($column_info['default'])) |
485
|
|
|
$default_clause = 'DEFAULT NULL'; |
486
|
|
|
elseif (is_numeric($column_info['default'])) |
487
|
|
|
$default_clause = 'DEFAULT ' . (strpos($column_info['default'], '.') ? floatval($column_info['default']) : intval($column_info['default'])); |
488
|
|
|
elseif (is_string($column_info['default'])) |
489
|
|
|
$default_clause = 'DEFAULT \'' . $smcFunc['db_escape_string']($column_info['default']) . '\''; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
if ($size !== null) |
493
|
|
|
$type = $type . '(' . $size . ')'; |
494
|
|
|
|
495
|
|
|
$smcFunc['db_query']('', ' |
496
|
|
|
ALTER TABLE ' . $short_table_name . ' |
497
|
|
|
CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' . $type . ' ' . |
498
|
|
|
(!empty($unsigned) ? $unsigned : '') . (!empty($column_info['not_null']) ? 'NOT NULL' : '') . ' ' . |
499
|
|
|
$default_clause . ' ' . |
500
|
|
|
(empty($column_info['auto']) ? '' : 'auto_increment') . ' ', |
501
|
|
|
array( |
502
|
|
|
'security_override' => true, |
503
|
|
|
) |
504
|
|
|
); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Add an index. |
509
|
|
|
* |
510
|
|
|
* @param string $table_name The name of the table to add the index to |
511
|
|
|
* @param array $index_info An array of index info (see {@link smf_db_create_table()}) |
512
|
|
|
* @param array $parameters Not used? |
513
|
|
|
* @param string $if_exists What to do if the index exists. If 'update', the definition will be updated. |
514
|
|
|
* @param string $error |
515
|
|
|
* @return boolean Whether or not the operation was successful |
516
|
|
|
*/ |
517
|
|
|
function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') |
518
|
|
|
{ |
519
|
|
|
global $smcFunc, $db_package_log, $db_prefix; |
520
|
|
|
|
521
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
522
|
|
|
|
523
|
|
|
// No columns = no index. |
524
|
|
|
if (empty($index_info['columns'])) |
525
|
|
|
return false; |
526
|
|
|
|
527
|
|
|
// MySQL If its a text column, we need to add a size. |
528
|
|
|
$cols = $smcFunc['db_list_columns']($table_name, true); |
529
|
|
|
foreach ($index_info['columns'] as &$c) |
530
|
|
|
{ |
531
|
|
|
$c = trim($c); |
532
|
|
|
$cols[$c]['size'] = isset($cols[$c]['size']) && is_numeric($cols[$c]['size']) ? $cols[$c]['size'] : null; |
533
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($cols[$c]['type'], $cols[$c]['size']); |
534
|
|
|
|
535
|
|
|
// If a size was already specified, we won't be able to match it anyways. |
536
|
|
|
if ( |
537
|
|
|
!isset($cols[$c]) |
538
|
|
|
|| !in_array($cols[$c]['type'], array('text', 'mediumtext', 'largetext', 'varchar', 'char')) |
539
|
|
|
|| ( |
540
|
|
|
isset($size) |
541
|
|
|
&& $size <= 191 |
542
|
|
|
) |
543
|
|
|
) |
544
|
|
|
continue; |
545
|
|
|
|
546
|
|
|
$c .= '(191)'; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
$columns = implode(',', $index_info['columns']); |
550
|
|
|
|
551
|
|
|
// No name - make it up! |
552
|
|
|
if (empty($index_info['name'])) |
553
|
|
|
{ |
554
|
|
|
// No need for primary. |
555
|
|
|
if (isset($index_info['type']) && $index_info['type'] == 'primary') |
556
|
|
|
$index_info['name'] = ''; |
557
|
|
|
else |
558
|
|
|
$index_info['name'] = trim(implode('_', preg_replace('~(\(\d+\))~', '', $index_info['columns']))); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
// Log that we are going to want to remove this! |
562
|
|
|
$db_package_log[] = array('remove_index', $short_table_name, $index_info['name']); |
563
|
|
|
|
564
|
|
|
// Let's get all our indexes. |
565
|
|
|
$indexes = $smcFunc['db_list_indexes']($table_name, true); |
566
|
|
|
// Do we already have it? |
567
|
|
|
foreach ($indexes as $index) |
568
|
|
|
{ |
569
|
|
|
if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary')) |
570
|
|
|
{ |
571
|
|
|
// If we want to overwrite simply remove the current one then continue. |
572
|
|
|
if ($if_exists != 'update' || $index['type'] == 'primary') |
573
|
|
|
return false; |
574
|
|
|
else |
575
|
|
|
$smcFunc['db_remove_index']($table_name, $index_info['name']); |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
// If we're here we know we don't have the index - so just add it. |
580
|
|
|
if (!empty($index_info['type']) && $index_info['type'] == 'primary') |
581
|
|
|
{ |
582
|
|
|
$smcFunc['db_query']('', ' |
583
|
|
|
ALTER TABLE ' . $short_table_name . ' |
584
|
|
|
ADD PRIMARY KEY (' . $columns . ')', |
585
|
|
|
array( |
586
|
|
|
'security_override' => true, |
587
|
|
|
) |
588
|
|
|
); |
589
|
|
|
} |
590
|
|
|
else |
591
|
|
|
{ |
592
|
|
|
$smcFunc['db_query']('', ' |
593
|
|
|
ALTER TABLE ' . $short_table_name . ' |
594
|
|
|
ADD ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')', |
595
|
|
|
array( |
596
|
|
|
'security_override' => true, |
597
|
|
|
) |
598
|
|
|
); |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Remove an index. |
604
|
|
|
* |
605
|
|
|
* @param string $table_name The name of the table to remove the index from |
606
|
|
|
* @param string $index_name The name of the index to remove |
607
|
|
|
* @param array $parameters Not used? |
608
|
|
|
* @param string $error |
609
|
|
|
* @return boolean Whether or not the operation was successful |
610
|
|
|
*/ |
611
|
|
|
function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal') |
612
|
|
|
{ |
613
|
|
|
global $smcFunc, $db_prefix; |
614
|
|
|
|
615
|
|
|
$short_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
616
|
|
|
|
617
|
|
|
// Better exist! |
618
|
|
|
$indexes = $smcFunc['db_list_indexes']($table_name, true); |
619
|
|
|
|
620
|
|
|
foreach ($indexes as $index) |
621
|
|
|
{ |
622
|
|
|
// If the name is primary we want the primary key! |
623
|
|
|
if ($index['type'] == 'primary' && $index_name == 'primary') |
624
|
|
|
{ |
625
|
|
|
// Dropping primary key? |
626
|
|
|
$smcFunc['db_query']('', ' |
627
|
|
|
ALTER TABLE ' . $short_table_name . ' |
628
|
|
|
DROP PRIMARY KEY', |
629
|
|
|
array( |
630
|
|
|
'security_override' => true, |
631
|
|
|
) |
632
|
|
|
); |
633
|
|
|
|
634
|
|
|
return true; |
635
|
|
|
} |
636
|
|
|
if ($index['name'] == $index_name) |
637
|
|
|
{ |
638
|
|
|
// Drop the bugger... |
639
|
|
|
$smcFunc['db_query']('', ' |
640
|
|
|
ALTER TABLE ' . $short_table_name . ' |
641
|
|
|
DROP INDEX ' . $index_name, |
642
|
|
|
array( |
643
|
|
|
'security_override' => true, |
644
|
|
|
) |
645
|
|
|
); |
646
|
|
|
|
647
|
|
|
return true; |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
// Not to be found ;( |
652
|
|
|
return false; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Get the schema formatted name for a type. |
657
|
|
|
* |
658
|
|
|
* @param string $type_name The data type (int, varchar, smallint, etc.) |
659
|
|
|
* @param int $type_size The size (8, 255, etc.) |
660
|
|
|
* @param boolean $reverse |
661
|
|
|
* @return array An array containing the appropriate type and size for this DB type |
662
|
|
|
*/ |
663
|
|
|
function smf_db_calculate_type($type_name, $type_size = null, $reverse = false) |
664
|
|
|
{ |
665
|
|
|
// MySQL is actually the generic baseline. |
666
|
|
|
|
667
|
|
|
$type_name = strtolower($type_name); |
668
|
|
|
// Generic => Specific. |
669
|
|
|
if (!$reverse) |
670
|
|
|
{ |
671
|
|
|
$types = array( |
672
|
|
|
'inet' => 'varbinary', |
673
|
|
|
); |
674
|
|
|
} |
675
|
|
|
else |
676
|
|
|
{ |
677
|
|
|
$types = array( |
678
|
|
|
'varbinary' => 'inet', |
679
|
|
|
); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
// Got it? Change it! |
683
|
|
|
if (isset($types[$type_name])) |
684
|
|
|
{ |
685
|
|
|
if ($type_name == 'inet' && !$reverse) |
686
|
|
|
{ |
687
|
|
|
$type_size = 16; |
688
|
|
|
$type_name = 'varbinary'; |
689
|
|
|
} |
690
|
|
|
elseif ($type_name == 'varbinary' && $reverse && $type_size == 16) |
691
|
|
|
{ |
692
|
|
|
$type_name = 'inet'; |
693
|
|
|
$type_size = null; |
694
|
|
|
} |
695
|
|
|
elseif ($type_name == 'varbinary') |
696
|
|
|
$type_name = 'varbinary'; |
697
|
|
|
else |
698
|
|
|
$type_name = $types[$type_name]; |
699
|
|
|
} |
700
|
|
|
elseif ($type_name == 'boolean') |
701
|
|
|
$type_size = null; |
702
|
|
|
|
703
|
|
|
return array($type_name, $type_size); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* Get table structure. |
708
|
|
|
* |
709
|
|
|
* @param string $table_name The name of the table |
710
|
|
|
* @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()} |
711
|
|
|
*/ |
712
|
|
|
function smf_db_table_structure($table_name) |
713
|
|
|
{ |
714
|
|
|
global $smcFunc, $db_prefix, $db_name; |
715
|
|
|
|
716
|
|
|
$parsed_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
717
|
|
|
$real_table_name = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $parsed_table_name, $match) === 1 ? $match[3] : $parsed_table_name; |
718
|
|
|
$database = !empty($match[2]) ? $match[2] : $db_name; |
719
|
|
|
|
720
|
|
|
// Find the table engine and add that to the info as well |
721
|
|
|
$table_status = $smcFunc['db_query']('', ' |
722
|
|
|
SHOW TABLE STATUS |
723
|
|
|
IN {raw:db} |
724
|
|
|
LIKE {string:table}', |
725
|
|
|
array( |
726
|
|
|
'db' => $database, |
727
|
|
|
'table' => $real_table_name |
728
|
|
|
) |
729
|
|
|
); |
730
|
|
|
|
731
|
|
|
// Only one row, so no need for a loop... |
732
|
|
|
$row = $smcFunc['db_fetch_assoc']($table_status); |
733
|
|
|
|
734
|
|
|
$smcFunc['db_free_result']($table_status); |
735
|
|
|
|
736
|
|
|
return array( |
737
|
|
|
'name' => $parsed_table_name, |
738
|
|
|
'columns' => $smcFunc['db_list_columns']($table_name, true), |
739
|
|
|
'indexes' => $smcFunc['db_list_indexes']($table_name, true), |
740
|
|
|
'engine' => $row['Engine'], |
741
|
|
|
); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Return column information for a table. |
746
|
|
|
* |
747
|
|
|
* @param string $table_name The name of the table to get column info for |
748
|
|
|
* @param bool $detail Whether or not to return detailed info. If true, returns the column info. If false, just returns the column names. |
749
|
|
|
* @param array $parameters Not used? |
750
|
|
|
* @return array An array of column names or detailed column info, depending on $detail |
751
|
|
|
*/ |
752
|
|
|
function smf_db_list_columns($table_name, $detail = false, $parameters = array()) |
753
|
|
|
{ |
754
|
|
|
global $smcFunc, $db_prefix, $db_name; |
755
|
|
|
|
756
|
|
|
$parsed_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
757
|
|
|
$real_table_name = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $parsed_table_name, $match) === 1 ? $match[3] : $parsed_table_name; |
758
|
|
|
$database = !empty($match[2]) ? $match[2] : $db_name; |
|
|
|
|
759
|
|
|
|
760
|
|
|
$result = $smcFunc['db_query']('', ' |
761
|
|
|
SELECT column_name "Field", COLUMN_TYPE "Type", is_nullable "Null", COLUMN_KEY "Key" , column_default "Default", extra "Extra" |
762
|
|
|
FROM information_schema.columns |
763
|
|
|
WHERE table_name = {string:table_name} |
764
|
|
|
AND table_schema = {string:db_name} |
765
|
|
|
ORDER BY ordinal_position', |
766
|
|
|
array( |
767
|
|
|
'table_name' => $real_table_name, |
768
|
|
|
'db_name' => $db_name, |
769
|
|
|
) |
770
|
|
|
); |
771
|
|
|
$columns = array(); |
772
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($result)) |
773
|
|
|
{ |
774
|
|
|
if (!$detail) |
775
|
|
|
{ |
776
|
|
|
$columns[] = $row['Field']; |
777
|
|
|
} |
778
|
|
|
else |
779
|
|
|
{ |
780
|
|
|
// Is there an auto_increment? |
781
|
|
|
$auto = strpos($row['Extra'], 'auto_increment') !== false ? true : false; |
782
|
|
|
|
783
|
|
|
// Can we split out the size? |
784
|
|
|
if (preg_match('~(.+?)\s*\((\d+)\)(?:(?:\s*)?(unsigned))?~i', $row['Type'], $matches) === 1) |
785
|
|
|
{ |
786
|
|
|
$type = $matches[1]; |
787
|
|
|
$size = $matches[2]; |
788
|
|
|
if (!empty($matches[3]) && $matches[3] == 'unsigned') |
789
|
|
|
$unsigned = true; |
790
|
|
|
} |
791
|
|
|
else |
792
|
|
|
{ |
793
|
|
|
$type = $row['Type']; |
794
|
|
|
$size = null; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
$columns[$row['Field']] = array( |
798
|
|
|
'name' => $row['Field'], |
799
|
|
|
'not_null' => $row['Null'] != 'YES', |
800
|
|
|
'null' => $row['Null'] == 'YES', |
801
|
|
|
'default' => isset($row['Default']) ? $row['Default'] : null, |
802
|
|
|
'type' => $type, |
803
|
|
|
'size' => $size, |
804
|
|
|
'auto' => $auto, |
805
|
|
|
); |
806
|
|
|
|
807
|
|
|
if (isset($unsigned)) |
808
|
|
|
{ |
809
|
|
|
$columns[$row['Field']]['unsigned'] = $unsigned; |
810
|
|
|
unset($unsigned); |
811
|
|
|
} |
812
|
|
|
} |
813
|
|
|
} |
814
|
|
|
$smcFunc['db_free_result']($result); |
815
|
|
|
|
816
|
|
|
return $columns; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
/** |
820
|
|
|
* Get index information. |
821
|
|
|
* |
822
|
|
|
* @param string $table_name The name of the table to get indexes for |
823
|
|
|
* @param bool $detail Whether or not to return detailed info. |
824
|
|
|
* @param array $parameters Not used? |
825
|
|
|
* @return array An array of index names or a detailed array of index info, depending on $detail |
826
|
|
|
*/ |
827
|
|
|
function smf_db_list_indexes($table_name, $detail = false, $parameters = array()) |
828
|
|
|
{ |
829
|
|
|
global $smcFunc, $db_prefix, $db_name; |
830
|
|
|
|
831
|
|
|
$parsed_table_name = str_replace('{db_prefix}', $db_prefix, $table_name); |
832
|
|
|
$real_table_name = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $parsed_table_name, $match) === 1 ? $match[3] : $parsed_table_name; |
833
|
|
|
$database = !empty($match[2]) ? $match[2] : $db_name; |
834
|
|
|
|
835
|
|
|
$result = $smcFunc['db_query']('', ' |
836
|
|
|
SHOW KEYS |
837
|
|
|
FROM {raw:table_name} |
838
|
|
|
IN {raw:db}', |
839
|
|
|
array( |
840
|
|
|
'db' => $database, |
841
|
|
|
'table_name' => $real_table_name, |
842
|
|
|
) |
843
|
|
|
); |
844
|
|
|
$indexes = array(); |
845
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($result)) |
846
|
|
|
{ |
847
|
|
|
if (!$detail) |
848
|
|
|
$indexes[] = $row['Key_name']; |
849
|
|
|
else |
850
|
|
|
{ |
851
|
|
|
// What is the type? |
852
|
|
|
if ($row['Key_name'] == 'PRIMARY') |
853
|
|
|
$type = 'primary'; |
854
|
|
|
elseif (empty($row['Non_unique'])) |
855
|
|
|
$type = 'unique'; |
856
|
|
|
elseif (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT') |
857
|
|
|
$type = 'fulltext'; |
858
|
|
|
else |
859
|
|
|
$type = 'index'; |
860
|
|
|
|
861
|
|
|
// This is the first column we've seen? |
862
|
|
|
if (empty($indexes[$row['Key_name']])) |
863
|
|
|
{ |
864
|
|
|
$indexes[$row['Key_name']] = array( |
865
|
|
|
'name' => $row['Key_name'], |
866
|
|
|
'type' => $type, |
867
|
|
|
'columns' => array(), |
868
|
|
|
); |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
// Is it a partial index? |
872
|
|
|
if (!empty($row['Sub_part'])) |
873
|
|
|
$indexes[$row['Key_name']]['columns'][] = $row['Column_name'] . '(' . $row['Sub_part'] . ')'; |
874
|
|
|
else |
875
|
|
|
$indexes[$row['Key_name']]['columns'][] = $row['Column_name']; |
876
|
|
|
} |
877
|
|
|
} |
878
|
|
|
$smcFunc['db_free_result']($result); |
879
|
|
|
|
880
|
|
|
return $indexes; |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* Creates a query for a column |
885
|
|
|
* |
886
|
|
|
* @param array $column An array of column info |
887
|
|
|
* @return string The column definition |
888
|
|
|
*/ |
889
|
|
|
function smf_db_create_query_column($column) |
890
|
|
|
{ |
891
|
|
|
global $smcFunc; |
892
|
|
|
|
893
|
|
|
$column = array_change_key_case($column); |
894
|
|
|
|
895
|
|
|
// Auto increment is easy here! |
896
|
|
|
if (!empty($column['auto'])) |
897
|
|
|
$default = 'auto_increment'; |
898
|
|
|
// Make it null. |
899
|
|
|
elseif (array_key_exists('default', $column) && is_null($column['default'])) |
900
|
|
|
$default = 'DEFAULT NULL'; |
901
|
|
|
// Numbers don't need quotes. |
902
|
|
|
elseif (isset($column['default']) && is_numeric($column['default'])) |
903
|
|
|
$default = 'DEFAULT ' . (strpos($column['default'], '.') ? floatval($column['default']) : intval($column['default'])); |
904
|
|
|
// Non empty string. |
905
|
|
|
elseif (isset($column['default'])) |
906
|
|
|
$default = 'DEFAULT \'' . $smcFunc['db_escape_string']($column['default']) . '\''; |
907
|
|
|
else |
908
|
|
|
$default = ''; |
909
|
|
|
|
910
|
|
|
// Backwards compatible with the nullable column. |
911
|
|
|
if (isset($column['null']) && !isset($column['not_null'])) |
912
|
|
|
$column['not_null'] = !$column['null']; |
913
|
|
|
|
914
|
|
|
// Sort out the size... and stuff... |
915
|
|
|
$column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null; |
916
|
|
|
list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']); |
917
|
|
|
|
918
|
|
|
// Allow unsigned integers (mysql only) |
919
|
|
|
$unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : ''; |
920
|
|
|
|
921
|
|
|
if ($size !== null) |
922
|
|
|
$type = $type . '(' . $size . ')'; |
923
|
|
|
|
924
|
|
|
// Now just put it together! |
925
|
|
|
return '`' . $column['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (!empty($column['not_null']) ? 'NOT NULL' : '') . ' ' . $default; |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
?> |