Skip to content
Permalink
Browse files

Add support for parsing Sqlite column types.

  • Loading branch information...
markstory committed Mar 31, 2013
1 parent ebb6bf1 commit 424e14742f41a3a7ed9c148734a8538493552a87
@@ -85,4 +85,61 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
}
}
/**
* Convert a column definition to the abstract types.
*
* The returned type will be a type that
* Cake\Model\Datasource\Database\Type can handle.
*
* @param string $column The column type + length
* @return array List of (type, length)
*/
public function convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
if (empty($matches)) {
throw new Error\Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
}
$col = strtolower($matches[1]);
$length = null;
if (isset($matches[2])) {
$length = (int)$matches[2];
}
if ($col === 'bigint') {
return ['biginteger', $length];
}
if (in_array($col, ['blob', 'clob'])) {
return ['binary', null];
}
if (in_array($col, ['date', 'time', 'timestamp', 'datetime'])) {
return [$col, null];
}
if (strpos($col, 'decimal') !== false) {
return ['decimal', null];
}
if (strpos($col, 'boolean') !== false) {
return ['boolean', null];
}
if (strpos($col, 'int') !== false) {
return ['integer', $length];
}
if (strpos($col, 'char') !== false) {
return ['string', $length];
}
if (in_array($col, ['float', 'real', 'double'])) {
return ['float', null];
}
return ['text', null];
}
/**
* Additional metadata columns in table descriptions.
*
* @return array
*/
public function extraSchemaColumns() {
return [];
}
}
@@ -17,13 +17,26 @@
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;
use Cake\Core\Configure;
use Cake\Model\Datasource\Database\Driver\Sqlite;
use Cake\Testsuite\TestCase;
use \PDO;
/**
* Tests Sqlite driver
*/
class SqliteTest extends TestCase {
/**
* setup method
*
* @return void
*/
class SqliteTest extends \Cake\TestSuite\TestCase {
public function setUp() {
parent::setUp();
$config = Configure::read('Datasource.test');
$this->skipIf(strpos($config['datasource'], 'Sqlite') === false, 'Not using Sqlite for test config');
}
/**
* Test connecting to Sqlite with default configuration
@@ -88,4 +101,85 @@ public function testConnectionConfigCustom() {
$driver->connect($config);
}
/**
* Dataprovider for column testing
*
* @return array
*/
public static function columnProvider() {
return [
[
'DATETIME',
['datetime', null]
],
[
'DATE',
['date', null]
],
[
'TIME',
['time', null]
],
[
'BOOLEAN',
['boolean', null]
],
[
'BIGINT',
['biginteger', null]
],
[
'VARCHAR(255)',
['string', 255]
],
[
'CHAR(25)',
['string', 25]
],
[
'BLOB',
['binary', null]
],
[
'INTEGER(11)',
['integer', 11]
],
[
'TINYINT(5)',
['integer', 5]
],
[
'MEDIUMINT(10)',
['integer', 10]
],
[
'FLOAT',
['float', null]
],
[
'DOUBLE',
['float', null]
],
[
'REAL',
['float', null]
],
[
'DECIMAL(11,2)',
['decimal', null]
],
];
}
/**
* Test parsing SQLite column types.
*
* @dataProvider columnProvider
* @return void
*/
public function testConvertColumnType($input, $expected) {
$driver = new Sqlite();
$this->assertEquals($driver->convertColumn($input), $expected);
}
}

0 comments on commit 424e147

Please sign in to comment.
You can’t perform that action at this time.