Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SQLite: Introspection and tests

  • Loading branch information...
commit e67d2d1cc87393dd5d306549de9f63093f31299e 1 parent 024a9f4
Chris Bandy authored
Showing with 147 additions and 1 deletion.
  1. +61 −1 classes/database/pdo/sqlite.php
  2. +86 −0 tests/database/pdo/sqlite.php
62 classes/database/pdo/sqlite.php
View
@@ -9,7 +9,7 @@
* @copyright (c) 2010 Chris Bandy
* @license http://www.opensource.org/licenses/isc-license.txt
*/
-class Database_PDO_SQLite extends Database_PDO implements Database_iEscape, Database_iInsert
+class Database_PDO_SQLite extends Database_PDO implements Database_iEscape, Database_iInsert, Database_iIntrospect
{
/**
* Create an INSERT command
@@ -131,4 +131,64 @@ public function quote_literal($value)
return parent::quote_literal($value);
}
+
+ public function table_columns($table)
+ {
+ $result = array();
+
+ if ($rows = $this->execute_query('PRAGMA table_info('.$this->quote_table($table).')'))
+ {
+ foreach ($rows as $row)
+ {
+ $type = strtolower($row['type']);
+
+ if ($open = strpos($type, '('))
+ {
+ $close = strpos($type, ')', $open);
+
+ $length = substr($type, $open + 1, $close - 1 - $open);
+ $type = substr($type, 0, $open);
+ }
+ else
+ {
+ $length = NULL;
+ }
+
+ $row = array(
+ 'column_name' => $row['name'],
+ 'ordinal_position' => $row['cid'] + 1,
+ 'column_default' => $row['dflt_value'],
+ 'is_nullable' => empty($row['notnull']) ? 'YES' : 'NO',
+ 'data_type' => $type,
+ 'character_maximum_length' => NULL,
+ 'numeric_precision' => NULL,
+ 'numeric_scale' => NULL,
+ );
+
+ if ($length)
+ {
+ if (strpos($type, 'char') !== FALSE
+ OR strpos($type, 'clob') !== FALSE
+ OR strpos($type, 'text') !== FALSE)
+ {
+ $row['character_maximum_length'] = $length;
+ }
+ else
+ {
+ $length = explode(',', $length);
+ $row['numeric_precision'] = reset($length);
+
+ if (next($length))
+ {
+ $row['numeric_scale'] = current($length);
+ }
+ }
+ }
+
+ $result[$row['column_name']] = $row;
+ }
+ }
+
+ return $result;
+ }
}
86 tests/database/pdo/sqlite.php
View
@@ -92,4 +92,90 @@ public function test_insert()
$this->assertEquals(array(1,6), $query->execute($this->_db));
}
+
+ public function provider_table_columns()
+ {
+ return array
+ (
+ array('integer', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'integer',
+ )),
+ array('numeric', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'numeric',
+ )),
+ array('numeric(10)', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'numeric',
+ 'numeric_precision' => 10,
+ )),
+ array('numeric(10,5)', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'numeric',
+ 'numeric_precision' => 10,
+ 'numeric_scale' => 5,
+ )),
+ array('real', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'real',
+ )),
+ array('text', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'text',
+ )),
+ array('varchar(50)', array(
+ 'column_default' => NULL,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'varchar',
+ 'character_maximum_length' => 50,
+ )),
+
+ array('int DEFAULT 5', array(
+ 'column_default' => 5,
+ 'is_nullable' => 'YES',
+ 'data_type' => 'int',
+ )),
+ array('int DEFAULT 5 NOT NULL', array(
+ 'column_default' => 5,
+ 'is_nullable' => 'NO',
+ 'data_type' => 'int',
+ )),
+ );
+ }
+
+ /**
+ * @dataProvider provider_table_columns
+ */
+ public function test_table_columns($column, $expected)
+ {
+ $expected = array_merge(array(
+ 'column_name' => 'field',
+ 'ordinal_position' => 1,
+ 'column_default' => NULL,
+ 'is_nullable' => NULL,
+ 'data_type' => NULL,
+ 'character_maximum_length' => NULL,
+ 'numeric_precision' => NULL,
+ 'numeric_scale' => NULL,
+ ), $expected);
+
+ $this->_db->execute_command('DROP TABLE '.$this->_table);
+ $this->_db->execute_command('CREATE TEMPORARY TABLE '.$this->_table."( field $column )");
+
+ $result = $this->_db->table_columns('temp_test_table');
+
+ $this->assertEquals($expected, $result['field']);
+ }
+
+ public function test_table_columns_no_table()
+ {
+ $this->assertSame(array(), $this->_db->table_columns('table-does-not-exist'));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.