Remove Table Builder classes

This commit is contained in:
Timothy Warren 2014-06-30 11:01:44 -04:00
parent 1abd835f47
commit 7fd90aba76
19 changed files with 24 additions and 1322 deletions

View File

@ -152,8 +152,6 @@ function Query($params = '')
$p->$k = $v; $p->$k = $v;
} }
//$params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
// Otherwise, return a new connection // Otherwise, return a new connection
return $cmanager->connect($p); return $cmanager->connect($p);
} }

View File

@ -113,7 +113,6 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
$this->sql = new $sql_class(); $this->sql = new $sql_class();
$this->util = new $util_class($this); $this->util = new $util_class($this);
$this->table = new Table_Builder('', array(), $this);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -1,99 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace Query\Table;
/**
* Base class for table builder component classes`
*/
abstract class Abstract_Table {
/**
* Valid options for the current class
* @var array
*/
protected $valid_options;
// --------------------------------------------------------------------------
// ! Abstract Methods
// --------------------------------------------------------------------------
/**
* String representation of the column/index
*/
abstract public function __toString();
// --------------------------------------------------------------------------
// ! Concrete methods
// --------------------------------------------------------------------------
/**
* Set options for the current column
*
* @param array $options
* return \Query\Table_Column
*/
public function set_options(Array $options)
{
$class_segments = explode('_', get_class($this));
$type = end($class_segments);
foreach($options as $option => $value)
{
if ( ! in_array($option, $this->valid_options))
{
throw new \InvalidArgumentException("{$option} is not a valid {$type}");
}
$func = "set_{$option}";
(method_exists($this, "set_{$option}"))
? $this->$func($value)
: $this->__set($option, $value);
}
return $this;
}
// --------------------------------------------------------------------------
/**
* Getters
* @param mixed $name
* @return mixed
*/
public function __get($name)
{
if ( ! isset($this->$name)) return NULL;
return $this->$name;
}
// --------------------------------------------------------------------------
/**
* Setters
* @param mixed $name
* @param mixed $val
* @return Abstract_Table
*/
public function __set($name, $val)
{
$this->$name = $val;
return $this;
}
}
// End of abstract_table.php

View File

