From 491b156022c326c3cca414744f768c6f0256c63d Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 28 Dec 2006 21:04:57 +0000 Subject: [PATCH] Refactored pgsql import driver --- lib/Doctrine/Import/Pgsql.php | 94 ++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/lib/Doctrine/Import/Pgsql.php b/lib/Doctrine/Import/Pgsql.php index 74fdf3235..7756868fc 100644 --- a/lib/Doctrine/Import/Pgsql.php +++ b/lib/Doctrine/Import/Pgsql.php @@ -31,6 +31,47 @@ Doctrine::autoload('Doctrine_Import'); * @since 1.0 */ class Doctrine_Import_Pgsql extends Doctrine_Import { + + protected $sql = array( + 'listDatabases' => 'SELECT datname FROM pg_database', + 'listFunctions' => "SELECT + proname + FROM + pg_proc pr, + pg_type tp + WHERE + tp.oid = pr.prorettype + AND pr.proisagg = FALSE + AND tp.typname <> 'trigger' + AND pr.pronamespace IN + (SELECT oid FROM pg_namespace + WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema'", + 'listSequences' => "SELECT + relname + FROM + pg_class + WHERE relkind = 'S' AND relnamespace IN + (SELECT oid FROM pg_namespace + WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')", + 'listTables' => "SELECT + c.relname AS table_name + FROM pg_class c, pg_user u + WHERE c.relowner = u.usesysid + AND c.relkind = 'r' + AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) + AND c.relname !~ '^(pg_|sql_)' + UNION + SELECT c.relname AS table_name + FROM pg_class c + WHERE c.relkind = 'r' + AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) + AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) + AND c.relname !~ '^pg_'", + 'listViews' => 'SELECT viewname FROM pg_views', + 'listUsers' => 'SELECT usename FROM pg_user' + + ); + /** * listDatabases * lists all databases @@ -38,9 +79,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @return array */ public function listDatabases() { - $query = 'SELECT datname FROM pg_database'; - - return $this->conn->fetchColumn($query); + return $this->conn->fetchColumn($this->sql['listDatabases']); } /** * lists all availible database functions @@ -48,20 +87,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @return array */ public function listFunctions() { - $query = " - SELECT - proname - FROM - pg_proc pr, - pg_type tp - WHERE - tp.oid = pr.prorettype - AND pr.proisagg = FALSE - AND tp.typname <> 'trigger' - AND pr.pronamespace IN - (SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')"; - - return $this->conn->fetchColumn($query); + return $this->conn->fetchColumn($this->sql['listFunctions']); } /** * lists all database triggers @@ -78,11 +104,8 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @param string|null $database * @return array */ - public function listSequences($database = null) { - $query = "SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN"; - $query.= "(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')"; - - return $this->conn->fetchColumn($query); + public function listSequences($database = null) { + return $this->conn->fetchColumn($this->sql['listSequences']); } /** * lists table constraints @@ -163,20 +186,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @return array */ public function listTables($database = null) { - $sql = "SELECT c.relname AS table_name " - . "FROM pg_class c, pg_user u " - . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' " - . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " - . "AND c.relname !~ '^(pg_|sql_)' " - . "UNION " - . "SELECT c.relname AS table_name " - . "FROM pg_class c " - . "WHERE c.relkind = 'r' " - . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " - . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) " - . "AND c.relname !~ '^pg_'"; - - return $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + return $this->dbh->query($this->sql['listTables'])->fetchAll(PDO::FETCH_ASSOC); } /** * lists table triggers @@ -193,9 +203,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @param string $table database table name * @return array */ - public function listTableViews($table) { - $query = 'SELECT viewname FROM pg_views'; - + public function listTableViews($table) { return $this->conn->fetchColumn($query); } /** @@ -204,9 +212,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @return array */ public function listUsers() { - $query = 'SELECT usename FROM pg_user'; - - return $this->conn->fetchColumn($query); + return $this->conn->fetchColumn($this->sql['listUsers']); } /** * lists database views @@ -215,8 +221,6 @@ class Doctrine_Import_Pgsql extends Doctrine_Import { * @return array */ public function listViews($database = null) { - $query = 'SELECT viewname FROM pg_views'; - - return $this->conn->fetchColumn($query); + return $this->conn->fetchColumn($this->sql['listViews']); } }