DbPackages-mysql.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <?php
  2. /**
  3. * This file contains database functionality specifically designed for packages (mods) to utilize.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Add the file functions to the $smcFunc array.
  18. */
  19. function db_packages_init()
  20. {
  21. global $smcFunc, $reservedTables, $db_package_log, $db_prefix;
  22. if (!isset($smcFunc['db_create_table']) || $smcFunc['db_create_table'] != 'smf_db_create_table')
  23. {
  24. $smcFunc += array(
  25. 'db_add_column' => 'smf_db_add_column',
  26. 'db_add_index' => 'smf_db_add_index',
  27. 'db_calculate_type' => 'smf_db_calculate_type',
  28. 'db_change_column' => 'smf_db_change_column',
  29. 'db_create_table' => 'smf_db_create_table',
  30. 'db_drop_table' => 'smf_db_drop_table',
  31. 'db_table_structure' => 'smf_db_table_structure',
  32. 'db_list_columns' => 'smf_db_list_columns',
  33. 'db_list_indexes' => 'smf_db_list_indexes',
  34. 'db_remove_column' => 'smf_db_remove_column',
  35. 'db_remove_index' => 'smf_db_remove_index',
  36. );
  37. $db_package_log = array();
  38. }
  39. // We setup an array of SMF tables we can't do auto-remove on - in case a mod writer cocks it up!
  40. $reservedTables = array('admin_info_files', 'approval_queue', 'attachments', 'ban_groups', 'ban_items',
  41. 'board_permissions', 'boards', 'calendar', 'calendar_holidays', 'categories', 'collapsed_categories',
  42. 'custom_fields', 'group_moderators', 'log_actions', 'log_activity', 'log_banned', 'log_boards',
  43. 'log_digest', 'log_errors', 'log_floodcontrol', 'log_group_requests', 'log_karma', 'log_mark_read',
  44. 'log_notify', 'log_online', 'log_packages', 'log_polls', 'log_reported', 'log_reported_comments',
  45. 'log_scheduled_tasks', 'log_search_messages', 'log_search_results', 'log_search_subjects',
  46. 'log_search_topics', 'log_topics', 'mail_queue', 'membergroups', 'members', 'message_icons',
  47. 'messages', 'moderators', 'package_servers', 'permission_profiles', 'permissions', 'personal_messages',
  48. 'pm_recipients', 'poll_choices', 'polls', 'scheduled_tasks', 'sessions', 'settings', 'smileys',
  49. 'themes', 'topics');
  50. foreach ($reservedTables as $k => $table_name)
  51. $reservedTables[$k] = strtolower($db_prefix . $table_name);
  52. // We in turn may need the extra stuff.
  53. db_extend('extra');
  54. }
  55. /**
  56. * This function can be used to create a table without worrying about schema
  57. * compatabilities across supported database systems.
  58. * - If the table exists will, by default, do nothing.
  59. * - Builds table with columns as passed to it - at least one column must be sent.
  60. * The columns array should have one sub-array for each column - these sub arrays contain:
  61. * 'name' = Column name
  62. * 'type' = Type of column - values from (smallint, mediumint, int, text, varchar, char, tinytext, mediumtext, largetext)
  63. * 'size' => Size of column (If applicable) - for example 255 for a large varchar, 10 for an int etc.
  64. * If not set SMF will pick a size.
  65. * - 'default' = Default value - do not set if no default required.
  66. * - 'null' => Can it be null (true or false) - if not set default will be false.
  67. * - 'auto' => Set to true to make it an auto incrementing column. Set to a numerical value to set from what
  68. * it should begin counting.
  69. * - Adds indexes as specified within indexes parameter. Each index should be a member of $indexes. Values are:
  70. * - 'name' => Index name (If left empty SMF will generate).
  71. * - 'type' => Type of index. Choose from 'primary', 'unique' or 'index'. If not set will default to 'index'.
  72. * - 'columns' => Array containing columns that form part of key - in the order the index is to be created.
  73. * - parameters: (None yet)
  74. * - if_exists values:
  75. * - 'ignore' will do nothing if the table exists. (And will return true)
  76. * - 'overwrite' will drop any existing table of the same name.
  77. * - 'error' will return false if the table already exists.
  78. * @param string $table_name
  79. * @param array $columns, in the format specified.
  80. * @param array $indexes, default array(), in the format specified.
  81. * @param array $parameters, default array()
  82. * @param string $if_exists, default 'ignore'
  83. * @param string $error, default 'fatal'
  84. */
  85. function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal')
  86. {
  87. global $reservedTables, $smcFunc, $db_package_log, $db_prefix, $db_character_set;
  88. // Strip out the table name, we might not need it in some cases
  89. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  90. // With or without the database name, the fullname looks like this.
  91. $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
  92. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  93. // First - no way do we touch SMF tables.
  94. if (in_array(strtolower($table_name), $reservedTables))
  95. return false;
  96. // Log that we'll want to remove this on uninstall.
  97. $db_package_log[] = array('remove_table', $table_name);
  98. // Slightly easier on MySQL than the others...
  99. $tables = $smcFunc['db_list_tables']();
  100. if (in_array($full_table_name, $tables))
  101. {
  102. // This is a sad day... drop the table? If not, return false (error) by default.
  103. if ($if_exists == 'overwrite')
  104. $smcFunc['db_drop_table']($table_name);
  105. else
  106. return $if_exists == 'ignore';
  107. }
  108. // Righty - let's do the damn thing!
  109. $table_query = 'CREATE TABLE ' . $table_name . "\n" . '(';
  110. foreach ($columns as $column)
  111. $table_query .= "\n\t" . smf_db_create_query_column($column) . ',';
  112. // Loop through the indexes next...
  113. foreach ($indexes as $index)
  114. {
  115. $columns = implode(',', $index['columns']);
  116. // Is it the primary?
  117. if (isset($index['type']) && $index['type'] == 'primary')
  118. $table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),';
  119. else
  120. {
  121. if (empty($index['name']))
  122. $index['name'] = implode('_', $index['columns']);
  123. $table_query .= "\n\t" . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : 'KEY') . ' ' . $index['name'] . ' (' . $columns . '),';
  124. }
  125. }
  126. // No trailing commas!
  127. if (substr($table_query, -1) == ',')
  128. $table_query = substr($table_query, 0, -1);
  129. $table_query .= ') ENGINE=MyISAM';
  130. if (!empty($db_character_set) && $db_character_set == 'utf8')
  131. $table_query .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
  132. // Create the table!
  133. $smcFunc['db_query']('', $table_query,
  134. array(
  135. 'security_override' => true,
  136. )
  137. );
  138. }
  139. /**
  140. * Drop a table.
  141. * @param string $table_name
  142. * @param array $parameters, default array()
  143. * @param string $error, default 'fatal'
  144. */
  145. function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal')
  146. {
  147. global $reservedTables, $smcFunc, $db_prefix;
  148. // After stripping away the database name, this is what's left.
  149. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  150. // Get some aliases.
  151. $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
  152. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  153. // God no - dropping one of these = bad.
  154. if (in_array(strtolower($table_name), $reservedTables))
  155. return false;
  156. // Does it exist?
  157. if (in_array($full_table_name, $smcFunc['db_list_tables']()))
  158. {
  159. $query = 'DROP TABLE ' . $table_name;
  160. $smcFunc['db_query']('',
  161. $query,
  162. array(
  163. 'security_override' => true,
  164. )
  165. );
  166. return true;
  167. }
  168. // Otherwise do 'nout.
  169. return false;
  170. }
  171. /**
  172. * This function adds a column.
  173. * @param string $table_name, the name of the table
  174. * @param array $column_info, with column information
  175. * @param array $parameters, default array()
  176. * @param string $if_exists, default 'update'
  177. * @param string $error, default 'fatal'
  178. */
  179. function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  180. {
  181. global $smcFunc, $db_package_log, $txt, $db_prefix;
  182. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  183. // Log that we will want to uninstall this!
  184. $db_package_log[] = array('remove_column', $table_name, $column_info['name']);
  185. // Does it exist - if so don't add it again!
  186. $columns = $smcFunc['db_list_columns']($table_name, false);
  187. foreach ($columns as $column)
  188. if ($column == $column_info['name'])
  189. {
  190. // If we're going to overwrite then use change column.
  191. if ($if_exists == 'update')
  192. return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info);
  193. else
  194. return false;
  195. }
  196. // Get the specifics...
  197. $column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null;
  198. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  199. // Allow unsigned integers (mysql only)
  200. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  201. if ($size !== null)
  202. $type = $type . '(' . $size . ')';
  203. // Now add the thing!
  204. $query = '
  205. ALTER TABLE ' . $table_name . '
  206. ADD ' . smf_db_create_query_column($column_info) . (empty($column_info['auto']) ? '' : ' primary key');
  207. $smcFunc['db_query']('', $query,
  208. array(
  209. 'security_override' => true,
  210. )
  211. );
  212. return true;
  213. }
  214. /**
  215. * Removes a column.
  216. * @param string $table_name
  217. * @param string $column_name
  218. * @param array $parameters, default array()
  219. * @param string $error, default 'fatal'
  220. */
  221. function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal')
  222. {
  223. global $smcFunc, $db_prefix;
  224. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  225. // Does it exist?
  226. $columns = $smcFunc['db_list_columns']($table_name, true);
  227. foreach ($columns as $column)
  228. if ($column['name'] == $column_name)
  229. {
  230. $smcFunc['db_query']('', '
  231. ALTER TABLE ' . $table_name . '
  232. DROP COLUMN ' . $column_name,
  233. array(
  234. 'security_override' => true,
  235. )
  236. );
  237. return true;
  238. }
  239. // If here we didn't have to work - joy!
  240. return false;
  241. }
  242. /**
  243. * Change a column.
  244. * @param string $table_name
  245. * @param $old_column
  246. * @param $column_info
  247. * @param array $parameters, default array()
  248. * @param string $error, default 'fatal'
  249. */
  250. function smf_db_change_column($table_name, $old_column, $column_info, $parameters = array(), $error = 'fatal')
  251. {
  252. global $smcFunc, $db_prefix;
  253. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  254. // Check it does exist!
  255. $columns = $smcFunc['db_list_columns']($table_name, true);
  256. $old_info = null;
  257. foreach ($columns as $column)
  258. if ($column['name'] == $old_column)
  259. $old_info = $column;
  260. // Nothing?
  261. if ($old_info == null)
  262. return false;
  263. // Get the right bits.
  264. if (!isset($column_info['name']))
  265. $column_info['name'] = $old_column;
  266. if (!isset($column_info['default']))
  267. $column_info['default'] = $old_info['default'];
  268. if (!isset($column_info['null']))
  269. $column_info['null'] = $old_info['null'];
  270. if (!isset($column_info['auto']))
  271. $column_info['auto'] = $old_info['auto'];
  272. if (!isset($column_info['type']))
  273. $column_info['type'] = $old_info['type'];
  274. if (!isset($column_info['size']) || !is_numeric($column_info['size']))
  275. $column_info['size'] = $old_info['size'];
  276. if (!isset($column_info['unsigned']) || !in_array($column_info['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')))
  277. $column_info['unsigned'] = '';
  278. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  279. // Allow for unsigned integers (mysql only)
  280. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  281. if ($size !== null)
  282. $type = $type . '(' . $size . ')';
  283. $smcFunc['db_query']('', '
  284. ALTER TABLE ' . $table_name . '
  285. CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
  286. (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
  287. (empty($column_info['auto']) ? '' : 'auto_increment') . ' ',
  288. array(
  289. 'security_override' => true,
  290. )
  291. );
  292. }
  293. /**
  294. * Add an index.
  295. * @param string $table_name
  296. * @param array $index_info
  297. * @param array $parameters, default array()
  298. * @param string $if_exists, default 'update'
  299. * @param string $error, default 'fatal'
  300. */
  301. function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  302. {
  303. global $smcFunc, $db_package_log, $db_prefix;
  304. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  305. // No columns = no index.
  306. if (empty($index_info['columns']))
  307. return false;
  308. $columns = implode(',', $index_info['columns']);
  309. // No name - make it up!
  310. if (empty($index_info['name']))
  311. {
  312. // No need for primary.
  313. if (isset($index_info['type']) && $index_info['type'] == 'primary')
  314. $index_info['name'] = '';
  315. else
  316. $index_info['name'] = implode('_', $index_info['columns']);
  317. }
  318. else
  319. $index_info['name'] = $index_info['name'];
  320. // Log that we are going to want to remove this!
  321. $db_package_log[] = array('remove_index', $table_name, $index_info['name']);
  322. // Let's get all our indexes.
  323. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  324. // Do we already have it?
  325. foreach ($indexes as $index)
  326. {
  327. if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary'))
  328. {
  329. // If we want to overwrite simply remove the current one then continue.
  330. if ($if_exists != 'update' || $index['type'] == 'primary')
  331. return false;
  332. else
  333. $smcFunc['db_remove_index']($table_name, $index_info['name']);
  334. }
  335. }
  336. // If we're here we know we don't have the index - so just add it.
  337. if (!empty($index_info['type']) && $index_info['type'] == 'primary')
  338. {
  339. $smcFunc['db_query']('', '
  340. ALTER TABLE ' . $table_name . '
  341. ADD PRIMARY KEY (' . $columns . ')',
  342. array(
  343. 'security_override' => true,
  344. )
  345. );
  346. }
  347. else
  348. {
  349. $smcFunc['db_query']('', '
  350. ALTER TABLE ' . $table_name . '
  351. ADD ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')',
  352. array(
  353. 'security_override' => true,
  354. )
  355. );
  356. }
  357. }
  358. /**
  359. * Remove an index.
  360. * @param string $table_name
  361. * @param string $index_name
  362. * @param array$parameters, default array()
  363. * @param string $error, default 'fatal'
  364. */
  365. function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal')
  366. {
  367. global $smcFunc, $db_prefix;
  368. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  369. // Better exist!
  370. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  371. foreach ($indexes as $index)
  372. {
  373. // If the name is primary we want the primary key!
  374. if ($index['type'] == 'primary' && $index_name == 'primary')
  375. {
  376. // Dropping primary key?
  377. $smcFunc['db_query']('', '
  378. ALTER TABLE ' . $table_name . '
  379. DROP PRIMARY KEY',
  380. array(
  381. 'security_override' => true,
  382. )
  383. );
  384. return true;
  385. }
  386. if ($index['name'] == $index_name)
  387. {
  388. // Drop the bugger...
  389. $smcFunc['db_query']('', '
  390. ALTER TABLE ' . $table_name . '
  391. DROP INDEX ' . $index_name,
  392. array(
  393. 'security_override' => true,
  394. )
  395. );
  396. return true;
  397. }
  398. }
  399. // Not to be found ;(
  400. return false;
  401. }
  402. /**
  403. * Get the schema formatted name for a type.
  404. * @param string $type_name
  405. * @param $type_size
  406. * @param $reverse
  407. */
  408. function smf_db_calculate_type($type_name, $type_size = null, $reverse = false)
  409. {
  410. // MySQL is actually the generic baseline.
  411. return array($type_name, $type_size);
  412. }
  413. /**
  414. * Get table structure.
  415. * @param string $table_name
  416. * @param array $parameters, default array()
  417. */
  418. function smf_db_table_structure($table_name, $parameters = array())
  419. {
  420. global $smcFunc, $db_prefix;
  421. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  422. return array(
  423. 'name' => $table_name,
  424. 'columns' => $smcFunc['db_list_columns']($table_name, true),
  425. 'indexes' => $smcFunc['db_list_indexes']($table_name, true),
  426. );
  427. }
  428. /**
  429. * Return column information for a table.
  430. * @param string $table_name
  431. * @param bool $detail
  432. * @param array $parameters, default array()
  433. * @return mixed
  434. */
  435. function smf_db_list_columns($table_name, $detail = false, $parameters = array())
  436. {
  437. global $smcFunc, $db_prefix;
  438. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  439. $result = $smcFunc['db_query']('', '
  440. SHOW FIELDS
  441. FROM {raw:table_name}',
  442. array(
  443. 'table_name' => substr($table_name, 0, 1) == '`' ? $table_name : '`' . $table_name . '`',
  444. )
  445. );
  446. $columns = array();
  447. while ($row = $smcFunc['db_fetch_assoc']($result))
  448. {
  449. if (!$detail)
  450. {
  451. $columns[] = $row['Field'];
  452. }
  453. else
  454. {
  455. // Is there an auto_increment?
  456. $auto = strpos($row['Extra'], 'auto_increment') !== false ? true : false;
  457. // Can we split out the size?
  458. if (preg_match('~(.+?)\s*\((\d+)\)(?:(?:\s*)?(unsigned))?~i', $row['Type'], $matches) === 1)
  459. {
  460. $type = $matches[1];
  461. $size = $matches[2];
  462. if (!empty($matches[3]) && $matches[3] == 'unsigned')
  463. $unsigned = true;
  464. }
  465. else
  466. {
  467. $type = $row['Type'];
  468. $size = null;
  469. }
  470. $columns[$row['Field']] = array(
  471. 'name' => $row['Field'],
  472. 'null' => $row['Null'] != 'YES' ? false : true,
  473. 'default' => isset($row['Default']) ? $row['Default'] : null,
  474. 'type' => $type,
  475. 'size' => $size,
  476. 'auto' => $auto,
  477. );
  478. if (isset($unsigned))
  479. {
  480. $columns[$row['Field']]['unsigned'] = $unsigned;
  481. unset($unsigned);
  482. }
  483. }
  484. }
  485. $smcFunc['db_free_result']($result);
  486. return $columns;
  487. }
  488. /**
  489. * Get index information.
  490. * @param string $table_name
  491. * @param bool $detail
  492. * @param array $parameters
  493. * @return mixed
  494. */
  495. function smf_db_list_indexes($table_name, $detail = false, $parameters = array())
  496. {
  497. global $smcFunc, $db_prefix;
  498. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  499. $result = $smcFunc['db_query']('', '
  500. SHOW KEYS
  501. FROM {raw:table_name}',
  502. array(
  503. 'table_name' => substr($table_name, 0, 1) == '`' ? $table_name : '`' . $table_name . '`',
  504. )
  505. );
  506. $indexes = array();
  507. while ($row = $smcFunc['db_fetch_assoc']($result))
  508. {
  509. if (!$detail)
  510. $indexes[] = $row['Key_name'];
  511. else
  512. {
  513. // What is the type?
  514. if ($row['Key_name'] == 'PRIMARY')
  515. $type = 'primary';
  516. elseif (empty($row['Non_unique']))
  517. $type = 'unique';
  518. elseif (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT')
  519. $type = 'fulltext';
  520. else
  521. $type = 'index';
  522. // This is the first column we've seen?
  523. if (empty($indexes[$row['Key_name']]))
  524. {
  525. $indexes[$row['Key_name']] = array(
  526. 'name' => $row['Key_name'],
  527. 'type' => $type,
  528. 'columns' => array(),
  529. );
  530. }
  531. // Is it a partial index?
  532. if (!empty($row['Sub_part']))
  533. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'] . '(' . $row['Sub_part'] . ')';
  534. else
  535. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'];
  536. }
  537. }
  538. $smcFunc['db_free_result']($result);
  539. return $indexes;
  540. }
  541. function smf_db_create_query_column($column)
  542. {
  543. global $smcFunc;
  544. // Auto increment is easy here!
  545. if (!empty($column['auto']))
  546. {
  547. $default = 'auto_increment';
  548. }
  549. elseif (isset($column['default']) && $column['default'] !== null)
  550. $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\'';
  551. else
  552. $default = '';
  553. // Sort out the size... and stuff...
  554. $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
  555. list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']);
  556. // Allow unsigned integers (mysql only)
  557. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';
  558. if ($size !== null)
  559. $type = $type . '(' . $size . ')';
  560. // Now just put it together!
  561. return '`' .$column['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default;
  562. }
  563. ?>