@ -60,7 +60,6 @@ abstract class Abstract_Util {
/** /**
* Convenience public function to generate sql for creating a db table * Convenience public function to generate sql for creating a db table
* *
* @deprecated Use the table builder class instead
* @param string $name * @param string $name
* @param array $fields * @param array $fields
* @param array $constraints * @param array $constraints

View File

@ -1,447 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace Query\Table;
use Query\Driver\Driver_Interface;
// --------------------------------------------------------------------------
/**
* Abstract class defining database / table creation methods
*
* @package Query
* @subpackage Table_Builder
*/
class Table_Builder {
/**
* The name of the current table
* @var string
*/
protected $name = '';
/**
* Driver for the current db
* @var Driver_Interface
*/
private $driver = NULL;
/**
* Options for the current table
* @var array
*/
private $table_options = array();
/**
* Columns to be added/updated for the current table
* @var array
*/
private $columns = array();
/**
* Indexes to be added/updated for the current table
* @var array
*/
private $indexes = array();
/**
* Foreign keys to be added/updated for the current table
* @var array
*/
private $foreign_keys = array();
// --------------------------------------------------------------------------
/**
* Constructor
*
* @param string $name
* @param array $options
* @param Driver_Interface $driver
* @return Table_Builder
*/
public function __construct($name = '', $options = array(), Driver_Interface $driver = NULL)
{
$this->table_options = array_merge($this->table_options, $options);
$this->set_driver($driver);
if ($name !== '')
{
$this->name = (isset($this->driver)) ? $this->driver->prefix_table($name) : $name;
}
return $this;
}
// --------------------------------------------------------------------------
/**
* Alias to constructor
*
* @param string $name
* @param array $options
* @param Driver_Interface $driver
* @return Table_Builder
*/
public function __invoke($name = '', $options = array(), Driver_Interface $driver = NULL)
{
return $this->__construct($name, $options, $driver);
}
// --------------------------------------------------------------------------
/**
* Set the reference to the current database driver
*
* @param Driver_Interface $driver
* @return Table_Builder
*/
public function set_driver(Driver_Interface $driver = NULL)
{
if ( ! is_null($driver))
{
$this->driver = $driver;
}
return $this;
}
// --------------------------------------------------------------------------
/**
* Get the current DB Driver
*
* @return Driver_Interface
*/
public function get_driver()
{
return $this->driver;
}
// --------------------------------------------------------------------------
// ! Column Methods
// --------------------------------------------------------------------------
/**
* Add a column to the current table
*
* @param string $column_name
* @param string $type
* @param array $options
* @return Table_Builder
*/
public function add_column($column_name, $type = NULL, $options = array())
{
$col = new Table_Column($column_name, $type, $options);
$this->columns[] = $col;
return $this;
}
// --------------------------------------------------------------------------
/**
* Remove the specified column name from the current table
*
* @param string $column_name
* @return \Query\Table\Table_Builder
*/
public function remove_column($column_name)
{
return $this;
}
// --------------------------------------------------------------------------
/**
* Rename the specified column on the current table
*
* @param string $old_name
* @param string $new_name
* @return \Query\Table\Table_Builder
*/
public function rename_column($old_name, $new_name)
{
return $this;
}
// --------------------------------------------------------------------------
/**
* Change the specified column on the current table
*
* @param string $column_name
* @param string $new_column_type
* @param array $options
* @return \Query\Table\Table_Builder
*/
public function change_column($column_name, $new_column_type, $options = array())
{
return $this;
}
// --------------------------------------------------------------------------
/**
* Determine whether the column currently exists on the current table
*
* @param string $column_name
* @param array $options
* @return bool
*/
public function has_column($column_name, $options = array())
{
// @TODO: implement
}
// --------------------------------------------------------------------------
// ! Index Methods
// --------------------------------------------------------------------------
/**
* Add an index to the current table
*
* @param array $columns
* @param array $options
* @return \Query\Table\Table_Builder
*/
public function add_index($columns, $options = array())
{
$col = new Table_Index($columns, $options);
$this->indexes[] = $col;
return $this;
}
// --------------------------------------------------------------------------
/**
* Remove an index from the current table
* @param array $columns
* @param array $options
* @return \Query\Table\Table_Builder
*/
public function remove_index($columns, $options = array())
{
// @TODO: implement
return $this;
}
// --------------------------------------------------------------------------
/**
* Remove an index by its name from the current table
*
* @param string $name
* @return \Query\Table\Table_Builder
*/
public function remove_index_by_name($name)
{
// @TODO: implement
return $this;
}
// --------------------------------------------------------------------------
/**
* Check if the current table has an index on the specified columns
*
* @param array $columns
* @param array $options
* @return bool
*/
public function has_index($columns, $options = array())
{
// @TODO: implement
}
// --------------------------------------------------------------------------
// ! Foreign Key Methods
// --------------------------------------------------------------------------
/**
* Add a foreign key to the current table
*
* @param array $columns
* @param string $referenced_table
* @param array $referenced_columns
* @param array $options
* @return \Query\Table\Table_Builder
*/
public function add_foreign_key($columns, $referenced_table, $referenced_columns = array('id'), $options = array())
{
$key = new Table_Foreign_Key($columns, $referenced_table, $referenced_columns, $options);
$this->foreign_keys[] = $key;
return $this;
}
// --------------------------------------------------------------------------
/**
* Drop the foreign key from the current table
*
* @param array $columns
* @param string $constraint
* @return \Query\Table\Table_Builder
*/
public function drop_foreign_key($columns, $constraint = NULL)
{
// @TODO: implement
return $this;
}
// --------------------------------------------------------------------------
/**
* Determine whether the current table has the specified foreign key
*
* @param array $columns
* @param string $constraint
* @return bool
*/
public function has_foreign_key($columns, $constraint = NULL)
{
// @TODO: implement
$keys = $this->get_driver()->get_fks($this->name);
foreach($keys as $key)
{
}
return FALSE;
}
// --------------------------------------------------------------------------
// ! Table-wide methods
// --------------------------------------------------------------------------
/**
* Check whether the current table exists
*
* @return bool
*/
public function exists()
{
$tables = $this->driver->get_tables();
return in_array($this->name, $tables);
}
// --------------------------------------------------------------------------
/**
* Drop the current table
*
* @return void
*/
public function drop()
{
// @TODO: implement
}
// --------------------------------------------------------------------------
/**
* Rename the current table
*
* @param string $new_table_name
* @return void
*/
public function rename($new_table_name)
{
// @TODO: implement
}
// --------------------------------------------------------------------------
/**
* Get the list of columns for the current table
*
* @return array
*/
public function get_columns()
{
return $this->driver->get_columns($this->name);
}
// --------------------------------------------------------------------------
// ! Action methods
// --------------------------------------------------------------------------
/**
* Create the table from the previously set options
*
* @return void
*/
public function create()
{
// @TODO: implement
$this->reset();
}
// --------------------------------------------------------------------------
/**
* Update the current table with the changes made
*
* @return void
*/
public function update()
{
// @TODO: implement
$this->reset();
}
// --------------------------------------------------------------------------
/**
* Save the changes made to the table
*
* @return void
*/
public function save()
{
($this->exists())
? $this->update()
: $this->create();
}
// --------------------------------------------------------------------------
/**
* Reset the state of the table builder
*
* @return void
*/
public function reset()
{
$skip = array(
'driver' => 'driver'
);
foreach($this as $key => $val)
{
if ( ! isset($skip[$key]))
{
$this->$key = NULL;
}
}
}
}
// End of table_builder.php

View File

@ -1,83 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace Query\Table;
/**
* Class representing a column when creating a table
*/
class Table_Column extends Abstract_Table {
/**
* The name of the column
* @var string
*/
protected $name;
/**
* The type of the column
* @var string
*/
protected $type;
/**
* Valid column options
* @var type array
*/
protected $valid_options = array(
'limit',
'length',
'default',
'null',
'precision',
'scale',
'after',
'update',
'comment'
);
// --------------------------------------------------------------------------
/**
* Set the attributes for the column
*
* @param string $name
* @param [string] $type
* @param [array] $options
*/
public function __construct($name, $type = NULL, $options = array())
{
$this->name = $name;
$this->type = $type;
$this->options = ( ! empty($options))
? $this->validate_options($options)
: array();
}
// --------------------------------------------------------------------------
/**
* Return the string to create the column
*
* @return string
*/
public function __toString()
{
// @TODO: implement
$num_args = func_num_args();
}
}
// End of table_column.php

View File

@ -1,41 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace Query\Table;
/**
* Class representing a foreign key
*/
class Table_Foreign_Key extends Abstract_Table {
/**
* Valid options for a foreign key
* @var type array
*/
protected $valid_options = array(
'delete',
'update',
'constraint'
);
/**
* String representation of the foreign key
*/
public function __toString()
{
// @TODO: implement
}
}
// End of table_foreign_key.php

View File

@ -1,42 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace Query\Table;
/**
* Class representing indicies when creating a table
*/
class Table_Index extends Abstract_Table {
/**
* Valid options for a table index
* @var array
*/
protected $valid_options = array(
'type',
'unique',
'name'
);
/**
* Return the string representation of the current index
*/
public function __toString()
{
// @TODO: implement
}
}
// End of table_index.php

View File

@ -101,7 +101,6 @@ require_once(QTEST_DIR . '/core/core.php');
require_once(QTEST_DIR . '/core/db_test.php'); require_once(QTEST_DIR . '/core/db_test.php');
require_once(QTEST_DIR . '/core/db_qp_test.php'); require_once(QTEST_DIR . '/core/db_qp_test.php');
require_once(QTEST_DIR . '/core/db_qb_test.php'); require_once(QTEST_DIR . '/core/db_qb_test.php');
require_once(QTEST_DIR . '/core/table_builder.php');
// Preset SQLite connection, so there aren't locking issues // Preset SQLite connection, so there aren't locking issues
if (extension_loaded('pdo_sqlite')) if (extension_loaded('pdo_sqlite'))

View File

@ -1,32 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Parent Table Builder Test Class
*/
abstract class TableBuilderTest extends Query_TestCase {
public function testExists()
{
$this->assertTrue($this->db->table('test')->exists());
}
public function testGetDriver()
{
$this->assertEqual($this->db, $this->db->table()->get_driver());
}
}
// End of table_builder.php

View File

@ -1,37 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Parent Table Builder Test Class
*/
class FirebirdTableTest extends TableBuilderTest {
public function setUp()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
if ( ! function_exists('\\fbird_connect'))
{
$this->markTestSkipped('Firebird extension does not exist');
}
// test the db driver directly
$this->db = new \Query\Driver\Firebird('localhost:'.$dbpath);
$this->db->table_prefix = 'create_';
$this->tables = $this->db->get_tables();
}
}
// End of FirebirdTableTest.php

