From 1769c5ce9d6a66e346563ed03e9cb7ba9b06f49e Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sat, 9 Nov 2013 21:58:40 -0500 Subject: [PATCH] Fixing all the things --- Cake/Database/Driver/Sqlserver.php | 8 ++-- Cake/Database/Schema/SqlserverSchema.php | 36 +++++++++--------- .../Database/Driver/SqlserverTest.php | 4 +- .../Database/Schema/SqlserverSchemaTest.php | 38 +++++++------------ phpunit.xml.dist | 2 +- 5 files changed, 37 insertions(+), 51 deletions(-) diff --git a/Cake/Database/Driver/Sqlserver.php b/Cake/Database/Driver/Sqlserver.php index f7d5caa3548..167623da73a 100644 --- a/Cake/Database/Driver/Sqlserver.php +++ b/Cake/Database/Driver/Sqlserver.php @@ -31,11 +31,11 @@ class Sqlserver extends \Cake\Database\Driver { */ protected $_baseConfig = [ 'persistent' => true, - 'host' => 'localhost\SQLEXPRESS', + 'host' => 'localhost', 'login' => '', 'password' => '', 'database' => 'cake', - 'encoding' => 'utf8', + 'encoding' => PDO::SQLSRV_ENCODING_UTF8, 'flags' => [], 'init' => [], 'settings' => [], @@ -43,7 +43,7 @@ class Sqlserver extends \Cake\Database\Driver { ]; /** - * Establishes a connection to the databse server + * Establishes a connection to the database server * * @return boolean true on success */ @@ -61,7 +61,7 @@ public function connect() { $config['flags'][PDO::SQLSRV_ATTR_ENCODING] = $config['encoding']; } if (empty($config['dsn'])) { - $config['dsn'] = "sqlsrv:server={$config['host']};Database={$config['database']}"; + $config['dsn'] = "sqlsrv:Server={$config['host']};Database={$config['database']}"; } $this->_connect($config); diff --git a/Cake/Database/Schema/SqlserverSchema.php b/Cake/Database/Schema/SqlserverSchema.php index ef3af581a4d..42d21d27fa7 100644 --- a/Cake/Database/Schema/SqlserverSchema.php +++ b/Cake/Database/Schema/SqlserverSchema.php @@ -30,7 +30,7 @@ class SqlserverSchema extends BaseSchema { */ public function listTablesSql($config) { $schema = empty($config['schema']) ? 'dbo' : $config['schema']; - return ['SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?', [$schema]]; + return ['SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME', [$schema]]; } /** @@ -81,7 +81,7 @@ protected function _convertColumn($column) { return ['type' => 'datetime', 'length' => null]; } - if ($col === 'int') { + if ($col === 'int' || $col === 'integer') { return ['type' => 'integer', 'length' => 10]; } if ($col === 'bigint') { @@ -94,7 +94,7 @@ protected function _convertColumn($column) { return ['type' => 'integer', 'length' => 3]; } if ($col === 'bit') { - return ['type' => 'integer', 'length' => 1]; + return ['type' => 'boolean', 'length' => null]; } if ( strpos($col, 'numeric') !== false || @@ -111,7 +111,7 @@ protected function _convertColumn($column) { return ['type' => 'string', 'length' => $length]; } if (strpos($col, 'char') !== false) { - return ['type' => 'string', 'length' => $length]; + return ['type' => 'string', 'fixed' => true, 'length' => $length]; } if (strpos($col, 'text') !== false) { return ['type' => 'text', 'length' => null]; @@ -135,20 +135,17 @@ protected function _convertColumn($column) { */ public function convertFieldDescription(Table $table, $row) { $field = $this->_convertColumn($row['type']); + if (!empty($row['default'])) { + $row['default'] = trim($row['default'], '()'); + } if ($field['type'] === 'boolean') { - if ($row['default'] === 'true') { - $row['default'] = 1; - } - if ($row['default'] === 'false') { - $row['default'] = 0; - } + $row['default'] = (int)$row['default']; } $field += [ 'null' => $row['null'] === 'YES' ? true : false, 'default' => $row['default'], - 'comment' => $row['comment'] ]; $field['length'] = $row['char_length'] ?: $field['length']; $table->addColumn($row['name'], $field); @@ -161,7 +158,7 @@ public function convertFieldDescription(Table $table, $row) { public function describeIndexSql($table, $config) { $sql = " SELECT - T.[name] AS [table_name], I.[name] AS [index_name], + I.[name] AS [index_name], IC.[index_column_id] AS [index_order], AC.[name] AS [column_name], I.[is_unique], I.[is_primary_key], @@ -171,7 +168,7 @@ public function describeIndexSql($table, $config) { INNER JOIN sys.[index_columns] IC ON I.[object_id] = IC.[object_id] AND I.[index_id] = IC.[index_id] INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id] AND IC.[column_id] = AC.[column_id] WHERE T.[is_ms_shipped] = 0 AND I.[type_desc] <> 'HEAP' AND T.[name] = ? AND OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) = ? - ORDER BY I.[index_id], IC.[index_column_id]; + ORDER BY I.[index_id], IC.[index_column_id] "; $schema = empty($config['schema']) ? 'dbo' : $config['schema']; @@ -184,7 +181,7 @@ public function describeIndexSql($table, $config) { */ public function convertIndexDescription(Table $table, $row) { $type = Table::INDEX_INDEX; - $name = $row['table_name']; + $name = $row['index_name']; if ($row['is_primary_key']) { $name = $type = Table::CONSTRAINT_PRIMARY; } @@ -246,7 +243,7 @@ public function convertForeignKeyDescription(Table $table, $row) { $data = [ 'type' => Table::CONSTRAINT_FOREIGN, 'columns' => [$row['column']], - 'references' => [$row['refernece_table'], $row['reference_column']], + 'references' => [$row['reference_table'], $row['reference_column']], 'update' => $this->_convertOnClause($row['update_type']), 'delete' => $this->_convertOnClause($row['delete_type']), ]; @@ -260,7 +257,7 @@ public function convertForeignKeyDescription(Table $table, $row) { */ protected function _foreignOnClause($on) { $parent = parent::_foreignOnClause($on); - return $parent === Table::ACTION_RESTRICT ? Table::ACTION_SET_NULL : $parent; + return $parent === 'RESTRICT' ? parent::_foreignOnClause(Table::ACTION_SET_NULL) : $parent; } /** @@ -290,7 +287,7 @@ public function columnSql(Table $table, $name) { $out = $this->_driver->quoteIdentifier($name); $typeMap = [ 'biginteger' => ' BIGINT', - 'boolean' => ' BOOLEAN', + 'boolean' => ' BIT', 'binary' => ' BINARY', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', @@ -342,8 +339,9 @@ public function columnSql(Table $table, $name) { $out .= ' DEFAULT NULL'; unset($data['default']); } - if (isset($data['default']) && $data['type'] !== 'timestamp') { - $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']); + if (isset($data['default']) && $data['type'] !== 'datetime') { + $default = is_bool($data['default']) ? (int)$data['default'] : $this->_driver->schemaValue($data['default']); + $out .= ' DEFAULT ' . $default; } return $out; } diff --git a/Cake/Test/TestCase/Database/Driver/SqlserverTest.php b/Cake/Test/TestCase/Database/Driver/SqlserverTest.php index 9364e98498e..f892be4017c 100644 --- a/Cake/Test/TestCase/Database/Driver/SqlserverTest.php +++ b/Cake/Test/TestCase/Database/Driver/SqlserverTest.php @@ -35,7 +35,7 @@ class SqlserverTest extends \Cake\TestSuite\TestCase { public function testConnectionConfigCustom() { $config = [ 'persistent' => false, - 'host' => 'foo\SQLSERVER', + 'host' => 'foo', 'login' => 'Administrator', 'password' => 'blablabla', 'database' => 'bar', @@ -51,7 +51,7 @@ public function testConnectionConfigCustom() { ); $expected = $config; - $expected['dsn'] = 'sqlsrv:server=foo\SQLSERVER;Database=bar'; + $expected['dsn'] = 'sqlsrv:Server=foo;Database=bar'; $expected['flags'] += [ PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, diff --git a/Cake/Test/TestCase/Database/Schema/SqlserverSchemaTest.php b/Cake/Test/TestCase/Database/Schema/SqlserverSchemaTest.php index 337f0a778b3..7ccaa6ff2cc 100644 --- a/Cake/Test/TestCase/Database/Schema/SqlserverSchemaTest.php +++ b/Cake/Test/TestCase/Database/Schema/SqlserverSchemaTest.php @@ -46,8 +46,8 @@ protected function _needsConnection() { protected function _createTables($connection) { $this->_needsConnection(); - $connection->execute("IF OBJECT_ID('dbo.Scores', 'U') IS NOT NULL DROP TABLE schema_articles"); - $connection->execute("IF OBJECT_ID('dbo.Scores', 'U') IS NOT NULL DROP TABLE schema_authors"); + $connection->execute("IF OBJECT_ID('schema_articles', 'U') IS NOT NULL DROP TABLE schema_articles"); + $connection->execute("IF OBJECT_ID('schema_authors', 'U') IS NOT NULL DROP TABLE schema_authors"); $table = <<execute($table); @@ -218,14 +218,14 @@ public function testDescribeTable() { 'default' => null, 'length' => 20, 'precision' => null, - 'comment' => 'a title', + 'comment' => null, 'fixed' => null, ], 'body' => [ - 'type' => 'text', + 'type' => 'string', 'null' => true, 'default' => null, - 'length' => null, + 'length' => 1000, 'precision' => null, 'fixed' => null, 'comment' => null, @@ -285,18 +285,6 @@ public function testDescribeTableIndexes() { $schema = new SchemaCollection($connection); $result = $schema->describe('schema_articles'); $this->assertInstanceOf('Cake\Database\Schema\Table', $result); - $expected = [ - 'primary' => [ - 'type' => 'primary', - 'columns' => ['id'], - 'length' => [] - ], - 'content_idx' => [ - 'type' => 'unique', - 'columns' => ['title', 'body'], - 'length' => [] - ] - ]; $this->assertCount(3, $result->constraints()); $expected = [ 'primary' => [ @@ -315,7 +303,7 @@ public function testDescribeTableIndexes() { 'references' => ['schema_authors', 'id'], 'length' => [], 'update' => 'cascade', - 'delete' => 'setDefault', + 'delete' => 'cascade', ] ]; $this->assertEquals($expected['primary'], $result->constraint('primary')); @@ -362,7 +350,7 @@ public static function columnSqlProvider() { [ 'role', ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'], - "[role] VARCHAR(10) NOT NULL DEFAULT 'admin'" + "[role] VARCHAR(10) NOT NULL DEFAULT [admin]" ], [ 'title', @@ -423,12 +411,12 @@ public static function columnSqlProvider() { [ 'checked', ['type' => 'boolean', 'default' => false], - '[checked] BOOLEAN DEFAULT FALSE' + '[checked] BIT DEFAULT 0' ], [ 'checked', ['type' => 'boolean', 'default' => true, 'null' => false], - '[checked] BOOLEAN NOT NULL DEFAULT TRUE' + '[checked] BIT NOT NULL DEFAULT 1' ], // datetimes [ @@ -503,7 +491,7 @@ public static function constraintSqlProvider() { 'author_id_idx', ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setDefault'], 'CONSTRAINT [author_id_idx] FOREIGN KEY ([author_id]) ' . - 'REFERENCES [authors] ([id]) ON UPDATE SET NULL ON DELETE SET DEFAULT' + 'REFERENCES [authors] ([id]) ON UPDATE SET DEFAULT ON DELETE SET NULL' ], [ 'author_id_idx', diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a448619218e..f3d6dfada2f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -46,7 +46,7 @@ -->