Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Port PR #326 from Propel 1.6

  • Loading branch information...
commit cdd36998238407e4ac30e143d5e57c883e666ed3 1 parent c5af3f1
@krichprollsch authored willdurand committed
View
30 src/Propel/Generator/Reverse/MssqlSchemaParser.php
@@ -27,7 +27,6 @@
*/
class MssqlSchemaParser extends AbstractSchemaParser
{
-
/**
* Map MSSQL native types to Propel types.
* @var array
@@ -87,7 +86,7 @@ public function parse(Database $database, Task $task = null)
// First load the tables (important that this happen before filling out details of tables)
$tables = array();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
- $name = $row[0];
+ $name = $this->cleanDelimitedIdentifiers($row[0]);
if ($name == $this->getMigrationTable()) {
continue;
}
@@ -123,7 +122,7 @@ protected function addColumns(Table $table)
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $name = $row['COLUMN_NAME'];
+ $name = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
$type = $row['TYPE_NAME'];
$size = $row['LENGTH'];
$isNullable = $row['NULLABLE'];
@@ -175,9 +174,9 @@ protected function addForeignKeys(Table $table)
$foreignKeys = array(); // local store to avoid duplicates
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $lcol = $row['COLUMN_NAME'];
- $ftbl = $row['FK_TABLE_NAME'];
- $fcol = $row['FK_COLUMN_NAME'];
+ $lcol = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
+ $ftbl = $this->cleanDelimitedIdentifiers($row['FK_TABLE_NAME']);
+ $fcol = $this->cleanDelimitedIdentifiers($row['FK_COLUMN_NAME']);
$foreignTable = $database->getTable($ftbl);
$foreignColumn = $foreignTable->getColumn($fcol);
@@ -206,8 +205,8 @@ protected function addIndexes(Table $table)
$indexes = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $colName = $row["COLUMN_NAME"];
- $name = $row['INDEX_NAME'];
+ $colName = $this->cleanDelimitedIdentifiers($row["COLUMN_NAME"]);
+ $name = $this->cleanDelimitedIdentifiers($row['INDEX_NAME']);
// FIXME -- Add UNIQUE support
if (!isset($indexes[$name])) {
@@ -234,9 +233,22 @@ protected function addPrimaryKey(Table $table)
// Loop through the returned results, grouping the same key_name together
// adding each column for that key.
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
- $name = $row[0];
+ $name = $this->cleanDelimitedIdentifiers($row[0]);
$table->getColumn($name)->setPrimaryKey(true);
}
}
+
+ /**
+ * according to the identifier definition, we have to clean simple quote (') around the identifier name
+ * returns by mssql
+ * @see http://msdn.microsoft.com/library/ms175874.aspx
+ *
+ * @param string $identifier
+ * @return string
+ */
+ protected function cleanDelimitedIdentifiers($identifier)
+ {
+ return preg_replace('/^\'(.*)\'$/U', '$1', $identifier);
+ }
}
View
68 tests/Propel/Tests/Generator/Reverse/MssqlSchemaParserTest.php
@@ -0,0 +1,68 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+namespace Propel\Tests\Generator\Reverse;
+
+use Propel\Generator\Reverse\MssqlSchemaParser;
+
+/**
+ * Tests for Mssql database schema parser.
+ *
+ * @author Pierre Tachoire
+ */
+class MssqlSchemaParserTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCleanDelimitedIdentifiers()
+ {
+ $parser = new TestableMssqlSchemaParser(null);
+
+ $expected = 'this is a tablename';
+
+ $tested = $parser->cleanDelimitedIdentifiers('\''.$expected.'\'');
+ $this->assertEquals($expected, $tested);
+
+ $tested = $parser->cleanDelimitedIdentifiers('\''.$expected);
+ $this->assertEquals('\''.$expected, $tested);
+
+ $tested = $parser->cleanDelimitedIdentifiers($expected.'\'');
+ $this->assertEquals($expected.'\'', $tested);
+
+ $expected = 'this is a tabl\'ename';
+
+ $tested = $parser->cleanDelimitedIdentifiers('\''.$expected.'\'');
+ $this->assertEquals($expected, $tested);
+
+ $tested = $parser->cleanDelimitedIdentifiers('\''.$expected);
+ $this->assertEquals('\''.$expected, $tested);
+
+ $tested = $parser->cleanDelimitedIdentifiers($expected.'\'');
+ $this->assertEquals($expected.'\'', $tested);
+
+ $expected = 'this is a\'tabl\'ename';
+
+ $tested = $parser->cleanDelimitedIdentifiers('\''.$expected.'\'');
+ $this->assertEquals($expected, $tested);
+
+ $tested = $parser->cleanDelimitedIdentifiers('\''.$expected);
+ $this->assertEquals('\''.$expected, $tested);
+
+ $tested = $parser->cleanDelimitedIdentifiers($expected.'\'');
+ $this->assertEquals($expected.'\'', $tested);
+
+ }
+}
+
+class TestableMssqlSchemaParser extends MssqlSchemaParser
+{
+ public function cleanDelimitedIdentifiers($identifier)
+ {
+ return parent::cleanDelimitedIdentifiers($identifier);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.