1
0
mirror of synced 2024-12-13 22:56:04 +03:00

Refactored pgsql import driver

This commit is contained in:
zYne 2006-12-28 21:04:57 +00:00
parent dbad26258b
commit 491b156022

View File

@ -31,6 +31,47 @@ Doctrine::autoload('Doctrine_Import');
* @since 1.0 * @since 1.0
*/ */
class Doctrine_Import_Pgsql extends Doctrine_Import { 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 * listDatabases
* lists all databases * lists all databases
@ -38,9 +79,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @return array * @return array
*/ */
public function listDatabases() { public function listDatabases() {
$query = 'SELECT datname FROM pg_database'; return $this->conn->fetchColumn($this->sql['listDatabases']);
return $this->conn->fetchColumn($query);
} }
/** /**
* lists all availible database functions * lists all availible database functions
@ -48,20 +87,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @return array * @return array
*/ */
public function listFunctions() { public function listFunctions() {
$query = " return $this->conn->fetchColumn($this->sql['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')";
return $this->conn->fetchColumn($query);
} }
/** /**
* lists all database triggers * lists all database triggers
@ -78,11 +104,8 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @param string|null $database * @param string|null $database
* @return array * @return array
*/ */
public function listSequences($database = null) { public function listSequences($database = null) {
$query = "SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN"; return $this->conn->fetchColumn($this->sql['listSequences']);
$query.= "(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
return $this->conn->fetchColumn($query);
} }
/** /**
* lists table constraints * lists table constraints
@ -163,20 +186,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @return array * @return array
*/ */
public function listTables($database = null) { public function listTables($database = null) {
$sql = "SELECT c.relname AS table_name " return $this->dbh->query($this->sql['listTables'])->fetchAll(PDO::FETCH_ASSOC);
. "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);
} }
/** /**
* lists table triggers * lists table triggers
@ -193,9 +203,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @param string $table database table name * @param string $table database table name
* @return array * @return array
*/ */
public function listTableViews($table) { public function listTableViews($table) {
$query = 'SELECT viewname FROM pg_views';
return $this->conn->fetchColumn($query); return $this->conn->fetchColumn($query);
} }
/** /**
@ -204,9 +212,7 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @return array * @return array
*/ */
public function listUsers() { public function listUsers() {
$query = 'SELECT usename FROM pg_user'; return $this->conn->fetchColumn($this->sql['listUsers']);
return $this->conn->fetchColumn($query);
} }
/** /**
* lists database views * lists database views
@ -215,8 +221,6 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
* @return array * @return array
*/ */
public function listViews($database = null) { public function listViews($database = null) {
$query = 'SELECT viewname FROM pg_views'; return $this->conn->fetchColumn($this->sql['listViews']);
return $this->conn->fetchColumn($query);
} }
} }