mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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:
parent
2842fe7021
commit
a9bbae31ee
2 changed files with 102 additions and 37 deletions
|
@ -88,7 +88,7 @@ class DboSqlite extends DboSource {
|
|||
'primary_key' => array('name' => 'integer primary key'),
|
||||
'string' => array('name' => 'varchar', 'limit' => '255'),
|
||||
'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'),
|
||||
'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'),
|
||||
|
@ -190,18 +190,19 @@ class DboSqlite extends DboSource {
|
|||
|
||||
foreach ($result as $column) {
|
||||
$fields[$column[0]['name']] = array(
|
||||
'type' => $this->column($column[0]['type']),
|
||||
'null' => !$column[0]['notnull'],
|
||||
'default' => $column[0]['dflt_value'],
|
||||
'length' => $this->length($column[0]['type'])
|
||||
'type' => $this->column($column[0]['type']),
|
||||
'null' => !$column[0]['notnull'],
|
||||
'default' => $column[0]['dflt_value'],
|
||||
'length' => $this->length($column[0]['type'])
|
||||
);
|
||||
if ($column[0]['pk'] == 1) {
|
||||
$colLength = $this->length($column[0]['type']);
|
||||
$fields[$column[0]['name']] = array(
|
||||
'type' => $fields[$column[0]['name']]['type'],
|
||||
'null' => false,
|
||||
'default' => $column[0]['dflt_value'],
|
||||
'key' => $this->index['PRI'],
|
||||
'length' => 11
|
||||
'type' => $fields[$column[0]['name']]['type'],
|
||||
'null' => false,
|
||||
'default' => $column[0]['dflt_value'],
|
||||
'key' => $this->index['PRI'],
|
||||
'length'=> ($colLength != null) ? $colLength : 11
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -449,34 +450,34 @@ class DboSqlite extends DboSource {
|
|||
}
|
||||
|
||||
$real = $this->columns[$type];
|
||||
if (isset($column['key']) && $column['key'] == 'primary') {
|
||||
$out = $this->name($name) . ' ' . $this->columns['primary_key']['name'];
|
||||
} else {
|
||||
$out = $this->name($name) . ' ' . $real['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') {
|
||||
$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 = $this->name($name) . ' ' . $real['name'];
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -204,5 +204,69 @@ class DboSqliteTest extends CakeTestCase {
|
|||
Cache::delete($fileName, '_cake_model_');
|
||||
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);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue