diff options
Diffstat (limited to 'framework')
-rw-r--r-- | framework/Data/Common/Mssql/TMssqlMetaData.php | 30 | ||||
-rw-r--r-- | framework/Data/Common/Mysql/TMysqlMetaData.php | 16 | ||||
-rw-r--r-- | framework/Data/Common/Oracle/TOracleMetaData.php | 38 | ||||
-rw-r--r-- | framework/Data/Common/Pgsql/TPgsqlMetaData.php | 28 | ||||
-rw-r--r-- | framework/Data/Common/Sqlite/TSqliteMetaData.php | 30 | ||||
-rw-r--r-- | framework/Data/Common/TDbMetaData.php | 10 | ||||
-rw-r--r-- | framework/Wsat/TWsatARGenerator.php | 62 | ||||
-rw-r--r-- | framework/Wsat/pages/TWsatGenerateAR.php | 2 |
8 files changed, 152 insertions, 64 deletions
diff --git a/framework/Data/Common/Mssql/TMssqlMetaData.php b/framework/Data/Common/Mssql/TMssqlMetaData.php index 3ff7ac7f..8ee455d5 100644 --- a/framework/Data/Common/Mssql/TMssqlMetaData.php +++ b/framework/Data/Common/Mssql/TMssqlMetaData.php @@ -25,7 +25,7 @@ Prado::using('System.Data.Common.Mssql.TMssqlTableInfo'); * @since 3.1 */ class TMssqlMetaData extends TDbMetaData -{ +{ /** * @return string TDbTableInfo class name. */ @@ -260,5 +260,33 @@ EOD; } return false; } + + /** + * Returns all table names in the database. + * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. + * If not empty, the returned table names will be prefixed with the schema name. + * @return array all table names in the database. + */ + public function findTableNames($schema='dbo') + { + $condition="TABLE_TYPE='BASE TABLE'"; + $sql=<<<EOD +SELECT TABLE_NAME, TABLE_SCHEMA FROM [INFORMATION_SCHEMA].[TABLES] +WHERE TABLE_SCHEMA=:schema AND $condition +EOD; + $command=$this->getDbConnection()->createCommand($sql); + $command->bindParam(":schema", $schema); + $rows=$command->queryAll(); + $names=array(); + foreach ($rows as $row) + { + if ($schema == self::DEFAULT_SCHEMA) + $names[]=$row['TABLE_NAME']; + else + $names[]=$schema.'.'.$row['TABLE_SCHEMA'].'.'.$row['TABLE_NAME']; + } + + return $names; + } } diff --git a/framework/Data/Common/Mysql/TMysqlMetaData.php b/framework/Data/Common/Mysql/TMysqlMetaData.php index 9dc995fc..f08a6fc0 100644 --- a/framework/Data/Common/Mysql/TMysqlMetaData.php +++ b/framework/Data/Common/Mysql/TMysqlMetaData.php @@ -382,5 +382,21 @@ EOD; } return false; } + + /** + * Returns all table names in the database. + * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. + * If not empty, the returned table names will be prefixed with the schema name. + * @return array all table names in the database. + */ + public function findTableNames($schema='') + { + if($schema==='') + return $this->getDbConnection()->createCommand('SHOW TABLES')->queryColumn(); + $names=$this->getDbConnection()->createCommand('SHOW TABLES FROM '.$this->quoteTableName($schema))->queryColumn(); + foreach($names as &$name) + $name=$schema.'.'.$name; + return $names; + } } diff --git a/framework/Data/Common/Oracle/TOracleMetaData.php b/framework/Data/Common/Oracle/TOracleMetaData.php index 793070ed..8648fcc9 100644 --- a/framework/Data/Common/Oracle/TOracleMetaData.php +++ b/framework/Data/Common/Oracle/TOracleMetaData.php @@ -336,5 +336,41 @@ EOD; } return false; } -} + + /** + * Returns all table names in the database. + * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. + * If not empty, the returned table names will be prefixed with the schema name. + * @return array all table names in the database. + */ + public function findTableNames($schema='') + { + if($schema==='') + { + $sql=<<<EOD +SELECT table_name, '{$schema}' as table_schema FROM user_tables +EOD; + $command=$this->getDbConnection()->createCommand($sql); + } + else + { + $sql=<<<EOD +SELECT object_name as table_name, owner as table_schema FROM all_objects +WHERE object_type = 'TABLE' AND owner=:schema +EOD; + $command=$this->getDbConnection()->createCommand($sql); + $command->bindParam(':schema',$schema); + } + $rows=$command->queryAll(); + $names=array(); + foreach($rows as $row) + { + if($schema===$this->getDefaultSchema() || $schema==='') + $names[]=$row['TABLE_NAME']; + else + $names[]=$row['TABLE_SCHEMA'].'.'.$row['TABLE_NAME']; + } + return $names; + } +}
\ No newline at end of file diff --git a/framework/Data/Common/Pgsql/TPgsqlMetaData.php b/framework/Data/Common/Pgsql/TPgsqlMetaData.php index dd91dfdc..a8e7bfed 100644 --- a/framework/Data/Common/Pgsql/TPgsqlMetaData.php +++ b/framework/Data/Common/Pgsql/TPgsqlMetaData.php @@ -418,5 +418,33 @@ EOD; } return false; } + + /** + * Returns all table names in the database. + * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. + * If not empty, the returned table names will be prefixed with the schema name. + * @return array all table names in the database. + */ + public function findTableNames($schema='public') + { + if($schema==='') + $schema=self::DEFAULT_SCHEMA; + $sql=<<<EOD +SELECT table_name, table_schema FROM information_schema.tables +WHERE table_schema=:schema AND table_type='BASE TABLE' +EOD; + $command=$this->getDbConnection()->createCommand($sql); + $command->bindParam(':schema',$schema); + $rows=$command->queryAll(); + $names=array(); + foreach($rows as $row) + { + if($schema===self::DEFAULT_SCHEMA) + $names[]=$row['table_name']; + else + $names[]=$row['table_schema'].'.'.$row['table_name']; + } + return $names; + } } diff --git a/framework/Data/Common/Sqlite/TSqliteMetaData.php b/framework/Data/Common/Sqlite/TSqliteMetaData.php index 3d789500..c66825c7 100644 --- a/framework/Data/Common/Sqlite/TSqliteMetaData.php +++ b/framework/Data/Common/Sqlite/TSqliteMetaData.php @@ -190,21 +190,15 @@ class TSqliteMetaData extends TDbMetaData } return false; } -} - -/** - -CREATE TABLE foo -( - id INTEGER NOT NULL PRIMARY KEY, - id2 CHAR(2) -); - -CREATE TABLE bar -( - id INTEGER NOT NULL PRIMARY KEY, - foo_id INTEGER - CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE -); -*/ - + + /** + * Returns all table names in the database. + * @param string $schema the schema of the tables. This is not used for sqlite database. + * @return array all table names in the database. + */ + public function findTableNames($schema='') + { + $sql="SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'"; + return $this->getDbConnection()->createCommand($sql)->queryColumn(); + } +}
\ No newline at end of file diff --git a/framework/Data/Common/TDbMetaData.php b/framework/Data/Common/TDbMetaData.php index 2ad5c592..0a3a6e90 100644 --- a/framework/Data/Common/TDbMetaData.php +++ b/framework/Data/Common/TDbMetaData.php @@ -180,5 +180,15 @@ abstract class TDbMetaData extends TComponent return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt; } + + /** + * Returns all table names in the database. + * This method should be overridden by child classes in order to support this feature + * because the default implementation simply throws an exception. + * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. + * If not empty, the returned table names will be prefixed with the schema name. + * @return array all table names in the database. + */ + abstract public function findTableNames($schema=''); } diff --git a/framework/Wsat/TWsatARGenerator.php b/framework/Wsat/TWsatARGenerator.php index 2d4aeb12..7ae5e46e 100644 --- a/framework/Wsat/TWsatARGenerator.php +++ b/framework/Wsat/TWsatARGenerator.php @@ -9,23 +9,20 @@ * @since 3.3 * @package Wsat */ + +Prado::using('System.Data.Common.TDbMetaData'); + class TWsatARGenerator { /** - * Gets the current Db connection, the connection object is obtained from - * the TActiveRecordManager if connection is currently null. - * @return TDbConnection current db connection for this object. + * @return TDbMetaData for retrieving metadata information, such as + * table and columns information, from a database connection. */ - private $_conn; + private $_dbMetaData; /** - * @return TActiveRecordGateway record table gateway. - */ - private $_gateway; - - /** - * Output folder where AR classes will be generated. + * Output folder where AR classes will be saved. */ private $_opFile; @@ -55,19 +52,9 @@ class TWsatARGenerator if(!class_exists("TActiveRecordManager", false)) throw new Exception("You need to enable the ActiveRecord module in your application configuration file."); $ar_manager = TActiveRecordManager::getInstance(); - $this->_conn = $ar_manager->getDbConnection(); - $this->_conn->Active = true; - $this->_gateway = $ar_manager->getRecordGateway(); - } - - /** - * Destructor. - * Disconnect the db connection. - */ - public function __destruct() - { - if ($this->_conn !== null) - $this->_conn->Active = false; + $_conn = $ar_manager->getDbConnection(); + $_conn->Active = true; + $this->_dbMetaData = TDbMetaData::getInstance($_conn); } public function setOpFile($op_file_namespace) @@ -94,17 +81,17 @@ class TWsatARGenerator // <editor-fold defaultstate="collapsed" desc="Main APIs"> public function generate($tableName) { - $tableInfo = $this->_gateway->getTableInfo($this->_conn, $tableName); + $tableInfo = $this->_dbMetaData->getTableInfo($tableName); $this->_commonGenerate($tableName, $tableInfo); } public function generateAll() { - foreach ($this->_getAllTableNames() as $tableName) + foreach ($this->_dbMetaData->findTableNames() as $tableName) { if ($tableName == "pradocache") continue; - $tableInfo = $this->_gateway->getTableInfo($this->_conn, $tableName); + $tableInfo = $this->_dbMetaData->getTableInfo($tableName); if (!empty($this->_relations)) { // Cancel generation of M-M relationships middle table @@ -118,9 +105,9 @@ class TWsatARGenerator public function buildRelations() { $this->_relations = array(); - foreach ($this->_getAllTableNames() as $table_name) + foreach ($this->_dbMetaData->findTableNames() as $table_name) { - $tableInfo = $this->_gateway->getTableInfo($this->_conn, $table_name); + $tableInfo = $this->_dbMetaData->getTableInfo($table_name); $pks = $tableInfo->getPrimaryKeys(); $fks = $tableInfo->getForeignKeys(); @@ -174,7 +161,7 @@ class TWsatARGenerator private function _commonGenerate($tableName, $tableInfo) { if (count($tableInfo->getColumns()) === 0) - throw new Exception("Unable to find table or view $tableName in " . $this->_conn->getConnectionString() . "."); + throw new Exception("Unable to find table or view $tableName in " . $this->_dbMetaData->getDbConnection()->getConnectionString() . "."); else { $properties = array(); @@ -189,17 +176,6 @@ class TWsatARGenerator file_put_contents($output, $class); } - private function _getAllTableNames() - { - $command = $this->_conn->createCommand("Show Tables"); - $dataReader = $command->query(); - $dataReader->bindColumn(1, $table); - $tables = array(); - while ($dataReader->read()) - $tables[] = $table; - return $tables; - } - private function _getProperClassName($tableName) { $table_name_words = str_replace("_", " ", strtolower($tableName)); @@ -209,11 +185,11 @@ class TWsatARGenerator public function renderAllTablesInformation() { - foreach ($this->_getAllTableNames() as $table_name) + foreach ($this->_dbMetaData->findTableNames() as $table_name) { echo $table_name . "<br>"; - $tableInfo = $this->_gateway->getTableInfo($this->_conn, $table_name); + $tableInfo = $this->_dbMetaData->getTableInfo($table_name); echo "Table info:" . "<br>"; echo "<pre>"; var_dump($tableInfo); @@ -259,7 +235,7 @@ class TWsatARGenerator { if (isset($column->IsPrimaryKey) && $column->IsPrimaryKey) $property = str_replace($this->uqChars, "", $column->ColumnName); - elseif ($column->PHPType == "string" && $column->DBType != "date") + elseif ($column->PdoType == PDO::PARAM_STR && $column->DBType != "date") { $property = str_replace($this->uqChars, "", $column->ColumnName); break; diff --git a/framework/Wsat/pages/TWsatGenerateAR.php b/framework/Wsat/pages/TWsatGenerateAR.php index 2c85445d..f0ce8430 100644 --- a/framework/Wsat/pages/TWsatGenerateAR.php +++ b/framework/Wsat/pages/TWsatGenerateAR.php @@ -51,7 +51,7 @@ class TWsatGenerateAR extends TPage public function preview($sender) { - throw new THttpException(500, "Not implemented yet."); +// throw new THttpException(500, "Not implemented yet."); } }
\ No newline at end of file |