View File

@ -1,48 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Parent Table Builder Test Class
*/
class MySQLTableTest extends TableBuilderTest {
public function setUp()
{
// If the database isn't installed, skip the tests
if ( ! class_exists("\\Query\\Driver\\MySQL"))
{
$this->markTestSkipped("MySQL extension for PDO not loaded");
}
// Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json"))
{
$params = json_decode(file_get_contents(QTEST_DIR . "/settings.json"));
$params = $params->mysql;
$this->db = new \Query\Driver\MySQL("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array(
PDO::ATTR_PERSISTENT => TRUE
));
}
elseif (($var = getenv('CI')))
{
$this->db = new \Query\Driver\MySQL('host=127.0.0.1;port=3306;dbname=test', 'root');
}
$this->db->table_prefix = 'create_';
}
}
// End of MySQLTableTest.php

View File

@ -1,48 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Parent Table Builder Test Class
*/
class PgSQLTableTest extends TableBuilderTest {
public function setUp()
{
$class = "\\Query\\Driver\\PgSQL";
// If the database isn't installed, skip the tests
if ( ! class_exists($class))
{
$this->markTestSkipped("Postgres extension for PDO not loaded");
}
// Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json"))
{
$params = json_decode(file_get_contents(QTEST_DIR . "/settings.json"));
$params = $params->pgsql;
$this->db = new $class("pgsql:dbname={$params->database}", $params->user, $params->pass);
}
elseif (($var = getenv('CI')))
{
$this->db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres');
}
$this->db->table_prefix = 'create_';
}
}
// End of PgSQLTableTest.php

