00001 <?php
00002
00003
00033 function db_query($query) {
00034 $args = func_get_args();
00035 array_shift($args);
00036 $query = db_prefix_tables($query);
00037 if (isset($args[0]) and is_array($args[0])) {
00038 $args = $args[0];
00039 }
00040 _db_query_callback($args, TRUE);
00041 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
00042 return _db_query($query);
00043 }
00044
00060 function db_create_table_sql($name, $table) {
00061
00062 if (empty($table['mysql_suffix'])) {
00063 $table['mysql_suffix'] = "/*!40100 DEFAULT CHARACTER SET UTF8 */";
00064 }
00065
00066 $sql = "CREATE TABLE {" . $name . "} (\n";
00067
00068
00069 foreach ($table['fields'] as $field_name => $field) {
00070 $sql .= _db_create_field_sql($field_name, _db_process_field($field)) . ", \n";
00071 }
00072
00073
00074 $keys = _db_create_keys_sql($table);
00075 if (count($keys)) {
00076 $sql .= implode(", \n", $keys) . ", \n";
00077 }
00078
00079
00080 $sql = substr($sql, 0, -3) . "\n) ";
00081
00082 $sql .= $table['mysql_suffix'];
00083
00084 return array($sql);
00085 }
00086
00087 function _db_create_keys_sql($spec) {
00088 $keys = array();
00089
00090 if (!empty($spec['primary key'])) {
00091 $keys[] = 'PRIMARY KEY (' . _db_create_key_sql($spec['primary key']) . ')';
00092 }
00093 if (!empty($spec['unique keys'])) {
00094 foreach ($spec['unique keys'] as $key => $fields) {
00095 $keys[] = 'UNIQUE KEY ' . $key . ' (' . _db_create_key_sql($fields) . ')';
00096 }
00097 }
00098 if (!empty($spec['indexes'])) {
00099 foreach ($spec['indexes'] as $index => $fields) {
00100 $keys[] = 'INDEX ' . $index . ' (' . _db_create_key_sql($fields) . ')';
00101 }
00102 }
00103
00104 return $keys;
00105 }
00106
00107 function _db_create_key_sql($fields) {
00108 $ret = array();
00109 foreach ($fields as $field) {
00110 if (is_array($field)) {
00111 $ret[] = $field[0] . '(' . $field[1] . ')';
00112 }
00113 else {
00114 $ret[] = $field;
00115 }
00116 }
00117 return implode(', ', $ret);
00118 }
00119
00126 function _db_process_field($field) {
00127
00128 if (!isset($field['size'])) {
00129 $field['size'] = 'normal';
00130 }
00131
00132
00133 if (!isset($field['mysql_type'])) {
00134 $map = db_type_map();
00135 $field['mysql_type'] = $map[$field['type'] . ':' . $field['size']];
00136 }
00137
00138 if ($field['type'] == 'serial') {
00139 $field['auto_increment'] = TRUE;
00140 }
00141
00142 return $field;
00143 }
00144
00156 function _db_create_field_sql($name, $spec) {
00157 $sql = "`" . $name . "` " . $spec['mysql_type'];
00158
00159 if (isset($spec['length'])) {
00160 $sql .= '(' . $spec['length'] . ')';
00161 }
00162 elseif (isset($spec['precision']) && isset($spec['scale'])) {
00163 $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
00164 }
00165
00166 if (!empty($spec['unsigned'])) {
00167 $sql .= ' unsigned';
00168 }
00169
00170 if (!empty($spec['not null'])) {
00171 $sql .= ' NOT NULL';
00172 }
00173
00174 if (!empty($spec['auto_increment'])) {
00175 $sql .= ' auto_increment';
00176 }
00177
00178 if (isset($spec['default'])) {
00179 if (is_string($spec['default'])) {
00180 $spec['default'] = "'" . $spec['default'] . "'";
00181 }
00182 $sql .= ' DEFAULT ' . $spec['default'];
00183 }
00184
00185 if (empty($spec['not null']) && !isset($spec['default'])) {
00186 $sql .= ' DEFAULT NULL';
00187 }
00188
00189 return $sql;
00190 }
00191
00196 function db_type_map() {
00197
00198
00199
00200 $map = array(
00201 'varchar:normal' => 'VARCHAR',
00202 'char:normal' => 'CHAR',
00203
00204 'text:tiny' => 'TINYTEXT',
00205 'text:small' => 'TINYTEXT',
00206 'text:medium' => 'MEDIUMTEXT',
00207 'text:big' => 'LONGTEXT',
00208 'text:normal' => 'TEXT',
00209
00210 'serial:tiny' => 'TINYINT',
00211 'serial:small' => 'SMALLINT',
00212 'serial:medium' => 'MEDIUMINT',
00213 'serial:big' => 'BIGINT',
00214 'serial:normal' => 'INT',
00215
00216 'int:tiny' => 'TINYINT',
00217 'int:small' => 'SMALLINT',
00218 'int:medium' => 'MEDIUMINT',
00219 'int:big' => 'BIGINT',
00220 'int:normal' => 'INT',
00221
00222 'float:tiny' => 'FLOAT',
00223 'float:small' => 'FLOAT',
00224 'float:medium' => 'FLOAT',
00225 'float:big' => 'DOUBLE',
00226 'float:normal' => 'FLOAT',
00227
00228 'numeric:normal' => 'DECIMAL',
00229
00230 'blob:big' => 'LONGBLOB',
00231 'blob:normal' => 'BLOB',
00232
00233 'datetime:normal' => 'DATETIME',
00234 );
00235 return $map;
00236 }
00237
00248 function db_rename_table(&$ret, $table, $new_name) {
00249 $ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
00250 }
00251
00260 function db_drop_table(&$ret, $table) {
00261 $ret[] = update_sql('DROP TABLE {' . $table . '}');
00262 }
00263
00287 function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
00288 $fixnull = FALSE;
00289 if (!empty($spec['not null']) && !isset($spec['default'])) {
00290 $fixnull = TRUE;
00291 $spec['not null'] = FALSE;
00292 }
00293 $query = 'ALTER TABLE {' . $table . '} ADD ';
00294 $query .= _db_create_field_sql($field, _db_process_field($spec));
00295 if (count($keys_new)) {
00296 $query .= ', ADD ' . implode(', ADD ', _db_create_keys_sql($keys_new));
00297 }
00298 $ret[] = update_sql($query);
00299 if (isset($spec['initial'])) {
00300
00301 $sql = 'UPDATE {' . $table . '} SET ' . $field . ' = ' . db_type_placeholder($spec['type']);
00302 $result = db_query($sql, $spec['initial']);
00303 $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql . ' (' . $spec['initial'] . ')'));
00304 }
00305 if ($fixnull) {
00306 $spec['not null'] = TRUE;
00307 db_change_field($ret, $table, $field, $field, $spec);
00308 }
00309 }
00310
00321 function db_drop_field(&$ret, $table, $field) {
00322 $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP ' . $field);
00323 }
00324
00337 function db_field_set_default(&$ret, $table, $field, $default) {
00338 if ($default == NULL) {
00339 $default = 'NULL';
00340 }
00341 else {
00342 $default = is_string($default) ? "'$default'" : $default;
00343 }
00344
00345 $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' SET DEFAULT ' . $default);
00346 }
00347
00358 function db_field_set_no_default(&$ret, $table, $field) {
00359 $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' DROP DEFAULT');
00360 }
00361
00372 function db_add_primary_key(&$ret, $table, $fields) {
00373 $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' .
00374 _db_create_key_sql($fields) . ')');
00375 }
00376
00385 function db_drop_primary_key(&$ret, $table) {
00386 $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
00387 }
00388
00401 function db_add_unique_key(&$ret, $table, $name, $fields) {
00402 $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD UNIQUE KEY ' .
00403 $name . ' (' . _db_create_key_sql($fields) . ')');
00404 }
00405
00416 function db_drop_unique_key(&$ret, $table, $name) {
00417 $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP KEY ' . $name);
00418 }
00419
00432 function db_add_index(&$ret, $table, $name, $fields) {
00433 $query = 'ALTER TABLE {' . $table . '} ADD INDEX ' . $name . ' (' . _db_create_key_sql($fields) . ')';
00434 $ret[] = update_sql($query);
00435 }
00436
00447 function db_drop_index(&$ret, $table, $name) {
00448 $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP INDEX ' . $name);
00449 }
00450
00514 function db_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
00515 $sql = 'ALTER TABLE {' . $table . '} CHANGE ' . $field . ' ' .
00516 _db_create_field_sql($field_new, _db_process_field($spec));
00517 if (count($keys_new)) {
00518 $sql .= ', ADD ' . implode(', ADD ', _db_create_keys_sql($keys_new));
00519 }
00520 $ret[] = update_sql($sql);
00521 }
00522
00531 function db_last_insert_id($table, $field) {
00532 return db_result(db_query('SELECT LAST_INSERT_ID()'));
00533 }