--- includes\database.mysql.inc.orig Thu Apr 27 22:38:49 2006 +++ includes\database.mysql.inc Sat May 13 17:12:33 2006 @@ -364,6 +364,224 @@ } /** + * 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, 'suffix' => 'UNSIGNED NOT NULL', '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, 'suffix' => 'NOT NULL', '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, it accepts the following keys: + * 'type' - the drupal datatype [required] + * 'length' [optional] + * 'suffix' - i.e. 'NULL' or 'UNSIGNED NOT NULL' [optional] + * 'default' - default value for the column [optional] + * 'key' - + * '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']; + + 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 (isset($type['suffix'])) + $query .= " " . $type['suffix']; + + 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 . "), "; + } + + 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 $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 string $table + * the table to be altered + * @param string name + * the index's name + * @param mixed $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 $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, it accepts the following keys: + * 'type' - the datatype [required] + * 'length' [optional] + * 'suffix' - i.e. 'NULL' or 'UNSIGNED NOT NULL' [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 (isset($type['suffix'])) + $query .= " " . $type['suffix']; + + 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); +} + +/** * @} End of "ingroup database". */