Author: dr
Date: Wed May 30 16:32:51 2007
New Revision: 5314

Log:
- Fixed issue #10804: Adding auto increment to field fails with oracle and
  sqlite.

Modified:
    trunk/DatabaseSchema/ChangeLog
    trunk/DatabaseSchema/src/handlers/oracle/writer.php
    trunk/DatabaseSchema/src/handlers/sqlite/writer.php
    trunk/DatabaseSchema/tests/generic_diff_test.php

Modified: trunk/DatabaseSchema/ChangeLog
==============================================================================
--- trunk/DatabaseSchema/ChangeLog [iso-8859-1] (original)
+++ trunk/DatabaseSchema/ChangeLog [iso-8859-1] Wed May 30 16:32:51 2007
@@ -5,6 +5,8 @@
   reflected in definition file names.
 - Fixed issue #10769: PersistentObjectDatabaseSchemaTiein: Generated
   getState() expects parameter.
+- Fixed issue #10804: Adding auto increment to field fails with oracle and
+  sqlite.
 
 
 1.2beta1 - Monday 07 May 2007

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] Wed May 30 
16:32:51 2007
@@ -174,11 +174,11 @@
                 {
                     $db->commit();
                     $db->beginTransaction();
-                    if ( preg_match ( "/ALTER TABLE (.*) MODYFY (.*) (.*) 
AUTO_INCREMENT/" , $query, $matches ) ) 
+                    if ( preg_match ( "/ALTER TABLE (.*) MODIFY (.*?) (.*) 
AUTO_INCREMENT/" , $query, $matches ) ) 
                     {
-                        $tableName = $matches[1];
-                        $autoIncrementFieldName = $matches[2];
-                        $autoIncrementFieldType = $matches[3];
+                        $tableName = trim( $matches[1], '"' );
+                        $autoIncrementFieldName = trim( $matches[2], '"' );
+                        $autoIncrementFieldType = trim( $matches[3], '"' );
                         $this->addAutoIncrementField( $db, $tableName, 
$autoIncrementFieldName, $autoIncrementFieldType );
                     }
                 }
@@ -205,11 +205,11 @@
         // @apichange This code piece would become orphan, with the new 
         // implementation. We still need it to drop the old sequences.
         // Remove until --END-- to not take care of them.
-        $resultArray = $this->db->query( "SELECT   a.column_name AS field, " . 
   
-                                         "         a.column_id AS field_pos " .
-                                         "FROM     user_tab_columns a " .
-                                         "WHERE    a.table_name = 
'{$tableName}' AND a.column_name = '{$autoIncrementFieldName}'" .
-                                         "ORDER BY a.column_id" );
+        $resultArray = $db->query( "SELECT   a.column_name AS field, " .    
+                                   "         a.column_id AS field_pos " .
+                                   "FROM     user_tab_columns a " .
+                                   "WHERE    a.table_name = '{$tableName}' AND 
a.column_name = '{$autoIncrementFieldName}'" .
+                                   "ORDER BY a.column_id" );
         $resultArray->setFetchMode( PDO::FETCH_ASSOC );
 
         if ( count( $resultArray) != 1 )
@@ -217,11 +217,12 @@
             return;
         }
 
-        $fieldPos = $resultArray[0]['field_pos'];
+        $result = $resultArray->fetch();
+        $fieldPos = $result['field_pos'];
 
         // emulation of autoincrement through adding sequence, trigger and 
constraint
         $oldName = "{$tableName}_{$fieldPos}";
-        $sequence = $this->db->query( "SELECT sequence_name FROM 
user_sequences WHERE sequence_name = '{$oldName}_seq'" )->fetchAll();
+        $sequence = $db->query( "SELECT sequence_name FROM user_sequences 
WHERE sequence_name = '{$oldName}_seq'" )->fetchAll();
         if ( count( $sequence) > 0  )
         {
             // assuming that if the seq exists, the trigger exists too
@@ -233,7 +234,7 @@
         // New sequence names, using field names
         $newName = "{$tableName}_{$autoIncrementFieldName}";
         // Emulation of autoincrement through adding sequence, trigger and 
constraint
-        $sequences = $this->db->query( "SELECT sequence_name FROM 
user_sequences WHERE sequence_name = '{$newName}_seq'" )->fetchAll();
+        $sequences = $db->query( "SELECT sequence_name FROM user_sequences 
WHERE sequence_name = '{$newName}_seq'" )->fetchAll();
         if ( count( $sequences ) > 0  )
         {
             $db->query( "DROP SEQUENCE \"{$newName}_seq\"" );
@@ -246,12 +247,12 @@
                                   "select \"{$newName}_seq\".nextval into 
:new.\"{$autoIncrementFieldName}\" from dual; ".
                                   "end;" );
 
-        $constraint = $this->db->query( "SELECT constraint_name FROM 
user_cons_columns WHERE constraint_name = '{$tableName}_pkey'" )->fetchAll();
+        $constraint = $db->query( "SELECT constraint_name FROM 
user_cons_columns WHERE constraint_name = '{$tableName}_pkey'" )->fetchAll();
         if ( count( $constraint) > 0  )
         {
             $db->query( "ALTER TABLE \"$tableName\" DROP CONSTRAINT 
\"{$tableName}_pkey\"" );
         }
-        $db->exec( "ALTER TABLE \"{$tableName}\" ADD CONSTRAINT 
\"{$tableName}_pkey\" PRIMARY KEY ( \"{$fieldName}\" )" );
+        $db->exec( "ALTER TABLE \"{$tableName}\" ADD CONSTRAINT 
\"{$tableName}_pkey\" PRIMARY KEY ( \"{$autoIncrementFieldName}\" )" );
         $this->context['skip_primary'] = true;
     }
 

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] Wed May 30 
16:32:51 2007
@@ -197,7 +197,7 @@
                     $db->beginTransaction();
                     try
                     {
-                        preg_match( "/ALTER TABLE (.*) CHANGE (.*) (.*) (.*)/" 
, $query, $matches );
+                        preg_match( "/ALTER TABLE (.*) CHANGE (.*?) (.*?) 
(.*)/" , $query, $matches );
                         $tableName = trim( $matches[1], "'" );
                         $changeFieldName = trim( $matches[2], "'" );
                         $changeFieldNewName = trim( $matches[3], "'" );

Modified: trunk/DatabaseSchema/tests/generic_diff_test.php
==============================================================================
--- trunk/DatabaseSchema/tests/generic_diff_test.php [iso-8859-1] (original)
+++ trunk/DatabaseSchema/tests/generic_diff_test.php [iso-8859-1] Wed May 30 
16:32:51 2007
@@ -236,5 +236,51 @@
         $sql = file_get_contents( $this->testFilesDir . 
"bug8900-diff_{$name}.sql" );
         self::assertEquals( $sql, $text );
     }
+
+    // bug #10801
+    public function testAddingAutoIncrementField()
+    {
+        $dbh = $this->db;
+
+        $schema1 = new ezcDbSchema( array(
+            'table10801' => new ezcDbSchemaTable( array(
+                'id' => ezcDbSchemaField::__set_state( array(
+                    'type' => 'integer',
+                    'length' => false,
+                    'notNull' => false,
+                    'default' => 0,
+                    'autoIncrement' => false,
+                    'unsigned' => false,
+                ) ),
+                'text' => new ezcDbSchemaField( 'text' )
+            ) )
+        ) );
+        $schema2 = new ezcDbSchema( array(
+            'table10801' => new ezcDbSchemaTable( array(
+                'id' => ezcDbSchemaField::__set_state( array(
+                    'type' => 'integer',
+                    'length' => false,
+                    'notNull' => true,
+                    'default' => null,
+                    'autoIncrement' => true,
+                    'unsigned' => false,
+                ) ),
+                'text' => new ezcDbSchemaField( 'text' )
+            ) )
+        ) );
+        $schema1->writeToDb( $dbh );
+        $diff = ezcDbSchemaComparator::compareSchemas( $schema1, $schema2 );
+        $diff->applyToDb( $dbh );
+
+        $q = $dbh->createInsertQuery();
+        $stmt = $q->insertInto( $dbh->quoteIdentifier('table10801') )->set( 
$dbh->quoteIdentifier('text'), $q->bindValue('text') )->prepare();
+        $stmt->execute();
+
+        $q = $dbh->createSelectQuery();
+        $stmt = $q->select( '*' )->from( $dbh->quoteIdentifier('table10801') 
)->prepare();
+        $stmt->execute();
+        $result = $stmt->fetchAll( PDO::FETCH_ASSOC );
+        $this->assertEquals( 1, $result[0]['id'] );
+    }
 }
 ?>


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to