public function SchemaDatabaseSchema_pgsql::inspect in Schema 7
File
- engines/pgsql.inc, line 45
- Schema module enhancements to DatabaseSchema_pgsql
Class
- SchemaDatabaseSchema_pgsql
- @file
Schema module enhancements to DatabaseSchema_pgsql
Code
public function inspect($connection = NULL, $table_name = NULL) {
if (isset($connection) && $connection != $this->connection
->getKey()) {
$this->connection = Database::getConnection('default', $connection);
}
$info = $this->connection
->getConnectionOptions();
$database = $info['database'];
if (isset($table_name)) {
$table_info = $this
->getPrefixInfo($table_name);
$table_name = $table_info['table'];
}
$sql = 'SELECT * FROM information_schema.COLUMNS ' . 'WHERE table_catalog=:database AND table_schema=current_schema()';
$tokens = array(
':database' => $database,
);
if (isset($table_name)) {
$sql .= 'AND table_name = :table ';
$tokens[':table'] = $table_name;
}
$sql .= 'ORDER BY table_name, ordinal_position';
$res = $this->connection
->query($sql, $tokens);
foreach ($res as $r) {
$col = array();
$r->new_table_name = schema_unprefix_table($r->table_name, $this->connection);
$numeric = !is_null($r->numeric_precision_radix);
list($col['type'], $col['size']) = schema_schema_type($r->data_type, $r->table_name, $r->column_name, 'pgsql');
if (!$numeric && $r->character_maximum_length) {
$col['length'] = $r->character_maximum_length;
}
if ($col['type'] == 'numeric') {
$col['precision'] = (int) $r->numeric_precision;
$col['scale'] = (int) $r->numeric_scale;
}
$col['not null'] = $r->is_nullable == 'YES' ? FALSE : TRUE;
if (!is_null($r->column_default)) {
if (strpos($r->column_default, '::') !== FALSE) {
list($col['default'], $def_type) = explode('::', $r->column_default);
}
else {
$col['default'] = $r->column_default;
$def_type = '';
}
if ($numeric) {
$col['default'] = preg_replace('/^\\((.*)\\)$/', '\\1', $col['default']);
if (strpos($col['default'], 'nextval(\'') !== FALSE && $def_type == 'regclass)') {
$col['type'] = 'serial';
unset($col['default']);
}
elseif ($col['type'] == 'float') {
$col['default'] = floatval($col['default']);
}
else {
$col['default'] = intval($col['default']);
}
}
else {
$col['default'] = substr($col['default'], 1, -1);
}
}
switch ($r->domain_name) {
case 'int_unsigned':
case 'smallint_unsigned':
case 'bigint_unsigned':
$col['unsigned'] = 1;
break;
}
if (isset($r->check_clause) && $r->check_clause == '((' . $r->column_name . ' => 0))') {
$col['unsigned'] = 1;
}
$tables[$r->table_name]['fields'][$r->column_name] = $col;
$tables[$r->table_name]['name'] = $r->new_table_name;
}
$res = $this->connection
->query('SELECT ccu.*, cc.check_clause
FROM information_schema.constraint_column_usage ccu
INNER JOIN information_schema.check_constraints cc ON ccu.constraint_name=cc.constraint_name
WHERE table_schema=current_schema()');
foreach ($res as $r) {
$r->table_name = schema_unprefix_table($r->table_name, $this->connection);
if ($r->check_clause == '((' . $r->column_name . ' >= 0))' || $r->check_clause == '((' . $r->column_name . ' >= (0)::numeric))') {
$tables[$r->table_name]['fields'][$r->column_name]['unsigned'] = TRUE;
}
}
$res = $this->connection
->query('SELECT n.nspname, c.relname AS tblname, ' . ' c2.relname AS indname, i.indisprimary, i.indisunique, ' . ' pg_get_indexdef(i.indexrelid) AS inddef ' . 'FROM pg_class c, pg_class c2, pg_index i, pg_namespace n ' . 'WHERE c.oid = i.indrelid AND i.indexrelid = c2.oid AND ' . ' c.relnamespace=n.oid AND n.nspname=current_schema() ' . 'ORDER BY c2.relname');
foreach ($res as $r) {
$r->tblname = schema_unprefix_table($r->tblname, $this->connection);
$r->indname = schema_unprefix_table($r->indname, $this->connection);
if (preg_match('@CREATE(?: UNIQUE)? INDEX \\w+ ON "?(\\w+)"?(?: USING \\w+)? \\((.*)\\)@', $r->inddef, $m)) {
list($all, $table, $keys) = $m;
$name = $r->indname;
if (preg_match('@^' . $r->tblname . '_(.*)_(?:idx|key)$@', $name, $m)) {
$name = $m[1];
}
preg_match_all('@((?:"?\\w+"?)|(?:substr\\(\\(?"?(\\w+)\\"?.*?, 1, (\\d+)\\)))(?:, |$)@', $keys, $m);
foreach ($m[1] as $idx => $colname) {
if ($m[2][$idx]) {
$key = array(
$m[2][$idx],
intval($m[3][$idx]),
);
}
else {
$key = str_replace('"', '', $colname);
}
if ($r->indisprimary == 't') {
$tables[$r->tblname]['primary key'][] = $key;
}
elseif ($r->indisunique == 't') {
$tables[$r->tblname]['unique keys'][$name][] = $key;
}
else {
$tables[$r->tblname]['indexes'][$name][] = $key;
}
}
}
else {
watchdog('schema', 'unrecognized pgsql index definition: @stmt', array(
'@stmt' => $r->inddef,
));
}
}
foreach ($tables as $tablename => $table) {
$newname = $tables[$tablename]['name'];
if ($tablename != $newname) {
$tables[$newname] = $table;
unset($tables[$tablename]);
}
}
return $tables;
}