Skip to content
This repository
Browse code

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...
commit ee8d12c939040a033c4db50f4689a210ce7add1c 1 parent f710ae7
Renan Gonçalves authored July 29, 2009
22  cake/libs/model/datasources/dbo/dbo_mssql.php
@@ -211,9 +211,10 @@ function describe(&$model) {
211 211
 			return $cache;
212 212
 		}
213 213
 
214  
-		$fields = false;
215  
-		$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);
  214
+		$table = $this->fullTableName($model, false);
  215
+		$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);
216 216
 
  217
+		$fields = false;
217 218
 		foreach ($cols as $column) {
218 219
 			$field = $column[0]['Field'];
219 220
 			$fields[$field] = array(
@@ -659,23 +660,11 @@ function insertMulti($table, $fields, $values) {
659 660
  * Generate a database-native column schema string
660 661
  *
661 662
  * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
662  
- *                      where options can be 'default', 'length', or 'key'.
  663
+ *   where options can be 'default', 'length', or 'key'.
663 664
  * @return string
664 665
  */
665 666
 	function buildColumn($column) {
666  
-		$result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
667  
-		$null = (
668  
-			(isset($column['null']) && $column['null'] == true) ||
669  
-			(array_key_exists('default', $column) && $column['default'] === null) ||
670  
-			(array_keys($column) == array('type', 'name'))
671  
-		);
672  
-		$primaryKey = (isset($column['key']) && $column['key'] == 'primary');
673  
-		$stringKey =  ($primaryKey && $column['type'] != 'integer');
674  
-
675  
-		if ($null && !$primaryKey) {
676  
-			$result .= " NULL";
677  
-		}
678  
-		return $result;
  667
+		return preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
679 668
 	}
680 669
 /**
681 670
  * Format indexes for create table
@@ -723,7 +712,6 @@ function _getPrimaryKey($model) {
723 712
 				return $field;
724 713
 			}
725 714
 		}
726  
-
727 715
 		return null;
728 716
 	}
729 717
 }
6  cake/tests/cases/libs/cake_test_fixture.test.php
@@ -60,9 +60,9 @@ class CakeTestFixtureTestFixture extends CakeTestFixture {
60 60
  * @var array
61 61
  */
62 62
 	var $records = array(
63  
-		array('name' => 'Gandalf'),
64  
-		array('name' => 'Captain Picard'),
65  
-		array('name' => 'Chewbacca')
  63
+		array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'),
  64
+		array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'),
  65
+		array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00')
66 66
 	);
67 67
 }
68 68
 /**
109  cake/tests/cases/libs/model/datasources/dbo/dbo_mssql.test.php
@@ -44,6 +44,13 @@ class DboMssqlTestDb extends DboMssql {
44 44
  */
45 45
 	var $simulated = array();
46 46
 /**
  47
+ * simalate property
  48
+ *
  49
+ * @var array
  50
+ * @access public
  51
+ */
  52
+	var $simulate = true;
  53
+/**
47 54
  * fetchAllResultsStack
48 55
  *
49 56
  * @var array
@@ -58,8 +65,12 @@ class DboMssqlTestDb extends DboMssql {
58 65
  * @return void
59 66
  */
60 67
 	function _execute($sql) {
61  
-		$this->simulated[] = $sql;
62  
-		return null;
  68
+		if ($this->simulate) {
  69
+			$this->simulated[] = $sql;
  70
+			return null;
  71
+		} else {
  72
+			return parent::_execute($sql);
  73
+		}
63 74
 	}
64 75
 /**
65 76
  * fetchAll method
@@ -204,6 +215,20 @@ class DboMssqlTest extends CakeTestCase {
204 215
  */
205 216
 	var $db = null;
206 217
 /**
  218
+ * autoFixtures property
  219
+ *
  220
+ * @var bool false
  221
+ * @access public
  222
+ */
  223
+	var $autoFixtures = false;
  224
+/**
  225
+ * fixtures property
  226
+ *
  227
+ * @var array
  228
+ * @access public
  229
+ */
  230
+	var $fixtures = array('core.category');
  231
+/**
207 232
  * Skip if cannot connect to mssql
208 233
  *
209 234
  * @access public
@@ -213,6 +238,26 @@ function skip() {
213 238
 		$this->skipUnless($this->db->config['driver'] == 'mssql', '%s SQL Server connection not available');
214 239
 	}
215 240
 /**
  241
+ * Make sure all fixtures tables are being created
  242
+ *
  243
+ * @access public
  244
+ */
  245
+	function start() {
  246
+		$this->db->simulate = false;
  247
+		parent::start();
  248
+		$this->db->simulate = true;
  249
+	}
  250
+/**
  251
+ * Make sure all fixtures tables are being dropped
  252
+ *
  253
+ * @access public
  254
+ */
  255
+	function end() {
  256
+		$this->db->simulate = false;
  257
+		parent::end();
  258
+		$this->db->simulate = true;
  259
+	}
  260
+/**
216 261
  * Sets up a Dbo class instance for testing
217 262
  *
218 263
  * @access public
@@ -331,6 +376,59 @@ function testDescribe() {
331 376
 		$this->assertEqual($result, $expected);
332 377
 	}
333 378
 /**
  379
+ * testBuildColumn
  380
+ *
  381
+ * @return unknown_type
  382
+ * @access public
  383
+ */
  384
+	function testBuildColumn() {
  385
+		$column = array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary');
  386
+		$result = $this->db->buildColumn($column);
  387
+		$expected = '[id] int IDENTITY (1, 1) NOT NULL';
  388
+		$this->assertEqual($result, $expected);
  389
+
  390
+		$column = array('name' => 'client_id', 'type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11');
  391
+		$result = $this->db->buildColumn($column);
  392
+		$expected = '[client_id] int DEFAULT 0 NOT NULL';
  393
+		$this->assertEqual($result, $expected);
  394
+
  395
+		// 'name' => 'type' format
  396
+		$column = array('name' => 'client_id', 'type' => 'integer');
  397
+		$result = $this->db->buildColumn($column);
  398
+		$expected = '[client_id] int';
  399
+		$this->assertEqual($result, $expected);
  400
+
  401
+		$column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
  402
+		$result = $this->db->buildColumn($column);
  403
+		$expected = '[client_id] int DEFAULT NULL';
  404
+		$this->assertEqual($result, $expected);
  405
+
  406
+		$column = array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255');
  407
+		$result = $this->db->buildColumn($column);
  408
+		$expected = '[name] varchar(255) DEFAULT \'\' NOT NULL';
  409
+		$this->assertEqual($result, $expected);
  410
+
  411
+		$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
  412
+		$result = $this->db->buildColumn($column);
  413
+		$expected = '[name] varchar(255) NOT NULL';
  414
+		$this->assertEqual($result, $expected);
  415
+
  416
+		$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
  417
+		$result = $this->db->buildColumn($column);
  418
+		$expected = '[name] varchar(255) NOT NULL';
  419
+		$this->assertEqual($result, $expected);
  420
+
  421
+		$column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
  422
+		$result = $this->db->buildColumn($column);
  423
+		$expected = '[name] varchar(255) DEFAULT NULL';
  424
+		$this->assertEqual($result, $expected);
  425
+
  426
+		$column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
  427
+		$result = $this->db->buildColumn($column);
  428
+		$expected = '[name] varchar(255) DEFAULT \'\'';
  429
+		$this->assertEqual($result, $expected);
  430
+	}
  431
+/**
334 432
  * testUpdateAllSyntax method
335 433
  *
336 434
  * @return void
@@ -353,6 +451,7 @@ function testUpdateAllSyntax() {
353 451
  * @access public
354 452
  */
355 453
 	function testGetPrimaryKey() {
  454
+		// When param is a model
356 455
 		$result = $this->db->getPrimaryKey($this->model);
357 456
 		$this->assertEqual($result, 'id');
358 457
 
@@ -361,6 +460,12 @@ function testGetPrimaryKey() {
361 460
 		$this->model->setSchema($schema);
362 461
 		$result = $this->db->getPrimaryKey($this->model);
363 462
 		$this->assertNull($result);
  463
+
  464
+		// When param is a table name
  465
+		$this->db->simulate = false;
  466
+		$this->loadFixtures('Category');
  467
+		$result = $this->db->getPrimaryKey('categories');
  468
+		$this->assertEqual($result, 'id');
364 469
 	}
365 470
 /**
366 471
  * testInsertMulti
3  cake/tests/cases/libs/model/datasources/dbo_source.test.php
@@ -1211,6 +1211,8 @@ function getLastQuery() {
1211 1211
 
1212 1212
 		$this->testDb =& new DboTest($this->__config);
1213 1213
 		$this->testDb->cacheSources = false;
  1214
+		$this->testDb->startQuote = '`';
  1215
+		$this->testDb->endQuote = '`';
1214 1216
 		Configure::write('debug', 1);
1215 1217
 		$this->debug = Configure::read('debug');
1216 1218
 		$this->Model =& new TestModel();
@@ -1235,6 +1237,7 @@ function endTest() {
1235 1237
 	function testFieldDoubleEscaping() {
1236 1238
 		$config = array_merge($this->__config, array('driver' => 'test'));
1237 1239
 		$test =& ConnectionManager::create('quoteTest', $config);
  1240
+		$test->simulated = array();
1238 1241
 
1239 1242
 		$this->Model =& new Article2(array('alias' => 'Article', 'ds' => 'quoteTest'));
1240 1243
 		$this->Model->setDataSource('quoteTest');

0 notes on commit ee8d12c

Please sign in to comment.
Something went wrong with that request. Please try again.