Add pdo_firebird driver

This commit is contained in:
Timothy Warren 2015-07-20 15:24:21 -04:00
parent 3d310be266
commit cd7f904889
11 changed files with 567 additions and 20 deletions

View File

@ -186,7 +186,7 @@ final class Connection_Manager {
{ {
$dsn = "{$params->host}:{$params->file}"; $dsn = "{$params->host}:{$params->file}";
} }
else if(strtolower($dbtype === 'sqlite')) else if(strtolower($dbtype) === 'sqlite')
{ {
$dsn = $params->file; $dsn = $params->file;
} }
@ -212,18 +212,17 @@ final class Connection_Manager {
{ {
if (strtolower($dbtype) === 'pdo_firebird') $dbtype = 'firebird'; if (strtolower($dbtype) === 'pdo_firebird') $dbtype = 'firebird';
$dsn = strtolower($dbtype) . ':'; $pairs = [];
if ( ! empty($params->database)) if ( ! empty($params->database))
{ {
$dsn .= "dbname={$params->database}"; $pairs[] = implode('=', ['dbname', $params->database]);
} }
$skip = array( $skip = array(
'name' => 'name', 'name' => 'name',
'pass' => 'pass', 'pass' => 'pass',
'user' => 'user', 'user' => 'user',
'file' => 'file',
'type' => 'type', 'type' => 'type',
'prefix' => 'prefix', 'prefix' => 'prefix',
'options' => 'options', 'options' => 'options',
@ -233,13 +232,13 @@ final class Connection_Manager {
foreach($params as $key => $val) foreach($params as $key => $val)
{ {
if (( ! array_key_exists($key, $skip)) && ! empty($val)) if (( ! array_key_exists($key, $skip)) && ( ! empty($val)))
{ {
$dsn .= ";{$key}={$val}"; $pairs[] = implode('=', [$key, $val]);
} }
} }
return $dsn; return strtolower($dbtype) . ':' . implode(';', $pairs);
} }
} }
// End of connection_manager.php // End of connection_manager.php

View File

@ -0,0 +1,88 @@
<?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\Drivers\Pdo_firebird;
/**
* Firebird specific class
*
* @package Query
* @subpackage Drivers
*/
class Driver extends \Query\Abstract_Driver {
/**
* Firebird doesn't have the truncate keyword
*
* @var bool
*/
protected $has_truncate = FALSE;
/**
* Connect to Firebird Database
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
*/
public function __construct($dsn, $username="SYSDBA", $password="masterkey", array $options=array())
{
parent::__construct($dsn, $username, $password, $options);
}
// --------------------------------------------------------------------------
/**
* Create sql for batch insert
*
* @param string $table
* @param array $data
* @return array
*/
public function insert_batch($table, $data=array())
{
// Each member of the data array needs to be an array
if ( ! is_array(current($data))) return NULL;
// Start the block of sql statements
$sql = "EXECUTE BLOCK AS BEGIN\n";
$table = $this->quote_table($table);
$fields = \array_keys(\current($data));
$insert_template = "INSERT INTO {$table} ("
. implode(',', $this->quote_ident($fields))
. ") VALUES (";
foreach($data as $item)
{
// Quote string values
$vals = array_map(array($this, 'quote'), $item);
// Add the values in the sql
$sql .= $insert_template . implode(', ', $vals) . ");\n";
}
// End the block of SQL statements
$sql .= "END";
// Return a null array value so the query is run as it is,
// not as a prepared statement, because a prepared statement
// doesn't work for this type of query in Firebird.
return array($sql, NULL);
}
}
//End of firebird_driver.php

View File

@ -0,0 +1,25 @@
<?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\Drivers\Pdo_firebird;
/**
* Firebird Specific SQL
*
* @package Query
* @subpackage Drivers
*/
class SQL extends \Query\Drivers\Firebird\SQL {}
//End of firebird_sql.php

View File

@ -0,0 +1,38 @@
<?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\Drivers\Pdo_firebird;
/**
* Firebird-specific backup, import and creation methods
*
* @package Query
* @subpackage Drivers
*/
class Util extends \Query\Drivers\Firebird\Util {
/**
* Create an SQL backup file for the current database's structure
* @codeCoverageIgnore
* @param string $db_path
* @param string $new_file
* @return string
*/
public function backup_structure()
{
return NULL;
}
}
// End of firebird_util.php

View File

@ -25,6 +25,7 @@ class SQL extends \Query\Abstract_SQL {
/** /**
* Get the query plan for the sql query * Get the query plan for the sql query
* *
* @codeCoverageIgnore
* @param string $sql * @param string $sql
* @return string * @return string
*/ */

View File

@ -47,8 +47,9 @@ class Driver extends \Query\Abstract_Driver {
*/ */
public function __construct($dsn, $user=NULL, $pass=NULL, array $driver_options=array()) public function __construct($dsn, $user=NULL, $pass=NULL, array $driver_options=array())
{ {
// DSN is simply `sqlite:/path/to/db` if (strpos($dsn, 'sqlite:') === FALSE) $dsn = "sqlite:{$dsn}";
parent::__construct("sqlite:{$dsn}", $user, $pass);
parent::__construct($dsn, $user, $pass);
} }

View File

@ -216,12 +216,7 @@ if ( ! function_exists('Query'))
} }
elseif ( ! is_scalar($params) && ! is_null($params)) elseif ( ! is_scalar($params) && ! is_null($params))
{ {
$params_object = new stdClass(); $params_object = (object) $params;
foreach($params as $k => $v)
{
$params_object->$k = $v;
}
// Otherwise, return a new connection // Otherwise, return a new connection
return $cmanager->connect($params_object); return $cmanager->connect($params_object);

View File

@ -32,9 +32,9 @@
<file>tests/databases/firebird/FirebirdTest.php</file> <file>tests/databases/firebird/FirebirdTest.php</file>
<file>tests/databases/firebird/FirebirdQBTest.php</file> <file>tests/databases/firebird/FirebirdQBTest.php</file>
</testsuite> </testsuite>
<!-- <testsuite name="PDOFirebirdTests"> <testsuite name="PDOFirebirdTests">
<file>tests/databases/pdo_firebird/PDOFirebirdTest.php</file> <file>tests/databases/pdo_firebird/PDOFirebirdTest.php</file>
<file>tests/databases/pdo_firebird/PDOFirebirdQBTest.php</file> <file>tests/databases/pdo_firebird/PDOFirebirdQBTest.php</file>
</testsuite> --> </testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>

View File

@ -0,0 +1,119 @@
<?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
*/
// --------------------------------------------------------------------------
/**
* Firebird Query Builder Tests
* @requires extension interbase
*/
class PDOFirebirdQBTest extends QBTest {
public function setUp()
{
if ( ! in_array('firebird', PDO::getAvailableDrivers()))
{
$this->markTestSkipped('PDO Firebird extension does not exist');
}
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// test the query builder
$params = new Stdclass();
$params->alias = 'fire';
$params->type = 'pdo_firebird';
$params->database = $dbpath;
$params->host = 'localhost';
$params->user = 'SYSDBA';
$params->pass = 'masterkey';
$params->prefix = 'create_';
$this->db = Query($params);
}
public function testQueryFunctionAlias()
{
$this->markTestSkipped();
$db = Query();
$this->assertTrue($this->db === $db);
}
public function testGetNamedConnectionException()
{
try
{
$db = Query('water');
}
catch(InvalidArgumentException $e)
{
$this->assertIsA($e, 'InvalidArgumentException');
}
}
public function testGetNamedConnection()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// test the query builder
$params = new Stdclass();
$params->alias = 'wood';
$params->type = 'pdo_firebird';
$params->database = $dbpath;
$params->host = 'localhost';
$params->user = 'sysdba';
$params->pass = 'masterkey';
$params->prefix = '';
$f_conn = Query($params);
$q_conn = Query('wood');
$this->assertReference($f_conn, $q_conn);
}
// --------------------------------------------------------------------------
public function testTypeList()
{
$this->markTestSkipped();
$this->doSetUp();
$sql = $this->db->get_sql()->type_list();
$query = $this->db->query($sql);
$this->assertIsA('PDOStatement', $query);
$res = $query->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($res));
}
// --------------------------------------------------------------------------
public function testQueryExplain()
{
$res = $this->db->select('id, key as k, val')
->explain()
->where('id >', 1)
->where('id <', 900)
->limit(2, 1)
->get_compiled_select();
$res2 = $this->db->select('id, key as k, val')
->where('id >', 1)
->where('id <', 900)
->limit(2, 1)
->get_compiled_select();
// Queries are equal because explain is not a keyword in Firebird
$this->assertEqual($res, $res2);
}
}

View File

@ -0,0 +1,283 @@
<?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
*/
// --------------------------------------------------------------------------
@chmod(QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB', 0777);
/**
* Firebirdtest class.
*
* @extends DBtest
* @requires extension interbase
*/
class PDOFirebirdTest extends DBtest {
public static function setUpBeforeClass()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// test the db driver directly
self::$db = new \Query\Drivers\Pdo_firebird\Driver('firebird:host=localhost;dbname='.$dbpath);
self::$db->table_prefix = 'create_';
}
public function setUp()
{
if ( ! in_array('firebird', PDO::getAvailableDrivers()))
{
$this->markTestSkipped('PDO Firebird extension does not exist');
}
$this->tables = self::$db->get_tables();
}
// --------------------------------------------------------------------------
public function tearDown()
{
unset($this->tables);
}
// --------------------------------------------------------------------------
public function testExists()
{
$this->assertTrue(in_array('firebird', PDO::getAvailableDrivers()));;
}
// --------------------------------------------------------------------------
public function testConnection()
{
$this->assertIsA(self::$db, '\\Query\\Drivers\\Pdo_firebird\\Driver');
}
// --------------------------------------------------------------------------
public function testGetSystemTables()
{
$only_system = TRUE;
$tables = self::$db->get_system_tables();
foreach($tables as $t)
{
if(stripos($t, 'rdb$') !== 0 && stripos($t, 'mon$') !== 0)
{
$only_system = FALSE;
break;
}
}
$this->assertTrue($only_system);
}
// --------------------------------------------------------------------------
// ! Create / Delete Tables
// --------------------------------------------------------------------------
public function testCreateTable()
{
$this->markTestSkipped();
//Attempt to create the table
$sql = self::$db->util->create_table('create_delete', array(
'id' => 'SMALLINT',
'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT'
));
self::$db->query($sql);
//Check
$this->assertTrue(in_array('create_delete', self::$db->get_tables()));
}
// --------------------------------------------------------------------------
public function testDeleteTable()
{
$this->markTestSkipped();
//Attempt to delete the table
$sql = self::$db->util->delete_table('create_delete');
self::$db->query($sql);
//Check
$table_exists = in_array('create_delete', self::$db->get_tables());
$this->assertFalse($table_exists);
}
// --------------------------------------------------------------------------
public function testTruncate()
{
$this->markTestSkipped();
self::$db->truncate('create_test');
$this->assertTrue(self::$db->affected_rows() > 0);
}
// --------------------------------------------------------------------------
public function testCommitTransaction()
{
$this->markTestSkipped();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
self::$db->query($sql);
$res = self::$db->commit();
$this->assertTrue($res);
}
// --------------------------------------------------------------------------
public function testRollbackTransaction()
{
$this->markTestSkipped();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
self::$db->query($sql);
$res = self::$db->rollback();
$this->assertTrue($res);
}
// --------------------------------------------------------------------------
public function testPreparedStatements()
{
$this->markTestSkipped();
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$query = self::$db->prepare($sql);
$query->execute(array(1,"booger's", "Gross"));
}
// --------------------------------------------------------------------------
public function testPrepareExecute()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
self::$db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
// --------------------------------------------------------------------------
/*public function testFetch()
{
$res = self::$db->query('SELECT "key","val" FROM "create_test"');
// Object
$fetchObj = $res->fetchObject();
$this->assertIsA($fetchObj, 'stdClass');
// Associative array
$fetchAssoc = $res->fetch(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($fetchAssoc));
$this->assertTrue(array_key_exists('key', $fetchAssoc));
// Numeric array
$res2 = self::$db->query('SELECT "id","key","val" FROM "create_test"');
$fetch = $res2->fetch(PDO::FETCH_NUM);
$this->assertTrue(is_array($fetch));
}*/
// --------------------------------------------------------------------------
/*public function testPrepareQuery()
{
$this->assertNull(self::$db->prepare_query('', array()));
}*/
// --------------------------------------------------------------------------
public function testErrorInfo()
{
$result = self::$db->errorInfo();
$expected = array (
0 => 0,
1 => false,
2 => false,
);
$this->assertEqual($expected, $result);
}
// --------------------------------------------------------------------------
public function testErrorCode()
{
$result = self::$db->errorCode();
$this->assertEquals(00000, $result);
}
// --------------------------------------------------------------------------
public function testDBList()
{
$res = self::$db->get_sql()->db_list();
$this->assertNULL($res);
}
// --------------------------------------------------------------------------
/*public function testExec()
{
$res = self::$db->exec('SELECT * FROM "create_test"');
$this->assertEquals(NULL, $res);
}*/
// --------------------------------------------------------------------------
public function testInTransaction()
{
$this->markTestSkipped();
self::$db->beginTransaction();
$this->assertTrue(self::$db->inTransaction());
self::$db->rollBack();
$this->assertFalse(self::$db->inTransaction());
}
// --------------------------------------------------------------------------
/*public function testGetAttribute()
{
$res = self::$db->getAttribute("foo");
$this->assertEquals(NULL, $res);
}
// --------------------------------------------------------------------------
public function testSetAttribute()
{
$this->assertFalse(self::$db->setAttribute(47, 'foo'));
}*/
public function testLastInsertId()
{
$this->markTestSkipped();
$this->assertEqual(0, self::$db->lastInsertId('NEWTABLE_SEQ'));
}
}

View File

@ -33,9 +33,7 @@ class SQLiteTest extends DBTest {
) )
); );
Query($params); self::$db = Query($params);
self::$db = Query('test_sqlite');
self::$db->table_prefix = 'create_'; self::$db->table_prefix = 'create_';
} }