From a9bbae31eedfffb805bab1dbb17a29bfb1d09549 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 2 Jun 2009 03:03:51 +0000 Subject: [PATCH] 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 --- .../libs/model/datasources/dbo/dbo_sqlite.php | 75 ++++++++++--------- .../model/datasources/dbo/dbo_sqlite.test.php | 64 ++++++++++++++++ 2 files changed, 102 insertions(+), 37 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index b51132185..c365b6fe0 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -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; } 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 882a91e9e..2f1ea4ad6 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 @@ -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); + } } ?> \ No newline at end of file