Refactored import and pgsql import driver
This commit is contained in:
parent
491b156022
commit
b40c969f74
@ -34,30 +34,43 @@ Doctrine::autoload('Doctrine_Connection_Module');
|
|||||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||||
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
|
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Import extends Doctrine_Connection_Module {
|
class Doctrine_Import extends Doctrine_Connection_Module
|
||||||
|
{
|
||||||
|
protected $sql = array();
|
||||||
/**
|
/**
|
||||||
* lists all databases
|
* lists all databases
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listDatabases() {
|
public function listDatabases()
|
||||||
|
{
|
||||||
|
if( ! isset($this->sql['listDatabases'])) {
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->conn->fetchColumn($this->sql['listDatabases']);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* lists all availible database functions
|
* lists all availible database functions
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listFunctions() {
|
public function listFunctions()
|
||||||
|
{
|
||||||
|
if( ! isset($this->sql['listFunctions'])) {
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->conn->fetchColumn($this->sql['listFunctions']);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* lists all database triggers
|
* lists all database triggers
|
||||||
*
|
*
|
||||||
* @param string|null $database
|
* @param string|null $database
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTriggers($database = null) {
|
public function listTriggers($database = null)
|
||||||
|
{
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -66,7 +79,22 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
* @param string|null $database
|
* @param string|null $database
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listSequences($database = null) {
|
public function listSequences($database = null)
|
||||||
|
{
|
||||||
|
if( ! isset($this->sql['listSequences'])) {
|
||||||
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->conn->fetchColumn($this->sql['listSequences']);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* lists table constraints
|
||||||
|
*
|
||||||
|
* @param string $table database table name
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function listTableConstraints($table)
|
||||||
|
{
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -75,7 +103,8 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
* @param string $table database table name
|
* @param string $table database table name
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableConstraints($table) {
|
public function listTableColumns($table)
|
||||||
|
{
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -84,16 +113,8 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
* @param string $table database table name
|
* @param string $table database table name
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableColumns($table) {
|
public function listTableIndexes($table)
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
{
|
||||||
}
|
|
||||||
/**
|
|
||||||
* lists table constraints
|
|
||||||
*
|
|
||||||
* @param string $table database table name
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listTableIndexes($table) {
|
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -102,7 +123,8 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
* @param string|null $database
|
* @param string|null $database
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTables($database = null) {
|
public function listTables($database = null)
|
||||||
|
{
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -111,7 +133,8 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
* @param string $table database table name
|
* @param string $table database table name
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableTriggers($table) {
|
public function listTableTriggers($table)
|
||||||
|
{
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -120,7 +143,8 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
* @param string $table database table name
|
* @param string $table database table name
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableViews($table) {
|
public function listTableViews($table)
|
||||||
|
{
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -128,16 +152,26 @@ class Doctrine_Import extends Doctrine_Connection_Module {
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listUsers() {
|
public function listUsers()
|
||||||
|
{
|
||||||
|
if( ! isset($this->sql['listUsers'])) {
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->conn->fetchColumn($this->sql['listUsers']);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* lists database views
|
* lists database views
|
||||||
*
|
*
|
||||||
* @param string|null $database
|
* @param string|null $database
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listViews($database = null) {
|
public function listViews($database = null)
|
||||||
|
{
|
||||||
|
if( ! isset($this->sql['listViews'])) {
|
||||||
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->conn->fetchColumn($this->sql['listViews']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,27 +68,54 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
|
|||||||
AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner)
|
AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner)
|
||||||
AND c.relname !~ '^pg_'",
|
AND c.relname !~ '^pg_'",
|
||||||
'listViews' => 'SELECT viewname FROM pg_views',
|
'listViews' => 'SELECT viewname FROM pg_views',
|
||||||
'listUsers' => 'SELECT usename FROM pg_user'
|
'listUsers' => 'SELECT usename FROM pg_user',
|
||||||
|
'listTableConstraints' => "SELECT
|
||||||
|
relname
|
||||||
|
FROM
|
||||||
|
pg_class
|
||||||
|
WHERE oid IN (
|
||||||
|
SELECT indexrelid
|
||||||
|
FROM pg_index, pg_class
|
||||||
|
WHERE pg_class.relname = %s
|
||||||
|
AND pg_class.oid = pg_index.indrelid
|
||||||
|
AND (indisunique = 't' OR indisprimary = 't')
|
||||||
|
)",
|
||||||
|
'listTableIndexes' => "SELECT
|
||||||
|
relname
|
||||||
|
FROM
|
||||||
|
pg_class
|
||||||
|
WHERE oid IN (
|
||||||
|
SELECT indexrelid
|
||||||
|
FROM pg_index, pg_class
|
||||||
|
WHERE pg_class.relname=%s
|
||||||
|
AND pg_class.oid=pg_index.indrelid
|
||||||
|
AND indisunique != 't'
|
||||||
|
AND indisprimary != 't'
|
||||||
|
)",
|
||||||
|
'listTableColumns' => "SELECT
|
||||||
|
a.attnum,
|
||||||
|
a.attname AS field,
|
||||||
|
t.typname AS type,
|
||||||
|
format_type(a.atttypid, a.atttypmod) AS complete_type,
|
||||||
|
a.attnotnull AS isnotnull,
|
||||||
|
(SELECT 't'
|
||||||
|
FROM pg_index
|
||||||
|
WHERE c.oid = pg_index.indrelid
|
||||||
|
AND pg_index.indkey[0] = a.attnum
|
||||||
|
AND pg_index.indisprimary = 't'
|
||||||
|
) AS pri,
|
||||||
|
(SELECT pg_attrdef.adsrc
|
||||||
|
FROM pg_attrdef
|
||||||
|
WHERE c.oid = pg_attrdef.adrelid
|
||||||
|
AND pg_attrdef.adnum=a.attnum
|
||||||
|
) AS default
|
||||||
|
FROM pg_attribute a, pg_class c, pg_type t
|
||||||
|
WHERE c.relname = %s
|
||||||
|
AND a.attnum > 0
|
||||||
|
AND a.attrelid = c.oid
|
||||||
|
AND a.atttypid = t.oid
|
||||||
|
ORDER BY a.attnum",
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* listDatabases
|
|
||||||
* lists all databases
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listDatabases() {
|
|
||||||
return $this->conn->fetchColumn($this->sql['listDatabases']);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* lists all availible database functions
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listFunctions() {
|
|
||||||
return $this->conn->fetchColumn($this->sql['listFunctions']);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* lists all database triggers
|
* lists all database triggers
|
||||||
*
|
*
|
||||||
@ -97,15 +124,6 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
|
|||||||
*/
|
*/
|
||||||
public function listTriggers($database = null) {
|
public function listTriggers($database = null) {
|
||||||
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* lists all database sequences
|
|
||||||
*
|
|
||||||
* @param string|null $database
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listSequences($database = null) {
|
|
||||||
return $this->conn->fetchColumn($this->sql['listSequences']);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* lists table constraints
|
* lists table constraints
|
||||||
@ -114,10 +132,8 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableConstraints($table) {
|
public function listTableConstraints($table) {
|
||||||
$table = $db->quote($table, 'text');
|
$table = $this->conn->quote($table);
|
||||||
$subquery = "SELECT indexrelid FROM pg_index, pg_class";
|
$query = sprintf($this->sql['listTableConstraints'], $table);
|
||||||
$subquery.= " WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
|
|
||||||
$query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
|
|
||||||
|
|
||||||
return $this->conn->fetchColumn($query);
|
return $this->conn->fetchColumn($query);
|
||||||
}
|
}
|
||||||
@ -128,24 +144,8 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableColumns($table) {
|
public function listTableColumns($table) {
|
||||||
$sql = "SELECT a.attnum, a.attname AS field, t.typname AS type, format_type(a.atttypid, a.atttypmod) AS complete_type, "
|
|
||||||
. "a.attnotnull AS isnotnull, "
|
$result = $this->dbh->query($this->sql['listTableColumns'])->fetchAll(PDO::FETCH_ASSOC);
|
||||||
. "( SELECT 't' "
|
|
||||||
. "FROM pg_index "
|
|
||||||
. "WHERE c.oid = pg_index.indrelid "
|
|
||||||
. "AND pg_index.indkey[0] = a.attnum "
|
|
||||||
. "AND pg_index.indisprimary = 't') AS pri, "
|
|
||||||
. "(SELECT pg_attrdef.adsrc "
|
|
||||||
. "FROM pg_attrdef "
|
|
||||||
. "WHERE c.oid = pg_attrdef.adrelid "
|
|
||||||
. "AND pg_attrdef.adnum=a.attnum) AS default "
|
|
||||||
. "FROM pg_attribute a, pg_class c, pg_type t "
|
|
||||||
. "WHERE c.relname = '" . $table . "' "
|
|
||||||
. "AND a.attnum > 0 "
|
|
||||||
. "AND a.attrelid = c.oid "
|
|
||||||
. "AND a.atttypid = t.oid "
|
|
||||||
. "ORDER BY a.attnum ";
|
|
||||||
$result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
|
|
||||||
$columns = array();
|
$columns = array();
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
if ($val['type'] === 'varchar') {
|
if ($val['type'] === 'varchar') {
|
||||||
@ -172,10 +172,8 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function listTableIndexes($table) {
|
public function listTableIndexes($table) {
|
||||||
$table = $db->quote($table, 'text');
|
$table = $this->conn->quote($table);
|
||||||
$subquery = "SELECT indexrelid FROM pg_index, pg_class";
|
$query = sprintf($this->sql['listTableIndexes'], $table);
|
||||||
$subquery.= " WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
|
|
||||||
$query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
|
|
||||||
|
|
||||||
return $this->conn->fetchColumn($query);
|
return $this->conn->fetchColumn($query);
|
||||||
}
|
}
|
||||||
@ -206,21 +204,4 @@ class Doctrine_Import_Pgsql extends Doctrine_Import {
|
|||||||
public function listTableViews($table) {
|
public function listTableViews($table) {
|
||||||
return $this->conn->fetchColumn($query);
|
return $this->conn->fetchColumn($query);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* lists database users
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listUsers() {
|
|
||||||
return $this->conn->fetchColumn($this->sql['listUsers']);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* lists database views
|
|
||||||
*
|
|
||||||
* @param string|null $database
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listViews($database = null) {
|
|
||||||
return $this->conn->fetchColumn($this->sql['listViews']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user