DbPackages-mysql.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
  207. (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
  208. (empty($column_info['auto']) ? '' : 'auto_increment primary key') . ' ';
  209. $smcFunc['db_query']('', $query,
  210. array(
  211. 'security_override' => true,
  212. )
  213. );
  214. return true;
  215. }
  216. /**
  217. * Removes a column.
  218. * @param string $table_name
  219. * @param string $column_name
  220. * @param array $parameters, default array()
  221. * @param string $error, default 'fatal'
  222. */
  223. function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal')
  224. {
  225. global $smcFunc, $db_prefix;
  226. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  227. // Does it exist?
  228. $columns = $smcFunc['db_list_columns']($table_name, true);
  229. foreach ($columns as $column)
  230. if ($column['name'] == $column_name)
  231. {
  232. $smcFunc['db_query']('', '
  233. ALTER TABLE ' . $table_name . '
  234. DROP COLUMN ' . $column_name,
  235. array(
  236. 'security_override' => true,
  237. )
  238. );
  239. return true;
  240. }
  241. // If here we didn't have to work - joy!
  242. return false;
  243. }
  244. /**
  245. * Change a column.
  246. * @param string $table_name
  247. * @param $old_column
  248. * @param $column_info
  249. * @param array $parameters, default array()
  250. * @param string $error, default 'fatal'
  251. */
  252. function smf_db_change_column($table_name, $old_column, $column_info, $parameters = array(), $error = 'fatal')
  253. {
  254. global $smcFunc, $db_prefix;
  255. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  256. // Check it does exist!
  257. $columns = $smcFunc['db_list_columns']($table_name, true);
  258. $old_info = null;
  259. foreach ($columns as $column)
  260. if ($column['name'] == $old_column)
  261. $old_info = $column;
  262. // Nothing?
  263. if ($old_info === null)
  264. return false;
  265. // Get the right bits.
  266. if (!isset($column_info['name']))
  267. $column_info['name'] = $old_column;
  268. if (!isset($column_info['default']))
  269. $column_info['default'] = $old_info['default'];
  270. if (!isset($column_info['null']))
  271. $column_info['null'] = $old_info['null'];
  272. if (!isset($column_info['auto']))
  273. $column_info['auto'] = $old_info['auto'];
  274. if (!isset($column_info['type']))
  275. $column_info['type'] = $old_info['type'];
  276. if (!isset($column_info['size']) || !is_numeric($column_info['size']))
  277. $column_info['size'] = $old_info['size'];
  278. if (!isset($column_info['unsigned']) || !in_array($column_info['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')))
  279. $column_info['unsigned'] = '';
  280. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  281. // Allow for unsigned integers (mysql only)
  282. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  283. if ($size !== null)
  284. $type = $type . '(' . $size . ')';
  285. $smcFunc['db_query']('', '
  286. ALTER TABLE ' . $table_name . '
  287. CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
  288. (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
  289. (empty($column_info['auto']) ? '' : 'auto_increment') . ' ',
  290. array(
  291. 'security_override' => true,
  292. )
  293. );
  294. }
  295. /**
  296. * Add an index.
  297. * @param string $table_name
  298. * @param array $index_info
  299. * @param array $parameters, default array()
  300. * @param string $if_exists, default 'update'
  301. * @param string $error, default 'fatal'
  302. */
  303. function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  304. {
  305. global $smcFunc, $db_package_log, $db_prefix;
  306. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  307. // No columns = no index.
  308. if (empty($index_info['columns']))
  309. return false;
  310. $columns = implode(',', $index_info['columns']);
  311. // No name - make it up!
  312. if (empty($index_info['name']))
  313. {
  314. // No need for primary.
  315. if (isset($index_info['type']) && $index_info['type'] == 'primary')
  316. $index_info['name'] = '';
  317. else
  318. $index_info['name'] = implode('_', $index_info['columns']);
  319. }
  320. else
  321. $index_info['name'] = $index_info['name'];
  322. // Log that we are going to want to remove this!
  323. $db_package_log[] = array('remove_index', $table_name, $index_info['name']);
  324. // Let's get all our indexes.
  325. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  326. // Do we already have it?
  327. foreach ($indexes as $index)
  328. {
  329. if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary'))
  330. {
  331. // If we want to overwrite simply remove the current one then continue.
  332. if ($if_exists != 'update' || $index['type'] == 'primary')
  333. return false;
  334. else
  335. $smcFunc['db_remove_index']($table_name, $index_info['name']);
  336. }
  337. }
  338. // If we're here we know we don't have the index - so just add it.
  339. if (!empty($index_info['type']) && $index_info['type'] == 'primary')
  340. {
  341. $smcFunc['db_query']('', '
  342. ALTER TABLE ' . $table_name . '
  343. ADD PRIMARY KEY (' . $columns . ')',
  344. array(
  345. 'security_override' => true,
  346. )
  347. );
  348. }
  349. else
  350. {
  351. $smcFunc['db_query']('', '
  352. ALTER TABLE ' . $table_name . '
  353. ADD ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')',
  354. array(
  355. 'security_override' => true,
  356. )
  357. );
  358. }
  359. }
  360. /**
  361. * Remove an index.
  362. * @param string $table_name
  363. * @param string $index_name
  364. * @param array$parameters, default array()
  365. * @param string $error, default 'fatal'
  366. */
  367. function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal')
  368. {
  369. global $smcFunc, $db_prefix;
  370. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  371. // Better exist!
  372. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  373. foreach ($indexes as $index)
  374. {
  375. // If the name is primary we want the primary key!
  376. if ($index['type'] == 'primary' && $index_name == 'primary')
  377. {
  378. // Dropping primary key?
  379. $smcFunc['db_query']('', '
  380. ALTER TABLE ' . $table_name . '
  381. DROP PRIMARY KEY',
  382. array(
  383. 'security_override' => true,
  384. )
  385. );
  386. return true;
  387. }
  388. if ($index['name'] == $index_name)
  389. {
  390. // Drop the bugger...
  391. $smcFunc['db_query']('', '
  392. ALTER TABLE ' . $table_name . '
  393. DROP INDEX ' . $index_name,
  394. array(
  395. 'security_override' => true,
  396. )
  397. );
  398. return true;
  399. }
  400. }
  401. // Not to be found ;(
  402. return false;
  403. }
  404. /**
  405. * Get the schema formatted name for a type.
  406. * @param string $type_name
  407. * @param $type_size
  408. * @param $reverse
  409. */
  410. function smf_db_calculate_type($type_name, $type_size = null, $reverse = false)
  411. {
  412. // MySQL is actually the generic baseline.
  413. return array($type_name, $type_size);
  414. }
  415. /**
  416. * Get table structure.
  417. * @param string $table_name
  418. * @param array $parameters, default array()
  419. */
  420. function smf_db_table_structure($table_name, $parameters = array())
  421. {
  422. global $smcFunc, $db_prefix;
  423. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  424. return array(
  425. 'name' => $table_name,
  426. 'columns' => $smcFunc['db_list_columns']($table_name, true),
  427. 'indexes' => $smcFunc['db_list_indexes']($table_name, true),
  428. );
  429. }
  430. /**
  431. * Return column information for a table.
  432. * @param string $table_name
  433. * @param bool $detail
  434. * @param array $parameters, default array()
  435. * @return mixed
  436. */
  437. function smf_db_list_columns($table_name, $detail = false, $parameters = array())
  438. {
  439. global $smcFunc, $db_prefix;
  440. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  441. $result = $smcFunc['db_query']('', '
  442. SHOW FIELDS
  443. FROM {raw:table_name}',
  444. array(
  445. 'table_name' => strpos($table_name, '`') === 0 ? $table_name : '`' . $table_name . '`',
  446. )
  447. );
  448. $columns = array();
  449. while ($row = $smcFunc['db_fetch_assoc']($result))
  450. {
  451. if (!$detail)
  452. {
  453. $columns[] = $row['Field'];
  454. }
  455. else
  456. {
  457. // Is there an auto_increment?
  458. $auto = strpos($row['Extra'], 'auto_increment') !== false ? true : false;
  459. // Can we split out the size?
  460. if (preg_match('~(.+?)\s*\((\d+)\)(?:(?:\s*)?(unsigned))?~i', $row['Type'], $matches) === 1)
  461. {
  462. $type = $matches[1];
  463. $size = $matches[2];
  464. if (!empty($matches[3]) && $matches[3] == 'unsigned')
  465. $unsigned = true;
  466. }
  467. else
  468. {
  469. $type = $row['Type'];
  470. $size = null;
  471. }
  472. $columns[$row['Field']] = array(
  473. 'name' => $row['Field'],
  474. 'null' => $row['Null'] != 'YES' ? false : true,
  475. 'default' => isset($row['Default']) ? $row['Default'] : null,
  476. 'type' => $type,
  477. 'size' => $size,
  478. 'auto' => $auto,
  479. );
  480. if (isset($unsigned))
  481. {
  482. $columns[$row['Field']]['unsigned'] = $unsigned;
  483. unset($unsigned);
  484. }
  485. }
  486. }
  487. $smcFunc['db_free_result']($result);
  488. return $columns;
  489. }
  490. /**
  491. * Get index information.
  492. * @param string $table_name
  493. * @param bool $detail
  494. * @param array $parameters
  495. * @return mixed
  496. */
  497. function smf_db_list_indexes($table_name, $detail = false, $parameters = array())
  498. {
  499. global $smcFunc, $db_prefix;
  500. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  501. $result = $smcFunc['db_query']('', '
  502. SHOW KEYS
  503. FROM {raw:table_name}',
  504. array(
  505. 'table_name' => strpos($table_name, '`') === 0 ? $table_name : '`' . $table_name . '`',
  506. )
  507. );
  508. $indexes = array();
  509. while ($row = $smcFunc['db_fetch_assoc']($result))
  510. {
  511. if (!$detail)
  512. $indexes[] = $row['Key_name'];
  513. else
  514. {
  515. // What is the type?
  516. if ($row['Key_name'] == 'PRIMARY')
  517. $type = 'primary';
  518. elseif (empty($row['Non_unique']))
  519. $type = 'unique';
  520. elseif (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT')
  521. $type = 'fulltext';
  522. else
  523. $type = 'index';
  524. // This is the first column we've seen?
  525. if (empty($indexes[$row['Key_name']]))
  526. {
  527. $indexes[$row['Key_name']] = array(
  528. 'name' => $row['Key_name'],
  529. 'type' => $type,
  530. 'columns' => array(),
  531. );
  532. }
  533. // Is it a partial index?
  534. if (!empty($row['Sub_part']))
  535. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'] . '(' . $row['Sub_part'] . ')';
  536. else
  537. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'];
  538. }
  539. }
  540. $smcFunc['db_free_result']($result);
  541. return $indexes;
  542. }
  543. function smf_db_create_query_column($column)
  544. {
  545. global $smcFunc;
  546. // Auto increment is easy here!
  547. if (!empty($column['auto']))
  548. {
  549. $default = 'auto_increment';
  550. }
  551. elseif (isset($column['default']) && $column['default'] !== null)
  552. $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\'';
  553. else
  554. $default = '';
  555. // Sort out the size... and stuff...
  556. $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
  557. list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']);
  558. // Allow unsigned integers (mysql only)
  559. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';
  560. if ($size !== null)
  561. $type = $type . '(' . $size . ')';
  562. // Now just put it together!
  563. return '`' .$column['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default . ',';
  564. }
  565. ?>