Author: dr
Date: Mon Jan 14 11:30:24 2008
New Revision: 7143

Log:
- Implemented issue #10364: Added support for table name prefixes.

Modified:
    trunk/DatabaseSchema/ChangeLog
    trunk/DatabaseSchema/src/db_schema_autoload.php
    trunk/DatabaseSchema/src/handlers/common_sql_writer.php
    trunk/DatabaseSchema/src/handlers/mysql/reader.php
    trunk/DatabaseSchema/src/handlers/mysql/writer.php
    trunk/DatabaseSchema/src/handlers/oracle/reader.php
    trunk/DatabaseSchema/src/handlers/oracle/writer.php
    trunk/DatabaseSchema/src/handlers/pgsql/reader.php
    trunk/DatabaseSchema/src/handlers/pgsql/writer.php
    trunk/DatabaseSchema/src/handlers/sqlite/reader.php
    trunk/DatabaseSchema/src/handlers/sqlite/writer.php
    trunk/DatabaseSchema/src/interfaces/db_reader.php
    trunk/DatabaseSchema/src/interfaces/db_writer.php
    trunk/DatabaseSchema/src/options/schema.php
    trunk/DatabaseSchema/src/schema.php
    trunk/DatabaseSchema/tests/generic_test.php

Modified: trunk/DatabaseSchema/ChangeLog
==============================================================================
--- trunk/DatabaseSchema/ChangeLog [iso-8859-1] (original)
+++ trunk/DatabaseSchema/ChangeLog [iso-8859-1] Mon Jan 14 11:30:24 2008
@@ -1,6 +1,7 @@
 1.4alpha1 - [RELEASEDATE]
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Implemented issue #10364: Added support for table name prefixes.
 - Implemented issue #11562: Schema validator for duplicate index names.
 
 

Modified: trunk/DatabaseSchema/src/db_schema_autoload.php
==============================================================================
--- trunk/DatabaseSchema/src/db_schema_autoload.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/db_schema_autoload.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -19,14 +19,14 @@
     'ezcDbSchemaInvalidWriterClassException'     => 
'DatabaseSchema/exceptions/invalid_writer_class.php',
     'ezcDbSchemaSqliteDropFieldException'        => 
'DatabaseSchema/exceptions/sqlite_drop_field_exception.php',
     'ezcDbSchemaUnknownFormatException'          => 
'DatabaseSchema/exceptions/unknown_format.php',
+    'ezcDbSchemaDiffWriter'                      => 
'DatabaseSchema/interfaces/schema_diff_writer.php',
+    'ezcDbSchemaWriter'                          => 
'DatabaseSchema/interfaces/schema_writer.php',
+    'ezcDbSchemaDbWriter'                        => 
'DatabaseSchema/interfaces/db_writer.php',
+    'ezcDbSchemaDiffDbWriter'                    => 
'DatabaseSchema/interfaces/db_diff_writer.php',
     'ezcDbSchemaDiffReader'                      => 
'DatabaseSchema/interfaces/schema_diff_reader.php',
-    'ezcDbSchemaDiffWriter'                      => 
'DatabaseSchema/interfaces/schema_diff_writer.php',
     'ezcDbSchemaReader'                          => 
'DatabaseSchema/interfaces/schema_reader.php',
-    'ezcDbSchemaWriter'                          => 
'DatabaseSchema/interfaces/schema_writer.php',
     'ezcDbSchemaCommonSqlWriter'                 => 
'DatabaseSchema/handlers/common_sql_writer.php',
     'ezcDbSchemaDbReader'                        => 
'DatabaseSchema/interfaces/db_reader.php',
-    'ezcDbSchemaDbWriter'                        => 
'DatabaseSchema/interfaces/db_writer.php',
-    'ezcDbSchemaDiffDbWriter'                    => 
'DatabaseSchema/interfaces/db_diff_writer.php',
     'ezcDbSchemaDiffFileReader'                  => 
'DatabaseSchema/interfaces/file_diff_reader.php',
     'ezcDbSchemaDiffFileWriter'                  => 
'DatabaseSchema/interfaces/file_diff_writer.php',
     'ezcDbSchemaFileReader'                      => 
'DatabaseSchema/interfaces/file_reader.php',

Modified: trunk/DatabaseSchema/src/handlers/common_sql_writer.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/common_sql_writer.php [iso-8859-1] 
(original)
+++ trunk/DatabaseSchema/src/handlers/common_sql_writer.php [iso-8859-1] Mon 
Jan 14 11:30:24 2008
@@ -15,7 +15,7 @@
  * @package DatabaseSchema
  * @version //autogentag//
  */
