function schema_mysql_inspect in Schema 5
Same name and namespace in other branches
- 6 engines/schema_mysql.inc \schema_mysql_inspect()
1 call to schema_mysql_inspect()
- schema_mysqli_inspect in engines/
schema_mysqli.inc
File
- engines/
schema_mysql.inc, line 127
Code
function schema_mysql_inspect($name = NULL) {
global $db_url;
// We need to ignore tables that are not in Drupal's schema since
// they may generate spurious warnings.
$schema = drupal_get_schema(NULL, TRUE);
$tables = array();
$url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
$database = substr($url['path'], 1);
$sql = 'SELECT * FROM information_schema.COLUMNS ' . 'WHERE TABLE_SCHEMA="%s" ';
if (isset($name)) {
$sql .= 'AND TABLE_NAME = "%s" ';
}
$sql .= 'ORDER BY TABLE_NAME, ORDINAL_POSITION';
$res = db_query($sql, $database, $name);
while ($r = db_fetch_array($res)) {
$r['TABLE_NAME'] = schema_unprefix_table($r['TABLE_NAME']);
$numeric = !is_null($r['NUMERIC_SCALE']);
$col = array();
$col['type'] = $r['COLUMN_TYPE'];
if (preg_match('@([a-z]+)(?:\\((\\d+)\\))?\\s*(unsigned)?@', $col['type'], $matches)) {
list($col['type'], $col['size']) = schema_schema_type($matches[1], $r['TABLE_NAME'], $r['COLUMN_NAME'], 'mysql');
if (isset($matches[2])) {
if ($numeric) {
$col['disp-width'] = $matches[2];
}
else {
$col['length'] = $matches[2];
}
}
if (isset($matches[3])) {
$col['unsigned'] = TRUE;
}
}
if ($col['type'] == 'int' && isset($r['EXTRA']) && $r['EXTRA'] == 'auto_increment') {
$col['type'] = 'serial';
}
$col['not null'] = $r['IS_NULLABLE'] == 'YES' ? FALSE : TRUE;
if (!is_null($r['COLUMN_DEFAULT'])) {
if ($numeric) {
// XXX floats!
$col['default'] = intval($r['COLUMN_DEFAULT']);
}
else {
$col['default'] = $r['COLUMN_DEFAULT'];
}
}
$tables[$r['TABLE_NAME']]['fields'][$r['COLUMN_NAME']] = $col;
$tables[$r['TABLE_NAME']]['name'] = $r['TABLE_NAME'];
}
$res = db_query('SELECT * FROM information_schema.STATISTICS ' . 'WHERE TABLE_SCHEMA="%s" ' . 'ORDER BY TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX', $database);
while ($r = db_fetch_array($res)) {
$r['TABLE_NAME'] = schema_unprefix_table($r['TABLE_NAME']);
if (isset($r['SUB_PART']) && !is_null($r['SUB_PART'])) {
$col = array(
$r['COLUMN_NAME'],
intval($r['SUB_PART']),
);
}
else {
$col = $r['COLUMN_NAME'];
}
if ($r['INDEX_NAME'] == 'PRIMARY') {
$type = 'primary key';
$tables[$r['TABLE_NAME']][$type][] = $col;
continue;
}
else {
if ($r['NON_UNIQUE'] == 0) {
$type = 'unique keys';
}
else {
$type = 'indexes';
}
}
$tables[$r['TABLE_NAME']][$type][$r['INDEX_NAME']][] = $col;
}
return $tables;
}