View File

@ -1,30 +0,0 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Parent Table Builder Test Class
*/
class SQLiteTableTest extends TableBuilderTest {
public function setUp()
{
// Set up in the bootstrap to mitigate
// connection locking issues
$this->db = Query('test_sqlite');
$this->db->table_prefix = 'create_';
}
}
// End of SQLiteTableTest.php

View File

@ -1,106 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Class for testing Query Builder with SQLite
*
* @requires extension pdo_sqlite
*/
class SQLiteQBTest extends QBTest {
public function setUp()
{
// Set up in the bootstrap to mitigate
// connection locking issues
$this->db = Query('test_sqlite');
}
// --------------------------------------------------------------------------
public function testQueryFunctionAlias()
{
$db = Query('test_sqlite');
$this->assertTrue($this->db === $db);
}
// --------------------------------------------------------------------------
public function testQueryExplain()
{
$query = $this->db->select('id, key as k, val')
->explain()
->where('id >', 1)
->where('id <', 900)
->get('create_test', 2, 1);
$res = $query->fetchAll(PDO::FETCH_ASSOC);
$expected_possibilities = array();
$expected_possibilities[] = array(
array(
'order' => '0',
'from' => '0',
'detail' => 'TABLE create_test USING PRIMARY KEY',
)
);
$expected_possibilities[] = array (
array (
'selectid' => '0',
'order' => '0',
'from' => '0',
'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~60000 rows)',
),
);
$expected_possibilities[] = array (
array (
'selectid' => '0',
'order' => '0',
'from' => '0',
'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?)',
),
);
$expected_possibilities[] = array (
array (
'selectid' => '0',
'order' => '0',
'from' => '0',
'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~62500 rows)',
),
);
$passed = FALSE;
// Check for a matching possibility
foreach($expected_possibilities as $ep)
{
if ($res == $ep)
{
$this->assertTrue(TRUE);
$passed = TRUE;
}
}
// Well, apparently not an expected possibility
if ( ! $passed)
{
var_export($res);
$this->assertTrue(FALSE);
}
}
}

