Browse Source

Fixing all the things

Juan Basso 12 years ago
parent
commit
1769c5ce9d

+ 4 - 4
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 @@ class Sqlserver extends \Cake\Database\Driver {
 			$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);

+ 17 - 19
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 @@ class SqlserverSchema extends BaseSchema {
 			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 @@ class SqlserverSchema extends BaseSchema {
 			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 @@ class SqlserverSchema extends BaseSchema {
 			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 @@ class SqlserverSchema extends BaseSchema {
  */
 	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 @@ class SqlserverSchema extends BaseSchema {
 	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 @@ class SqlserverSchema extends BaseSchema {
 			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 @@ class SqlserverSchema extends BaseSchema {
  */
 	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 @@ class SqlserverSchema extends BaseSchema {
 		$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 @@ class SqlserverSchema extends BaseSchema {
  */
 	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 @@ class SqlserverSchema extends BaseSchema {
 		$out = $this->_driver->quoteIdentifier($name);
 		$typeMap = [
 			'biginteger' => ' BIGINT',
-			'boolean' => ' BOOLEAN',
+			'boolean' => ' BIT',
 			'binary' => ' BINARY',
 			'float' => ' FLOAT',
 			'decimal' => ' DECIMAL',
@@ -342,8 +339,9 @@ class SqlserverSchema extends BaseSchema {
 			$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;
 	}

+ 2 - 2
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 @@ class SqlserverTest extends \Cake\TestSuite\TestCase {
 		);
 
 		$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,

+ 13 - 25
Cake/Test/TestCase/Database/Schema/SqlserverSchemaTest.php

@@ -46,8 +46,8 @@ class SqlserverSchemaTest extends TestCase {
 	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 (
@@ -63,13 +63,13 @@ SQL;
 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);
@@ -218,14 +218,14 @@ SQL;
 				'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 @@ SQL;
 		$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 @@ SQL;
 				'references' => ['schema_authors', 'id'],
 				'length' => [],
 				'update' => 'cascade',
-				'delete' => 'setDefault',
+				'delete' => 'cascade',
 			]
 		];
 		$this->assertEquals($expected['primary'], $result->constraint('primary'));
@@ -362,7 +350,7 @@ SQL;
 			[
 				'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 @@ SQL;
 			[
 				'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 @@ SQL;
 				'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',

+ 1 - 1
phpunit.xml.dist

@@ -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=""/>
 		-->