Browse files

Fixing issue with "DEFAULT NULL NULL" generated by buildColumn.

Making the tests pass by just changing the start and end quotes.
Providing "created" values for CakeTestFixtureTestFixture Fixture.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8262 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent f710ae7 commit ee8d12c939040a033c4db50f4689a210ce7add1c @renan renan committed Jul 29, 2009
View
22 cake/libs/model/datasources/dbo/dbo_mssql.php
@@ -211,9 +211,10 @@ function describe(&$model) {
return $cache;
}
- $fields = false;
- $cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $this->fullTableName($model, false) . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $this->fullTableName($model, false) . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $this->fullTableName($model, false) . "'", false);
+ $table = $this->fullTableName($model, false);
+ $cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $table . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $table . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $table . "'", false);
+ $fields = false;
foreach ($cols as $column) {
$field = $column[0]['Field'];
$fields[$field] = array(
@@ -659,23 +660,11 @@ function insertMulti($table, $fields, $values) {
* Generate a database-native column schema string
*
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
- * where options can be 'default', 'length', or 'key'.
+ * where options can be 'default', 'length', or 'key'.
* @return string
*/
function buildColumn($column) {
- $result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
- $null = (
- (isset($column['null']) && $column['null'] == true) ||
- (array_key_exists('default', $column) && $column['default'] === null) ||
- (array_keys($column) == array('type', 'name'))
- );
- $primaryKey = (isset($column['key']) && $column['key'] == 'primary');
- $stringKey = ($primaryKey && $column['type'] != 'integer');
-
- if ($null && !$primaryKey) {
- $result .= " NULL";
- }
- return $result;
+ return preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
}
/**
* Format indexes for create table
@@ -723,7 +712,6 @@ function _getPrimaryKey($model) {
return $field;
}
}
-
return null;
}
}
View
6 cake/tests/cases/libs/cake_test_fixture.test.php
@@ -60,9 +60,9 @@ class CakeTestFixtureTestFixture extends CakeTestFixture {
* @var array
*/
var $records = array(
- array('name' => 'Gandalf'),
- array('name' => 'Captain Picard'),
- array('name' => 'Chewbacca')
+ array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'),
+ array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'),
+ array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00')
);
}
/**
View
109 cake/tests/cases/libs/model/datasources/dbo/dbo_mssql.test.php
@@ -44,6 +44,13 @@ class DboMssqlTestDb extends DboMssql {
*/
var $simulated = array();
/**
+ * simalate property
+ *
+ * @var array
+ * @access public
+ */
+ var $simulate = true;
+/**
* fetchAllResultsStack
*
* @var array
@@ -58,8 +65,12 @@ class DboMssqlTestDb extends DboMssql {
* @return void
*/
function _execute($sql) {
- $this->simulated[] = $sql;
- return null;
+ if ($this->simulate) {
+ $this->simulated[] = $sql;
+ return null;
+ } else {
+ return parent::_execute($sql);
+ }
}
/**
* fetchAll method
@@ -204,6 +215,20 @@ class DboMssqlTest extends CakeTestCase {
*/
var $db = null;
/**
+ * autoFixtures property
+ *
+ * @var bool false
+ * @access public
+ */
+ var $autoFixtures = false;
+/**
+ * fixtures property
+ *
+ * @var array
+ * @access public
+ */
+ var $fixtures = array('core.category');
+/**
* Skip if cannot connect to mssql
*
* @access public
@@ -213,6 +238,26 @@ function skip() {
$this->skipUnless($this->db->config['driver'] == 'mssql', '%s SQL Server connection not available');
}
/**
+ * Make sure all fixtures tables are being created
+ *
+ * @access public
+ */
+ function start() {
+ $this->db->simulate = false;
+ parent::start();
+ $this->db->simulate = true;
+ }
+/**
+ * Make sure all fixtures tables are being dropped
+ *
+ * @access public
+ */
+ function end() {
+ $this->db->simulate = false;
+ parent::end();
+ $this->db->simulate = true;
+ }
+/**
* Sets up a Dbo class instance for testing
*
* @access public
@@ -331,6 +376,59 @@ function testDescribe() {
$this->assertEqual($result, $expected);
}
/**
+ * testBuildColumn
+ *
+ * @return unknown_type
+ * @access public
+ */
+ function testBuildColumn() {
+ $column = array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary');
+ $result = $this->db->buildColumn($column);
+ $expected = '[id] int IDENTITY (1, 1) NOT NULL';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'client_id', 'type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11');
+ $result = $this->db->buildColumn($column);
+ $expected = '[client_id] int DEFAULT 0 NOT NULL';
+ $this->assertEqual($result, $expected);
+
+ // 'name' => 'type' format
+ $column = array('name' => 'client_id', 'type' => 'integer');
+ $result = $this->db->buildColumn($column);
+ $expected = '[client_id] int';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
+ $result = $this->db->buildColumn($column);
+ $expected = '[client_id] int DEFAULT NULL';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255');
+ $result = $this->db->buildColumn($column);
+ $expected = '[name] varchar(255) DEFAULT \'\' NOT NULL';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
+ $result = $this->db->buildColumn($column);
+ $expected = '[name] varchar(255) NOT NULL';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
+ $result = $this->db->buildColumn($column);
+ $expected = '[name] varchar(255) NOT NULL';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
+ $result = $this->db->buildColumn($column);
+ $expected = '[name] varchar(255) DEFAULT NULL';
+ $this->assertEqual($result, $expected);
+
+ $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
+ $result = $this->db->buildColumn($column);
+ $expected = '[name] varchar(255) DEFAULT \'\'';
+ $this->assertEqual($result, $expected);
+ }
+/**
* testUpdateAllSyntax method
*
* @return void
@@ -353,6 +451,7 @@ function testUpdateAllSyntax() {
* @access public
*/
function testGetPrimaryKey() {
+ // When param is a model
$result = $this->db->getPrimaryKey($this->model);
$this->assertEqual($result, 'id');
@@ -361,6 +460,12 @@ function testGetPrimaryKey() {
$this->model->setSchema($schema);
$result = $this->db->getPrimaryKey($this->model);
$this->assertNull($result);
+
+ // When param is a table name
+ $this->db->simulate = false;
+ $this->loadFixtures('Category');
+ $result = $this->db->getPrimaryKey('categories');
+ $this->assertEqual($result, 'id');
}
/**
* testInsertMulti
View
3 cake/tests/cases/libs/model/datasources/dbo_source.test.php
@@ -1211,6 +1211,8 @@ function getLastQuery() {
$this->testDb =& new DboTest($this->__config);
$this->testDb->cacheSources = false;
+ $this->testDb->startQuote = '`';
+ $this->testDb->endQuote = '`';
Configure::write('debug', 1);
$this->debug = Configure::read('debug');
$this->Model =& new TestModel();
@@ -1235,6 +1237,7 @@ function endTest() {
function testFieldDoubleEscaping() {
$config = array_merge($this->__config, array('driver' => 'test'));
$test =& ConnectionManager::create('quoteTest', $config);
+ $test->simulated = array();
$this->Model =& new Article2(array('alias' => 'Article', 'ds' => 'quoteTest'));
$this->Model->setDataSource('quoteTest');

0 comments on commit ee8d12c

Please sign in to comment.