View File

@ -1,292 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* SQLiteTest class.
*
* @extends DBTest
* @requires extension pdo_sqlite
*/
class SQLiteTest extends DBTest {
public function setUp()
{
// Set up in the bootstrap to mitigate
// connection locking issues
$this->db = Query('test_sqlite');
$this->db->table_prefix = 'create_';
}
// --------------------------------------------------------------------------
// ! Util Method tests
// --------------------------------------------------------------------------
public function testCreateTable()
{
$this->db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql'));
//Check
$dbs = $this->db->get_tables();
$this->assertTrue(in_array('TEST1', $dbs));
$this->assertTrue(in_array('TEST2', $dbs));
$this->assertTrue(in_array('NUMBERS', $dbs));
$this->assertTrue(in_array('NEWTABLE', $dbs));
$this->assertTrue(in_array('create_test', $dbs));
$this->assertTrue(in_array('create_join', $dbs));
$this->assertTrue(in_array('create_delete', $dbs));
}
// --------------------------------------------------------------------------
/*public function testBackupData()
{
$sql = mb_trim($this->db->util->backup_data(array('create_join', 'create_test')));
$sql_array = explode("\n", $sql);
$expected = <<<SQL
INSERT INTO "create_test" ("id","key","val") VALUES (1,'boogers','Gross');
INSERT INTO "create_test" ("id","key","val") VALUES (2,'works','also?');
INSERT INTO "create_test" ("id","key","val") VALUES (10,12,14);
INSERT INTO "create_test" ("id","key","val") VALUES (587,1,2);
INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale''');
SQL;
$expected_array = explode("\n", $expected);
$this->assertEqual($expected_array, $sql_array);
}*/
// --------------------------------------------------------------------------
public function testBackupStructure()
{
$sql = mb_trim($this->db->util->backup_structure());
$expected = <<<SQL
CREATE TABLE "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
CREATE TABLE "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
CREATE TABLE "create_delete" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
CREATE TABLE TEST1 (
TEST_NAME TEXT NOT NULL,
TEST_ID INTEGER DEFAULT '0' NOT NULL,
TEST_DATE TEXT NOT NULL,
CONSTRAINT PK_TEST PRIMARY KEY (TEST_ID)
);
CREATE TABLE TEST2 (
ID INTEGER NOT NULL,
FIELD1 INTEGER,
FIELD2 TEXT,
FIELD3 TEXT,
FIELD4 INTEGER,
FIELD5 INTEGER,
ID2 INTEGER NOT NULL,
CONSTRAINT PK_TEST2 PRIMARY KEY (ID2),
CONSTRAINT TEST2_FIELD1ID_IDX UNIQUE (ID, FIELD1),
CONSTRAINT TEST2_FIELD4_IDX UNIQUE (FIELD4)
);
;
;
CREATE INDEX TEST2_FIELD5_IDX ON TEST2 (FIELD5);
CREATE TABLE NUMBERS (
NUMBER INTEGER DEFAULT 0 NOT NULL,
EN TEXT NOT NULL,
FR TEXT NOT NULL
);
CREATE TABLE NEWTABLE (
ID INTEGER DEFAULT 0 NOT NULL,
SOMENAME TEXT,
SOMEDATE TEXT NOT NULL,
CONSTRAINT PKINDEX_IDX PRIMARY KEY (ID)
);
CREATE VIEW "testview" AS
SELECT *
FROM TEST1
WHERE TEST_NAME LIKE 't%';
CREATE VIEW "numbersview" AS
SELECT *
FROM NUMBERS
WHERE NUMBER > 100;
CREATE TABLE "testconstraints" (
someid integer NOT NULL,
somename TEXT NOT NULL,
CONSTRAINT testconstraints_id_pk PRIMARY KEY (someid)
);
CREATE TABLE "testconstraints2" (
ext_id integer NOT NULL,
modified text,
uniquefield text NOT NULL,
usraction integer NOT NULL,
CONSTRAINT testconstraints_id_fk FOREIGN KEY (ext_id)
REFERENCES testconstraints (someid)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT unique_2_fields_idx UNIQUE (modified, usraction),
CONSTRAINT uniquefld_idx UNIQUE (uniquefield)
);
;
;
SQL;
$expected_array = explode("\n", $expected);
$result_array = explode("\n", $sql);
$this->assertEqual($expected_array, $result_array);
}
// --------------------------------------------------------------------------
public function testDeleteTable()
{
$sql = $this->db->util->delete_table('create_delete');
$this->db->query($sql);
//Check
$dbs = $this->db->get_tables();
$this->assertFalse(in_array('create_delete', $dbs));
}
// --------------------------------------------------------------------------
// ! General tests
// --------------------------------------------------------------------------
public function testConnection()
{
$db = new \Query\Driver\SQLite(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
$this->assertIsA($db, '\\Query\\Driver\\SQLite');
$this->assertIsA($this->db->db, '\\Query\\Driver\\SQLite');
unset($db);
}
// --------------------------------------------------------------------------
public function testTruncate()
{
$this->db->truncate('create_test');
}
// --------------------------------------------------------------------------
public function testPreparedStatements()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
// --------------------------------------------------------------------------
public function testPrepareExecute()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
// --------------------------------------------------------------------------
public function testCommitTransaction()
{
$res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
$res = $this->db->commit();
$this->assertTrue($res);
}
// --------------------------------------------------------------------------
public function testRollbackTransaction()
{
$res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
$res = $this->db->rollback();
$this->assertTrue($res);
}
// --------------------------------------------------------------------------
public function testGetDBs()
{
$this->assertTrue(is_array($this->db->get_dbs()));
}
// --------------------------------------------------------------------------
public function testGetSchemas()
{
$this->assertNull($this->db->get_schemas());
}
// --------------------------------------------------------------------------
// ! SQL tests
// --------------------------------------------------------------------------
public function testNullMethods()
{
$sql = $this->db->sql->function_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->procedure_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->sequence_list();
$this->assertEqual(NULL, $sql);
}
// --------------------------------------------------------------------------
public function testGetSystemTables()
{
$sql = $this->db->get_system_tables();
$this->assertTrue(is_array($sql));
}
// --------------------------------------------------------------------------
public function testGetSequences()
{
$this->assertNull($this->db->get_sequences());
}
// --------------------------------------------------------------------------
public function testGetFunctions()
{
$this->assertNull($this->db->get_functions());
}
// --------------------------------------------------------------------------
public function testGetProcedures()
{
$this->assertNull($this->db->get_procedures());
}
}

Binary file not shown.

View File

@ -32,12 +32,12 @@ if ( ! defined('IS_QUERCUS'))
// Include simpletest // Include simpletest
// it has to be in the tests folder // it has to be in the tests folder
require_once('simpletest/autorun.php'); require_once('/htdocs/__lib/simpletest/autorun.php');
/** /**
* Base class for TestCases * Base class for TestCases
*/ */
class Query_TestCase extends UnitTestCase { abstract class Query_TestCase extends UnitTestCase {
/** /**
* Define assertInstanceOf for simpletest * Define assertInstanceOf for simpletest
@ -110,12 +110,28 @@ require_once(QTEST_DIR . '/core/core.php');
require_once(QTEST_DIR . '/core/db_test.php'); require_once(QTEST_DIR . '/core/db_test.php');
require_once(QTEST_DIR . '/core/db_qb_test.php'); require_once(QTEST_DIR . '/core/db_qb_test.php');
require_once("{$test_path}sqlite/SQLiteTest.php"); $drivers = PDO::getAvailableDrivers();
//require_once("{$test_path}mysql/MySQLTest.php");
//require_once("{$test_path}mysql/MySQLQBTest.php");
require_once("{$test_path}pgsql/PgSQLTest.php");
require_once("{$test_path}pgsql/PgSQLQBTest.php");
require_once("{$test_path}sqlite/SQLiteQBTest.php");
if (function_exists('fbird_connect'))
{
$drivers[] = 'interbase';
}
$driver_test_map = array(
'Firebird' => in_array('interbase', $drivers),
'SQLite' => in_array('sqlite', $drivers),
'PgSQL' => in_array('pgsql', $drivers),
);
// Determine which testcases to load
foreach($driver_test_map as $name => $do_load)
{
$path = $test_path . strtolower($name) . '/';
if ($do_load && ($name != 'SQLite' && ! IS_QUERCUS))
{
require_once("{$path}{$name}Test.php");
require_once("{$path}{$name}QBTest.php");
}
}
// End of index.php // End of index.php

View File

@ -18,22 +18,18 @@
</testsuite> </testsuite>
<testsuite name="FirebirdTests"> <testsuite name="FirebirdTests">
<file>databases/firebird/FirebirdTest.php</file> <file>databases/firebird/FirebirdTest.php</file>
<file>databases/firebird/FirebirdTableTest.php</file>
<file>databases/firebird/FirebirdQBTest.php</file> <file>databases/firebird/FirebirdQBTest.php</file>
</testsuite> </testsuite>
<testsuite name="MySQLTests"> <testsuite name="MySQLTests">
<file>databases/mysql/MySQLTest.php</file> <file>databases/mysql/MySQLTest.php</file>
<file>databases/mysql/MySQLTableTest.php</file>
<file>databases/mysql/MySQLQBTest.php</file> <file>databases/mysql/MySQLQBTest.php</file>
</testsuite> </testsuite>
<testsuite name="PgSQLTests"> <testsuite name="PgSQLTests">
<file>databases/pgsql/PgSQLTest.php</file> <file>databases/pgsql/PgSQLTest.php</file>
<file>databases/pgsql/PgSQLTableTest.php</file>
<file>databases/pgsql/PgSQLQBTest.php</file> <file>databases/pgsql/PgSQLQBTest.php</file>
</testsuite> </testsuite>
<testsuite name="SQLiteTests"> <testsuite name="SQLiteTests">
<file>databases/sqlite/SQLiteTest.php</file> <file>databases/sqlite/SQLiteTest.php</file>
<!--<file>databases/sqlite/SQLiteTableTest.php</file>-->
<file>databases/sqlite/SQLiteQBTest.php</file> <file>databases/sqlite/SQLiteQBTest.php</file>
</testsuite> </testsuite>
</testsuites> </testsuites>