Merge branch 'sqlite-tableparams' into 1.3-misc

This commit is contained in:
mark_story 2009-10-29 09:50:45 -04:00
commit 9446f7dd38
4 changed files with 148 additions and 47 deletions

View file

@ -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);
}
/**

View file

@ -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;
}
/**

View file

@ -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.
*

View file

@ -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);
}
/**