diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index aa6311be3..215542628 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -107,6 +107,25 @@ class DboSqlite extends DboSource { 'boolean' => array('name' => 'boolean') ); +/** + * List of engine specific additional field parameters used on table creating + * + * @var array + * @access public + */ + var $fieldParameters = array( + 'collate' => array( + 'value' => 'COLLATE', + 'quote' => false, + 'join' => ' ', + 'column' => 'Collate', + 'position' => 'afterDefault', + 'options' => array( + 'BINARY', 'NOCASE', 'RTRIM' + ) + ), + ); + /** * Connects to the database using config['database'] as a filename. * @@ -481,32 +500,7 @@ class DboSqlite extends DboSource { if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') { return $this->name($name) . ' ' . $this->columns['primary_key']['name']; } - if (isset($real['limit']) || isset($real['length']) || isset($column['limit']) || isset($column['length'])) { - if (isset($column['length'])) { - $length = $column['length']; - } elseif (isset($column['limit'])) { - $length = $column['limit']; - } elseif (isset($real['length'])) { - $length = $real['length']; - } else { - $length = $real['limit']; - } - $out .= '(' . $length . ')'; - } - if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') { - $out .= ' ' . $this->columns['primary_key']['name']; - } elseif (isset($column['key']) && $column['key'] == 'primary') { - $out .= ' NOT NULL'; - } elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) { - $out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL'; - } elseif (isset($column['default'])) { - $out .= ' DEFAULT ' . $this->value($column['default'], $type); - } elseif (isset($column['null']) && $column['null'] == true) { - $out .= ' DEFAULT NULL'; - } elseif (isset($column['null']) && $column['null'] == false) { - $out .= ' NOT NULL'; - } - return $out; + return parent::buildColumn($column); } /** diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 141364da8..5f2ac8de8 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -2459,16 +2459,7 @@ class DboSource extends DataSource { if (($column['type'] == 'integer' || $column['type'] == 'float' ) && isset($column['default']) && $column['default'] === '') { $column['default'] = null; } - - foreach ($this->fieldParameters as $paramName => $value) { - if (isset($column[$paramName]) && $value['position'] == 'beforeDefault') { - $val = $column[$paramName]; - if ($value['quote']) { - $val = $this->value($val); - } - $out .= ' ' . $value['value'] . $value['join'] . $val; - } - } + $out = $this->_buildFieldParameters($out, $column, 'beforeDefault'); if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') { $out .= ' ' . $this->columns['primary_key']['name']; @@ -2483,18 +2474,32 @@ class DboSource extends DataSource { } elseif (isset($column['null']) && $column['null'] == false) { $out .= ' NOT NULL'; } + $out = $this->_buildFieldParameters($out, $column, 'afterDefault'); + return $out; + } +/** + * Build the field parameters, in a position + * + * @param string $columnString The partially built column string + * @param array $columnData The array of column data. + * @param string $position The position type to use. 'beforeDefault' or 'afterDefault' are common + * @return string a built column with the field parameters added. + **/ + function _buildFieldParameters($columnString, $columnData, $position) { foreach ($this->fieldParameters as $paramName => $value) { - if (isset($column[$paramName]) && $value['position'] == 'afterDefault') { - $val = $column[$paramName]; + if (isset($columnData[$paramName]) && $value['position'] == $position) { + if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'])) { + continue; + } + $val = $columnData[$paramName]; if ($value['quote']) { $val = $this->value($val); } - $out .= ' ' . $value['value'] . $value['join'] . $val; + $columnString .= ' ' . $value['value'] . $value['join'] . $val; } } - - return $out; + return $columnString; } /** diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php index 75acb73e6..51ede159b 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php @@ -222,6 +222,78 @@ class DboSqliteTest extends CakeTestCase { Configure::write('Cache.disable', true); } +/** + * test building columns with SQLite + * + * @return void + **/ + function testBuildColumn() { + $data = array( + 'name' => 'int_field', + 'type' => 'integer', + 'null' => false, + ); + $result = $this->db->buildColumn($data); + $expected = '"int_field" integer(11) NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'name', + 'type' => 'string', + 'length' => 20, + 'null' => false, + ); + $result = $this->db->buildColumn($data); + $expected = '"name" varchar(20) NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'testName', + 'type' => 'string', + 'length' => 20, + 'default' => null, + 'null' => true, + 'collate' => 'NOCASE' + ); + $result = $this->db->buildColumn($data); + $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'testName', + 'type' => 'string', + 'length' => 20, + 'default' => 'test-value', + 'null' => false, + ); + $result = $this->db->buildColumn($data); + $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'testName', + 'type' => 'integer', + 'length' => 10, + 'default' => 10, + 'null' => false, + ); + $result = $this->db->buildColumn($data); + $expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'testName', + 'type' => 'integer', + 'length' => 10, + 'default' => 10, + 'null' => false, + 'collate' => 'BADVALUE' + ); + $result = $this->db->buildColumn($data); + $expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL'; + $this->assertEqual($result, $expected); + } + /** * test describe() and normal results. * diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index fa98837ae..c35a8314a 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -3591,16 +3591,16 @@ class DboSourceTest extends CakeTestCase { ); $this->testDb->buildColumn($data); - $this->testDb->columns = array('varchar(255)' => 1); $data = array( 'name' => 'testName', - 'type' => 'varchar(255)', + 'type' => 'string', + 'length' => 255, 'default', 'null' => true, 'key' ); $result = $this->testDb->buildColumn($data); - $expected = '`testName` DEFAULT NULL'; + $expected = '`testName` varchar(255) DEFAULT NULL'; $this->assertEqual($result, $expected); $data = array( @@ -3612,7 +3612,37 @@ class DboSourceTest extends CakeTestCase { $this->testDb->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), ); $result = $this->testDb->buildColumn($data); $expected = '`int_field` int(11) NOT NULL'; - $this->assertTrue($result, $expected); + $this->assertEqual($result, $expected); + + $this->testDb->fieldParameters['param'] = array( + 'value' => 'COLLATE', + 'quote' => false, + 'join' => ' ', + 'column' => 'Collate', + 'position' => 'beforeDefault', + 'options' => array('GOOD', 'OK') + ); + $data = array( + 'name' => 'int_field', + 'type' => 'integer', + 'default' => '', + 'null' => false, + 'param' => 'BAD' + ); + $result = $this->testDb->buildColumn($data); + $expected = '`int_field` int(11) NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'int_field', + 'type' => 'integer', + 'default' => '', + 'null' => false, + 'param' => 'GOOD' + ); + $result = $this->testDb->buildColumn($data); + $expected = '`int_field` int(11) COLLATE GOOD NOT NULL'; + $this->assertEqual($result, $expected); } /** @@ -3623,11 +3653,11 @@ class DboSourceTest extends CakeTestCase { function testHasAny() { $this->testDb->hasAny($this->Model, array()); $expected = 'SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1'; - $this->assertEqual($this->testDb->simulated[1], $expected); + $this->assertEqual(end($this->testDb->simulated), $expected); $this->testDb->hasAny($this->Model, array('TestModel.name' => 'harry')); $expected = "SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = 'harry'"; - $this->assertEqual($this->testDb->simulated[2], $expected); + $this->assertEqual(end($this->testDb->simulated), $expected); } /**