Skip to content

Commit a9bbae3

Browse files
committed
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
1 parent 2842fe7 commit a9bbae3

File tree

2 files changed

+102
-37
lines changed

2 files changed

+102
-37
lines changed

cake/libs/model/datasources/dbo/dbo_sqlite.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class DboSqlite extends DboSource {
8888
'primary_key' => array('name' => 'integer primary key'),
8989
'string' => array('name' => 'varchar', 'limit' => '255'),
9090
'text' => array('name' => 'text'),
91-
'integer' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'),
91+
'integer' => array('name' => 'integer', 'limit' => 11, 'formatter' => 'intval'),
9292
'float' => array('name' => 'float', 'formatter' => 'floatval'),
9393
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
9494
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
@@ -190,18 +190,19 @@ function describe(&$model) {
190190

191191
foreach ($result as $column) {
192192
$fields[$column[0]['name']] = array(
193-
'type' => $this->column($column[0]['type']),
194-
'null' => !$column[0]['notnull'],
195-
'default' => $column[0]['dflt_value'],
196-
'length' => $this->length($column[0]['type'])
193+
'type' => $this->column($column[0]['type']),
194+
'null' => !$column[0]['notnull'],
195+
'default' => $column[0]['dflt_value'],
196+
'length' => $this->length($column[0]['type'])
197197
);
198198
if ($column[0]['pk'] == 1) {
199+
$colLength = $this->length($column[0]['type']);
199200
$fields[$column[0]['name']] = array(
200-
'type' => $fields[$column[0]['name']]['type'],
201-
'null' => false,
202-
'default' => $column[0]['dflt_value'],
203-
'key' => $this->index['PRI'],
204-
'length' => 11
201+
'type' => $fields[$column[0]['name']]['type'],
202+
'null' => false,
203+
'default' => $column[0]['dflt_value'],
204+
'key' => $this->index['PRI'],
205+
'length'=> ($colLength != null) ? $colLength : 11
205206
);
206207
}
207208
}
@@ -449,34 +450,34 @@ function buildColumn($column) {
449450
}
450451

451452
$real = $this->columns[$type];
452-
if (isset($column['key']) && $column['key'] == 'primary') {
453-
$out = $this->name($name) . ' ' . $this->columns['primary_key']['name'];
454-
} else {
455-
$out = $this->name($name) . ' ' . $real['name'];
456-
457-
if (isset($real['limit']) || isset($real['length']) || isset($column['limit']) || isset($column['length'])) {
458-
if (isset($column['length'])) {
459-
$length = $column['length'];
460-
} elseif (isset($column['limit'])) {
461-
$length = $column['limit'];
462-
} elseif (isset($real['length'])) {
463-
$length = $real['length'];
464-
} else {
465-
$length = $real['limit'];
466-
}
467-
$out .= '(' . $length . ')';
468-
}
469-
if (isset($column['key']) && $column['key'] == 'primary') {
470-
$out .= ' NOT NULL';
471-
} elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) {
472-
$out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
473-
} elseif (isset($column['default'])) {
474-
$out .= ' DEFAULT ' . $this->value($column['default'], $type);
475-
} elseif (isset($column['null']) && $column['null'] == true) {
476-
$out .= ' DEFAULT NULL';
477-
} elseif (isset($column['null']) && $column['null'] == false) {
478-
$out .= ' NOT NULL';
453+
$out = $this->name($name) . ' ' . $real['name'];
454+
if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') {
455+
return $this->name($name) . ' ' . $this->columns['primary_key']['name'];
456+
}
457+
if (isset($real['limit']) || isset($real['length']) || isset($column['limit']) || isset($column['length'])) {
458+
if (isset($column['length'])) {
459+
$length = $column['length'];
460+
} elseif (isset($column['limit'])) {
461+
$length = $column['limit'];
462+
} elseif (isset($real['length'])) {
463+
$length = $real['length'];
464+
} else {
465+
$length = $real['limit'];
479466
}
467+
$out .= '(' . $length . ')';
468+
}
469+
if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') {
470+
$out .= ' ' . $this->columns['primary_key']['name'];
471+
} elseif (isset($column['key']) && $column['key'] == 'primary') {
472+
$out .= ' NOT NULL';
473+
} elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) {
474+
$out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
475+
} elseif (isset($column['default'])) {
476+
$out .= ' DEFAULT ' . $this->value($column['default'], $type);
477+
} elseif (isset($column['null']) && $column['null'] == true) {
478+
$out .= ' DEFAULT NULL';
479+
} elseif (isset($column['null']) && $column['null'] == false) {
480+
$out .= ' NOT NULL';
480481
}
481482
return $out;
482483
}

cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,69 @@ function testCacheKeyName() {
204204
Cache::delete($fileName, '_cake_model_');
205205
Configure::write('Cache.disable', true);
206206
}
207+
/**
208+
* test describe() and normal results.
209+
*
210+
* @return void
211+
**/
212+
function testDescribe() {
213+
$Model =& new Model(array('name' => 'User', 'ds' => 'test_suite', 'table' => 'users'));
214+
$result = $this->db->describe($Model);
215+
$expected = array(
216+
'id' => array(
217+
'type' => 'integer',
218+
'key' => 'primary',
219+
'null' => false,
220+
'default' => null,
221+
'length' => 11
222+
),
223+
'user' => array(
224+
'type' => 'string',
225+
'length' => 255,
226+
'null' => false,
227+
'default' => null
228+
),
229+
'password' => array(
230+
'type' => 'string',
231+
'length' => 255,
232+
'null' => false,
233+
'default' => null
234+
),
235+
'created' => array(
236+
'type' => 'datetime',
237+
'null' => true,
238+
'default' => null,
239+
'length' => null,
240+
),
241+
'updated' => array(
242+
'type' => 'datetime',
243+
'null' => true,
244+
'default' => null,
245+
'length' => null,
246+
)
247+
);
248+
$this->assertEqual($result, $expected);
249+
}
250+
251+
/**
252+
* test that describe does not corrupt UUID primary keys
253+
*
254+
* @return void
255+
**/
256+
function testDescribeWithUuidPrimaryKey() {
257+
$tableName = 'uuid_tests';
258+
$this->db->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
259+
$Model =& new Model(array('name' => 'UuidTest', 'ds' => 'test_suite', 'table' => 'uuid_tests'));
260+
$result = $this->db->describe($Model);
261+
$expected = array(
262+
'type' => 'string',
263+
'length' => 36,
264+
'null' => false,
265+
'default' => null,
266+
'key' => 'primary',
267+
);
268+
$this->assertEqual($result['id'], $expected);
269+
$this->db->query('DROP TABLE ' . $tableName);
270+
}
207271
}
208272
?>

0 commit comments

Comments
 (0)