--- database.mysql.inc.old 2006-05-16 18:27:07.093750000 +0200 +++ database.mysql.inc 2006-05-16 20:40:04.000000000 +0200 @@ -364,7 +364,268 @@ function db_unlock_tables() { } /** - * @} End of "ingroup database". +* Get drupal datatypes. +* +* @return +* Array containing all valid drupal datatypes with their default properties +*/ +function db_get_types() { + $types = array ( + 'string' => array('type' => 'VARCHAR', 'length' => 255), + 'text' => array('type' => 'TEXT'), + 'long text' => array('type' => 'LONGTEXT'), + 'timestamp' => array('type' => 'INT', 'length' => 11, 'NULL' => FALSE, 'unsigned' => TRUE, 'default' => 0), + 'int' => array('type' => 'INT', 'length' => 10), + 'medium int' => array('type' => 'MEDIUMINT', 'length' => 9), + 'tiny int' => array('type' => 'TINYINT', 'length' => 4), + 'blob' => array('type' => 'BLOB'), + 'long blob' => array('type' => 'LONGBLOB'), + 'boolean' => array('type' => 'TINYINT', 'length' => 1, 'NULL' => 'FALSE', 'default' => 0) + ); + return $types; +} + +/** +* Create a new table. +* +* @param string $table +* Name of the table to be created. +* @param array $columns +* Is an associative array with 'name' => 'type', +* 'name' should be a string containing the name of the column, +* 'type' can either be a string (containing a drupal datatype, the default properties will be used) or +* an associative array (to specify more properties). +* +* If the latter is used, all default properties specified in the drupal datatype array can be overriden +* by setting the following keys in the 'type'-array: +* +* 'type' - The drupal datatype [required]. +* 'length' - For varchar or numeric types [optional] +* 'NULL' - FALSE for NOT NULL, TRUE for NULL [optional] +* 'unsigned' - TRUE for UNSIGNED [optional] +* 'auto_increment' - TRUE for 'auto_increment' [optional] +* 'default' - Default value for the column [optional]. +* 'key' - [optional] +* 'primary' to make the column the primary key, +* 'unique' for unique key or +* use any other string $string to create a new key $string. All columns that have type['key'] = $string will be part of it. +*/ +function db_create_table($table, $columns) { + $query = "CREATE TABLE IF NOT EXISTS `" . db_escape_table($table) . "` ("; + + $typedefs = db_get_types(); + + foreach ($columns as $name => $type) { + if (is_string($type)) + $type = array('type' => $type); + + $datatype = $type['type']; + $type['type'] = $typedefs[$datatype]['type']; + + // Get default properties for unset keys. + foreach ($typedefs[$datatype] as $key => $value) { + if (!isset($type[$key])) + $type[$key] = $value; + } + + $query .= "`" . $name . "` " . $type['type']; + + if (isset($type['length'])) + $query .= "(" . $type['length'] . ") "; + + if (!empty($type['unsigned'])) + $query .= " unsigned "; + + // Isset check to provide the ability to override 'NOT NULL' from a drupal datatype with 'NULL' for specific query. + if (isset($type['NULL'])) { + if (!$type['NULL']) + $query .= " NOT NULL "; + } + + if (!empty($type['auto_increment'])) + $query .= " auto_increment "; + + if (isset($type['default'])) + $query .= " DEFAULT '" . $type['default'] . "'"; + + if (isset($type['key'])) { + switch ($type['key']) { + case 'primary': + $primarykey = $name; + break; + case 'unique': + $uniquekeys[] = $name; + break; + default: + $keys[$type['key']][] = $name; + break; + } + } + + $query .= ", "; + } + + if (isset($primarykey)) + $query .= " PRIMARY KEY (" . $primarykey . "), "; + + if (isset($uniquekeys)) { + foreach($uniquekeys as $current) + $query .= " UNIQUE KEY (" . $current . "), "; + } + + // Add keys. + if (isset($keys)) { + foreach($keys as $name => $columns) { + $query .= " KEY " . $name . " ("; + foreach ($columns as $current) + $query .= $current . ", "; + $query = substr($query, 0, strlen($query) - 2); + $query .= "), "; + } + } + + $query = substr($query, 0, strlen($query) - 2); + $query .= ");"; + + return db_query($query); +} + +/** +* Drop table. +* +* @param string $table +* The table to be dropped. +*/ +function db_drop_table($table) { + return db_query('DROP TABLE IF EXISTS ' . db_escape_table($table)); +} + +/** +* Create index. +* +* @param $table +* The table to be altered. +* @param $name +* The index's name. +* @param $columns +* Is an array of column names, or just a string with a single column name. +*/ +function db_create_index($table, $name, $columns) { + $query = 'ALTER TABLE ' . db_escape_table($table); + + if (is_array($columns)) { + $query .= ' ADD INDEX ' . $name . ' ('; + foreach ($columns as $current) + $query .= $current . ', '; + $query = substr($query, 0, strlen($query) - 2); + $query .= ')'; + } + + if (is_string($columns)) + $query .= ' ADD INDEX ' . $name . ' (' . $columns . ')'; + + return db_query($query); +} + +/** +* Drop index. +* +* @param string $table +* The table to be altered. +* @param string $name +* Name of the index to be dropped. +*/ +function db_drop_index($table, $name) { + return db_query('ALTER TABLE ' . db_escape_table($table) . ' DROP INDEX ' . $name); +} + +/** +* Adds a new column to a table. +* +* @param string $table +* Name of the table to be altered. +* @param string $column +* Name of the column to be added. +* @param mixed $type +* Can either be a string (containing a datatype, the default properties will be used) or +* an associative array (to specify more properties). +* +* If the latter is used, all default properties specified in the drupal datatype array can be overriden +* by setting the following keys in the 'type'-array: +* +* 'type' - the datatype [required]. +* 'length' [optional] +* 'NULL' - FALSE for NOT NULL, TRUE for NULL [optional] +* 'unsigned' - TRUE for UNSIGNED [optional] +* 'auto_increment' - TRUE for 'auto_increment' [optional] +* 'default' - default value for the column [optional]. +*/ +function db_add_column($table, $column, $type) { + $query = 'ALTER TABLE ' . db_escape_table($table) . ' ADD ' . $column . ' '; + + $typedefs = db_get_types(); + + if (is_string($type)) + $type = array('type' => $type); + + $datatype = $type['type']; + $type['type'] = $typedefs[$datatype]['type']; + + foreach ($typedefs[$datatype] as $key => $value) { + if (!isset($type[$key])) + $type[$key] = $value; + } + + $query .= $type['type']; + + if (isset($type['length'])) + $query .= "(" . $type['length'] . ") "; + + if (!empty($type['unsigned'])) + $query .= " unsigned "; + + // Isset check to provide the ability to override 'NOT NULL' from a drupal datatype with 'NULL' for specific query. + if (isset($type['NULL'])) { + if (!$type['NULL']) + $query .= " NOT NULL "; + } + + if (!empty($type['auto_increment'])) + $query .= " auto_increment "; + + if (isset($type['default'])) + $query .= " DEFAULT '" . $type['default'] . "'"; + + return db_query($query); +} + +/** +* Drop column. +* +* @param string $table +* The table to be altered. +* @param string $column +* The column to be dropped. +*/ +function db_drop_column($table, $column) { + return db_query('ALTER TABLE ' . db_escape_table($table) . ' DROP ' . $column); +} + +/** + * Set default value for $column. + * + * @param $table + * @param $column + * The column to be altered. + * @param $default + * Default value to be set. */ +function db_column_set_default($table, $column, $default) { + return db_query("ALTER TABLE " . db_escape_table($table) . " ALTER COLUMN " . $column . " SET DEFAULT '" . $default . "'"); +} + +/** +* } End of "ingroup database". +*/