2012-03-15 09:25:18 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-10 14:06:34 -04:00
|
|
|
* OpenSQLManager
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
2012-04-10 14:06:34 -04:00
|
|
|
* Free Database manager for Open Source Databases
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
2014-02-18 15:18:01 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
2012-04-10 14:06:34 -04:00
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLiteTest class.
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2014-02-14 22:08:19 -05:00
|
|
|
* @extends DBTest
|
|
|
|
* @requires extension pdo_sqlite
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2014-02-14 22:08:19 -05:00
|
|
|
class SQLiteTest extends DBTest {
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function setUp()
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2014-02-21 15:02:41 -05:00
|
|
|
// Set up in the bootstrap to mitigate
|
|
|
|
// connection locking issues
|
|
|
|
$this->db = Query('test_sqlite');
|
2014-04-09 10:30:01 -04:00
|
|
|
$this->db->table_prefix = 'create_';
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-07 14:18:08 -05:00
|
|
|
// ! Util Method tests
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCreateTable()
|
2014-02-21 15:02:41 -05:00
|
|
|
{
|
2014-04-08 15:38:18 -04:00
|
|
|
$this->db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql'));
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
//Check
|
|
|
|
$dbs = $this->db->get_tables();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-08 15:38:18 -04:00
|
|
|
$this->assertTrue(in_array('TEST1', $dbs));
|
|
|
|
$this->assertTrue(in_array('TEST2', $dbs));
|
|
|
|
$this->assertTrue(in_array('NUMBERS', $dbs));
|
|
|
|
$this->assertTrue(in_array('NEWTABLE', $dbs));
|
2012-04-10 14:06:34 -04:00
|
|
|
$this->assertTrue(in_array('create_test', $dbs));
|
2014-04-08 15:38:18 -04:00
|
|
|
$this->assertTrue(in_array('create_join', $dbs));
|
|
|
|
$this->assertTrue(in_array('create_delete', $dbs));
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-04-03 16:49:01 -04:00
|
|
|
/*public function testBackupData()
|
2014-02-07 14:18:08 -05:00
|
|
|
{
|
2014-02-21 15:02:41 -05:00
|
|
|
$sql = mb_trim($this->db->util->backup_data(array('create_join', 'create_test')));
|
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
$sql_array = explode("\n", $sql);
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
$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);
|
2014-02-21 15:02:41 -05:00
|
|
|
INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale''');
|
2014-02-07 14:18:08 -05:00
|
|
|
SQL;
|
2014-04-03 16:49:01 -04:00
|
|
|
$expected_array = explode("\n", $expected);
|
2014-02-07 14:18:08 -05:00
|
|
|
$this->assertEqual($expected_array, $sql_array);
|
2014-04-03 16:49:01 -04:00
|
|
|
}*/
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-21 15:02:41 -05:00
|
|
|
|
|
|
|
public function testBackupStructure()
|
2014-02-07 14:18:08 -05:00
|
|
|
{
|
|
|
|
$sql = mb_trim($this->db->util->backup_structure());
|
|
|
|
$expected = <<<SQL
|
2014-04-03 16:49:01 -04:00
|
|
|
CREATE TABLE "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
|
|
|
|
CREATE TABLE "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
|
2014-04-08 15:38:18 -04:00
|
|
|
CREATE TABLE "create_delete" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
|
2014-04-08 15:18:51 -04:00
|
|
|
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;
|
2014-04-15 16:15:08 -04:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
;
|
|
|
|
;
|
2014-02-07 14:18:08 -05:00
|
|
|
SQL;
|
|
|
|
|
|
|
|
$expected_array = explode("\n", $expected);
|
|
|
|
$result_array = explode("\n", $sql);
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
$this->assertEqual($expected_array, $result_array);
|
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testDeleteTable()
|
2014-02-07 14:18:08 -05:00
|
|
|
{
|
|
|
|
$sql = $this->db->util->delete_table('create_delete');
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
$this->db->query($sql);
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
//Check
|
|
|
|
$dbs = $this->db->get_tables();
|
|
|
|
$this->assertFalse(in_array('create_delete', $dbs));
|
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-14 22:08:19 -05:00
|
|
|
// ! General tests
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testConnection()
|
2014-02-07 14:18:08 -05:00
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
$db = new \Query\Driver\SQLite(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
$this->assertIsA($db, '\\Query\\Driver\\SQLite');
|
|
|
|
$this->assertIsA($this->db->db, '\\Query\\Driver\\SQLite');
|
2014-02-21 15:02:41 -05:00
|
|
|
|
|
|
|
unset($db);
|
2014-02-07 14:18:08 -05:00
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testTruncate()
|
2014-02-21 15:02:41 -05:00
|
|
|
{
|
2012-03-15 09:25:18 -04:00
|
|
|
$this->db->truncate('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testPreparedStatements()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
|
|
|
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
|
|
|
|
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testPrepareExecute()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
|
|
|
$this->db->prepare_execute($sql, array(
|
|
|
|
2, "works", 'also?'
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCommitTransaction()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testRollbackTransaction()
|
2014-02-21 15:02:41 -05:00
|
|
|
{
|
2012-04-10 14:06:34 -04:00
|
|
|
$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);
|
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetDBs()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2014-04-08 14:26:28 -04:00
|
|
|
$this->assertTrue(is_array($this->db->get_dbs()));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetSchemas()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2014-02-14 22:08:19 -05:00
|
|
|
$this->assertNull($this->db->get_schemas());
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-14 22:08:19 -05:00
|
|
|
// ! SQL tests
|
2013-12-09 20:11:49 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testNullMethods()
|
2013-12-09 20:11:49 -05:00
|
|
|
{
|
|
|
|
$sql = $this->db->sql->function_list();
|
|
|
|
$this->assertEqual(NULL, $sql);
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2013-12-09 20:11:49 -05:00
|
|
|
$sql = $this->db->sql->procedure_list();
|
|
|
|
$this->assertEqual(NULL, $sql);
|
2014-02-21 15:02:41 -05:00
|
|
|
|
2013-12-09 20:11:49 -05:00
|
|
|
$sql = $this->db->sql->sequence_list();
|
|
|
|
$this->assertEqual(NULL, $sql);
|
2014-04-07 16:49:49 -04:00
|
|
|
}
|
2014-04-10 15:54:43 -04:00
|
|
|
|
|
|
|
public function testGetSystemTables()
|
|
|
|
{
|
|
|
|
$sql = $this->db->get_system_tables();
|
|
|
|
$this->assertTrue(is_array($sql));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetTriggers()
|
|
|
|
{
|
|
|
|
$sql = $this->db->get_triggers();
|
|
|
|
$this->assertTrue(is_array($sql));
|
|
|
|
}
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|