Skip navigation
Help

schema.inc

  1. drupal
    1. 7 drupal/includes/database/schema.inc
    2. 7 drupal/includes/database/mysql/schema.inc
    3. 7 drupal/includes/database/sqlite/schema.inc
    4. 7 drupal/includes/database/pgsql/schema.inc

Generic Database schema code.

Classes

NameDescription
DatabaseSchema
DatabaseSchemaObjectDoesNotExistExceptionException thrown if an object being modified doesn't exist yet.
DatabaseSchemaObjectExistsExceptionException thrown if an object being created already exists.

File

drupal/includes/database/schema.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Generic Database schema code.
  5. */
  6. require_once dirname(__FILE__) . '/query.inc';
  7. /**
  8. * @defgroup schemaapi Schema API
  9. * @{
  10. * API to handle database schemas.
  11. *
  12. * A Drupal schema definition is an array structure representing one or
  13. * more tables and their related keys and indexes. A schema is defined by
  14. * hook_schema(), which usually lives in a modulename.install file.
  15. *
  16. * By implementing hook_schema() and specifying the tables your module
  17. * declares, you can easily create and drop these tables on all
  18. * supported database engines. You don't have to deal with the
  19. * different SQL dialects for table creation and alteration of the
  20. * supported database engines.
  21. *
  22. * hook_schema() should return an array with a key for each table that
  23. * the module defines.
  24. *
  25. * The following keys are defined:
  26. * - 'description': A string in non-markup plain text describing this table
  27. * and its purpose. References to other tables should be enclosed in
  28. * curly-brackets. For example, the node_revisions table
  29. * description field might contain "Stores per-revision title and
  30. * body data for each {node}."
  31. * - 'fields': An associative array ('fieldname' => specification)
  32. * that describes the table's database columns. The specification
  33. * is also an array. The following specification parameters are defined:
  34. * - 'description': A string in non-markup plain text describing this field
  35. * and its purpose. References to other tables should be enclosed in
  36. * curly-brackets. For example, the node table vid field
  37. * description might contain "Always holds the largest (most
  38. * recent) {node_revision}.vid value for this nid."
  39. * - 'type': The generic datatype: 'char', 'varchar', 'text', 'blob', 'int',
  40. * 'float', 'numeric', or 'serial'. Most types just map to the according
  41. * database engine specific datatypes. Use 'serial' for auto incrementing
  42. * fields. This will expand to 'INT auto_increment' on MySQL.
  43. * - 'mysql_type', 'pgsql_type', 'sqlite_type', etc.: If you need to
  44. * use a record type not included in the officially supported list
  45. * of types above, you can specify a type for each database
  46. * backend. In this case, you can leave out the type parameter,
  47. * but be advised that your schema will fail to load on backends that
  48. * do not have a type specified. A possible solution can be to
  49. * use the "text" type as a fallback.
  50. * - 'serialize': A boolean indicating whether the field will be stored as
  51. * a serialized string.
  52. * - 'size': The data size: 'tiny', 'small', 'medium', 'normal',
  53. * 'big'. This is a hint about the largest value the field will
  54. * store and determines which of the database engine specific
  55. * datatypes will be used (e.g. on MySQL, TINYINT vs. INT vs. BIGINT).
  56. * 'normal', the default, selects the base type (e.g. on MySQL,
  57. * INT, VARCHAR, BLOB, etc.).
  58. * Not all sizes are available for all data types. See
  59. * DatabaseSchema::getFieldTypeMap() for possible combinations.
  60. * - 'not null': If true, no NULL values will be allowed in this
  61. * database column. Defaults to false.
  62. * - 'default': The field's default value. The PHP type of the
  63. * value matters: '', '0', and 0 are all different. If you
  64. * specify '0' as the default value for a type 'int' field it
  65. * will not work because '0' is a string containing the
  66. * character "zero", not an integer.
  67. * - 'length': The maximal length of a type 'char', 'varchar' or 'text'
  68. * field. Ignored for other field types.
  69. * - 'unsigned': A boolean indicating whether a type 'int', 'float'
  70. * and 'numeric' only is signed or unsigned. Defaults to
  71. * FALSE. Ignored for other field types.
  72. * - 'precision', 'scale': For type 'numeric' fields, indicates
  73. * the precision (total number of significant digits) and scale
  74. * (decimal digits right of the decimal point). Both values are
  75. * mandatory. Ignored for other field types.
  76. * All parameters apart from 'type' are optional except that type
  77. * 'numeric' columns must specify 'precision' and 'scale'.
  78. * - 'primary key': An array of one or more key column specifiers (see below)
  79. * that form the primary key.
  80. * - 'unique keys': An associative array of unique keys ('keyname' =>
  81. * specification). Each specification is an array of one or more
  82. * key column specifiers (see below) that form a unique key on the table.
  83. * - 'foreign keys': An associative array of relations ('my_relation' =>
  84. * specification). Each specification is an array containing the name of
  85. * the referenced table ('table'), and an array of column mappings
  86. * ('columns'). Column mappings are defined by key pairs ('source_column' =>
  87. * 'referenced_column').
  88. * - 'indexes': An associative array of indexes ('indexname' =>
  89. * specification). Each specification is an array of one or more
  90. * key column specifiers (see below) that form an index on the
  91. * table.
  92. *
  93. * A key column specifier is either a string naming a column or an
  94. * array of two elements, column name and length, specifying a prefix
  95. * of the named column.
  96. *
  97. * As an example, here is a SUBSET of the schema definition for
  98. * Drupal's 'node' table. It show four fields (nid, vid, type, and
  99. * title), the primary key on field 'nid', a unique key named 'vid' on
  100. * field 'vid', and two indexes, one named 'nid' on field 'nid' and
  101. * one named 'node_title_type' on the field 'title' and the first four
  102. * bytes of the field 'type':
  103. *
  104. * @code
  105. * $schema['node'] = array(
  106. * 'description' => 'The base table for nodes.',
  107. * 'fields' => array(
  108. * 'nid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
  109. * 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
  110. * 'type' => array('type' => 'varchar','length' => 32,'not null' => TRUE, 'default' => ''),
  111. * 'language' => array('type' => 'varchar','length' => 12,'not null' => TRUE,'default' => ''),
  112. * 'title' => array('type' => 'varchar','length' => 255,'not null' => TRUE, 'default' => ''),
  113. * 'uid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  114. * 'status' => array('type' => 'int', 'not null' => TRUE, 'default' => 1),
  115. * 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  116. * 'changed' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  117. * 'comment' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  118. * 'promote' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  119. * 'moderate' => array('type' => 'int', 'not null' => TRUE,'default' => 0),
  120. * 'sticky' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  121. * 'tnid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  122. * 'translate' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  123. * ),
  124. * 'indexes' => array(
  125. * 'node_changed' => array('changed'),
  126. * 'node_created' => array('created'),
  127. * 'node_moderate' => array('moderate'),
  128. * 'node_frontpage' => array('promote', 'status', 'sticky', 'created'),
  129. * 'node_status_type' => array('status', 'type', 'nid'),
  130. * 'node_title_type' => array('title', array('type', 4)),
  131. * 'node_type' => array(array('type', 4)),
  132. * 'uid' => array('uid'),
  133. * 'tnid' => array('tnid'),
  134. * 'translate' => array('translate'),
  135. * ),
  136. * 'unique keys' => array(
  137. * 'vid' => array('vid'),
  138. * ),
  139. * 'foreign keys' => array(
  140. * 'node_revision' => array(
  141. * 'table' => 'node_revision',
  142. * 'columns' => array('vid' => 'vid'),
  143. * ),
  144. * 'node_author' => array(
  145. * 'table' => 'users',
  146. * 'columns' => array('uid' => 'uid'),
  147. * ),
  148. * ),
  149. * 'primary key' => array('nid'),
  150. * );
  151. * @endcode
  152. *
  153. * @see drupal_install_schema()
  154. */
  155. abstract class DatabaseSchema implements QueryPlaceholderInterface {
  156. protected $connection;
  157. /**
  158. * The placeholder counter.
  159. */
  160. protected $placeholder = 0;
  161. /**
  162. * Definition of prefixInfo array structure.
  163. *
  164. * Rather than redefining DatabaseSchema::getPrefixInfo() for each driver,
  165. * by defining the defaultSchema variable only MySQL has to re-write the
  166. * method.
  167. *
  168. * @see DatabaseSchema::getPrefixInfo()
  169. */
  170. protected $defaultSchema = 'public';
  171. public function __construct($connection) {
  172. $this->connection = $connection;
  173. }
  174. public function nextPlaceholder() {
  175. return $this->placeholder++;
  176. }
  177. /**
  178. * Get information about the table name and schema from the prefix.
  179. *
  180. * @param
  181. * Name of table to look prefix up for. Defaults to 'default' because thats
  182. * default key for prefix.
  183. * @param $add_prefix
  184. * Boolean that indicates whether the given table name should be prefixed.
  185. *
  186. * @return
  187. * A keyed array with information about the schema, table name and prefix.
  188. */
  189. protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
  190. $info = array(
  191. 'schema' => $this->defaultSchema,
  192. 'prefix' => $this->connection->tablePrefix($table),
  193. );
  194. if ($add_prefix) {
  195. $table = $info['prefix'] . $table;
  196. }
  197. // If the prefix contains a period in it, then that means the prefix also
  198. // contains a schema reference in which case we will change the schema key
  199. // to the value before the period in the prefix. Everything after the dot
  200. // will be prefixed onto the front of the table.
  201. if (($pos = strpos($table, '.')) !== FALSE) {
  202. // Grab everything before the period.
  203. $info['schema'] = substr($table, 0, $pos);
  204. // Grab everything after the dot.
  205. $info['table'] = substr($table, ++$pos);
  206. }
  207. else {
  208. $info['table'] = $table;
  209. }
  210. return $info;
  211. }
  212. /**
  213. * Create names for indexes, primary keys and constraints.
  214. *
  215. * This prevents using {} around non-table names like indexes and keys.
  216. */
  217. function prefixNonTable($table) {
  218. $args = func_get_args();
  219. $info = $this->getPrefixInfo($table);
  220. $args[0] = $info['table'];
  221. return implode('_', $args);
  222. }
  223. /**
  224. * Build a condition to match a table name against a standard information_schema.
  225. *
  226. * The information_schema is a SQL standard that provides information about the
  227. * database server and the databases, schemas, tables, columns and users within
  228. * it. This makes information_schema a useful tool to use across the drupal
  229. * database drivers and is used by a few different functions. The function below
  230. * describes the conditions to be meet when querying information_schema.tables
  231. * for drupal tables or information associated with drupal tables. Even though
  232. * this is the standard method, not all databases follow standards and so this
  233. * method should be overwritten by a database driver if the database provider
  234. * uses alternate methods. Because information_schema.tables is used in a few
  235. * different functions, a database driver will only need to override this function
  236. * to make all the others work. For example see includes/databases/mysql/schema.inc.
  237. *
  238. * @param $table_name
  239. * The name of the table in question.
  240. * @param $operator
  241. * The operator to apply on the 'table' part of the condition.
  242. * @param $add_prefix
  243. * Boolean to indicate whether the table name needs to be prefixed.
  244. *
  245. * @return QueryConditionInterface
  246. * A DatabaseCondition object.
  247. */
  248. protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
  249. $info = $this->connection->getConnectionOptions();
  250. // Retrive the table name and schema
  251. $table_info = $this->getPrefixInfo($table_name, $add_prefix);
  252. $condition = new DatabaseCondition('AND');
  253. $condition->condition('table_catalog', $info['database']);
  254. $condition->condition('table_schema', $table_info['schema']);
  255. $condition->condition('table_name', $table_info['table'], $operator);
  256. return $condition;
  257. }
  258. /**
  259. * Check if a table exists.
  260. *
  261. * @param $table
  262. * The name of the table in drupal (no prefixing).
  263. *
  264. * @return
  265. * TRUE if the given table exists, otherwise FALSE.
  266. */
  267. public function tableExists($table) {
  268. $condition = $this->buildTableNameCondition($table);
  269. $condition->compile($this->connection, $this);
  270. // Normally, we would heartily discourage the use of string
  271. // concatenation for conditionals like this however, we
  272. // couldn't use db_select() here because it would prefix
  273. // information_schema.tables and the query would fail.
  274. // Don't use {} around information_schema.tables table.
  275. return (bool) $this->connection->query("SELECT 1 FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
  276. }
  277. /**
  278. * Find all tables that are like the specified base table name.
  279. *
  280. * @param $table_expression
  281. * An SQL expression, for example "simpletest%" (without the quotes).
  282. * BEWARE: this is not prefixed, the caller should take care of that.
  283. *
  284. * @return
  285. * Array, both the keys and the values are the matching tables.
  286. */
  287. public function findTables($table_expression) {
  288. $condition = $this->buildTableNameCondition($table_expression, 'LIKE', FALSE);
  289. $condition->compile($this->connection, $this);
  290. // Normally, we would heartily discourage the use of string
  291. // concatenation for conditionals like this however, we
  292. // couldn't use db_select() here because it would prefix
  293. // information_schema.tables and the query would fail.
  294. // Don't use {} around information_schema.tables table.
  295. return $this->connection->query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0);
  296. }
  297. /**
  298. * Check if a column exists in the given table.
  299. *
  300. * @param $table
  301. * The name of the table in drupal (no prefixing).
  302. * @param $name
  303. * The name of the column.
  304. *
  305. * @return
  306. * TRUE if the given column exists, otherwise FALSE.
  307. */
  308. public function fieldExists($table, $column) {
  309. $condition = $this->buildTableNameCondition($table);
  310. $condition->condition('column_name', $column);
  311. $condition->compile($this->connection, $this);
  312. // Normally, we would heartily discourage the use of string
  313. // concatenation for conditionals like this however, we
  314. // couldn't use db_select() here because it would prefix
  315. // information_schema.tables and the query would fail.
  316. // Don't use {} around information_schema.columns table.
  317. return (bool) $this->connection->query("SELECT 1 FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
  318. }
  319. /**
  320. * Returns a mapping of Drupal schema field names to DB-native field types.
  321. *
  322. * Because different field types do not map 1:1 between databases, Drupal has
  323. * its own normalized field type names. This function returns a driver-specific
  324. * mapping table from Drupal names to the native names for each database.
  325. *
  326. * @return array
  327. * An array of Schema API field types to driver-specific field types.
  328. */
  329. abstract public function getFieldTypeMap();
  330. /**
  331. * Rename a table.
  332. *
  333. * @param $table
  334. * The table to be renamed.
  335. * @param $new_name
  336. * The new name for the table.
  337. *
  338. * @throws DatabaseSchemaObjectDoesNotExistException
  339. * If the specified table doesn't exist.
  340. * @throws DatabaseSchemaObjectExistsException
  341. * If a table with the specified new name already exists.
  342. */
  343. abstract public function renameTable($table, $new_name);
  344. /**
  345. * Drop a table.
  346. *
  347. * @param $table
  348. * The table to be dropped.
  349. *
  350. * @return
  351. * TRUE if the table was successfully dropped, FALSE if there was no table
  352. * by that name to begin with.
  353. */
  354. abstract public function dropTable($table);
  355. /**
  356. * Add a new field to a table.
  357. *
  358. * @param $table
  359. * Name of the table to be altered.
  360. * @param $field
  361. * Name of the field to be added.
  362. * @param $spec
  363. * The field specification array, as taken from a schema definition.
  364. * The specification may also contain the key 'initial', the newly
  365. * created field will be set to the value of the key in all rows.
  366. * This is most useful for creating NOT NULL columns with no default
  367. * value in existing tables.
  368. * @param $keys_new
  369. * Optional keys and indexes specification to be created on the
  370. * table along with adding the field. The format is the same as a
  371. * table specification but without the 'fields' element. If you are
  372. * adding a type 'serial' field, you MUST specify at least one key
  373. * or index including it in this array. See db_change_field() for more
  374. * explanation why.
  375. *
  376. * @throws DatabaseSchemaObjectDoesNotExistException
  377. * If the specified table doesn't exist.
  378. * @throws DatabaseSchemaObjectExistsException
  379. * If the specified table already has a field by that name.
  380. */
  381. abstract public function addField($table, $field, $spec, $keys_new = array());
  382. /**
  383. * Drop a field.
  384. *
  385. * @param $table
  386. * The table to be altered.
  387. * @param $field
  388. * The field to be dropped.
  389. *
  390. * @return
  391. * TRUE if the field was successfully dropped, FALSE if there was no field
  392. * by that name to begin with.
  393. */
  394. abstract public function dropField($table, $field);
  395. /**
  396. * Set the default value for a field.
  397. *
  398. * @param $table
  399. * The table to be altered.
  400. * @param $field
  401. * The field to be altered.
  402. * @param $default
  403. * Default value to be set. NULL for 'default NULL'.
  404. *
  405. * @throws DatabaseSchemaObjectDoesNotExistException
  406. * If the specified table or field doesn't exist.
  407. */
  408. abstract public function fieldSetDefault($table, $field, $default);
  409. /**
  410. * Set a field to have no default value.
  411. *
  412. * @param $table
  413. * The table to be altered.
  414. * @param $field
  415. * The field to be altered.
  416. *
  417. * @throws DatabaseSchemaObjectDoesNotExistException
  418. * If the specified table or field doesn't exist.
  419. */
  420. abstract public function fieldSetNoDefault($table, $field);
  421. /**
  422. * Checks if an index exists in the given table.
  423. *
  424. * @param $table
  425. * The name of the table in drupal (no prefixing).
  426. * @param $name
  427. * The name of the index in drupal (no prefixing).
  428. *
  429. * @return
  430. * TRUE if the given index exists, otherwise FALSE.
  431. */
  432. abstract public function indexExists($table, $name);
  433. /**
  434. * Add a primary key.
  435. *
  436. * @param $table
  437. * The table to be altered.
  438. * @param $fields
  439. * Fields for the primary key.
  440. *
  441. * @throws DatabaseSchemaObjectDoesNotExistException
  442. * If the specified table doesn't exist.
  443. * @throws DatabaseSchemaObjectExistsException
  444. * If the specified table already has a primary key.
  445. */
  446. abstract public function addPrimaryKey($table, $fields);
  447. /**
  448. * Drop the primary key.
  449. *
  450. * @param $table
  451. * The table to be altered.
  452. *
  453. * @return
  454. * TRUE if the primary key was successfully dropped, FALSE if there was no
  455. * primary key on this table to begin with.
  456. */
  457. abstract public function dropPrimaryKey($table);
  458. /**
  459. * Add a unique key.
  460. *
  461. * @param $table
  462. * The table to be altered.
  463. * @param $name
  464. * The name of the key.
  465. * @param $fields
  466. * An array of field names.
  467. *
  468. * @throws DatabaseSchemaObjectDoesNotExistException
  469. * If the specified table doesn't exist.
  470. * @throws DatabaseSchemaObjectExistsException
  471. * If the specified table already has a key by that name.
  472. */
  473. abstract public function addUniqueKey($table, $name, $fields);
  474. /**
  475. * Drop a unique key.
  476. *
  477. * @param $table
  478. * The table to be altered.
  479. * @param $name
  480. * The name of the key.
  481. *
  482. * @return
  483. * TRUE if the key was successfully dropped, FALSE if there was no key by
  484. * that name to begin with.
  485. */
  486. abstract public function dropUniqueKey($table, $name);
  487. /**
  488. * Add an index.
  489. *
  490. * @param $table
  491. * The table to be altered.
  492. * @param $name
  493. * The name of the index.
  494. * @param $fields
  495. * An array of field names.
  496. *
  497. * @throws DatabaseSchemaObjectDoesNotExistException
  498. * If the specified table doesn't exist.
  499. * @throws DatabaseSchemaObjectExistsException
  500. * If the specified table already has an index by that name.
  501. */
  502. abstract public function addIndex($table, $name, $fields);
  503. /**
  504. * Drop an index.
  505. *
  506. * @param $table
  507. * The table to be altered.
  508. * @param $name
  509. * The name of the index.
  510. *
  511. * @return
  512. * TRUE if the index was successfully dropped, FALSE if there was no index
  513. * by that name to begin with.
  514. */
  515. abstract public function dropIndex($table, $name);
  516. /**
  517. * Change a field definition.
  518. *
  519. * IMPORTANT NOTE: To maintain database portability, you have to explicitly
  520. * recreate all indices and primary keys that are using the changed field.
  521. *
  522. * That means that you have to drop all affected keys and indexes with
  523. * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
  524. * To recreate the keys and indices, pass the key definitions as the
  525. * optional $keys_new argument directly to db_change_field().
  526. *
  527. * For example, suppose you have:
  528. * @code
  529. * $schema['foo'] = array(
  530. * 'fields' => array(
  531. * 'bar' => array('type' => 'int', 'not null' => TRUE)
  532. * ),
  533. * 'primary key' => array('bar')
  534. * );
  535. * @endcode
  536. * and you want to change foo.bar to be type serial, leaving it as the
  537. * primary key. The correct sequence is:
  538. * @code
  539. * db_drop_primary_key('foo');
  540. * db_change_field('foo', 'bar', 'bar',
  541. * array('type' => 'serial', 'not null' => TRUE),
  542. * array('primary key' => array('bar')));
  543. * @endcode
  544. *
  545. * The reasons for this are due to the different database engines:
  546. *
  547. * On PostgreSQL, changing a field definition involves adding a new field
  548. * and dropping an old one which* causes any indices, primary keys and
  549. * sequences (from serial-type fields) that use the changed field to be dropped.
  550. *
  551. * On MySQL, all type 'serial' fields must be part of at least one key
  552. * or index as soon as they are created. You cannot use
  553. * db_add_{primary_key,unique_key,index}() for this purpose because
  554. * the ALTER TABLE command will fail to add the column without a key
  555. * or index specification. The solution is to use the optional
  556. * $keys_new argument to create the key or index at the same time as
  557. * field.
  558. *
  559. * You could use db_add_{primary_key,unique_key,index}() in all cases
  560. * unless you are converting a field to be type serial. You can use
  561. * the $keys_new argument in all cases.
  562. *
  563. * @param $table
  564. * Name of the table.
  565. * @param $field
  566. * Name of the field to change.
  567. * @param $field_new
  568. * New name for the field (set to the same as $field if you don't want to change the name).
  569. * @param $spec
  570. * The field specification for the new field.
  571. * @param $keys_new
  572. * Optional keys and indexes specification to be created on the
  573. * table along with changing the field. The format is the same as a
  574. * table specification but without the 'fields' element.
  575. *
  576. * @throws DatabaseSchemaObjectDoesNotExistException
  577. * If the specified table or source field doesn't exist.
  578. * @throws DatabaseSchemaObjectExistsException
  579. * If the specified destination field already exists.
  580. */
  581. abstract public function changeField($table, $field, $field_new, $spec, $keys_new = array());
  582. /**
  583. * Create a new table from a Drupal table definition.
  584. *
  585. * @param $name
  586. * The name of the table to create.
  587. * @param $table
  588. * A Schema API table definition array.
  589. *
  590. * @throws DatabaseSchemaObjectExistsException
  591. * If the specified table already exists.
  592. */
  593. public function createTable($name, $table) {
  594. if ($this->tableExists($name)) {
  595. throw new DatabaseSchemaObjectExistsException(t('Table %name already exists.', array('%name' => $name)));
  596. }
  597. $statements = $this->createTableSql($name, $table);
  598. foreach ($statements as $statement) {
  599. $this->connection->query($statement);
  600. }
  601. }
  602. /**
  603. * Return an array of field names from an array of key/index column specifiers.
  604. *
  605. * This is usually an identity function but if a key/index uses a column prefix
  606. * specification, this function extracts just the name.
  607. *
  608. * @param $fields
  609. * An array of key/index column specifiers.
  610. *
  611. * @return
  612. * An array of field names.
  613. */
  614. public function fieldNames($fields) {
  615. $return = array();
  616. foreach ($fields as $field) {
  617. if (is_array($field)) {
  618. $return[] = $field[0];
  619. }
  620. else {
  621. $return[] = $field;
  622. }
  623. }
  624. return $return;
  625. }
  626. /**
  627. * Prepare a table or column comment for database query.
  628. *
  629. * @param $comment
  630. * The comment string to prepare.
  631. * @param $length
  632. * Optional upper limit on the returned string length.
  633. *
  634. * @return
  635. * The prepared comment.
  636. */
  637. public function prepareComment($comment, $length = NULL) {
  638. return $this->connection->quote($comment);
  639. }
  640. }
  641. /**
  642. * Exception thrown if an object being created already exists.
  643. *
  644. * For example, this exception should be thrown whenever there is an attempt to
  645. * create a new database table, field, or index that already exists in the
  646. * database schema.
  647. */
  648. class DatabaseSchemaObjectExistsException extends Exception {}
  649. /**
  650. * Exception thrown if an object being modified doesn't exist yet.
  651. *
  652. * For example, this exception should be thrown whenever there is an attempt to
  653. * modify a database table, field, or index that does not currently exist in
  654. * the database schema.
  655. */
  656. class DatabaseSchemaObjectDoesNotExistException extends Exception {}
  657. /**
  658. * @} End of "defgroup schemaapi".
  659. */