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
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PgTest class.
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2014-02-14 22:08:19 -05:00
|
|
|
* @extends DBTest
|
|
|
|
* @requires extension pdo_pgsql
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-03-19 13:38:49 -04:00
|
|
|
class PgTest extends DBTest {
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function setUp()
|
2014-02-25 13:47:35 -05:00
|
|
|
{
|
2015-07-16 16:56:13 -04:00
|
|
|
$class = "Query\\Drivers\\Pgsql\\Driver";
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-17 19:12:53 -05:00
|
|
|
// If the database isn't installed, skip the tests
|
2015-07-16 16:56:13 -04:00
|
|
|
if (( ! class_exists($class)) && ! IS_QUERCUS)
|
2014-02-17 19:12:53 -05:00
|
|
|
{
|
2014-02-18 14:50:20 -05:00
|
|
|
$this->markTestSkipped("Postgres extension for PDO not loaded");
|
2014-02-17 19:12:53 -05:00
|
|
|
}
|
2015-07-17 15:38:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
$class = "Query\\Drivers\\Pgsql\\Driver";
|
|
|
|
|
2015-11-10 16:29:17 -05:00
|
|
|
$params = get_json_config();
|
2016-07-15 16:17:49 -04:00
|
|
|
if (($var = getenv('TRAVIS')))
|
2015-11-10 16:29:17 -05:00
|
|
|
{
|
|
|
|
self::$db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres');
|
|
|
|
}
|
2012-03-19 10:40:16 -04:00
|
|
|
// Attempt to connect, if there is a test config file
|
2015-11-10 16:29:17 -05:00
|
|
|
else if ($params !== FALSE)
|
2012-03-19 10:40:16 -04:00
|
|
|
{
|
|
|
|
$params = $params->pgsql;
|
2016-07-19 11:53:37 -04:00
|
|
|
self::$db = new $class("pgsql:host={$params->host};dbname={$params->database};port=5432", $params->user, $params->pass);
|
2012-03-19 10:40:16 -04:00
|
|
|
}
|
2014-04-09 10:30:01 -04:00
|
|
|
|
2015-07-30 16:40:30 -04:00
|
|
|
self::$db->set_table_prefix('create_');
|
2012-03-19 10:40:16 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -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 testExists()
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2015-07-16 16:56:13 -04:00
|
|
|
$drivers = \PDO::getAvailableDrivers();
|
|
|
|
$this->assertTrue(in_array('pgsql', $drivers));
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -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 testConnection()
|
2012-03-19 10:40:16 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertIsA(self::$db, '\\Query\\Drivers\\Pgsql\\Driver');
|
2012-03-19 10:40:16 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-19 10:40:16 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCreateTable()
|
2012-03-19 10:40:16 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/pgsql.sql'));
|
2014-04-08 14:26:28 -04:00
|
|
|
|
2012-03-22 16:46:47 -04:00
|
|
|
// Drop the table(s) if they exist
|
|
|
|
$sql = 'DROP TABLE IF EXISTS "create_test"';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-03-22 16:46:47 -04:00
|
|
|
$sql = 'DROP TABLE IF EXISTS "create_join"';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
|
2012-03-19 10:40:16 -04:00
|
|
|
//Attempt to create the table
|
2015-07-30 16:40:30 -04:00
|
|
|
$sql = self::$db->get_util()->create_table('create_test',
|
2012-03-19 10:40:16 -04:00
|
|
|
array(
|
|
|
|
'id' => 'integer',
|
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2012-04-10 14:06:34 -04:00
|
|
|
),
|
2012-03-19 10:40:16 -04:00
|
|
|
array(
|
|
|
|
'id' => 'PRIMARY KEY'
|
|
|
|
)
|
|
|
|
);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 10:40:16 -04:00
|
|
|
//Attempt to create the table
|
2015-07-30 16:40:30 -04:00
|
|
|
$sql = self::$db->get_util()->create_table('create_join',
|
2012-03-19 10:40:16 -04:00
|
|
|
array(
|
|
|
|
'id' => 'integer',
|
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2012-04-10 14:06:34 -04:00
|
|
|
),
|
2012-03-19 10:40:16 -04:00
|
|
|
array(
|
|
|
|
'id' => 'PRIMARY KEY'
|
|
|
|
)
|
|
|
|
);
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-22 16:46:47 -04:00
|
|
|
//echo $sql.'<br />';
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-22 16:46:47 -04:00
|
|
|
//Reset
|
2015-07-17 15:38:06 -04:00
|
|
|
//unset(self::$db);
|
|
|
|
//$this->setUp();
|
2012-03-19 10:40:16 -04:00
|
|
|
|
|
|
|
//Check
|
2015-07-17 15:38:06 -04:00
|
|
|
$dbs = self::$db->get_tables();
|
2012-03-19 10:40:16 -04:00
|
|
|
$this->assertTrue(in_array('create_test', $dbs));
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testTruncate()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->truncate('create_test');
|
|
|
|
self::$db->truncate('create_join');
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$ct_query = self::$db->query('SELECT * FROM create_test');
|
|
|
|
$cj_query = self::$db->query('SELECT * FROM create_join');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:47:35 -05: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;
|
2015-07-17 15:38:06 -04:00
|
|
|
$statement = self::$db->prepare_query($sql, array(1,"boogers", "Gross"));
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
public function testBadPreparedStatement()
|
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2014-02-25 13:47:35 -05:00
|
|
|
try
|
2014-02-18 19:29:58 -05:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$statement = self::$db->prepare_query($sql, 'foo');
|
2014-02-18 19:29:58 -05:00
|
|
|
}
|
|
|
|
catch(InvalidArgumentException $e)
|
|
|
|
{
|
|
|
|
$this->assertTrue(TRUE);
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -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
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->prepare_execute($sql, array(
|
2012-04-10 14:06:34 -04:00
|
|
|
2, "works", 'also?'
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2014-02-25 13:47:35 -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
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->beginTransaction();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->commit();
|
2012-04-10 14:06:34 -04:00
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
2014-02-25 13:47:35 -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()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->beginTransaction();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->rollback();
|
2012-04-10 14:06:34 -04:00
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetSchemas()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertTrue(is_array(self::$db->get_schemas()));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:47:35 -05:00
|
|
|
|
|
|
|
public function testGetDBs()
|
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertTrue(is_array(self::$db->get_dbs()));
|
2014-02-25 13:47:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function testGetFunctions()
|
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertNull(self::$db->get_functions());
|
2014-02-25 13:47:35 -05:00
|
|
|
}
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|