DbPackages-mysql.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 2013 Simple Machines and individual contributors
  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('No direct access...');
  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. *
  79. * @param string $table_name
  80. * @param array $columns in the format specified.
  81. * @param array $indexes default array(), in the format specified.
  82. * @param array $parameters default array()
  83. * @param string $if_exists default 'ignore'
  84. * @param string $error default 'fatal'
  85. */
  86. function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal')
  87. {
  88. global $reservedTables, $smcFunc, $db_package_log, $db_prefix, $db_character_set;
  89. // Strip out the table name, we might not need it in some cases
  90. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  91. // With or without the database name, the fullname looks like this.
  92. $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
  93. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  94. // First - no way do we touch SMF tables.
  95. if (in_array(strtolower($table_name), $reservedTables))
  96. return false;
  97. // Log that we'll want to remove this on uninstall.
  98. $db_package_log[] = array('remove_table', $table_name);
  99. // Slightly easier on MySQL than the others...
  100. $tables = $smcFunc['db_list_tables']();
  101. if (in_array($full_table_name, $tables))
  102. {
  103. // This is a sad day... drop the table? If not, return false (error) by default.
  104. if ($if_exists == 'overwrite')
  105. $smcFunc['db_drop_table']($table_name);
  106. else
  107. return $if_exists == 'ignore';
  108. }
  109. // Righty - let's do the damn thing!
  110. $table_query = 'CREATE TABLE ' . $table_name . "\n" . '(';
  111. foreach ($columns as $column)
  112. $table_query .= "\n\t" . smf_db_create_query_column($column) . ',';
  113. // Loop through the indexes next...
  114. foreach ($indexes as $index)
  115. {
  116. $columns = implode(',', $index['columns']);
  117. // Is it the primary?
  118. if (isset($index['type']) && $index['type'] == 'primary')
  119. $table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),';
  120. else
  121. {
  122. if (empty($index['name']))
  123. $index['name'] = implode('_', $index['columns']);
  124. $table_query .= "\n\t" . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : 'KEY') . ' ' . $index['name'] . ' (' . $columns . '),';
  125. }
  126. }
  127. // No trailing commas!
  128. if (substr($table_query, -1) == ',')
  129. $table_query = substr($table_query, 0, -1);
  130. $table_query .= ') ENGINE=MyISAM';
  131. if (!empty($db_character_set) && $db_character_set == 'utf8')
  132. $table_query .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
  133. // Create the table!
  134. $smcFunc['db_query']('', $table_query,
  135. array(
  136. 'security_override' => true,
  137. )
  138. );
  139. }
  140. /**
  141. * Drop a table.
  142. *
  143. * @param string $table_name
  144. * @param array $parameters default array()
  145. * @param string $error default 'fatal'
  146. */
  147. function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal')
  148. {
  149. global $reservedTables, $smcFunc, $db_prefix;
  150. // After stripping away the database name, this is what's left.
  151. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  152. // Get some aliases.
  153. $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
  154. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  155. // God no - dropping one of these = bad.
  156. if (in_array(strtolower($table_name), $reservedTables))
  157. return false;
  158. // Does it exist?
  159. if (in_array($full_table_name, $smcFunc['db_list_tables']()))
  160. {
  161. $query = 'DROP TABLE ' . $table_name;
  162. $smcFunc['db_query']('',
  163. $query,
  164. array(
  165. 'security_override' => true,
  166. )
  167. );
  168. return true;
  169. }
  170. // Otherwise do 'nout.
  171. return false;
  172. }
  173. /**
  174. * This function adds a column.
  175. *
  176. * @param string $table_name the name of the table
  177. * @param array $column_info with column information
  178. * @param array $parameters default array()
  179. * @param string $if_exists default 'update'
  180. * @param string $error default 'fatal'
  181. */
  182. function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  183. {
  184. global $smcFunc, $db_package_log, $db_prefix;
  185. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  186. // Log that we will want to uninstall this!
  187. $db_package_log[] = array('remove_column', $table_name, $column_info['name']);
  188. // Does it exist - if so don't add it again!
  189. $columns = $smcFunc['db_list_columns']($table_name, false);
  190. foreach ($columns as $column)
  191. if ($column == $column_info['name'])
  192. {
  193. // If we're going to overwrite then use change column.
  194. if ($if_exists == 'update')
  195. return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info);
  196. else
  197. return false;
  198. }
  199. // Get the specifics...
  200. $column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null;
  201. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  202. // Allow unsigned integers (mysql only)
  203. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  204. if ($size !== null)
  205. $type = $type . '(' . $size . ')';
  206. // Now add the thing!
  207. $query = '
  208. ALTER TABLE ' . $table_name . '
  209. ADD ' . smf_db_create_query_column($column_info) . (empty($column_info['auto']) ? '' : ' primary key');
  210. $smcFunc['db_query']('', $query,
  211. array(
  212. 'security_override' => true,
  213. )
  214. );
  215. return true;
  216. }
  217. /**
  218. * Removes a column.
  219. *
  220. * @param string $table_name
  221. * @param string $column_name
  222. * @param array $parameters default array()
  223. * @param string $error default 'fatal'
  224. */
  225. function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal')
  226. {
  227. global $smcFunc, $db_prefix;
  228. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  229. // Does it exist?
  230. $columns = $smcFunc['db_list_columns']($table_name, true);
  231. foreach ($columns as $column)
  232. if ($column['name'] == $column_name)
  233. {
  234. $smcFunc['db_query']('', '
  235. ALTER TABLE ' . $table_name . '
  236. DROP COLUMN ' . $column_name,
  237. array(
  238. 'security_override' => true,
  239. )
  240. );
  241. return true;
  242. }
  243. // If here we didn't have to work - joy!
  244. return false;
  245. }
  246. /**
  247. * Change a column.
  248. *
  249. * @param string $table_name
  250. * @param $old_column
  251. * @param $column_info
  252. * @param array $parameters default array()
  253. * @param string $error default 'fatal'
  254. */
  255. function smf_db_change_column($table_name, $old_column, $column_info, $parameters = array(), $error = 'fatal')
  256. {
  257. global $smcFunc, $db_prefix;
  258. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  259. // Check it does exist!
  260. $columns = $smcFunc['db_list_columns']($table_name, true);
  261. $old_info = null;
  262. foreach ($columns as $column)
  263. if ($column['name'] == $old_column)
  264. $old_info = $column;
  265. // Nothing?
  266. if ($old_info == null)
  267. return false;
  268. // Get the right bits.
  269. if (!isset($column_info['name']))
  270. $column_info['name'] = $old_column;
  271. if (!isset($column_info['default']))
  272. $column_info['default'] = $old_info['default'];
  273. if (!isset($column_info['null']))
  274. $column_info['null'] = $old_info['null'];
  275. if (!isset($column_info['auto']))
  276. $column_info['auto'] = $old_info['auto'];
  277. if (!isset($column_info['type']))
  278. $column_info['type'] = $old_info['type'];
  279. if (!isset($column_info['size']) || !is_numeric($column_info['size']))
  280. $column_info['size'] = $old_info['size'];
  281. if (!isset($column_info['unsigned']) || !in_array($column_info['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')))
  282. $column_info['unsigned'] = '';
  283. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  284. // Allow for unsigned integers (mysql only)
  285. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  286. if ($size !== null)
  287. $type = $type . '(' . $size . ')';
  288. $smcFunc['db_query']('', '
  289. ALTER TABLE ' . $table_name . '
  290. CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
  291. (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
  292. (empty($column_info['auto']) ? '' : 'auto_increment') . ' ',
  293. array(
  294. 'security_override' => true,
  295. )
  296. );
  297. }
  298. /**
  299. * Add an index.
  300. *
  301. * @param string $table_name
  302. * @param array $index_info
  303. * @param array $parameters default array()
  304. * @param string $if_exists default 'update'
  305. * @param string $error default 'fatal'
  306. */
  307. function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  308. {
  309. global $smcFunc, $db_package_log, $db_prefix;
  310. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  311. // No columns = no index.
  312. if (empty($index_info['columns']))
  313. return false;
  314. $columns = implode(',', $index_info['columns']);
  315. // No name - make it up!
  316. if (empty($index_info['name']))
  317. {
  318. // No need for primary.
  319. if (isset($index_info['type']) && $index_info['type'] == 'primary')
  320. $index_info['name'] = '';
  321. else
  322. $index_info['name'] = implode('_', $index_info['columns']);
  323. }
  324. else
  325. $index_info['name'] = $index_info['name'];
  326. // Log that we are going to want to remove this!
  327. $db_package_log[] = array('remove_index', $table_name, $index_info['name']);
  328. // Let's get all our indexes.
  329. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  330. // Do we already have it?
  331. foreach ($indexes as $index)
  332. {
  333. if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary'))
  334. {
  335. // If we want to overwrite simply remove the current one then continue.
  336. if ($if_exists != 'update' || $index['type'] == 'primary')
  337. return false;
  338. else
  339. $smcFunc['db_remove_index']($table_name, $index_info['name']);
  340. }
  341. }
  342. // If we're here we know we don't have the index - so just add it.
  343. if (!empty($index_info['type']) && $index_info['type'] == 'primary')
  344. {
  345. $smcFunc['db_query']('', '
  346. ALTER TABLE ' . $table_name . '
  347. ADD PRIMARY KEY (' . $columns . ')',
  348. array(
  349. 'security_override' => true,
  350. )
  351. );
  352. }
  353. else
  354. {
  355. $smcFunc['db_query']('', '
  356. ALTER TABLE ' . $table_name . '
  357. ADD ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')',
  358. array(
  359. 'security_override' => true,
  360. )
  361. );
  362. }
  363. }
  364. /**
  365. * Remove an index.
  366. *
  367. * @param string $table_name
  368. * @param string $index_name
  369. * @param array$parameters default array()
  370. * @param string $error default 'fatal'
  371. */
  372. function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal')
  373. {
  374. global $smcFunc, $db_prefix;
  375. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  376. // Better exist!
  377. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  378. foreach ($indexes as $index)
  379. {
  380. // If the name is primary we want the primary key!
  381. if ($index['type'] == 'primary' && $index_name == 'primary')
  382. {
  383. // Dropping primary key?
  384. $smcFunc['db_query']('', '
  385. ALTER TABLE ' . $table_name . '
  386. DROP PRIMARY KEY',
  387. array(
  388. 'security_override' => true,
  389. )
  390. );
  391. return true;
  392. }
  393. if ($index['name'] == $index_name)
  394. {
  395. // Drop the bugger...
  396. $smcFunc['db_query']('', '
  397. ALTER TABLE ' . $table_name . '
  398. DROP INDEX ' . $index_name,
  399. array(
  400. 'security_override' => true,
  401. )
  402. );
  403. return true;
  404. }
  405. }
  406. // Not to be found ;(
  407. return false;
  408. }
  409. /**
  410. * Get the schema formatted name for a type.
  411. *
  412. * @param string $type_name
  413. * @param $type_size
  414. * @param $reverse
  415. */
  416. function smf_db_calculate_type($type_name, $type_size = null, $reverse = false)
  417. {
  418. // MySQL is actually the generic baseline.
  419. return array($type_name, $type_size);
  420. }
  421. /**
  422. * Get table structure.
  423. *
  424. * @param string $table_name
  425. * @param array $parameters default array()
  426. */
  427. function smf_db_table_structure($table_name, $parameters = array())
  428. {
  429. global $smcFunc, $db_prefix;
  430. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  431. return array(
  432. 'name' => $table_name,
  433. 'columns' => $smcFunc['db_list_columns']($table_name, true),
  434. 'indexes' => $smcFunc['db_list_indexes']($table_name, true),
  435. );
  436. }
  437. /**
  438. * Return column information for a table.
  439. *
  440. * @param string $table_name
  441. * @param bool $detail
  442. * @param array $parameters default array()
  443. * @return mixed
  444. */
  445. function smf_db_list_columns($table_name, $detail = false, $parameters = array())
  446. {
  447. global $smcFunc, $db_prefix;
  448. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  449. $result = $smcFunc['db_query']('', '
  450. SHOW FIELDS
  451. FROM {raw:table_name}',
  452. array(
  453. 'table_name' => substr($table_name, 0, 1) == '`' ? $table_name : '`' . $table_name . '`',
  454. )
  455. );
  456. $columns = array();
  457. while ($row = $smcFunc['db_fetch_assoc']($result))
  458. {
  459. if (!$detail)
  460. {
  461. $columns[] = $row['Field'];
  462. }
  463. else
  464. {
  465. // Is there an auto_increment?
  466. $auto = strpos($row['Extra'], 'auto_increment') !== false ? true : false;
  467. // Can we split out the size?
  468. if (preg_match('~(.+?)\s*\((\d+)\)(?:(?:\s*)?(unsigned))?~i', $row['Type'], $matches) === 1)
  469. {
  470. $type = $matches[1];
  471. $size = $matches[2];
  472. if (!empty($matches[3]) && $matches[3] == 'unsigned')
  473. $unsigned = true;
  474. }
  475. else
  476. {
  477. $type = $row['Type'];
  478. $size = null;
  479. }
  480. $columns[$row['Field']] = array(
  481. 'name' => $row['Field'],
  482. 'null' => $row['Null'] != 'YES' ? false : true,
  483. 'default' => isset($row['Default']) ? $row['Default'] : null,
  484. 'type' => $type,
  485. 'size' => $size,
  486. 'auto' => $auto,
  487. );
  488. if (isset($unsigned))
  489. {
  490. $columns[$row['Field']]['unsigned'] = $unsigned;
  491. unset($unsigned);
  492. }
  493. }
  494. }
  495. $smcFunc['db_free_result']($result);
  496. return $columns;
  497. }
  498. /**
  499. * Get index information.
  500. *
  501. * @param string $table_name
  502. * @param bool $detail
  503. * @param array $parameters
  504. * @return mixed
  505. */
  506. function smf_db_list_indexes($table_name, $detail = false, $parameters = array())
  507. {
  508. global $smcFunc, $db_prefix;
  509. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  510. $result = $smcFunc['db_query']('', '
  511. SHOW KEYS
  512. FROM {raw:table_name}',
  513. array(
  514. 'table_name' => substr($table_name, 0, 1) == '`' ? $table_name : '`' . $table_name . '`',
  515. )
  516. );
  517. $indexes = array();
  518. while ($row = $smcFunc['db_fetch_assoc']($result))
  519. {
  520. if (!$detail)
  521. $indexes[] = $row['Key_name'];
  522. else
  523. {
  524. // What is the type?
  525. if ($row['Key_name'] == 'PRIMARY')
  526. $type = 'primary';
  527. elseif (empty($row['Non_unique']))
  528. $type = 'unique';
  529. elseif (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT')
  530. $type = 'fulltext';
  531. else
  532. $type = 'index';
  533. // This is the first column we've seen?
  534. if (empty($indexes[$row['Key_name']]))
  535. {
  536. $indexes[$row['Key_name']] = array(
  537. 'name' => $row['Key_name'],
  538. 'type' => $type,
  539. 'columns' => array(),
  540. );
  541. }
  542. // Is it a partial index?
  543. if (!empty($row['Sub_part']))
  544. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'] . '(' . $row['Sub_part'] . ')';
  545. else
  546. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'];
  547. }
  548. }
  549. $smcFunc['db_free_result']($result);
  550. return $indexes;
  551. }
  552. /**
  553. * Creates a query for a column
  554. *
  555. * @param array $column
  556. * @return type
  557. */
  558. function smf_db_create_query_column($column)
  559. {
  560. global $smcFunc;
  561. // Auto increment is easy here!
  562. if (!empty($column['auto']))
  563. {
  564. $default = 'auto_increment';
  565. }
  566. elseif (isset($column['default']) && $column['default'] !== null)
  567. $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\'';
  568. else
  569. $default = '';
  570. // Sort out the size... and stuff...
  571. $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
  572. list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']);
  573. // Allow unsigned integers (mysql only)
  574. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';
  575. if ($size !== null)
  576. $type = $type . '(' . $size . ')';
  577. // Now just put it together!
  578. return '`' .$column['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default;
  579. }
  580. ?>