Skip to content

Commit

Permalink
Port PR propelorm#326 from Propel 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch authored and willdurand committed Mar 30, 2012
1 parent c5af3f1 commit cdd3699
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/Propel/Generator/Reverse/MssqlSchemaParser.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/ */
class MssqlSchemaParser extends AbstractSchemaParser class MssqlSchemaParser extends AbstractSchemaParser
{ {

/** /**
* Map MSSQL native types to Propel types. * Map MSSQL native types to Propel types.
* @var array * @var array
Expand Down Expand Up @@ -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) // First load the tables (important that this happen before filling out details of tables)
$tables = array(); $tables = array();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) { while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$name = $row[0]; $name = $this->cleanDelimitedIdentifiers($row[0]);
if ($name == $this->getMigrationTable()) { if ($name == $this->getMigrationTable()) {
continue; continue;
} }
Expand Down Expand Up @@ -123,7 +122,7 @@ protected function addColumns(Table $table)


while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {


$name = $row['COLUMN_NAME']; $name = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
$type = $row['TYPE_NAME']; $type = $row['TYPE_NAME'];
$size = $row['LENGTH']; $size = $row['LENGTH'];
$isNullable = $row['NULLABLE']; $isNullable = $row['NULLABLE'];
Expand Down Expand Up @@ -175,9 +174,9 @@ protected function addForeignKeys(Table $table)
$foreignKeys = array(); // local store to avoid duplicates $foreignKeys = array(); // local store to avoid duplicates
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {


$lcol = $row['COLUMN_NAME']; $lcol = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
$ftbl = $row['FK_TABLE_NAME']; $ftbl = $this->cleanDelimitedIdentifiers($row['FK_TABLE_NAME']);
$fcol = $row['FK_COLUMN_NAME']; $fcol = $this->cleanDelimitedIdentifiers($row['FK_COLUMN_NAME']);


$foreignTable = $database->getTable($ftbl); $foreignTable = $database->getTable($ftbl);
$foreignColumn = $foreignTable->getColumn($fcol); $foreignColumn = $foreignTable->getColumn($fcol);
Expand Down Expand Up @@ -206,8 +205,8 @@ protected function addIndexes(Table $table)


$indexes = array(); $indexes = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$colName = $row["COLUMN_NAME"]; $colName = $this->cleanDelimitedIdentifiers($row["COLUMN_NAME"]);
$name = $row['INDEX_NAME']; $name = $this->cleanDelimitedIdentifiers($row['INDEX_NAME']);


// FIXME -- Add UNIQUE support // FIXME -- Add UNIQUE support
if (!isset($indexes[$name])) { if (!isset($indexes[$name])) {
Expand All @@ -234,9 +233,22 @@ protected function addPrimaryKey(Table $table)
// Loop through the returned results, grouping the same key_name together // Loop through the returned results, grouping the same key_name together
// adding each column for that key. // adding each column for that key.
while ($row = $stmt->fetch(PDO::FETCH_NUM)) { while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$name = $row[0]; $name = $this->cleanDelimitedIdentifiers($row[0]);
$table->getColumn($name)->setPrimaryKey(true); $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);
}
} }
68 changes: 68 additions & 0 deletions tests/Propel/Tests/Generator/Reverse/MssqlSchemaParserTest.php
Original file line number Original file line Diff line number Diff line change
@@ -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);
}
}

0 comments on commit cdd3699

Please sign in to comment.