Commit 01c5461f authored by Thomas Müller's avatar Thomas Müller

fix quoted sequence name

parent d8a96fcf
...@@ -494,7 +494,10 @@ BEGIN ...@@ -494,7 +494,10 @@ BEGIN
END IF; END IF;
END;'; END;';
$sequenceName = $this->getIdentitySequenceName($unquotedTableName, $unquotedName); $sequenceName = $this->getIdentitySequenceName(
$tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName,
$nameIdentifier->isQuoted() ? $quotedName : $unquotedName
);
$sequence = new Sequence($sequenceName, $start); $sequence = new Sequence($sequenceName, $start);
$sql[] = $this->getCreateSequenceSQL($sequence); $sql[] = $this->getCreateSequenceSQL($sequence);
...@@ -512,7 +515,7 @@ BEGIN ...@@ -512,7 +515,7 @@ BEGIN
ELSE ELSE
SELECT NVL(Last_Number, 0) INTO last_Sequence SELECT NVL(Last_Number, 0) INTO last_Sequence
FROM User_Sequences FROM User_Sequences
WHERE Sequence_Name = \'' . $sequenceName . '\'; WHERE Sequence_Name = \'' . $sequence->getName() . '\';
SELECT :NEW.' . $quotedName . ' INTO last_InsertID FROM DUAL; SELECT :NEW.' . $quotedName . ' INTO last_InsertID FROM DUAL;
WHILE (last_InsertID > last_Sequence) LOOP WHILE (last_InsertID > last_Sequence) LOOP
SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL; SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
......
...@@ -146,12 +146,12 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -146,12 +146,12 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$this->assertEquals( $this->assertEquals(
'NUMBER(10)', 'NUMBER(10)',
$this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true) $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
)); ));
$this->assertEquals( $this->assertEquals(
'NUMBER(10)', 'NUMBER(10)',
$this->_platform->getIntegerTypeDeclarationSQL( $this->_platform->getIntegerTypeDeclarationSQL(
array('autoincrement' => true, 'primary' => true) array('autoincrement' => true, 'primary' => true)
)); ));
} }
public function testGeneratesTypeDeclarationsForStrings() public function testGeneratesTypeDeclarationsForStrings()
...@@ -160,7 +160,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -160,7 +160,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase
'CHAR(10)', 'CHAR(10)',
$this->_platform->getVarcharTypeDeclarationSQL( $this->_platform->getVarcharTypeDeclarationSQL(
array('length' => 10, 'fixed' => true) array('length' => 10, 'fixed' => true)
)); ));
$this->assertEquals( $this->assertEquals(
'VARCHAR2(50)', 'VARCHAR2(50)',
$this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)), $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
...@@ -235,19 +235,19 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -235,19 +235,19 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$column = $table->addColumn($columnName, 'integer'); $column = $table->addColumn($columnName, 'integer');
$column->setAutoincrement(true); $column->setAutoincrement(true);
$targets = array( $targets = array(
"CREATE TABLE {$tableName} ({$columnName} NUMBER(10) NOT NULL)", "CREATE TABLE {$tableName} ({$columnName} NUMBER(10) NOT NULL)",
"DECLARE constraints_Count NUMBER; BEGIN SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = '{$tableName}' AND CONSTRAINT_TYPE = 'P'; IF constraints_Count = 0 OR constraints_Count = '' THEN EXECUTE IMMEDIATE 'ALTER TABLE {$tableName} ADD CONSTRAINT {$tableName}_AI_PK PRIMARY KEY ({$columnName})'; END IF; END;", "DECLARE constraints_Count NUMBER; BEGIN SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = '{$tableName}' AND CONSTRAINT_TYPE = 'P'; IF constraints_Count = 0 OR constraints_Count = '' THEN EXECUTE IMMEDIATE 'ALTER TABLE {$tableName} ADD CONSTRAINT {$tableName}_AI_PK PRIMARY KEY ({$columnName})'; END IF; END;",
"CREATE SEQUENCE {$tableName}_SEQ START WITH 1 MINVALUE 1 INCREMENT BY 1", "CREATE SEQUENCE {$tableName}_SEQ START WITH 1 MINVALUE 1 INCREMENT BY 1",
"CREATE TRIGGER {$tableName}_AI_PK BEFORE INSERT ON {$tableName} FOR EACH ROW DECLARE last_Sequence NUMBER; last_InsertID NUMBER; BEGIN SELECT {$tableName}_SEQ.NEXTVAL INTO :NEW.{$columnName} FROM DUAL; IF (:NEW.{$columnName} IS NULL OR :NEW.{$columnName} = 0) THEN SELECT {$tableName}_SEQ.NEXTVAL INTO :NEW.{$columnName} FROM DUAL; ELSE SELECT NVL(Last_Number, 0) INTO last_Sequence FROM User_Sequences WHERE Sequence_Name = '{$tableName}_SEQ'; SELECT :NEW.{$columnName} INTO last_InsertID FROM DUAL; WHILE (last_InsertID > last_Sequence) LOOP SELECT {$tableName}_SEQ.NEXTVAL INTO last_Sequence FROM DUAL; END LOOP; END IF; END;" "CREATE TRIGGER {$tableName}_AI_PK BEFORE INSERT ON {$tableName} FOR EACH ROW DECLARE last_Sequence NUMBER; last_InsertID NUMBER; BEGIN SELECT {$tableName}_SEQ.NEXTVAL INTO :NEW.{$columnName} FROM DUAL; IF (:NEW.{$columnName} IS NULL OR :NEW.{$columnName} = 0) THEN SELECT {$tableName}_SEQ.NEXTVAL INTO :NEW.{$columnName} FROM DUAL; ELSE SELECT NVL(Last_Number, 0) INTO last_Sequence FROM User_Sequences WHERE Sequence_Name = '{$tableName}_SEQ'; SELECT :NEW.{$columnName} INTO last_InsertID FROM DUAL; WHILE (last_InsertID > last_Sequence) LOOP SELECT {$tableName}_SEQ.NEXTVAL INTO last_Sequence FROM DUAL; END LOOP; END IF; END;"
); );
$statements = $this->_platform->getCreateTableSQL($table); $statements = $this->_platform->getCreateTableSQL($table);
//strip all the whitespace from the statements //strip all the whitespace from the statements
array_walk($statements, function(&$value){ array_walk($statements, function(&$value){
$value = preg_replace('/\s+/', ' ',$value); $value = preg_replace('/\s+/', ' ',$value);
}); });
foreach($targets as $key => $sql){ foreach($targets as $key => $sql){
$this->assertArrayHasKey($key,$statements); $this->assertArrayHasKey($key,$statements);
$this->assertEquals($sql, $statements[$key]); $this->assertEquals($sql, $statements[$key]);
} }
} }
...@@ -285,8 +285,8 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -285,8 +285,8 @@ class OraclePlatformTest extends AbstractPlatformTestCase
public function getBitOrComparisonExpressionSql($value1, $value2) public function getBitOrComparisonExpressionSql($value1, $value2)
{ {
return '(' . $value1 . '-' . return '(' . $value1 . '-' .
$this->getBitAndComparisonExpressionSql($value1, $value2) $this->getBitAndComparisonExpressionSql($value1, $value2)
. '+' . $value2 . ')'; . '+' . $value2 . ')';
} }
protected function getQuotedColumnInPrimaryKeySQL() protected function getQuotedColumnInPrimaryKeySQL()
...@@ -340,7 +340,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -340,7 +340,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$expectedSql = array( $expectedSql = array(
"ALTER TABLE mytable MODIFY (foo VARCHAR2(255) DEFAULT 'bla', baz VARCHAR2(255) DEFAULT 'bla' NOT NULL, metar VARCHAR2(2000) DEFAULT NULL NULL)", "ALTER TABLE mytable MODIFY (foo VARCHAR2(255) DEFAULT 'bla', baz VARCHAR2(255) DEFAULT 'bla' NOT NULL, metar VARCHAR2(2000) DEFAULT NULL NULL)",
); );
$this->assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff)); $this->assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
} }
...@@ -602,4 +602,44 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -602,4 +602,44 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$this->_platform->getAlterTableSQL($tableDiff) $this->_platform->getAlterTableSQL($tableDiff)
); );
} }
public function testQuotedTableNames()
{
$table = new Table('"test"');
$table->addColumn('"id"', 'integer', array('autoincrement' => true));
// assert tabel
$this->assertTrue($table->isQuoted());
$this->assertEquals('test', $table->getName());
$this->assertEquals('"test"', $table->getQuotedName($this->_platform));
$sql = $this->_platform->getCreateTableSQL($table);
$this->assertEquals('CREATE TABLE "test" ("id" NUMBER(10) NOT NULL)', $sql[0]);
$this->assertEquals('CREATE SEQUENCE "test_SEQ" START WITH 1 MINVALUE 1 INCREMENT BY 1', $sql[2]);
$createTriggerStatement = <<<EOD
CREATE TRIGGER "test_AI_PK"
BEFORE INSERT
ON "test"
FOR EACH ROW
DECLARE
last_Sequence NUMBER;
last_InsertID NUMBER;
BEGIN
SELECT "test_SEQ".NEXTVAL INTO :NEW."id" FROM DUAL;
IF (:NEW."id" IS NULL OR :NEW."id" = 0) THEN
SELECT "test_SEQ".NEXTVAL INTO :NEW."id" FROM DUAL;
ELSE
SELECT NVL(Last_Number, 0) INTO last_Sequence
FROM User_Sequences
WHERE Sequence_Name = 'test_SEQ';
SELECT :NEW."id" INTO last_InsertID FROM DUAL;
WHILE (last_InsertID > last_Sequence) LOOP
SELECT "test_SEQ".NEXTVAL INTO last_Sequence FROM DUAL;
END LOOP;
END IF;
END;
EOD;
$this->assertEquals($createTriggerStatement, $sql[3]);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment