Fixing primary key column length in DboSqlite. UUID primary keys now return defined length.

Test cases added for describe()
Fixes #6412

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8184 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2009-06-02 03:03:51 +00:00
parent 2842fe7021
commit a9bbae31ee
2 changed files with 102 additions and 37 deletions

View file

@ -88,7 +88,7 @@ class DboSqlite extends DboSource {
'primary_key' => array('name' => 'integer primary key'), 'primary_key' => array('name' => 'integer primary key'),
'string' => array('name' => 'varchar', 'limit' => '255'), 'string' => array('name' => 'varchar', 'limit' => '255'),
'text' => array('name' => 'text'), 'text' => array('name' => 'text'),
'integer' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'), 'integer' => array('name' => 'integer', 'limit' => 11, 'formatter' => 'intval'),
'float' => array('name' => 'float', 'formatter' => 'floatval'), 'float' => array('name' => 'float', 'formatter' => 'floatval'),
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
@ -190,18 +190,19 @@ class DboSqlite extends DboSource {
foreach ($result as $column) { foreach ($result as $column) {
$fields[$column[0]['name']] = array( $fields[$column[0]['name']] = array(
'type' => $this->column($column[0]['type']), 'type' => $this->column($column[0]['type']),
'null' => !$column[0]['notnull'], 'null' => !$column[0]['notnull'],
'default' => $column[0]['dflt_value'], 'default' => $column[0]['dflt_value'],
'length' => $this->length($column[0]['type']) 'length' => $this->length($column[0]['type'])
); );
if ($column[0]['pk'] == 1) { if ($column[0]['pk'] == 1) {
$colLength = $this->length($column[0]['type']);
$fields[$column[0]['name']] = array( $fields[$column[0]['name']] = array(
'type' => $fields[$column[0]['name']]['type'], 'type' => $fields[$column[0]['name']]['type'],
'null' => false, 'null' => false,
'default' => $column[0]['dflt_value'], 'default' => $column[0]['dflt_value'],
'key' => $this->index['PRI'], 'key' => $this->index['PRI'],
'length' => 11 'length'=> ($colLength != null) ? $colLength : 11
); );
} }
} }
@ -449,34 +450,34 @@ class DboSqlite extends DboSource {
} }
$real = $this->columns[$type]; $real = $this->columns[$type];
if (isset($column['key']) && $column['key'] == 'primary') { $out = $this->name($name) . ' ' . $real['name'];
$out = $this->name($name) . ' ' . $this->columns['primary_key']['name']; if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') {
} else { return $this->name($name) . ' ' . $this->columns['primary_key']['name'];
$out = $this->name($name) . ' ' . $real['name']; }
if (isset($real['limit']) || isset($real['length']) || isset($column['limit']) || isset($column['length'])) {
if (isset($real['limit']) || isset($real['length']) || isset($column['limit']) || isset($column['length'])) { if (isset($column['length'])) {
if (isset($column['length'])) { $length = $column['length'];
$length = $column['length']; } elseif (isset($column['limit'])) {
} elseif (isset($column['limit'])) { $length = $column['limit'];
$length = $column['limit']; } elseif (isset($real['length'])) {
} elseif (isset($real['length'])) { $length = $real['length'];
$length = $real['length']; } else {
} else { $length = $real['limit'];
$length = $real['limit'];
}
$out .= '(' . $length . ')';
}
if (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';
} }
$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 $out;
} }

View file

@ -204,5 +204,69 @@ class DboSqliteTest extends CakeTestCase {
Cache::delete($fileName, '_cake_model_'); Cache::delete($fileName, '_cake_model_');
Configure::write('Cache.disable', true); Configure::write('Cache.disable', true);
} }
/**
* test describe() and normal results.
*
* @return void
**/
function testDescribe() {
$Model =& new Model(array('name' => 'User', 'ds' => 'test_suite', 'table' => 'users'));
$result = $this->db->describe($Model);
$expected = array(
'id' => array(
'type' => 'integer',
'key' => 'primary',
'null' => false,
'default' => null,
'length' => 11
),
'user' => array(
'type' => 'string',
'length' => 255,
'null' => false,
'default' => null
),
'password' => array(
'type' => 'string',
'length' => 255,
'null' => false,
'default' => null
),
'created' => array(
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null,
),
'updated' => array(
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null,
)
);
$this->assertEqual($result, $expected);
}
/**
* test that describe does not corrupt UUID primary keys
*
* @return void
**/
function testDescribeWithUuidPrimaryKey() {
$tableName = 'uuid_tests';
$this->db->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
$Model =& new Model(array('name' => 'UuidTest', 'ds' => 'test_suite', 'table' => 'uuid_tests'));
$result = $this->db->describe($Model);
$expected = array(
'type' => 'string',
'length' => 36,
'null' => false,
'default' => null,
'key' => 'primary',
);
$this->assertEqual($result['id'], $expected);
$this->db->query('DROP TABLE ' . $tableName);
}
} }
?> ?>