| 1 | <?php |
| 2 | // $Id: schema.inc,v 1.19 2009/08/02 08:16:15 webchick Exp $ |
| 3 | |
| 4 | /** |
| 5 | * @file |
| 6 | * Generic Database schema code. |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * @defgroup schemaapi Schema API |
| 11 | * @{ |
| 12 | * |
| 13 | * A Drupal schema definition is an array structure representing one or |
| 14 | * more tables and their related keys and indexes. A schema is defined by |
| 15 | * hook_schema(), which usually lives in a modulename.install file. |
| 16 | * |
| 17 | * By implementing hook_schema() and specifying the tables your module |
| 18 | * declares, you can easily create and drop these tables on all |
| 19 | * supported database engines. You don't have to deal with the |
| 20 | * different SQL dialects for table creation and alteration of the |
| 21 | * supported database engines. |
| 22 | * |
| 23 | * hook_schema() should return an array with a key for each table that |
| 24 | * the module defines. |
| 25 | * |
| 26 | * The following keys are defined: |
| 27 | * |
| 28 | * - 'description': A string in non-markup plain text describing this table |
| 29 | * and its purpose. References to other tables should be enclosed in |
| 30 | * curly-brackets. For example, the node_revisions table |
| 31 | * description field might contain "Stores per-revision title and |
| 32 | * body data for each {node}." |
| 33 | * - 'fields': An associative array ('fieldname' => specification) |
| 34 | * that describes the table's database columns. The specification |
| 35 | * is also an array. The following specification parameters are defined: |
| 36 | * |
| 37 | * - 'description': A string in non-markup plain text describing this field |
| 38 | * and its purpose. References to other tables should be enclosed in |
| 39 | * curly-brackets. For example, the node table vid field |
| 40 | * description might contain "Always holds the largest (most |
| 41 | * recent) {node_revision}.vid value for this nid." |
| 42 | * - 'type': The generic datatype: 'varchar', 'int', 'serial' |
| 43 | * 'float', 'numeric', 'text', 'blob' or 'datetime'. Most types |
| 44 | * just map to the according database engine specific |
| 45 | * datatypes. Use 'serial' for auto incrementing fields. This |
| 46 | * will expand to 'int auto_increment' on mysql. |
| 47 | * - 'serialize': A boolean indicating whether the field will be stored as |
| 48 | * a serialized string. |
| 49 | * - 'size': The data size: 'tiny', 'small', 'medium', 'normal', |
| 50 | * 'big'. This is a hint about the largest value the field will |
| 51 | * store and determines which of the database engine specific |
| 52 | * datatypes will be used (e.g. on MySQL, TINYINT vs. INT vs. BIGINT). |
| 53 | * 'normal', the default, selects the base type (e.g. on MySQL, |
| 54 | * INT, VARCHAR, BLOB, etc.). |
| 55 | * |
| 56 | * Not all sizes are available for all data types. See |
| 57 | * db_type_map() for possible combinations. |
| 58 | * - 'not null': If true, no NULL values will be allowed in this |
| 59 | * database column. Defaults to false. |
| 60 | * - 'default': The field's default value. The PHP type of the |
| 61 | * value matters: '', '0', and 0 are all different. If you |
| 62 | * specify '0' as the default value for a type 'int' field it |
| 63 | * will not work because '0' is a string containing the |
| 64 | * character "zero", not an integer. |
| 65 | * - 'length': The maximal length of a type 'char', 'varchar' or 'text' |
| 66 | * field. Ignored for other field types. |
| 67 | * - 'unsigned': A boolean indicating whether a type 'int', 'float' |
| 68 | * and 'numeric' only is signed or unsigned. Defaults to |
| 69 | * FALSE. Ignored for other field types. |
| 70 | * - 'precision', 'scale': For type 'numeric' fields, indicates |
| 71 | * the precision (total number of significant digits) and scale |
| 72 | * (decimal digits right of the decimal point). Both values are |
| 73 | * mandatory. Ignored for other field types. |
| 74 | * |
| 75 | * All parameters apart from 'type' are optional except that type |
| 76 | * 'numeric' columns must specify 'precision' and 'scale'. |
| 77 | * |
| 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, each key references a column |
| 84 | * of the local table, each value is an array with a single key pair as |
| 85 | * 'tablename' => 'column' where 'column' is the foreign column to |
| 86 | * reference. |
| 87 | * - 'indexes': An associative array of indexes ('indexame' => |
| 88 | * specification). Each specification is an array of one or more |
| 89 | * key column specifiers (see below) that form an index on the |
| 90 | * table. |
| 91 | * |
| 92 | * A key column specifier is either a string naming a column or an |
| 93 | * array of two elements, column name and length, specifying a prefix |
| 94 | * of the named column. |
| 95 | * |
| 96 | * As an example, here is a SUBSET of the schema definition for |
| 97 | * Drupal's 'node' table. It show four fields (nid, vid, type, and |
| 98 | * title), the primary key on field 'nid', a unique key named 'vid' on |
| 99 | * field 'vid', and two indexes, one named 'nid' on field 'nid' and |
| 100 | * one named 'node_title_type' on the field 'title' and the first four |
| 101 | * bytes of the field 'type': |
| 102 | * |
| 103 | * @code |
| 104 | * $schema['node'] = array( |
| 105 | * 'description' => 'The base table for nodes.', |
| 106 | * 'fields' => array( |
| 107 | * 'nid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), |
| 108 | * 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE,'default' => 0), |
| 109 | * 'type' => array('type' => 'varchar','length' => 32,'not null' => TRUE, 'default' => ''), |
| 110 | * 'language' => array('type' => 'varchar','length' => 12,'not null' => TRUE,'default' => ''), |
| 111 | * 'title' => array('type' => 'varchar','length' => 255,'not null' => TRUE, 'default' => ''), |
| 112 | * 'uid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 113 | * 'status' => array('type' => 'int', 'not null' => TRUE, 'default' => 1), |
| 114 | * 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 115 | * 'changed' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 116 | * 'comment' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 117 | * 'promote' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 118 | * 'moderate' => array('type' => 'int', 'not null' => TRUE,'default' => 0), |
| 119 | * 'sticky' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 120 | * 'tnid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), |
| 121 | * 'translate' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
| 122 | * ), |
| 123 | * 'indexes' => array( |
| 124 | * 'node_changed' => array('changed'), |
| 125 | * 'node_created' => array('created'), |
| 126 | * 'node_moderate' => array('moderate'), |
| 127 | * 'node_frontpage' => array('promote', 'status', 'sticky', 'created'), |
| 128 | * 'node_status_type' => array('status', 'type', 'nid'), |
| 129 | * 'node_title_type' => array('title', array('type', 4)), |
| 130 | * 'node_type' => array(array('type', 4)), |
| 131 | * 'uid' => array('uid'), |
| 132 | * 'tnid' => array('tnid'), |
| 133 | * 'translate' => array('translate'), |
| 134 | * ), |
| 135 | * 'unique keys' => array( |
| 136 | * 'vid' => array('vid'), |
| 137 | * ), |
| 138 | * 'foreign keys' => array( |
| 139 | * 'vid' => array('node_revision' => 'vid'), |
| 140 | * 'uid' => array('users' => 'uid'), |
| 141 | * ), |
| 142 | * 'primary key' => array('nid'), |
| 143 | * ); |
| 144 | * @endcode |
| 145 | * |
| 146 | * @see drupal_install_schema() |
| 147 | */ |
| 148 | |
| 149 | abstract class DatabaseSchema { |
| 150 | |
| 151 | protected $connection; |
| 152 | |
| 153 | public function __construct($connection) { |
| 154 | $this->connection = $connection; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Build a condition to match a table name against a standard information_schema. |
| 159 | * |
| 160 | * The information_schema is a SQL standard that provides information about the |
| 161 | * database server and the databases, schemas, tables, columns and users within |
| 162 | * it. This makes information_schema a useful tool to use across the drupal |
| 163 | * database drivers and is used by a few different functions. The function below |
| 164 | * describes the conditions to be meet when querying information_schema.tables |
| 165 | * for drupal tables or information associated with drupal tables. Even though |
| 166 | * this is the standard method, not all databases follow standards and so this |
| 167 | * method should be overwritten by a database driver if the database provider |
| 168 | * uses alternate methods. Because information_schema.tables is used in a few |
| 169 | * different functions, a database driver will only need to override this function |
| 170 | * to make all the others work. For example see includes/databases/mysql/schema.inc. |
| 171 | * |
| 172 | * @param $table_name |
| 173 | * The name of the table to explode. |
| 174 | * @param $operator |
| 175 | * The operator to apply on the 'table' part of the condition. |
| 176 | * @return |
| 177 | * A DatabaseCondition object. |
| 178 | */ |
| 179 | protected function buildTableNameCondition($table_name, $operator = '=') { |
| 180 | $info = Database::getConnectionInfo(); |
| 181 | |
| 182 | // The table name may describe the schema eg. schema.table. |
| 183 | if (strpos($table_name, '.')) { |
| 184 | list($schema, $table_name) = explode('.', $table_name); |
| 185 | } |
| 186 | else { |
| 187 | $schema = 'public'; |
| 188 | } |
| 189 | |
| 190 | $condition = new DatabaseCondition('AND'); |
| 191 | $condition->condition('table_catalog', $info['default']['database']); |
| 192 | $condition->condition('table_schema', $schema); |
| 193 | $condition->condition('table_name', $table_name, $operator); |
| 194 | return $condition; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Check if a table exists. |
| 199 | * |
| 200 | * @param $table |
| 201 | * The name of the table in drupal (no prefixing). |
| 202 | * @return |
| 203 | * false is no table exists otherwise the actual table name. |
| 204 | */ |
| 205 | public function tableExists($table) { |
| 206 | $condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}')); |
| 207 | $condition->compile($this->connection); |
| 208 | // Normally, we would heartily discourage the use of string |
| 209 | // concatination for conditionals like this however, we |
| 210 | // couldn't use db_select() here because it would prefix |
| 211 | // information_schema.tables and the query would fail. |
| 212 | // Don't use {} around information_schema.tables table. |
| 213 | return db_query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Find all tables that are like the specified base table name. |
| 218 | * |
| 219 | * @param $table_expression |
| 220 | * An SQL expression, for example "simpletest%" (without the quotes). |
| 221 | * BEWARE: this is not prefixed, the caller should take care of that. |
| 222 | * @return |
| 223 | * Array, both the keys and the values are the matching tables. |
| 224 | */ |
| 225 | public function findTables($table_expression) { |
| 226 | $condition = $this->buildTableNameCondition($table_expression, 'LIKE'); |
| 227 | $condition->compile($this->connection); |
| 228 | // Normally, we would heartily discourage the use of string |
| 229 | // concatination for conditionals like this however, we |
| 230 | // couldn't use db_select() here because it would prefix |
| 231 | // information_schema.tables and the query would fail. |
| 232 | // Don't use {} around information_schema.tables table. |
| 233 | return db_query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Check if a column exists in the given table. |
| 238 | */ |
| 239 | public function columnExists($table, $column) { |
| 240 | $condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}')); |
| 241 | $condition->condition('column_name', $column); |
| 242 | $condition->compile($this->connection); |
| 243 | // Normally, we would heartily discourage the use of string |
| 244 | // concatination for conditionals like this however, we |
| 245 | // couldn't use db_select() here because it would prefix |
| 246 | // information_schema.tables and the query would fail. |
| 247 | // Don't use {} around information_schema.columns table. |
| 248 | return db_query("SELECT column_name FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * This maps a generic data type in combination with its data size |
| 253 | * to the engine-specific data type. |
| 254 | */ |
| 255 | abstract public function getFieldTypeMap(); |
| 256 | |
| 257 | /** |
| 258 | * Rename a table. |
| 259 | * |
| 260 | * @param $ret |
| 261 | * Array to which query results will be added. |
| 262 | * @param $table |
| 263 | * The table to be renamed. |
| 264 | * @param $new_name |
| 265 | * The new name for the table. |
| 266 | */ |
| 267 | abstract public function renameTable(&$ret, $table, $new_name); |
| 268 | |
| 269 | /** |
| 270 | * Drop a table. |
| 271 | * |
| 272 | * @param $ret |
| 273 | * Array to which query results will be added. |
| 274 | * @param $table |
| 275 | * The table to be dropped. |
| 276 | */ |
| 277 | abstract public function dropTable(&$ret, $table); |
| 278 | |
| 279 | /** |
| 280 | * Add a new field to a table. |
| 281 | * |
| 282 | * @param $ret |
| 283 | * Array to which query results will be added. |
| 284 | * @param $table |
| 285 | * Name of the table to be altered. |
| 286 | * @param $field |
| 287 | * Name of the field to be added. |
| 288 | * @param $spec |
| 289 | * The field specification array, as taken from a schema definition. |
| 290 | * The specification may also contain the key 'initial', the newly |
| 291 | * created field will be set to the value of the key in all rows. |
| 292 | * This is most useful for creating NOT NULL columns with no default |
| 293 | * value in existing tables. |
| 294 | * @param $keys_new |
| 295 | * Optional keys and indexes specification to be created on the |
| 296 | * table along with adding the field. The format is the same as a |
| 297 | * table specification but without the 'fields' element. If you are |
| 298 | * adding a type 'serial' field, you MUST specify at least one key |
| 299 | * or index including it in this array. @see db_change_field for more |
| 300 | * explanation why. |
| 301 | */ |
| 302 | abstract public function addField(&$ret, $table, $field, $spec, $keys_new = array()); |
| 303 | |
| 304 | /** |
| 305 | * Drop a field. |
| 306 | * |
| 307 | * @param $ret |
| 308 | * Array to which query results will be added. |
| 309 | * @param $table |
| 310 | * The table to be altered. |
| 311 | * @param $field |
| 312 | * The field to be dropped. |
| 313 | */ |
| 314 | abstract public function dropField(&$ret, $table, $field); |
| 315 | |
| 316 | /** |
| 317 | * Set the default value for a field. |
| 318 | * |
| 319 | * @param $ret |
| 320 | * Array to which query results will be added. |
| 321 | * @param $table |
| 322 | * The table to be altered. |
| 323 | * @param $field |
| 324 | * The field to be altered. |
| 325 | * @param $default |
| 326 | * Default value to be set. NULL for 'default NULL'. |
| 327 | */ |
| 328 | abstract public function fieldSetDefault(&$ret, $table, $field, $default); |
| 329 | |
| 330 | /** |
| 331 | * Set a field to have no default value. |
| 332 | * |
| 333 | * @param $ret |
| 334 | * Array to which query results will be added. |
| 335 | * @param $table |
| 336 | * The table to be altered. |
| 337 | * @param $field |
| 338 | * The field to be altered. |
| 339 | */ |
| 340 | abstract public function fieldSetNoDefault(&$ret, $table, $field); |
| 341 | |
| 342 | /** |
| 343 | * Add a primary key. |
| 344 | * |
| 345 | * @param $ret |
| 346 | * Array to which query results will be added. |
| 347 | * @param $table |
| 348 | * The table to be altered. |
| 349 | * @param $fields |
| 350 | * Fields for the primary key. |
| 351 | */ |
| 352 | abstract public function addPrimaryKey(&$ret, $table, $fields); |
| 353 | |
| 354 | /** |
| 355 | * Drop the primary key. |
| 356 | * |
| 357 | * @param $ret |
| 358 | * Array to which query results will be added. |
| 359 | * @param $table |
| 360 | * The table to be altered. |
| 361 | */ |
| 362 | abstract public function dropPrimaryKey(&$ret, $table); |
| 363 | |
| 364 | /** |
| 365 | * Add a unique key. |
| 366 | * |
| 367 | * @param $ret |
| 368 | * Array to which query results will be added. |
| 369 | * @param $table |
| 370 | * The table to be altered. |
| 371 | * @param $name |
| 372 | * The name of the key. |
| 373 | * @param $fields |
| 374 | * An array of field names. |
| 375 | */ |
| 376 | abstract public function addUniqueKey(&$ret, $table, $name, $fields); |
| 377 | |
| 378 | /** |
| 379 | * Drop a unique key. |
| 380 | * |
| 381 | * @param $ret |
| 382 | * Array to which query results will be added. |
| 383 | * @param $table |
| 384 | * The table to be altered. |
| 385 | * @param $name |
| 386 | * The name of the key. |
| 387 | */ |
| 388 | abstract public function dropUniqueKey(&$ret, $table, $name); |
| 389 | |
| 390 | /** |
| 391 | * Add an index. |
| 392 | * |
| 393 | * @param $ret |
| 394 | * Array to which query results will be added. |
| 395 | * @param $table |
| 396 | * The table to be altered. |
| 397 | * @param $name |
| 398 | * The name of the index. |
| 399 | * @param $fields |
| 400 | * An array of field names. |
| 401 | */ |
| 402 | abstract public function addIndex(&$ret, $table, $name, $fields); |
| 403 | |
| 404 | /** |
| 405 | * Drop an index. |
| 406 | * |
| 407 | * @param $ret |
| 408 | * Array to which query results will be added. |
| 409 | * @param $table |
| 410 | * The table to be altered. |
| 411 | * @param $name |
| 412 | * The name of the index. |
| 413 | */ |
| 414 | abstract public function dropIndex(&$ret, $table, $name); |
| 415 | |
| 416 | |
| 417 | /** |
| 418 | * Change a field definition. |
| 419 | * |
| 420 | * IMPORTANT NOTE: To maintain database portability, you have to explicitly |
| 421 | * recreate all indices and primary keys that are using the changed field. |
| 422 | * |
| 423 | * That means that you have to drop all affected keys and indexes with |
| 424 | * db_drop_{primary_key,unique_key,index}() before calling db_change_field(). |
| 425 | * To recreate the keys and indices, pass the key definitions as the |
| 426 | * optional $keys_new argument directly to db_change_field(). |
| 427 | * |
| 428 | * For example, suppose you have: |
| 429 | * @code |
| 430 | * $schema['foo'] = array( |
| 431 | * 'fields' => array( |
| 432 | * 'bar' => array('type' => 'int', 'not null' => TRUE) |
| 433 | * ), |
| 434 | * 'primary key' => array('bar') |
| 435 | * ); |
| 436 | * @endcode |
| 437 | * and you want to change foo.bar to be type serial, leaving it as the |
| 438 | * primary key. The correct sequence is: |
| 439 | * @code |
| 440 | * db_drop_primary_key($ret, 'foo'); |
| 441 | * db_change_field($ret, 'foo', 'bar', 'bar', |
| 442 | * array('type' => 'serial', 'not null' => TRUE), |
| 443 | * array('primary key' => array('bar'))); |
| 444 | * @endcode |
| 445 | * |
| 446 | * The reasons for this are due to the different database engines: |
| 447 | * |
| 448 | * On PostgreSQL, changing a field definition involves adding a new field |
| 449 | * and dropping an old one which* causes any indices, primary keys and |
| 450 | * sequences (from serial-type fields) that use the changed field to be dropped. |
| 451 | * |
| 452 | * On MySQL, all type 'serial' fields must be part of at least one key |
| 453 | * or index as soon as they are created. You cannot use |
| 454 | * db_add_{primary_key,unique_key,index}() for this purpose because |
| 455 | * the ALTER TABLE command will fail to add the column without a key |
| 456 | * or index specification. The solution is to use the optional |
| 457 | * $keys_new argument to create the key or index at the same time as |
| 458 | * field. |
| 459 | * |
| 460 | * You could use db_add_{primary_key,unique_key,index}() in all cases |
| 461 | * unless you are converting a field to be type serial. You can use |
| 462 | * the $keys_new argument in all cases. |
| 463 | * |
| 464 | * @param $ret |
| 465 | * Array to which query results will be added. |
| 466 | * @param $table |
| 467 | * Name of the table. |
| 468 | * @param $field |
| 469 | * Name of the field to change. |
| 470 | * @param $field_new |
| 471 | * New name for the field (set to the same as $field if you don't want to change the name). |
| 472 | * @param $spec |
| 473 | * The field specification for the new field. |
| 474 | * @param $keys_new |
| 475 | * Optional keys and indexes specification to be created on the |
| 476 | * table along with changing the field. The format is the same as a |
| 477 | * table specification but without the 'fields' element. |
| 478 | */ |
| 479 | abstract public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()); |
| 480 | |
| 481 | /** |
| 482 | * Create a new table from a Drupal table definition. |
| 483 | * |
| 484 | * @param $ret |
| 485 | * Array to which query results will be added. |
| 486 | * @param $name |
| 487 | * The name of the table to create. |
| 488 | * @param $table |
| 489 | * A Schema API table definition array. |
| 490 | */ |
| 491 | public function createTable(&$ret, $name, $table) { |
| 492 | $statements = $this->createTableSql($name, $table); |
| 493 | foreach ($statements as $statement) { |
| 494 | $ret[] = update_sql($statement); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Return an array of field names from an array of key/index column specifiers. |
| 500 | * |
| 501 | * This is usually an identity function but if a key/index uses a column prefix |
| 502 | * specification, this function extracts just the name. |
| 503 | * |
| 504 | * @param $fields |
| 505 | * An array of key/index column specifiers. |
| 506 | * @return |
| 507 | * An array of field names. |
| 508 | */ |
| 509 | public function fieldNames($fields) { |
| 510 | $ret = array(); |
| 511 | foreach ($fields as $field) { |
| 512 | if (is_array($field)) { |
| 513 | $ret[] = $field[0]; |
| 514 | } |
| 515 | else { |
| 516 | $ret[] = $field; |
| 517 | } |
| 518 | } |
| 519 | return $ret; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Prepare a table or column comment for database query. |
| 524 | * |
| 525 | * @param $comment |
| 526 | * The comment string to prepare. |
| 527 | * @param $length |
| 528 | * Optional upper limit on the returned string length. |
| 529 | * @return |
| 530 | * The prepared comment. |
| 531 | */ |
| 532 | public function prepareComment($comment, $length = NULL) { |
| 533 | return $this->connection->quote($comment); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * @} End of "defgroup schemaapi". |
| 539 | */ |
| 540 | |
| 541 |