From e4fe182506a038e2b6d85bbbbf8cc7faad709eb6 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 28 Dec 2006 12:27:44 +0000 Subject: [PATCH] Moved schema reading functionality from datadict drivers to import drivers --- lib/Doctrine/DataDict/Firebird.php | 85 ----------- lib/Doctrine/DataDict/Informix.php | 20 --- lib/Doctrine/DataDict/Mssql.php | 203 +----------------------- lib/Doctrine/DataDict/Mysql.php | 131 ---------------- lib/Doctrine/DataDict/Oracle.php | 117 -------------- lib/Doctrine/DataDict/Pgsql.php | 188 ----------------------- lib/Doctrine/DataDict/Sqlite.php | 142 +---------------- lib/Doctrine/Import/Firebird.php | 119 +++++++++++++++ lib/Doctrine/Import/Informix.php | 54 +++++++ lib/Doctrine/Import/Mssql.php | 237 +++++++++++++++++++++++++++++ lib/Doctrine/Import/Mysql.php | 164 ++++++++++++++++++++ lib/Doctrine/Import/Oracle.php | 149 ++++++++++++++++++ lib/Doctrine/Import/Pgsql.php | 222 +++++++++++++++++++++++++++ lib/Doctrine/Import/Sqlite.php | 173 +++++++++++++++++++++ 14 files changed, 1120 insertions(+), 884 deletions(-) create mode 100644 lib/Doctrine/Import/Firebird.php create mode 100644 lib/Doctrine/Import/Informix.php create mode 100644 lib/Doctrine/Import/Mssql.php create mode 100644 lib/Doctrine/Import/Mysql.php create mode 100644 lib/Doctrine/Import/Oracle.php create mode 100644 lib/Doctrine/Import/Pgsql.php create mode 100644 lib/Doctrine/Import/Sqlite.php diff --git a/lib/Doctrine/DataDict/Firebird.php b/lib/Doctrine/DataDict/Firebird.php index 590408ff1..ebb362c1f 100644 --- a/lib/Doctrine/DataDict/Firebird.php +++ b/lib/Doctrine/DataDict/Firebird.php @@ -195,89 +195,4 @@ class Doctrine_DataDict_Firebird extends Doctrine_DataDict { public function getCollationFieldDeclaration($collation) { return 'COLLATE '.$collation; } - /** - * list all tables in the current database - * - * @return array data array - */ - public function listTables() { - $query = 'SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$VIEW_BLR IS NULL'; - - return $this->conn->fetchColumn($query); - } - /** - * list all fields in a tables in the current database - * - * @param string $table name of table that should be used in method - * @return mixed data array on success, a MDB2 error on failure - * @access public - */ - public function listTableFields($table) { - $table = $db->quote(strtoupper($table), 'text'); - $query = 'SELECT RDB\$FIELD_NAME FROM RDB$RELATION_FIELDS WHERE UPPER(RDB$RELATION_NAME) = ' . $table; - - return $this->conn->fetchColumn($query); - } - /** - * list all users - * - * @return array data array containing all database users - */ - public function listUsers() { - return $this->conn->fetchColumn('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES'); - } - /** - * list the views in the database - * - * @return array data array containing all database views - */ - public function listViews() { - $result = $db->queryCol('SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS'); - - return $this->conn->fetchColumn($query); - } - /** - * list the views in the database that reference a given table - * - * @param string $table table for which all references views should be found - * @return array data array containing all views for given table - */ - public function listTableViews($table) { - $query = 'SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS'; - $table = $db->quote(strtoupper($table), 'text'); - $query .= 'WHERE UPPER(RDB\$RELATION_NAME) = ' . $table; - - return $this->conn->fetchColumn($query); - } - /** - * list all functions in the current database - * - * @return array data array containing all availible functions - */ - public function listFunctions() { - $query = 'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE RDB$SYSTEM_FLAG IS NULL'; - - return $this->conn->fetchColumn($query); - } - /** - * This function will be called to get all triggers of the - * current database ($db->getDatabase()) - * - * @param string $table The name of the table from the - * previous database to query against. - * @return array data array containing all triggers for given table - */ - public function listTableTriggers($table = null) { - $query = 'SELECT RDB$TRIGGER_NAME - FROM RDB$TRIGGERS - WHERE RDB$SYSTEM_FLAG IS NULL - OR RDB$SYSTEM_FLAG = 0'; - - if( ! is_null($table)) { - $table = $db->quote(strtoupper($table), 'text'); - $query .= 'WHERE UPPER(RDB$RELATION_NAME) = ' . $table; - } - - return $this->conn->fetchColumn($query); - } } diff --git a/lib/Doctrine/DataDict/Informix.php b/lib/Doctrine/DataDict/Informix.php index c32741eb0..a19652d3a 100644 --- a/lib/Doctrine/DataDict/Informix.php +++ b/lib/Doctrine/DataDict/Informix.php @@ -30,26 +30,6 @@ Doctrine::autoload('Doctrine_DataDict'); * @since 1.0 */ class Doctrine_DataDict_Informix extends Doctrine_DataDict { - protected static $sql = array( - 'listTables' => "SELECT tabname,tabtype FROM systables WHERE tabtype IN ('T','V') AND owner != 'informix'", - 'listColumns' => "SELECT c.colname, c.coltype, c.collength, d.default, c.colno - FROM syscolumns c, systables t,outer sysdefaults d - WHERE c.tabid = t.tabid AND d.tabid = t.tabid AND d.colno = c.colno - AND tabname='%s' ORDER BY c.colno", - 'listPk' => "SELECT part1, part2, part3, part4, part5, part6, part7, part8 FROM - systables t, sysconstraints s, sysindexes i WHERE t.tabname='%s' - AND s.tabid=t.tabid AND s.constrtype='P' - AND i.idxname=s.idxname", - 'listForeignKeys' => "SELECT tr.tabname,updrule,delrule, - i.part1 o1,i2.part1 d1,i.part2 o2,i2.part2 d2,i.part3 o3,i2.part3 d3,i.part4 o4,i2.part4 d4, - i.part5 o5,i2.part5 d5,i.part6 o6,i2.part6 d6,i.part7 o7,i2.part7 d7,i.part8 o8,i2.part8 d8 - from systables t,sysconstraints s,sysindexes i, - sysreferences r,systables tr,sysconstraints s2,sysindexes i2 - where t.tabname='%s' - and s.tabid=t.tabid and s.constrtype='R' and r.constrid=s.constrid - and i.idxname=s.idxname and tr.tabid=r.ptabid - and s2.constrid=r.primary and i2.idxname=s2.idxname", - ); /** * Obtain DBMS specific SQL code portion needed to declare an text type * field to be used in statements like CREATE TABLE. diff --git a/lib/Doctrine/DataDict/Mssql.php b/lib/Doctrine/DataDict/Mssql.php index b61f51083..1f33ef6d4 100644 --- a/lib/Doctrine/DataDict/Mssql.php +++ b/lib/Doctrine/DataDict/Mssql.php @@ -18,6 +18,7 @@ * and is licensed under the LGPL. For more information, see * . */ +Doctrine::autoload('Doctrine_DataDict'); /** * @package Doctrine * @license http://www.opensource.org/licenses/lgpl-license.php LGPL @@ -166,206 +167,4 @@ class Doctrine_DataDict_Mssql extends Doctrine_DataDict { return array($type, $length, $unsigned, $fixed); } - /** - * lists all databases - * - * @return array - */ - public function listDatabases() { - - } - /** - * lists all availible database functions - * - * @return array - */ - public function listFunctions() { - - } - /** - * lists all database triggers - * - * @param string|null $database - * @return array - */ - public function listTriggers($database = null) { - - } - /** - * lists all database sequences - * - * @param string|null $database - * @return array - */ - public function listSequences($database = null) { - $query = "SELECT name FROM sysobjects WHERE xtype = 'U'"; - $table_names = $db->queryCol($query); - if (PEAR::isError($table_names)) { - return $table_names; - } - $result = array(); - foreach ($table_names as $table_name) { - if ($sqn = $this->_fixSequenceName($table_name, true)) { - $result[] = $sqn; - } - } - if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { - $result = array_map(($db->options['field_case'] == CASE_LOWER ? - 'strtolower' : 'strtoupper'), $result); - } - return $result; - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableConstraints($table) { - - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableColumns($table) { - $sql = 'EXEC sp_columns @table_name = ' . $this->quoteIdentifier($table); - $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - $columns = array(); - - foreach ($result as $key => $val) { - if (strstr($val['type_name'], ' ')) { - list($type, $identity) = explode(' ', $val['type_name']); - } else { - $type = $val['type_name']; - $identity = ''; - } - - if ($type == 'varchar') { - $type .= '('.$val['length'].')'; - } - - $description = array( - 'name' => $val['column_name'], - 'type' => $type, - 'notnull' => (bool) ($val['is_nullable'] === 'NO'), - 'default' => $val['column_def'], - 'primary' => (strtolower($identity) == 'identity'), - ); - $columns[$val['column_name']] = new Doctrine_Schema_Column($description); - } - - return $columns; - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableIndexes($table) { - - } - /** - * lists tables - * - * @param string|null $database - * @return array - */ - public function listTables($database = null) { - $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; - - return $this->dbh->fetchCol($sql); - } - /** - * lists table triggers - * - * @param string $table database table name - * @return array - */ - public function listTableTriggers($table) { - $table = $db->quote($table, 'text'); - $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'"; - if (!is_null($table)) { - $query .= "AND object_name(parent_obj) = $table"; - } - - $result = $db->queryCol($query); - if (PEAR::isError($results)) { - return $result; - } - - if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE && - $db->options['field_case'] == CASE_LOWER) - { - $result = array_map(($db->options['field_case'] == CASE_LOWER ? - 'strtolower' : 'strtoupper'), $result); - } - return $result; - } - /** - * lists table views - * - * @param string $table database table name - * @return array - */ - public function listTableViews($table) { - $keyName = 'INDEX_NAME'; - $pkName = 'PK_NAME'; - if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) { - if ($db->options['field_case'] == CASE_LOWER) { - $keyName = strtolower($keyName); - $pkName = strtolower($pkName); - } else { - $keyName = strtoupper($keyName); - $pkName = strtoupper($pkName); - } - } - $table = $db->quote($table, 'text'); - $query = 'EXEC sp_statistics @table_name = ' . $table; - $indexes = $db->queryCol($query, 'text', $keyName); - - $query = 'EXEC sp_pkeys @table_name = ' . $table; - $pkAll = $db->queryCol($query, 'text', $pkName); - $result = array(); - foreach ($indexes as $index) { - if (!in_array($index, $pkAll) && $index != null) { - $result[$this->_fixIndexName($index)] = true; - } - } - - if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) { - $result = array_change_key_case($result, $db->options['field_case']); - } - return array_keys($result); - } - /** - * lists database users - * - * @return array - */ - public function listUsers() { - - } - /** - * lists database views - * - * @param string|null $database - * @return array - */ - public function listViews($database = null) { - $query = "SELECT name FROM sysobjects WHERE xtype = 'V'"; - - $result = $db->queryCol($query); - - if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE && - $db->options['field_case'] == CASE_LOWER) - { - $result = array_map(($db->options['field_case'] == CASE_LOWER ? - 'strtolower' : 'strtoupper'), $result); - } - return $result; - } } diff --git a/lib/Doctrine/DataDict/Mysql.php b/lib/Doctrine/DataDict/Mysql.php index 9fbe8fbc1..67a6058e1 100644 --- a/lib/Doctrine/DataDict/Mysql.php +++ b/lib/Doctrine/DataDict/Mysql.php @@ -418,135 +418,4 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict { return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned . $default . $notnull . $autoinc; } - /** - * lists all databases - * - * @return array - */ - public function listDatabases() { - $sql = 'SHOW DATABASES'; - - return $this->dbh->query($sql)->fetchAll(PDO::FETCH_COLUMN); - } - /** - * lists all availible database functions - * - * @return array - */ - public function listFunctions() { - - } - /** - * lists all database triggers - * - * @param string|null $database - * @return array - */ - public function listTriggers($database = null) { - - } - /** - * lists all database sequences - * - * @param string|null $database - * @return array - */ - public function listSequences($database = null) { - - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableConstraints($table) { - $sql = 'select KCU.COLUMN_NAME as referencingColumn, TC.CONSTRAINT_NAME as constraintName, KCU.REFERENCED_TABLE_SCHEMA as referencedTableSchema, KCU.REFERENCED_TABLE_NAME as referencedTable, KCU.REFERENCED_COLUMN_NAME as referencedColumn from INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC inner JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU on TC.CONSTRAINT_NAME=KCU.CONSTRAINT_NAME and TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA and TC.TABLE_NAME=KCU.TABLE_NAME WHERE TC.TABLE_SCHEMA=database() AND TC.TABLE_NAME="'.$table.'" AND CONSTRAINT_TYPE="FOREIGN KEY"'; - - return $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableColumns($table) { - $sql = "DESCRIBE $table"; - $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - $description = array(); - foreach ($result as $key => $val2) { - $val = array(); - foreach(array_keys($val2) as $valKey){ // lowercase the key names - $val[strtolower($valKey)] = $val2[$valKey]; - } - $description = array( - 'name' => $val['field'], - 'type' => $val['type'], - 'primary' => (strtolower($val['key']) == 'pri'), - 'default' => $val['default'], - 'notnull' => (bool) ($val['null'] != 'YES'), - 'autoinc' => (bool) (strpos($val['extra'], 'auto_increment') > -1), - ); - $columns[$val['field']] = new Doctrine_Schema_Column($description); - } - - - return $columns; - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableIndexes($table) { - - } - /** - * lists tables - * - * @param string|null $database - * @return array - */ - public function listTables($database = null) { - $sql = 'SHOW TABLES'; - - return $this->dbh->query($sql)->fetchAll(PDO::FETCH_COLUMN); - } - /** - * lists table triggers - * - * @param string $table database table name - * @return array - */ - public function listTableTriggers($table) { - - } - /** - * lists table views - * - * @param string $table database table name - * @return array - */ - public function listTableViews($table) { - - } - /** - * lists database users - * - * @return array - */ - public function listUsers() { - - } - /** - * lists database views - * - * @param string|null $database - * @return array - */ - public function listViews($database = null) { - - } } diff --git a/lib/Doctrine/DataDict/Oracle.php b/lib/Doctrine/DataDict/Oracle.php index 430d7e710..073194940 100644 --- a/lib/Doctrine/DataDict/Oracle.php +++ b/lib/Doctrine/DataDict/Oracle.php @@ -171,121 +171,4 @@ class Doctrine_DataDict_Oracle extends Doctrine_DataDict { return array($type, $length, $unsigned, $fixed); } - /** - * lists all databases - * - * @return array - */ - public function listDatabases() { - - } - /** - * lists all availible database functions - * - * @return array - */ - public function listFunctions() { - - } - /** - * lists all database triggers - * - * @param string|null $database - * @return array - */ - public function listTriggers($database = null) { - - } - /** - * lists all database sequences - * - * @param string|null $database - * @return array - */ - public function listSequences($database = null) { - - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableConstraints($table) { - - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableColumns($table) { - $sql = "DESCRIBE $table"; - $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - $columns = array(); - foreach ($result as $key => $val) { - $description = array( - 'name' => $val['Field'], - 'notnull' => (bool) ($val['Null'] === ''), - 'type' => $val['Type'], - ); - $columns[$val['Field']] = new Doctrine_Schema_Column($description); - } - - return $columns; - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableIndexes($table) { - - } - /** - * lists tables - * - * @param string|null $database - * @return array - */ - public function listTables($database = null) { - return $this->dbh->fetchCol('SELECT table_name FROM all_tables ORDER BY table_name'); - } - /** - * lists table triggers - * - * @param string $table database table name - * @return array - */ - public function listTableTriggers($table) { - - } - /** - * lists table views - * - * @param string $table database table name - * @return array - */ - public function listTableViews($table) { - - } - /** - * lists database users - * - * @return array - */ - public function listUsers() { - - } - /** - * lists database views - * - * @param string|null $database - * @return array - */ - public function listViews($database = null) { - - } } diff --git a/lib/Doctrine/DataDict/Pgsql.php b/lib/Doctrine/DataDict/Pgsql.php index db8b3c487..7fba0b180 100644 --- a/lib/Doctrine/DataDict/Pgsql.php +++ b/lib/Doctrine/DataDict/Pgsql.php @@ -584,192 +584,4 @@ class Doctrine_DataDict_Pgsql extends Doctrine_DataDict { $name = $this->conn->quoteIdentifier($name, true); return $name . ' ' . $this->getNativeDeclaration($field) . $default . $notnull; } - /** - * listDatabases - * lists all databases - * - * @return array - */ - public function listDatabases() { - $query = 'SELECT datname FROM pg_database'; - - return $this->conn->fetchColumn($query); - } - /** - * lists all availible database functions - * - * @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); - } - /** - * lists all database triggers - * - * @param string|null $database - * @return array - */ - public function listTriggers($database = null) { - - } - /** - * lists all database sequences - * - * @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); - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableConstraints($table) { - $table = $db->quote($table, 'text'); - $subquery = "SELECT indexrelid FROM pg_index, pg_class"; - $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); - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - 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, " - . "( 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(); - foreach ($result as $key => $val) { - if ($val['type'] === 'varchar') { - // need to add length to the type so we are compatible with - // Zend_Db_Adapter_Pdo_Pgsql! - $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $val['complete_type']); - $val['type'] .= '(' . $length . ')'; - } - $description = array( - 'name' => $val['field'], - 'type' => $val['type'], - 'notnull' => ($val['isnotnull'] == ''), - 'default' => $val['default'], - 'primary' => ($val['pri'] == 't'), - ); - $columns[$val['field']] = new Doctrine_Schema_Column($description); - } - return $columns; - } - /** - * list all indexes in a table - * - * @param string $table database table name - * @return array - */ - public function listTableIndexes($table) { - $table = $db->quote($table, 'text'); - $subquery = "SELECT indexrelid FROM pg_index, pg_class"; - $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); - } - /** - * lists tables - * - * @param string|null $database - * @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); - } - /** - * lists table triggers - * - * @param string $table database table name - * @return array - */ - public function listTableTriggers($table) { - - } - /** - * list the views in the database that reference a given table - * - * @param string $table database table name - * @return array - */ - public function listTableViews($table) { - $query = 'SELECT viewname FROM pg_views'; - - return $this->conn->fetchColumn($query); - } - /** - * lists database users - * - * @return array - */ - public function listUsers() { - $query = 'SELECT usename FROM pg_user'; - - return $this->conn->fetchColumn($query); - } - /** - * lists database views - * - * @param string|null $database - * @return array - */ - public function listViews($database = null) { - $query = 'SELECT viewname FROM pg_views'; - - return $this->conn->fetchColumn($query); - } } diff --git a/lib/Doctrine/DataDict/Sqlite.php b/lib/Doctrine/DataDict/Sqlite.php index 383e9c953..1380379db 100644 --- a/lib/Doctrine/DataDict/Sqlite.php +++ b/lib/Doctrine/DataDict/Sqlite.php @@ -274,144 +274,4 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { $name = $this->conn->quoteIdentifier($name, true); return $name . ' ' . $type . $unsigned . $default . $notnull . $autoinc; } - /** - * lists all databases - * - * @return array - */ - public function listDatabases() { - - } - /** - * lists all availible database functions - * - * @return array - */ - public function listFunctions() { - - } - /** - * lists all database triggers - * - * @param string|null $database - * @return array - */ - public function listTriggers($database = null) { - - } - /** - * lists all database sequences - * - * @param string|null $database - * @return array - */ - public function listSequences($database = null) { - - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableConstraints($table) { - - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableColumns($table) { - - $sql = 'PRAGMA table_info(' . $table . ')'; - $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - - $description = array(); - $columns = array(); - foreach($result as $key => $val) { - $description = array( - 'name' => $val['name'], - 'type' => $val['type'], - 'notnull' => (bool) $val['notnull'], - 'default' => $val['dflt_value'], - 'primary' => (bool) $val['pk'], - ); - $columns[$val['name']] = new Doctrine_Schema_Column($description); - } - return $columns; - } - /** - * lists table constraints - * - * @param string $table database table name - * @return array - */ - public function listTableIndexes($table) { - $sql = 'PRAGMA index_list(' . $table . ')'; - $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - - $indexes = array(); - foreach($result as $key => $val) { - - } - } - /** - * lists tables - * - * @param string|null $database - * @return array - */ - public function listTables($database = null) { - $sql = "SELECT name FROM sqlite_master WHERE type = 'table' " - . "UNION ALL SELECT name FROM sqlite_temp_master " - . "WHERE type = 'table' ORDER BY name"; - - $tables = array(); - $stmt = $this->dbh->query($sql); - - $data = $stmt->fetchAll(PDO::FETCH_COLUMN); - - foreach($data as $table) { - $tables[] = new Doctrine_Schema_Table(array('name' => $table)); - } - return $tables; - } - /** - * lists table triggers - * - * @param string $table database table name - * @return array - */ - public function listTableTriggers($table) { - - } - /** - * lists table views - * - * @param string $table database table name - * @return array - */ - public function listTableViews($table) { - - } - /** - * lists database users - * - * @return array - */ - public function listUsers() { - - } - /** - * lists database views - * - * @param string|null $database - * @return array - */ - public function listViews($database = null) { - - } -} - +} diff --git a/lib/Doctrine/Import/Firebird.php b/lib/Doctrine/Import/Firebird.php new file mode 100644 index 000000000..b547c7855 --- /dev/null +++ b/lib/Doctrine/Import/Firebird.php @@ -0,0 +1,119 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Lorenzo Alberton (PEAR MDB2 Interbase driver) + * @author Lukas Smith (PEAR MDB2 library) + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Firebird extends Doctrine_Import { + /** + * list all tables in the current database + * + * @return array data array + */ + public function listTables() { + $query = 'SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$VIEW_BLR IS NULL'; + + return $this->conn->fetchColumn($query); + } + /** + * list all fields in a tables in the current database + * + * @param string $table name of table that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + public function listTableFields($table) { + $table = $db->quote(strtoupper($table), 'text'); + $query = 'SELECT RDB\$FIELD_NAME FROM RDB$RELATION_FIELDS WHERE UPPER(RDB$RELATION_NAME) = ' . $table; + + return $this->conn->fetchColumn($query); + } + /** + * list all users + * + * @return array data array containing all database users + */ + public function listUsers() { + return $this->conn->fetchColumn('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES'); + } + /** + * list the views in the database + * + * @return array data array containing all database views + */ + public function listViews() { + $result = $db->queryCol('SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS'); + + return $this->conn->fetchColumn($query); + } + /** + * list the views in the database that reference a given table + * + * @param string $table table for which all references views should be found + * @return array data array containing all views for given table + */ + public function listTableViews($table) { + $query = 'SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS'; + $table = $db->quote(strtoupper($table), 'text'); + $query .= 'WHERE UPPER(RDB\$RELATION_NAME) = ' . $table; + + return $this->conn->fetchColumn($query); + } + /** + * list all functions in the current database + * + * @return array data array containing all availible functions + */ + public function listFunctions() { + $query = 'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE RDB$SYSTEM_FLAG IS NULL'; + + return $this->conn->fetchColumn($query); + } + /** + * This function will be called to get all triggers of the + * current database ($db->getDatabase()) + * + * @param string $table The name of the table from the + * previous database to query against. + * @return array data array containing all triggers for given table + */ + public function listTableTriggers($table = null) { + $query = 'SELECT RDB$TRIGGER_NAME + FROM RDB$TRIGGERS + WHERE RDB$SYSTEM_FLAG IS NULL + OR RDB$SYSTEM_FLAG = 0'; + + if( ! is_null($table)) { + $table = $db->quote(strtoupper($table), 'text'); + $query .= 'WHERE UPPER(RDB$RELATION_NAME) = ' . $table; + } + + return $this->conn->fetchColumn($query); + } +} diff --git a/lib/Doctrine/Import/Informix.php b/lib/Doctrine/Import/Informix.php new file mode 100644 index 000000000..ec22004d0 --- /dev/null +++ b/lib/Doctrine/Import/Informix.php @@ -0,0 +1,54 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Lukas Smith (PEAR MDB2 library) + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Informix extends Doctrine_Import { + protected static $sql = array( + 'listTables' => "SELECT tabname,tabtype FROM systables WHERE tabtype IN ('T','V') AND owner != 'informix'", + 'listColumns' => "SELECT c.colname, c.coltype, c.collength, d.default, c.colno + FROM syscolumns c, systables t,outer sysdefaults d + WHERE c.tabid = t.tabid AND d.tabid = t.tabid AND d.colno = c.colno + AND tabname='%s' ORDER BY c.colno", + 'listPk' => "SELECT part1, part2, part3, part4, part5, part6, part7, part8 FROM + systables t, sysconstraints s, sysindexes i WHERE t.tabname='%s' + AND s.tabid=t.tabid AND s.constrtype='P' + AND i.idxname=s.idxname", + 'listForeignKeys' => "SELECT tr.tabname,updrule,delrule, + i.part1 o1,i2.part1 d1,i.part2 o2,i2.part2 d2,i.part3 o3,i2.part3 d3,i.part4 o4,i2.part4 d4, + i.part5 o5,i2.part5 d5,i.part6 o6,i2.part6 d6,i.part7 o7,i2.part7 d7,i.part8 o8,i2.part8 d8 + from systables t,sysconstraints s,sysindexes i, + sysreferences r,systables tr,sysconstraints s2,sysindexes i2 + where t.tabname='%s' + and s.tabid=t.tabid and s.constrtype='R' and r.constrid=s.constrid + and i.idxname=s.idxname and tr.tabid=r.ptabid + and s2.constrid=r.primary and i2.idxname=s2.idxname", + ); + +} diff --git a/lib/Doctrine/Import/Mssql.php b/lib/Doctrine/Import/Mssql.php new file mode 100644 index 000000000..baed547b1 --- /dev/null +++ b/lib/Doctrine/Import/Mssql.php @@ -0,0 +1,237 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Lukas Smith (PEAR MDB2 library) + * @author Frank M. Kromann (PEAR MDB2 Mssql driver) + * @author David Coallier (PEAR MDB2 Mssql driver) + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Mssql extends Doctrine_Import { + /** + * lists all databases + * + * @return array + */ + public function listDatabases() { + + } + /** + * lists all availible database functions + * + * @return array + */ + public function listFunctions() { + + } + /** + * lists all database triggers + * + * @param string|null $database + * @return array + */ + public function listTriggers($database = null) { + + } + /** + * lists all database sequences + * + * @param string|null $database + * @return array + */ + public function listSequences($database = null) { + $query = "SELECT name FROM sysobjects WHERE xtype = 'U'"; + $table_names = $db->queryCol($query); + if (PEAR::isError($table_names)) { + return $table_names; + } + $result = array(); + foreach ($table_names as $table_name) { + if ($sqn = $this->_fixSequenceName($table_name, true)) { + $result[] = $sqn; + } + } + if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) { + $result = array_map(($db->options['field_case'] == CASE_LOWER ? + 'strtolower' : 'strtoupper'), $result); + } + return $result; + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableConstraints($table) { + + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableColumns($table) { + $sql = 'EXEC sp_columns @table_name = ' . $this->quoteIdentifier($table); + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + $columns = array(); + + foreach ($result as $key => $val) { + if (strstr($val['type_name'], ' ')) { + list($type, $identity) = explode(' ', $val['type_name']); + } else { + $type = $val['type_name']; + $identity = ''; + } + + if ($type == 'varchar') { + $type .= '('.$val['length'].')'; + } + + $description = array( + 'name' => $val['column_name'], + 'type' => $type, + 'notnull' => (bool) ($val['is_nullable'] === 'NO'), + 'default' => $val['column_def'], + 'primary' => (strtolower($identity) == 'identity'), + ); + $columns[$val['column_name']] = new Doctrine_Schema_Column($description); + } + + return $columns; + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableIndexes($table) { + + } + /** + * lists tables + * + * @param string|null $database + * @return array + */ + public function listTables($database = null) { + $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; + + return $this->dbh->fetchCol($sql); + } + /** + * lists table triggers + * + * @param string $table database table name + * @return array + */ + public function listTableTriggers($table) { + $table = $db->quote($table, 'text'); + $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'"; + if (!is_null($table)) { + $query .= "AND object_name(parent_obj) = $table"; + } + + $result = $db->queryCol($query); + if (PEAR::isError($results)) { + return $result; + } + + if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE && + $db->options['field_case'] == CASE_LOWER) + { + $result = array_map(($db->options['field_case'] == CASE_LOWER ? + 'strtolower' : 'strtoupper'), $result); + } + return $result; + } + /** + * lists table views + * + * @param string $table database table name + * @return array + */ + public function listTableViews($table) { + $keyName = 'INDEX_NAME'; + $pkName = 'PK_NAME'; + if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) { + if ($db->options['field_case'] == CASE_LOWER) { + $keyName = strtolower($keyName); + $pkName = strtolower($pkName); + } else { + $keyName = strtoupper($keyName); + $pkName = strtoupper($pkName); + } + } + $table = $db->quote($table, 'text'); + $query = 'EXEC sp_statistics @table_name = ' . $table; + $indexes = $db->queryCol($query, 'text', $keyName); + + $query = 'EXEC sp_pkeys @table_name = ' . $table; + $pkAll = $db->queryCol($query, 'text', $pkName); + $result = array(); + foreach ($indexes as $index) { + if (!in_array($index, $pkAll) && $index != null) { + $result[$this->_fixIndexName($index)] = true; + } + } + + if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) { + $result = array_change_key_case($result, $db->options['field_case']); + } + return array_keys($result); + } + /** + * lists database users + * + * @return array + */ + public function listUsers() { + + } + /** + * lists database views + * + * @param string|null $database + * @return array + */ + public function listViews($database = null) { + $query = "SELECT name FROM sysobjects WHERE xtype = 'V'"; + + $result = $db->queryCol($query); + + if ($db->options['portability'] & Doctrine::PORTABILITY_FIX_CASE && + $db->options['field_case'] == CASE_LOWER) + { + $result = array_map(($db->options['field_case'] == CASE_LOWER ? + 'strtolower' : 'strtoupper'), $result); + } + return $result; + } +} diff --git a/lib/Doctrine/Import/Mysql.php b/lib/Doctrine/Import/Mysql.php new file mode 100644 index 000000000..18571bba8 --- /dev/null +++ b/lib/Doctrine/Import/Mysql.php @@ -0,0 +1,164 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Lukas Smith (PEAR MDB2 library) + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Mysql extends Doctrine_Import { + /** + * lists all databases + * + * @return array + */ + public function listDatabases() { + $sql = 'SHOW DATABASES'; + + return $this->dbh->query($sql)->fetchAll(PDO::FETCH_COLUMN); + } + /** + * lists all availible database functions + * + * @return array + */ + public function listFunctions() { + + } + /** + * lists all database triggers + * + * @param string|null $database + * @return array + */ + public function listTriggers($database = null) { + + } + /** + * lists all database sequences + * + * @param string|null $database + * @return array + */ + public function listSequences($database = null) { + + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableConstraints($table) { + $sql = 'select KCU.COLUMN_NAME as referencingColumn, TC.CONSTRAINT_NAME as constraintName, KCU.REFERENCED_TABLE_SCHEMA as referencedTableSchema, KCU.REFERENCED_TABLE_NAME as referencedTable, KCU.REFERENCED_COLUMN_NAME as referencedColumn from INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC inner JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU on TC.CONSTRAINT_NAME=KCU.CONSTRAINT_NAME and TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA and TC.TABLE_NAME=KCU.TABLE_NAME WHERE TC.TABLE_SCHEMA=database() AND TC.TABLE_NAME="'.$table.'" AND CONSTRAINT_TYPE="FOREIGN KEY"'; + + return $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableColumns($table) { + $sql = "DESCRIBE $table"; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + $description = array(); + foreach ($result as $key => $val2) { + $val = array(); + foreach(array_keys($val2) as $valKey){ // lowercase the key names + $val[strtolower($valKey)] = $val2[$valKey]; + } + $description = array( + 'name' => $val['field'], + 'type' => $val['type'], + 'primary' => (strtolower($val['key']) == 'pri'), + 'default' => $val['default'], + 'notnull' => (bool) ($val['null'] != 'YES'), + 'autoinc' => (bool) (strpos($val['extra'], 'auto_increment') > -1), + ); + $columns[$val['field']] = new Doctrine_Schema_Column($description); + } + + + return $columns; + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableIndexes($table) { + + } + /** + * lists tables + * + * @param string|null $database + * @return array + */ + public function listTables($database = null) { + $sql = 'SHOW TABLES'; + + return $this->dbh->query($sql)->fetchAll(PDO::FETCH_COLUMN); + } + /** + * lists table triggers + * + * @param string $table database table name + * @return array + */ + public function listTableTriggers($table) { + + } + /** + * lists table views + * + * @param string $table database table name + * @return array + */ + public function listTableViews($table) { + + } + /** + * lists database users + * + * @return array + */ + public function listUsers() { + + } + /** + * lists database views + * + * @param string|null $database + * @return array + */ + public function listViews($database = null) { + + } +} diff --git a/lib/Doctrine/Import/Oracle.php b/lib/Doctrine/Import/Oracle.php new file mode 100644 index 000000000..9fc9f074a --- /dev/null +++ b/lib/Doctrine/Import/Oracle.php @@ -0,0 +1,149 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Oracle extends Doctrine_Import { + /** + * lists all databases + * + * @return array + */ + public function listDatabases() { + + } + /** + * lists all availible database functions + * + * @return array + */ + public function listFunctions() { + + } + /** + * lists all database triggers + * + * @param string|null $database + * @return array + */ + public function listTriggers($database = null) { + + } + /** + * lists all database sequences + * + * @param string|null $database + * @return array + */ + public function listSequences($database = null) { + + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableConstraints($table) { + + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableColumns($table) { + $sql = "DESCRIBE $table"; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + $columns = array(); + foreach ($result as $key => $val) { + $description = array( + 'name' => $val['Field'], + 'notnull' => (bool) ($val['Null'] === ''), + 'type' => $val['Type'], + ); + $columns[$val['Field']] = new Doctrine_Schema_Column($description); + } + + return $columns; + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableIndexes($table) { + + } + /** + * lists tables + * + * @param string|null $database + * @return array + */ + public function listTables($database = null) { + return $this->dbh->fetchCol('SELECT table_name FROM all_tables ORDER BY table_name'); + } + /** + * lists table triggers + * + * @param string $table database table name + * @return array + */ + public function listTableTriggers($table) { + + } + /** + * lists table views + * + * @param string $table database table name + * @return array + */ + public function listTableViews($table) { + + } + /** + * lists database users + * + * @return array + */ + public function listUsers() { + + } + /** + * lists database views + * + * @param string|null $database + * @return array + */ + public function listViews($database = null) { + + } +} diff --git a/lib/Doctrine/Import/Pgsql.php b/lib/Doctrine/Import/Pgsql.php new file mode 100644 index 000000000..74fdf3235 --- /dev/null +++ b/lib/Doctrine/Import/Pgsql.php @@ -0,0 +1,222 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Paul Cooper + * @author Lukas Smith (PEAR MDB2 library) + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Pgsql extends Doctrine_Import { + /** + * listDatabases + * lists all databases + * + * @return array + */ + public function listDatabases() { + $query = 'SELECT datname FROM pg_database'; + + return $this->conn->fetchColumn($query); + } + /** + * lists all availible database functions + * + * @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); + } + /** + * lists all database triggers + * + * @param string|null $database + * @return array + */ + public function listTriggers($database = null) { + + } + /** + * lists all database sequences + * + * @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); + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableConstraints($table) { + $table = $db->quote($table, 'text'); + $subquery = "SELECT indexrelid FROM pg_index, pg_class"; + $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); + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + 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, " + . "( 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(); + foreach ($result as $key => $val) { + if ($val['type'] === 'varchar') { + // need to add length to the type so we are compatible with + // Zend_Db_Adapter_Pdo_Pgsql! + $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $val['complete_type']); + $val['type'] .= '(' . $length . ')'; + } + $description = array( + 'name' => $val['field'], + 'type' => $val['type'], + 'notnull' => ($val['isnotnull'] == ''), + 'default' => $val['default'], + 'primary' => ($val['pri'] == 't'), + ); + $columns[$val['field']] = new Doctrine_Schema_Column($description); + } + return $columns; + } + /** + * list all indexes in a table + * + * @param string $table database table name + * @return array + */ + public function listTableIndexes($table) { + $table = $db->quote($table, 'text'); + $subquery = "SELECT indexrelid FROM pg_index, pg_class"; + $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); + } + /** + * lists tables + * + * @param string|null $database + * @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); + } + /** + * lists table triggers + * + * @param string $table database table name + * @return array + */ + public function listTableTriggers($table) { + + } + /** + * list the views in the database that reference a given table + * + * @param string $table database table name + * @return array + */ + public function listTableViews($table) { + $query = 'SELECT viewname FROM pg_views'; + + return $this->conn->fetchColumn($query); + } + /** + * lists database users + * + * @return array + */ + public function listUsers() { + $query = 'SELECT usename FROM pg_user'; + + return $this->conn->fetchColumn($query); + } + /** + * lists database views + * + * @param string|null $database + * @return array + */ + public function listViews($database = null) { + $query = 'SELECT viewname FROM pg_views'; + + return $this->conn->fetchColumn($query); + } +} diff --git a/lib/Doctrine/Import/Sqlite.php b/lib/Doctrine/Import/Sqlite.php new file mode 100644 index 000000000..79f2fc6ef --- /dev/null +++ b/lib/Doctrine/Import/Sqlite.php @@ -0,0 +1,173 @@ +. + */ +Doctrine::autoload('Doctrine_Import'); +/** + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Lukas Smith (PEAR MDB2 library) + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Import_Sqlite extends Doctrine_Import { + /** + * lists all databases + * + * @return array + */ + public function listDatabases() { + + } + /** + * lists all availible database functions + * + * @return array + */ + public function listFunctions() { + + } + /** + * lists all database triggers + * + * @param string|null $database + * @return array + */ + public function listTriggers($database = null) { + + } + /** + * lists all database sequences + * + * @param string|null $database + * @return array + */ + public function listSequences($database = null) { + + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableConstraints($table) { + + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableColumns($table) { + + $sql = 'PRAGMA table_info(' . $table . ')'; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + + $description = array(); + $columns = array(); + foreach($result as $key => $val) { + $description = array( + 'name' => $val['name'], + 'type' => $val['type'], + 'notnull' => (bool) $val['notnull'], + 'default' => $val['dflt_value'], + 'primary' => (bool) $val['pk'], + ); + $columns[$val['name']] = new Doctrine_Schema_Column($description); + } + return $columns; + } + /** + * lists table constraints + * + * @param string $table database table name + * @return array + */ + public function listTableIndexes($table) { + $sql = 'PRAGMA index_list(' . $table . ')'; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + + $indexes = array(); + foreach($result as $key => $val) { + + } + } + /** + * lists tables + * + * @param string|null $database + * @return array + */ + public function listTables($database = null) { + $sql = "SELECT name FROM sqlite_master WHERE type = 'table' " + . "UNION ALL SELECT name FROM sqlite_temp_master " + . "WHERE type = 'table' ORDER BY name"; + + $tables = array(); + $stmt = $this->dbh->query($sql); + + $data = $stmt->fetchAll(PDO::FETCH_COLUMN); + + foreach($data as $table) { + $tables[] = new Doctrine_Schema_Table(array('name' => $table)); + } + return $tables; + } + /** + * lists table triggers + * + * @param string $table database table name + * @return array + */ + public function listTableTriggers($table) { + + } + /** + * lists table views + * + * @param string $table database table name + * @return array + */ + public function listTableViews($table) { + + } + /** + * lists database users + * + * @return array + */ + public function listUsers() { + + } + /** + * lists database views + * + * @param string|null $database + * @return array + */ + public function listViews($database = null) { + + } +} +