From e765a9657b0491c6d4fd6436c9cd489342a465c9 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 30 Jan 2010 18:45:10 +1300 Subject: move the schema DDL sql off into seperate files for each db we support --- lib/schema.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/schema.php') diff --git a/lib/schema.php b/lib/schema.php index a7f64ebed..164387ff3 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -75,8 +75,11 @@ class Schema static function get() { + $type = common_config('db', 'type'); if (empty(self::$_single)) { - self::$_single = new Schema(); + include "lib/schema.{$type}.php"; + $class = $type.='Schema'; + self::$_single = new $class(); } return self::$_single; } -- cgit v1.2.3-54-g00ecf From 870c83c17de9710800163570cdc5321591e73c34 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 10 Jan 2010 05:21:23 +0000 Subject: getTableDef() mostly working in postgres --- lib/schema.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'lib/schema.php') diff --git a/lib/schema.php b/lib/schema.php index 164387ff3..c1636c21d 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -97,7 +97,12 @@ class Schema public function getTableDef($name) { - $res = $this->conn->query('DESCRIBE ' . $name); + if(common_config('db','type') == 'pgsql') { + $res = $this->conn->query("select column_default as default, is_nullable as Null, udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'"); + } + else { + $res = $this->conn->query('DESCRIBE ' . $name); + } if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -111,12 +116,16 @@ class Schema $row = array(); while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { + //lower case the keys, because the php postgres driver is case insentive for column names + foreach($row as $k=>$v) { + $row[strtolower($k)] = $row[$k]; + } $cd = new ColumnDef(); - $cd->name = $row['Field']; + $cd->name = $row['field']; - $packed = $row['Type']; + $packed = $row['type']; if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) { $cd->type = $match[1]; @@ -125,9 +134,9 @@ class Schema $cd->type = $packed; } - $cd->nullable = ($row['Null'] == 'YES') ? true : false; + $cd->nullable = ($row['null'] == 'YES') ? true : false; $cd->key = $row['Key']; - $cd->default = $row['Default']; + $cd->default = $row['default']; $cd->extra = $row['Extra']; $td->columns[] = $cd; -- cgit v1.2.3-54-g00ecf From 22a6e46b45226647b6e7b9bfdc013e59c4a52428 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 30 Jan 2010 21:22:30 +1300 Subject: removed describeTable from base class, and fixed it up in pgsql --- lib/schema.pgsql.php | 13 ++++++------ lib/schema.php | 60 ---------------------------------------------------- 2 files changed, 6 insertions(+), 67 deletions(-) (limited to 'lib/schema.php') diff --git a/lib/schema.pgsql.php b/lib/schema.pgsql.php index 7291106dc..91bc09667 100644 --- a/lib/schema.pgsql.php +++ b/lib/schema.pgsql.php @@ -61,7 +61,7 @@ class PgsqlSchema extends Schema public function getTableDef($name) { - $res = $this->conn->query('DESCRIBE ' . $name); + $res = $this->conn->query("select *, column_default as default, is_nullable as Null, udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'"); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -75,12 +75,12 @@ class PgsqlSchema extends Schema $row = array(); while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { - +// var_dump($row); $cd = new ColumnDef(); - $cd->name = $row['Field']; + $cd->name = $row['field']; - $packed = $row['Type']; + $packed = $row['type']; if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) { $cd->type = $match[1]; @@ -89,14 +89,13 @@ class PgsqlSchema extends Schema $cd->type = $packed; } - $cd->nullable = ($row['Null'] == 'YES') ? true : false; + $cd->nullable = ($row['null'] == 'YES') ? true : false; $cd->key = $row['Key']; - $cd->default = $row['Default']; + $cd->default = $row['default']; $cd->extra = $row['Extra']; $td->columns[] = $cd; } - return $td; } diff --git a/lib/schema.php b/lib/schema.php index c1636c21d..27a4deda1 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -84,66 +84,6 @@ class Schema return self::$_single; } - /** - * Returns a TableDef object for the table - * in the schema with the given name. - * - * Throws an exception if the table is not found. - * - * @param string $name Name of the table to get - * - * @return TableDef tabledef for that table. - */ - - public function getTableDef($name) - { - if(common_config('db','type') == 'pgsql') { - $res = $this->conn->query("select column_default as default, is_nullable as Null, udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'"); - } - else { - $res = $this->conn->query('DESCRIBE ' . $name); - } - - if (PEAR::isError($res)) { - throw new Exception($res->getMessage()); - } - - $td = new TableDef(); - - $td->name = $name; - $td->columns = array(); - - $row = array(); - - while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { - //lower case the keys, because the php postgres driver is case insentive for column names - foreach($row as $k=>$v) { - $row[strtolower($k)] = $row[$k]; - } - - $cd = new ColumnDef(); - - $cd->name = $row['field']; - - $packed = $row['type']; - - if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) { - $cd->type = $match[1]; - $cd->size = $match[2]; - } else { - $cd->type = $packed; - } - - $cd->nullable = ($row['null'] == 'YES') ? true : false; - $cd->key = $row['Key']; - $cd->default = $row['default']; - $cd->extra = $row['Extra']; - - $td->columns[] = $cd; - } - - return $td; - } /** * Gets a ColumnDef object for a single column. -- cgit v1.2.3-54-g00ecf