2012-04-10 14:06:34 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
2012-04-20 13:17:39 -04:00
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
2014-01-02 12:36:50 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
2012-04-10 14:06:34 -04:00
|
|
|
* @link https://github.com/aviat4ion/Query
|
2012-04-20 13:17:39 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
namespace Query\Driver;
|
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
/**
|
|
|
|
* Firebird Database class
|
|
|
|
*
|
|
|
|
* PDO-firebird isn't stable, so this is a wrapper of the fbird_ public functions.
|
2012-04-20 13:17:39 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @subpackage Drivers
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
class Firebird extends Abstract_Driver {
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
/**
|
|
|
|
* Reference to the last query executed
|
2012-04-20 16:33:40 -04:00
|
|
|
*
|
|
|
|
* @var object
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2014-03-17 19:34:48 -04:00
|
|
|
protected $statement = NULL;
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
/**
|
|
|
|
* Reference to the resource returned by
|
|
|
|
* the last query executed
|
2012-04-20 16:33:40 -04:00
|
|
|
*
|
|
|
|
* @var resource
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
protected $statement_link = NULL;
|
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
/**
|
|
|
|
* Reference to the current transaction
|
2012-04-20 16:33:40 -04:00
|
|
|
*
|
|
|
|
* @var resource
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
protected $trans = NULL;
|
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
/**
|
|
|
|
* Reference to the connection resource
|
2012-04-20 16:33:40 -04:00
|
|
|
*
|
|
|
|
* @var resource
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2014-03-17 19:34:48 -04:00
|
|
|
protected $conn = NULL;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-08 14:43:07 -04:00
|
|
|
/**
|
|
|
|
* Reference to the service resource
|
|
|
|
*
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
protected $service = NULL;
|
|
|
|
|
2014-04-24 16:25:04 -04:00
|
|
|
/**
|
|
|
|
* Firebird doesn't have the truncate keyword
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $has_truncate = FALSE;
|
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
/**
|
|
|
|
* Open the link to the database
|
|
|
|
*
|
2012-04-19 11:42:50 -04:00
|
|
|
* @param string $dbpath
|
2012-04-10 14:06:34 -04:00
|
|
|
* @param string $user
|
|
|
|
* @param string $pass
|
2014-02-18 14:50:20 -05:00
|
|
|
* @param array $options
|
2014-04-22 14:02:54 -04:00
|
|
|
* @throws \PDOException
|
|
|
|
* @return void
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = array())
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2014-04-02 10:31:59 -04:00
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
$connect_function = (isset($options[\PDO::ATTR_PERSISTENT]) && $options[\PDO::ATTR_PERSISTENT] == TRUE)
|
|
|
|
? '\\fbird_pconnect'
|
|
|
|
: '\\fbird_connect';
|
2014-04-02 10:31:59 -04:00
|
|
|
|
|
|
|
$this->conn = $connect_function($dbpath, $user, $pass, 'utf-8', 0);
|
2014-04-10 15:54:43 -04:00
|
|
|
$this->service = \fbird_service_attach('localhost', $user, $pass);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
// Throw an exception to make this match other pdo classes
|
2014-04-02 17:08:50 -04:00
|
|
|
if ( ! \is_resource($this->conn)) throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-04-18 15:53:06 -04:00
|
|
|
// Load these classes here because this
|
|
|
|
// driver does not call the constructor
|
2014-04-10 15:54:43 -04:00
|
|
|
// of DB_PDO, which defines these
|
2012-04-18 15:53:06 -04:00
|
|
|
// class variables for the other drivers
|
2014-04-24 15:32:09 -04:00
|
|
|
$this->_load_sub_classes();
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-08 14:43:07 -04:00
|
|
|
/**
|
|
|
|
* Cleanup some loose ends
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
\fbird_service_detach($this->service);
|
2014-04-08 14:43:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return service handle
|
|
|
|
*
|
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
public function get_service()
|
|
|
|
{
|
|
|
|
return $this->service;
|
|
|
|
}
|
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-03-26 20:49:33 -04:00
|
|
|
/**
|
|
|
|
* Execute an sql statement and return number of affected rows
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function exec($sql)
|
|
|
|
{
|
2014-03-31 16:01:58 -04:00
|
|
|
return NULL;
|
2014-03-26 20:49:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement for compatibility with PDO
|
|
|
|
*
|
|
|
|
* @param int $attribute
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAttribute($attribute)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the current statement is in a transaction
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function inTransaction()
|
|
|
|
{
|
|
|
|
return ! is_null($this->trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last value of the specified generator
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function lastInsertId($name = NULL)
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
return \fbird_gen_id($name, 0, $this->conn);
|
2014-03-26 20:49:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
/**
|
|
|
|
* Wrapper public function to better match PDO
|
|
|
|
*
|
|
|
|
* @param string $sql
|
2014-03-17 19:35:25 -04:00
|
|
|
* @return Firebird_Result
|
2014-02-10 13:35:57 -05:00
|
|
|
* @throws PDOException
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
public function query($sql = '')
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
if (empty($sql)) throw new \PDOException("Query method requires an sql query!", 0, NULL);
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
$this->statement_link = (isset($this->trans))
|
2014-04-02 17:08:50 -04:00
|
|
|
? \fbird_query($this->trans, $sql)
|
|
|
|
: \fbird_query($this->conn, $sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
// Throw the error as a exception
|
2014-04-02 17:08:50 -04:00
|
|
|
$err_string = \fbird_errmsg() . "Last query:" . $this->last_query;
|
|
|
|
if ($this->statement_link === FALSE) throw new \PDOException($err_string, \fbird_errcode(), NULL);
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2014-04-09 13:20:30 -04:00
|
|
|
$this->statement = new FireBird_Result($this->statement_link, $this);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-20 16:33:40 -04:00
|
|
|
return $this->statement;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emulate PDO prepare
|
|
|
|
*
|
|
|
|
* @param string $query
|
2012-04-19 11:42:50 -04:00
|
|
|
* @param array $options
|
2014-03-17 19:35:25 -04:00
|
|
|
* @return Firebird_Result
|
2014-04-22 14:02:54 -04:00
|
|
|
* @throws \PDOException
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
public function prepare($query, $options=array())
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
$this->statement_link = \fbird_prepare($this->conn, $query);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
// Throw the error as an exception
|
2014-04-02 17:08:50 -04:00
|
|
|
if ($this->statement_link === FALSE) throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-09 13:20:30 -04:00
|
|
|
$this->statement = new FireBird_Result($this->statement_link, $this);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-20 16:33:40 -04:00
|
|
|
return $this->statement;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a database transaction
|
|
|
|
*
|
2014-03-17 19:35:25 -04:00
|
|
|
* @return boolean|null
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
|
|
|
public function beginTransaction()
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
return (($this->trans = \fbird_trans($this->conn)) !== NULL) ? TRUE : NULL;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit a database transaction
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function commit()
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
$res = \fbird_commit($this->trans);
|
2014-03-26 20:49:33 -04:00
|
|
|
$this->trans = NULL;
|
|
|
|
return $res;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback a transaction
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function rollBack()
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
$res = \fbird_rollback($this->trans);
|
2014-03-26 20:49:33 -04:00
|
|
|
$this->trans = NULL;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a connection attribute
|
|
|
|
* @param int $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setAttribute($attribute, $value)
|
|
|
|
{
|
|
|
|
return FALSE;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare and execute a query
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $args
|
2014-03-17 19:35:25 -04:00
|
|
|
* @return Firebird_Result
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
|
|
|
public function prepare_execute($sql, $args)
|
|
|
|
{
|
|
|
|
$query = $this->prepare($sql);
|
|
|
|
|
|
|
|
// Set the statement in the class variable for easy later access
|
2014-03-26 20:49:33 -04:00
|
|
|
$this->statement_link =& $query;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
return $query->execute($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to emulate PDO->quote
|
|
|
|
*
|
|
|
|
* @param string $str
|
2012-04-19 11:42:50 -04:00
|
|
|
* @param int $param_type
|
2012-04-10 14:06:34 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-02 17:08:50 -04:00
|
|
|
public function quote($str, $param_type = \PDO::PARAM_STR)
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if(is_numeric($str))
|
|
|
|
{
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "'".str_replace("'", "''", $str)."'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function errorInfo()
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
$code = \fbird_errcode();
|
|
|
|
$msg = \fbird_errmsg();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
return array(0, $code, $msg);
|
|
|
|
}
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-04-18 11:42:19 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-04-18 11:42:19 -04:00
|
|
|
/**
|
2014-03-26 20:49:33 -04:00
|
|
|
* Method to emulate PDO->errorCode
|
2012-04-18 11:42:19 -04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function errorCode()
|
|
|
|
{
|
2014-04-22 14:02:54 -04:00
|
|
|
return \fbird_errcode();
|
2012-04-18 11:42:19 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind a prepared query with arguments for executing
|
|
|
|
*
|
2012-04-19 11:42:50 -04:00
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
2013-05-01 15:59:23 -04:00
|
|
|
* @return NULL
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
|
|
|
public function prepare_query($sql, $params)
|
|
|
|
{
|
|
|
|
// You can't bind query statements before execution with
|
|
|
|
// the firebird database
|
2013-05-01 15:59:23 -04:00
|
|
|
return NULL;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2013-05-03 13:07:34 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-03-26 20:49:33 -04:00
|
|
|
|
|
|
|
/**
|
2013-05-03 13:07:34 -04:00
|
|
|
* Create sql for batch insert
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $data
|
2014-04-03 13:28:30 -04:00
|
|
|
* @return array
|
2013-05-03 13:07:34 -04:00
|
|
|
*/
|
|
|
|
public function insert_batch($table, $data=array())
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
// 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);
|
2014-04-03 14:44:03 -04:00
|
|
|
$fields = \array_keys(\current($data));
|
2014-04-03 13:28:30 -04:00
|
|
|
|
|
|
|
$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";
|
|
|
|
|
2014-04-24 20:14:19 -04:00
|
|
|
// Return a null array value so the query is run as it is,
|
2014-04-03 13:28:30 -04:00
|
|
|
// not as a prepared statement, because a prepared statement
|
|
|
|
// doesn't work for this type of query in Firebird.
|
|
|
|
return array($sql, NULL);
|
2013-05-03 13:07:34 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-10 13:35:57 -05:00
|
|
|
// End of firebird_driver.php
|