pkrenn: tableList() fixed, indexList() added (merged from branches/gsoc)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41711 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-14 01:41:13 +00:00
parent c892ad24a3
commit b29cc1fbc7

View File

@ -441,58 +441,63 @@ class PDODatabase extends Database {
case "pgsql": case "pgsql":
foreach ($dbh->query(" foreach ($dbh->query("
SELECT SELECT
a.attname AS field, column_name AS cname,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS type, column_default AS cdefault,
a.attnotnull AS null is_nullable AS nullable,
data_type AS dtype,
character_maximum_length AS maxlength
FROM FROM
pg_class c, information_schema.columns
pg_attribute a,
pg_type t
WHERE WHERE
c.relname = $table table_name = $table
AND a.attnum > 0
AND a.attrelid = c.oid
AND a.atttypid = t.oid
AND (NOT A.attisdropped)
ORDER BY
a.attnum
") as $field) { ") as $field) {
$fieldSpec = $field['type']; if ($field['maxlength']) {
if ($field['null'] == 't') { $fieldSpec = $field['dtype'] . "(" . $field['maxlength'] . ")";
} else {
$fieldSpec = $field['dtype'];
}
if ($field['nullable'] == 'NO') {
$fieldSpec .= ' not null'; $fieldSpec .= ' not null';
} }
if($field['cdefault'] || $field['cdefault'] === "0") {
$fieldList[$field['field']] = $fieldSpec; $fieldSpec .= " default '" . addslashes($field['cdefault']) . "'";
}
$fieldList[$field['cname']] = $fieldSpec;
} }
break; break;
case "mssql": case "mssql":
foreach ($dbh->query(" foreach ($dbh->query("
SELECT SELECT
COLUMN_NAME as 'field', COLUMN_NAME AS 'cname',
COLUMN_DEFAULT as 'default', COLUMN_DEFAULT AS 'cdefault',
IS_NULLABLE as 'null', IS_NULLABLE AS 'nullable',
DATA_TYPE as 'type', DATA_TYPE AS 'dtype',
COLLATION_NAME as 'collation', COLLATION_NAME AS 'collname',
CHARACTER_SET_NAME as 'characterset' CHARACTER_SET_NAME AS 'cset',
CHARACTER_MAXIMUM_LENGTH AS 'maxlength'
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
TABLE_NAME = 'tableb' TABLE_NAME = '$table'
") as $field) { ") as $field) {
$fieldSpec = $field['type']; if ($field['maxlength']) {
if ($field['null'] == 't') { $fieldSpec = $field['dtype'] . "(" . $field['maxlength'] . ")";
} else {
$fieldSpec = $field['dtype'];
}
if ($field['nullable'] == 'NO') {
$fieldSpec .= ' not null'; $fieldSpec .= ' not null';
} }
if($field['collation'] && $field['collation'] != 'NULL') { if($field['collname'] && $field['collname'] != 'NULL') {
$fieldSpec .= " character set $field[characterset] collate $field[collation]"; $fieldSpec .= " character set $field[cset] collate $field[collname]";
} }
if($field['default'] || $field['default'] === "0") { if($field['cdefault'] || $field['cdefault'] === "0") {
$fieldSpec .= " default '" . addslashes($field['default']) . "'"; $fieldSpec .= " default '" . addslashes($field['cdefault']) . "'";
} }
$fieldList[$field['field']] = $fieldSpec; $fieldList[$field['cname']] = $fieldSpec;
} }
break; break;
default: default:
@ -508,9 +513,54 @@ class PDODatabase extends Database {
* Returns a map of indexes. * Returns a map of indexes.
*/ */
public function indexList($table) { public function indexList($table) {
switch (PDO::ATTR_DRIVER_NAME) {
// to be done - SHOW is used extensively but very MySQL specific case "mysql":
foreach($dbh->query("SHOW INDEXES IN '$table'") as $index) {
$groupedIndexes[$index['Key_name']]['fields'][$index['Seq_in_index']] = $index['Column_name'];
if($index['Index_type'] == 'FULLTEXT') {
$groupedIndexes[$index['Key_name']]['type'] = 'fulltext ';
} else if(!$index['Non_unique']) {
$groupedIndexes[$index['Key_name']]['type'] = 'unique ';
} else {
$groupedIndexes[$index['Key_name']]['type'] = '';
}
}
foreach($groupedIndexes as $index => $details) {
ksort($details['fields']);
$indexList[$index] = $details['type'] . '(' . implode(',',$details['fields']) . ')';
}
break;
case "pgsql":
foreach($dbh->query("select indexname, indexdef from pg_indexes where tablename = '$table'") as $index) {
$indexList[$index['indexname']] = $index['indexdef'];
}
break;
case "mssql":
foreach($dbh->query("
SELECT
i.name AS 'iname',
i.type_desc AS 'itype',
s.name AS 'sname'
FROM
sys.indexes i,
sys.objects o,
sys.index_columns c,
sys.columns s
WHERE
o.name = '$table'
AND o.object_id = i.object_id
AND o.object_id = c.object_id
AND o.object_id = s.object_id
AND s.column_id = c.column_id
") as $index) {
$indexList[$index['iname']] = $index['itype'] . " (" . $index['sname'] . ")";
}
break;
default:
$this->databaseError("This database server is not available");
}
return $indexList;
} }
/** /**
@ -594,7 +644,7 @@ class PDOQuery extends Query {
/** /**
* *
*/ */
public function nextRecord() { public function nextRecord() {
$record = $this->stmt->fetch(PDO::FETCH_ASSOC); $record = $this->stmt->fetch(PDO::FETCH_ASSOC);