-abstract class ezcDbSchemaCommonSqlWriter
+abstract class ezcDbSchemaCommonSqlWriter implements ezcDbSchemaDbWriter, 
ezcDbSchemaDiffDbWriter
 {
     /**
      * Stores a list of queries that is generated by the various Database 
writing backends.
@@ -30,6 +30,81 @@
      * @var ezcDbSchema
      */
     protected $schema;
+
+    /**
+     * Returns what type of schema writer this class implements.
+     *
+     * This method always returns ezcDbSchema::DATABASE
+     *
+     * @return int
+     */
+    public function getWriterType()
+    {
+        return ezcDbSchema::DATABASE;
+    }
+
+    /**
+     * Creates the tables contained in $schema in the database that is related 
to $db
+     *
+     * This method takes the table definitions from $schema and will create the
+     * tables according to this definition in the database that is references
+     * by the $db handler. If tables with the same name as contained in the
+     * definitions already exist they will be removed and recreated with the
+     * new definition.
+     *
+     * @param ezcDbHandler $db
+     * @param ezcDbSchema  $schema
+     */
+    public function saveToDb( ezcDbHandler $db, ezcDbSchema $dbSchema )
+    {
+        $db->beginTransaction();
+        foreach ( $this->convertToDDL( $dbSchema ) as $query )
+        {
+            if ( $this->isQueryAllowed( $db, $query ) ) 
+            {
+                $db->exec( $query );
+            }
+        }
+        $db->commit();
+    }
+
+    /**
+     * Returns an array with SQL DDL statements that creates the database 
definition in $dbSchema
+     *
+     * Converts the schema definition contained in $dbSchema to DDL SQL. This
+     * SQL can be used to create tables in an existing database according to
+     * the definition.  The SQL queries are returned as an array.
+     * 
+     * @param ezcDbSchemaDiff $schemaDiff
+     * @return array(string)
+     */
+    public function convertToDDL( ezcDbSchema $dbSchema )
+    {
+        $this->schema = $dbSchema->getSchema();
+
+        // reset queries
+        $this->queries = array();
+        $this->context = array();
+
+        $this->generateSchemaAsSql();
+        return $this->queries;
+    }
+
+    /**
+     * Checks if the query is allowed.
+     *
+     * Perform testing if table exist for DROP TABLE query 
+     * to avoid stoping execution while try to drop not existent table. 
+     * 
+     * @param ezcDbHandler $db
+     * @param string       $query
+     * 
+     * @return boolean
+     */
+    public function isQueryAllowed( ezcDbHandler $db, $query )
+    {
+        return true;
+    }
 
     /**
      * Creates SQL DDL statements from a schema definitin.
@@ -40,10 +115,11 @@
      */
     protected function generateSchemaAsSql()
     {
+        $prefix = ezcDbSchema::$options->tableNamePrefix;
         foreach ( $this->schema as $tableName => $tableDefinition )
         {
-            $this->generateDropTableSql( $tableName );
-            $this->generateCreateTableSql( $tableName, $tableDefinition );
+            $this->generateDropTableSql( $prefix . $tableName );
+            $this->generateCreateTableSql( $prefix . $tableName, 
$tableDefinition );
         }
     }
 

Modified: trunk/DatabaseSchema/src/handlers/mysql/reader.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/mysql/reader.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/mysql/reader.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -14,7 +14,7 @@
  * @package DatabaseSchema
  * @version //autogentag//
  */
-class ezcDbSchemaMysqlReader implements ezcDbSchemaDbReader
+class ezcDbSchemaMysqlReader extends ezcDbSchemaDbReader
 {
     /**
      * Contains a type map from MySQL native types to generic DbSchema types.
@@ -71,18 +71,6 @@
     }
 
     /**
-     * Returns a ezcDbSchema object from the database that is referenced with 
$db.
-     *
-     * @param ezcDbHandler $db
-     * @return ezcDbSchema
-     */
-    public function loadFromDb( ezcDbHandler $db )
-    {
-        $this->db = $db;
-        return new ezcDbSchema( $this->fetchSchema() );
-    }
-
-    /**
      * Loops over all the tables in the database and extracts schema 
information.
      *
      * This method extracts information about a database's schema from the
@@ -90,19 +78,26 @@
      *
      * @return ezcDbSchema
      */
-    private function fetchSchema()
+    protected function fetchSchema()
     {
         $schemaDefinition = array();
 
         $tables = $this->db->query( "SHOW TABLES" )->fetchAll();
         array_walk( $tables, create_function( '&$item,$key', '$item = 
$item[0];' ) );
 
+        // strip out the prefix and only return tables with the prefix set.
+        $prefix = ezcDbSchema::$options->tableNamePrefix;
+
         foreach ( $tables as $tableName )
         {
-            $fields  = $this->fetchTableFields( $tableName );
-            $indexes = $this->fetchTableIndexes( $tableName );
-
-            $schemaDefinition[$tableName] = ezcDbSchema::createNewTable( 
$fields, $indexes );
+            $tableNameWithoutPrefix = substr( $tableName, strlen( $prefix ) );
+            if ( $prefix === '' || $tableName !== $tableNameWithoutPrefix )
+            {
+                $fields  = $this->fetchTableFields( $tableName );
+                $indexes = $this->fetchTableIndexes( $tableName );
+
+                $schemaDefinition[$tableNameWithoutPrefix] = 
ezcDbSchema::createNewTable( $fields, $indexes );
+            }
         }
 
         return $schemaDefinition;

Modified: trunk/DatabaseSchema/src/handlers/mysql/writer.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/mysql/writer.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/mysql/writer.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -45,47 +45,6 @@
     }
 
     /**
-     * Creates tables defined in $dbSchema in the database referenced by $db.
-     *
-     * This method uses [EMAIL PROTECTED] convertToDDL} to create SQL for the 
schema
-     * definition and then executes the return SQL statements on the database
-     * handler $db.
-     *
-     * @todo check for failed transaction
-     *
-     * @param ezcDbHandler $db
-     * @param ezcDbSchema  $dbSchema
-     */
-    public function saveToDb( ezcDbHandler $db, ezcDbSchema $dbSchema )
-    {
-        $db->beginTransaction();
-        foreach ( $this->convertToDDL( $dbSchema ) as $query )
-        {
-            $db->exec( $query );
-        }
-        $db->commit();
-    }
-
-    /**
-     * Returns the definition in $dbSchema as database specific SQL DDL 
queries.
-     *
-     * @param ezcDbSchema $dbSchema
-     *
-     * @return array(string)
-     */
-    public function convertToDDL( ezcDbSchema $dbSchema )
-    {
-        $this->schema = $dbSchema->getSchema();
-
-        // reset queries
-        $this->queries = array();
-        $this->context = array();
-
-        $this->generateSchemaAsSql();
-        return $this->queries;
-    }
-
-    /**
      * Returns what type of schema difference writer this class implements.
      *
      * This method always returns ezcDbSchema::DATABASE
@@ -182,7 +141,7 @@
      */
     protected function generateCreateTableSqlStatement( $tableName )
     {
-        return "CREATE TABLE `$tableName`";
+        return "CREATE TABLE `{$tableName}`";
     }
 
     /**

Modified: trunk/DatabaseSchema/src/handlers/oracle/reader.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/oracle/reader.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/oracle/reader.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -14,7 +14,7 @@
  * @package DatabaseSchema
  * @version //autogentag//
  */
-class ezcDbSchemaOracleReader implements ezcDbSchemaDbReader
+class ezcDbSchemaOracleReader extends ezcDbSchemaDbReader
 {
     /**
      * Contains a type map from Oracle native types to generic DbSchema types.
@@ -48,18 +48,6 @@
     }
 
     /**
-     * Returns a ezcDbSchema object from the database that is referenced with 
$db.
-     *
-     * @param ezcDbHandler $db
-     * @return ezcDbSchema
-     */
-    public function loadFromDb( ezcDbHandler $db )
-    {
-        $this->db = $db;
-        return new ezcDbSchema( $this->fetchSchema() );
-    }
-
-    /**
      * Loops over all the tables in the database and extracts schema 
information.
      *
      * This method extracts information about a database's schema from the
@@ -67,19 +55,26 @@
      *
      * @return ezcDbSchema
      */
-    private function fetchSchema()
+    protected function fetchSchema()
     {
         $schemaDefinition = array();
 
         $tables = $this->db->query( "SELECT table_name FROM user_tables ORDER 
BY table_name" )->fetchAll();
         array_walk( $tables, create_function( '&$item,$key', '$item = 
$item[0];' ) );
 
+        // strip out the prefix and only return tables with the prefix set.
+        $prefix = ezcDbSchema::$options->tableNamePrefix;
+
         foreach ( $tables as $tableName )
         {
-            $fields  = $this->fetchTableFields( $tableName );
-            $indexes = $this->fetchTableIndexes( $tableName );
-
-            $schemaDefinition[$tableName] = ezcDbSchema::createNewTable( 
$fields, $indexes );
+            $tableNameWithoutPrefix = substr( $tableName, strlen( $prefix ) );
+            if ( $prefix === '' || $tableName !== $tableNameWithoutPrefix )
+            {
+                $fields  = $this->fetchTableFields( $tableName );
+                $indexes = $this->fetchTableIndexes( $tableName );
+
+                $schemaDefinition[$tableNameWithoutPrefix] = 
ezcDbSchema::createNewTable( $fields, $indexes );
+            }
         }
 
         return $schemaDefinition;

Modified: trunk/DatabaseSchema/src/handlers/oracle/writer.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/oracle/writer.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/oracle/writer.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -33,55 +33,17 @@
     );
 
     /**
-     * Returns what type of schema writer this class implements.
-     *
-     * This method always returns ezcDbSchema::DATABASE
-     *
-     * @return int
-     */
-    public function getWriterType()
-    {
-        return ezcDbSchema::DATABASE;
-    }
-
-    /**
-     * Creates tables defined in $dbSchema in the database referenced by $db.
-     *
-     * This method uses [EMAIL PROTECTED] convertToDDL} to create SQL for the 
schema
-     * definition and then executes the return SQL statements on the database
-     * handler $db.
-     *
-     * @todo check for failed transaction
-     *
-     * @param ezcDbHandler $db
-     * @param ezcDbSchema  $dbSchema
-     */
-    public function saveToDb( ezcDbHandler $db, ezcDbSchema $dbSchema )
-    {
-        $db->beginTransaction();
-        foreach ( $this->convertToDDL( $dbSchema ) as $query )
-        {
-            if ( $this->isQueryAllowed( $db, $query ) ) 
-            {
-                $db->exec( $query );
-            }
-        }
-        $db->commit();
-    }
-
-    /**
      * Checks if query allowed.
      *
      * Perform testing if table exist for DROP TABLE query 
      * to avoid stoping execution while try to drop not existent table.
      * 
-     * @param ezcDbHandler    $db
-     * @param string $query
+     * @param ezcDbHandler $db
+     * @param string       $query
      * 
-     *
      * @return boolean false if query should not be executed.
      */
-    private function isQueryAllowed( ezcDbHandler $db, $query )
+    public function isQueryAllowed( ezcDbHandler $db, $query )
     {
         if ( strstr( $query, 'AUTO_INCREMENT' ) ) // detect AUTO_INCREMENT and 
return imediately. Will process later.
         {
@@ -114,25 +76,6 @@
         }
 
         return true;
-    }
-
-    /**
-     * Returns the definition in $dbSchema as database specific SQL DDL 
queries.
-     *
-     * @param ezcDbSchema $dbSchema
-     *
-     * @return array(string)
-     */
-    public function convertToDDL( ezcDbSchema $dbSchema )
-    {
-        $this->schema = $dbSchema->getSchema();
-
-        // reset queries
-        $this->queries = array();
-        $this->context = array();
-
-        $this->generateSchemaAsSql();
-        return $this->queries;
     }
 
     /**

Modified: trunk/DatabaseSchema/src/handlers/pgsql/reader.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/pgsql/reader.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/pgsql/reader.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -14,7 +14,7 @@
  * @package DatabaseSchema
  * @version //autogentag//
  */
-class ezcDbSchemaPgsqlReader implements ezcDbSchemaDbReader
+class ezcDbSchemaPgsqlReader extends ezcDbSchemaDbReader
 {
     /**
      * Contains a type map from PostgreSQL native types to generic DbSchema 
types.
@@ -75,18 +75,6 @@
     }
 
     /**
-     * Returns a ezcDbSchema object from the database that is referenced with 
$db.
-     *
-     * @param ezcDbHandler $db
-     * @return ezcDbSchema
-     */
-    public function loadFromDb( ezcDbHandler $db )
-    {
-        $this->db = $db;
-        return new ezcDbSchema( $this->fetchSchema() );
-    }
-
-    /**
      * Loops over all the tables in the database and extracts schema 
information.
      *
      * This method extracts information about a database's schema from the
@@ -94,19 +82,26 @@
      *
      * @return ezcDbSchema
      */
-    private function fetchSchema()
+    protected function fetchSchema()
     {
         $schemaDefinition = array();
 
         $tables = $this->db->query( "SELECT table_name FROM 
information_schema.tables WHERE table_schema = 'public'" )->fetchAll();
         array_walk( $tables, create_function( '&$item,$key', '$item = 
$item[0];' ) );
 
+        // strip out the prefix and only return tables with the prefix set.
+        $prefix = ezcDbSchema::$options->tableNamePrefix;
+
         foreach ( $tables as $tableName )
         {
-            $fields  = $this->fetchTableFields( $tableName );
-            $indexes = $this->fetchTableIndexes( $tableName );
-
-            $schemaDefinition[$tableName] = ezcDbSchema::createNewTable( 
$fields, $indexes );
+            $tableNameWithoutPrefix = substr( $tableName, strlen( $prefix ) );
+            if ( $prefix === '' || $tableName !== $tableNameWithoutPrefix )
+            {
+                $fields  = $this->fetchTableFields( $tableName );
+                $indexes = $this->fetchTableIndexes( $tableName );
+
+                $schemaDefinition[$tableNameWithoutPrefix] = 
ezcDbSchema::createNewTable( $fields, $indexes );
+            }
         }
 
         return $schemaDefinition;

Modified: trunk/DatabaseSchema/src/handlers/pgsql/writer.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/pgsql/writer.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/pgsql/writer.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -31,18 +31,6 @@
         'blob' => 'bytea',
         'clob' => 'text'
     );
-
-    /**
-     * Returns what type of schema writer this class implements.
-     *
-     * This method always returns ezcDbSchema::DATABASE
-     *
-     * @return int
-     */
-    public function getWriterType()
-    {
-        return ezcDbSchema::DATABASE;
-    }
 
     /**
      * Creates tables defined in $dbSchema in the database referenced by $db.
@@ -99,13 +87,13 @@
      * Perform testing if table exist for DROP TABLE query 
      * to avoid stoping execution while try to drop not existent table.
      * 
-     * @param ezcDbHandler    $db
-     * @param string $query
+     * @param ezcDbHandler $db
+     * @param string       $query
      * 
      *
      * @return boolean false if query should not be executed.
      */
-    private function isQueryAllowed( ezcDbHandler $db, $query )
+    public function isQueryAllowed( ezcDbHandler $db, $query )
     {
         if ( substr( $query, 0, 10 ) == 'DROP TABLE' )
         {
@@ -128,25 +116,6 @@
             return false;
         }
         return true;
-    }
-
-    /**
-     * Returns the definition in $dbSchema as database specific SQL DDL 
queries.
-     *
-     * @param ezcDbSchema $dbSchema
-     *
-     * @return array(string)
-     */
-    public function convertToDDL( ezcDbSchema $dbSchema )
-    {
-        $this->schema = $dbSchema->getSchema();
-
-        // reset queries
-        $this->queries = array();
-        $this->context = array();
-
-        $this->generateSchemaAsSql();
-        return $this->queries;
     }
 
     /**
@@ -217,7 +186,7 @@
      */
     protected function generateCreateTableSqlStatement( $tableName )
     {
-        return "CREATE TABLE \"$tableName\"";
+        return "CREATE TABLE \"{$tableName}\"";
     }
 
     /**

Modified: trunk/DatabaseSchema/src/handlers/sqlite/reader.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/sqlite/reader.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/sqlite/reader.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -14,7 +14,7 @@
  * @package DatabaseSchema
  * @version //autogentag//
  */
-class ezcDbSchemaSqliteReader implements ezcDbSchemaDbReader
+class ezcDbSchemaSqliteReader extends ezcDbSchemaDbReader
 {
     /**
      * Contains a type map from SQLite native types to generic DbSchema types.
@@ -51,18 +51,6 @@
     }
 
     /**
-     * Returns a ezcDbSchema object from the database that is referenced with 
$db.
-     *
-     * @param ezcDbHandler $db
-     * @return ezcDbSchema
-     */
-    public function loadFromDb( ezcDbHandler $db )
-    {
-        $this->db = $db;
-        return new ezcDbSchema( $this->fetchSchema() );
-    }
-
-    /**
      * Loops over all the tables in the database and extracts schema 
information.
      *
      * This method extracts information about a database's schema from the
@@ -70,19 +58,26 @@
      *
      * @return ezcDbSchema
      */
-    private function fetchSchema()
+    protected function fetchSchema()
     {
         $schemaDefinition = array();
 
         $tables = $this->db->query( "SELECT name FROM sqlite_master WHERE 
type='table' AND name != 'sqlite_sequence' ORDER BY name" )->fetchAll();
         array_walk( $tables, create_function( '&$item,$key', '$item = 
$item[0];' ) );
 
+        // strip out the prefix and only return tables with the prefix set.
+        $prefix = ezcDbSchema::$options->tableNamePrefix;
+
         foreach ( $tables as $tableName )
         {
-            $fields  = $this->fetchTableFields( $tableName );
-            $indexes = $this->fetchTableIndexes( $tableName );
-
-            $schemaDefinition[$tableName] = ezcDbSchema::createNewTable( 
$fields, $indexes );
+            $tableNameWithoutPrefix = substr( $tableName, strlen( $prefix ) );
+            if ( $prefix === '' || $tableName !== $tableNameWithoutPrefix )
+            {
+                $fields  = $this->fetchTableFields( $tableName );
+                $indexes = $this->fetchTableIndexes( $tableName );
+
+                $schemaDefinition[$tableNameWithoutPrefix] = 
ezcDbSchema::createNewTable( $fields, $indexes );
+            }
         }
 
         return $schemaDefinition;

Modified: trunk/DatabaseSchema/src/handlers/sqlite/writer.php
==============================================================================
--- trunk/DatabaseSchema/src/handlers/sqlite/writer.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/handlers/sqlite/writer.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -13,7 +13,7 @@
  *
  * @package DatabaseSchema
  */
-class ezcDbSchemaSqliteWriter extends ezcDbSchemaCommonSqlWriter implements 
ezcDbSchemaDbWriter, ezcDbSchemaDiffDbWriter
+class ezcDbSchemaSqliteWriter extends ezcDbSchemaCommonSqlWriter
 {
     /**
      * Contains a type map from DbSchema types to SQLite native types.
@@ -45,43 +45,17 @@
     }
 
     /**
-     * Creates tables defined in $dbSchema in the database referenced by $db.
-     *
-     * This method uses [EMAIL PROTECTED] convertToDDL} to create SQL for the 
schema
-     * definition and then executes the return SQL statements on the database
-     * handler $db.
-     *
-     * @todo check for failed transaction
-     *
-     * @param ezcDbHandler $db
-     * @param ezcDbSchema  $dbSchema
-     */
-    public function saveToDb( ezcDbHandler $db, ezcDbSchema $dbSchema )
-    {
-        $db->beginTransaction();
-        foreach ( $this->convertToDDL( $dbSchema ) as $query )
-        {
-            if ( $this->isQueryAllowed( $db, $query ) ) 
-            {
-                $db->exec( $query );
-            }
-        }
-        $db->commit();
-    }
-
-    /**
-     * Checks if sertain query allowed.
+     * Checks if certain query allowed.
      *
      * Perform testing if table exist for DROP TABLE query 
      * to avoid stoping execution while try to drop not existent table.
      * 
-     * @param ezcDbHandler    $db
-     * @param string $query
-     * 
+     * @param ezcDbHandler $db
+     * @param string       $query
      *
      * @return boolean false if query should not be executed.
      */
-    private function isQueryAllowed( ezcDbHandler $db, $query )
+    public function isQueryAllowed( ezcDbHandler $db, $query )
     {
         if ( strstr($query, 'DROP COLUMN') || strstr($query, 'CHANGE') ) // 
detecting DROP COLUMN clause or field CHANGE clause 
         {
@@ -108,26 +82,6 @@
         return true;
     }
 
-
-    /**
-     * Returns the definition in $dbSchema as database specific SQL DDL 
queries.
-     *
-     * @param ezcDbSchema $dbSchema
-     *
-     * @return array(string)
-     */
-    public function convertToDDL( ezcDbSchema $dbSchema )
-    {
-        $this->schema = $dbSchema->getSchema();
-
-        // reset queries
-        $this->queries = array();
-        $this->context = array();
-
-        $this->generateSchemaAsSql();
-        return $this->queries;
-    }
-
     /**
      * Returns what type of schema difference writer this class implements.
      *
@@ -139,7 +93,6 @@
     {
         return ezcDbSchema::DATABASE;
     }
-
     /**
      * Applies the differences defined in $dbSchemaDiff to the database 
referenced by $db.
      *
@@ -463,7 +416,7 @@
      */
     protected function generateCreateTableSqlStatement( $tableName )
     {
-        return "CREATE TABLE '$tableName'";
+        return "CREATE TABLE '{$tableName}'";
     }
 
     /**

Modified: trunk/DatabaseSchema/src/interfaces/db_reader.php
==============================================================================
--- trunk/DatabaseSchema/src/interfaces/db_reader.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/interfaces/db_reader.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -13,7 +13,7 @@
  * @package DatabaseSchema
  * @version //autogen//
  */
-interface ezcDbSchemaDbReader extends ezcDbSchemaReader
+abstract class ezcDbSchemaDbReader implements ezcDbSchemaReader
 {
     /**
      * Returns an ezcDbSchema created from the database schema in the database 
referenced by $db
@@ -25,6 +25,10 @@
      * @param ezcDbHandler $db
      * @return ezcDbSchema
      */
-    public function loadFromDb( ezcDbHandler $db );
+    public function loadFromDb( ezcDbHandler $db )
+    {
+        $this->db = $db;
+        return new ezcDbSchema( $this->fetchSchema() );
+    }
 }
 ?>

Modified: trunk/DatabaseSchema/src/interfaces/db_writer.php
==============================================================================
--- trunk/DatabaseSchema/src/interfaces/db_writer.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/interfaces/db_writer.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -27,7 +27,7 @@
      * @param ezcDbHandler $db
      * @param ezcDbSchema  $schema
      */
-    public function saveToDb( ezcDbHandler $db, ezcDbSchema $schema );
+    public function saveToDb( ezcDbHandler $db, ezcDbSchema $dbSchema );
 
     /**
      * Returns an array with SQL DDL statements that creates the database 
definition in $dbSchema
@@ -40,5 +40,18 @@
      * @return array(string)
      */
     public function convertToDDL( ezcDbSchema $dbSchema );
+
+    /**
+     * Checks if the query is allowed.
+     *
+     * Perform testing if table exist for DROP TABLE query 
+     * to avoid stoping execution while try to drop not existent table. 
+     * 
+     * @param ezcDbHandler $db
+     * @param string       $query
+     * 
+     * @return boolean
+     */
+    public function isQueryAllowed( ezcDbHandler $db, $query );
 }
 ?>

Modified: trunk/DatabaseSchema/src/options/schema.php
==============================================================================
--- trunk/DatabaseSchema/src/options/schema.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/options/schema.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -41,6 +41,7 @@
         $this->properties['fieldClassName'] = 'ezcDbSchemaField';
         $this->properties['indexClassName'] = 'ezcDbSchemaIndex';
         $this->properties['indexFieldClassName'] = 'ezcDbSchemaIndexField';
+        $this->properties['tableNamePrefix'] = '';
         parent::__construct( $options );
     }
 
@@ -90,6 +91,14 @@
                 $this->properties[$propertyName] = $propertyValue;
                 break;
 
+            case 'tableNamePrefix':
+                if ( !is_string( $propertyValue ) )
+                {
+                    throw new ezcBaseValueException( $propertyName, 
$propertyValue, 'string' );
+                }
+                $this->properties[$propertyName] = $propertyValue;
+                break;
+
             default:
                 throw new ezcBasePropertyNotFoundException( $propertyName );
                 break;

Modified: trunk/DatabaseSchema/src/schema.php
==============================================================================
--- trunk/DatabaseSchema/src/schema.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/src/schema.php [iso-8859-1] Mon Jan 14 11:30:24 2008
@@ -152,6 +152,7 @@
      */
     static public function createFromDb( ezcDbHandler $db )
     {
+        self::initOptions();
         $className = ezcDbSchemaHandlerManager::getReaderByFormat( 
$db->getName() );
         $reader = new $className();
         self::checkSchemaReader( $reader, self::DATABASE );
@@ -202,6 +203,7 @@
      */
     public function writeToDb( ezcDbHandler $db )
     {
+        self::initOptions();
         $className = ezcDbSchemaHandlerManager::getWriterByFormat( 
$db->getName() );
         $writer = new $className();
         self::checkSchemaWriter( $writer, self::DATABASE );
@@ -226,6 +228,7 @@
      */
     public function convertToDDL( $db )
     {
+        self::initOptions();
         if ( $db instanceof ezcDbHandler )
         {
             $db = $db->getName();

Modified: trunk/DatabaseSchema/tests/generic_test.php
==============================================================================
--- trunk/DatabaseSchema/tests/generic_test.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/tests/generic_test.php [iso-8859-1] Mon Jan 14 
11:30:24 2008
@@ -51,6 +51,38 @@
         return $tables;
     }
 
+    private static function getSchemaWithPrefixedTableNames()
+    {
+        $tables = array(
+            'prefix_bugdb' => new ezcDbSchemaTable(
+                array (
+                    'id' => new ezcDbSchemaField( 'integer', false, true, 
null, true ),
+                    'bug_type' => new ezcDbSchemaField( 'text', 32, true ),
+                    'severity' => new ezcDbSchemaField( 'integer', false, 
true, 0 ),
+                    'sdesc'    => new ezcDbSchemaField( 'text', 80, true ),
+                    'ldesc'    => new ezcDbSchemaField( 'clob', false, true ),
+                    'php_version' => new ezcDbSchemaField( 'text', 100, true ),
+                ),
+                array (
+                    'bug_type' => new ezcDbSchemaIndex( array ( 'bug_type' => 
new ezcDbSchemaIndexField() ), false, false ),
+                    'php_version' => new ezcDbSchemaIndex( array ( 
'php_version' => new ezcDbSchemaIndexField() ) ),
+                    'primary'  => new ezcDbSchemaIndex( array ( 'id' => new 
ezcDbSchemaIndexField() ), true ),
+                )
+            ),
+            'prefix_bugdb_comments' => new ezcDbSchemaTable(
+                array (
+                    'bug_id' => new ezcDbSchemaField( 'integer', false, true, 
0 ),
+                    'comment' => new ezcDbSchemaField( 'clob', false, true ),
+                    'email' => new ezcDbSchemaField( 'text', 32 ),
+                ),
+                array (
+                    'email' => new ezcDbSchemaIndex( array ( 'email' => new 
ezcDbSchemaIndexField() ) ),
+                )
+            ),
+        );
+        return $tables;
+    }
+
     public function testSimple()
     {
         $schema = new ezcDbSchema( self::getSchema() );
@@ -302,5 +334,57 @@
         self::assertEquals( $file_orig, $file_dump );
     }
 
+    public function testWriteWithPrefixReadWithPrefix()
+    {
+        xdebug_break();
+        $optionsWithPrefix = new ezcDbSchemaOptions;
+        $optionsWithPrefix->tableNamePrefix = 'prefix_';
+        $schema = new ezcDbSchema( self::getSchema() );
+
+        ezcDbSchema::setOptions( $optionsWithPrefix );
+        $schema->writeToDb( $this->db );
+
+        ezcDbSchema::setOptions( $optionsWithPrefix );
+        $newSchema = ezcDbSchema::createFromDb( $this->db );
+
+        self::assertEquals( $schema, $newSchema );
+    }
+
+    public function testWriteWithPrefixReadWithoutPrefix()
+    {
+        xdebug_break();
+        $optionsWithoutPrefix = new ezcDbSchemaOptions;
+        $optionsWithoutPrefix->tableNamePrefix = '';
+        $optionsWithPrefix = new ezcDbSchemaOptions;
+        $optionsWithPrefix->tableNamePrefix = 'prefix_';
+        $schema = new ezcDbSchema( self::getSchema() );
+        $schemaWithPrefix = new ezcDbSchema( 
self::getSchemaWithPrefixedTableNames() );
+
+        ezcDbSchema::setOptions( $optionsWithPrefix );
+        $schema->writeToDb( $this->db );
+
+        ezcDbSchema::setOptions( $optionsWithoutPrefix );
+        $newSchema = ezcDbSchema::createFromDb( $this->db );
+
+        self::assertEquals( $schemaWithPrefix, $newSchema );
+    }
+
+    public function testWriteWithoutPrefixReadWithPrefix()
+    {
+        $optionsWithoutPrefix = new ezcDbSchemaOptions;
+        $optionsWithoutPrefix->tableNamePrefix = '';
+        $optionsWithPrefix = new ezcDbSchemaOptions;
+        $optionsWithPrefix->tableNamePrefix = 'prefix_';
+
+        $schema = new ezcDbSchema( self::getSchema() );
+        $schemaWithPrefix = new ezcDbSchema( 
self::getSchemaWithPrefixedTableNames() );
+
+        ezcDbSchema::setOptions( $optionsWithoutPrefix );
+        $schemaWithPrefix->writeToDb( $this->db );
+
+        ezcDbSchema::setOptions( $optionsWithPrefix );
+        $newSchema = ezcDbSchema::createFromDb( $this->db );
+        self::assertEquals( $schema, $newSchema );
+    }
 }
 ?>


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to