Skip to content

Commit

Permalink
Fixing all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jan 14, 2014
1 parent 4e2367c commit 1769c5c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 51 deletions.
8 changes: 4 additions & 4 deletions Cake/Database/Driver/Sqlserver.php
Expand Up @@ -31,19 +31,19 @@ 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' => [],
'dsn' => null
];

/**
* Establishes a connection to the databse server
* Establishes a connection to the database server
*
* @return boolean true on success
*/
Expand All @@ -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);
Expand Down
36 changes: 17 additions & 19 deletions Cake/Database/Schema/SqlserverSchema.php
Expand Up @@ -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]];
}

/**
Expand Down Expand Up @@ -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') {
Expand All @@ -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 ||
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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],
Expand All @@ -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'];
Expand All @@ -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;
}
Expand Down Expand Up @@ -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']),
];
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions Cake/Test/TestCase/Database/Driver/SqlserverTest.php
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
38 changes: 13 additions & 25 deletions Cake/Test/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -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 = <<<SQL
CREATE TABLE schema_authors (
Expand All @@ -63,13 +63,13 @@ protected function _createTables($connection) {
CREATE TABLE schema_articles (
id BIGINT PRIMARY KEY,
title VARCHAR(20),
body TEXT,
body VARCHAR(1000),
author_id INTEGER NOT NULL,
published BOOLEAN DEFAULT false,
published BIT DEFAULT 0,
views SMALLINT DEFAULT 0,
created DATETIME,
CONSTRAINT [content_idx] UNIQUE ([title], [body]),
CONSTRAINT [author_idx] FOREIGN KEY ([author_id]) REFERENCES [schema_authors] ([id]) ON DELETE SET DEFAULT ON UPDATE CASCADE
CONSTRAINT [author_idx] FOREIGN KEY ([author_id]) REFERENCES [schema_authors] ([id]) ON DELETE CASCADE ON UPDATE CASCADE
)
SQL;
$connection->execute($table);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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' => [
Expand All @@ -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'));
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
[
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Expand Up @@ -46,7 +46,7 @@
-->
<!-- SQL Server
<env name="db_class" value="Cake\Database\Driver\Sqlserver"/>
<env name="db_dsn" value="sqlsrv:server=localhost\SQLEXPRESS;Database=cake_test"/>
<env name="db_dsn" value="sqlsrv:Server=localhost;Database=cake_test"/>
<env name="db_login" value=""/>
<env name="db_password" value=""/>
-->
Expand Down

0 comments on commit 1769c5c

Please sign in to comment.