DbPackages-mysql.php 22 KB

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