Query/drivers/firebird/firebird_driver.php

285 lines
6.1 KiB
PHP
Raw Normal View History

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
*/
// --------------------------------------------------------------------------
/**
* 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
*/
class Firebird extends DB_PDO {
2012-04-10 14:06:34 -04:00
/**
* Reference to the last query executed
*
* @var object
*/
2014-03-17 19:34:48 -04:00
protected $statement = NULL;
/**
* Reference to the resource returned by
* the last query executed
*
* @var resource
*/
protected $statement_link;
/**
* Reference to the current transaction
*
* @var resource
*/
protected $trans;
/**
* Reference to the connection resource
*
* @var resource
*/
2014-03-17 19:34:48 -04:00
protected $conn = NULL;
2012-04-10 14:06:34 -04:00
/**
* Open the link to the database
*
* @param string $dbpath
2012-04-10 14:06:34 -04:00
* @param string $user
* @param string $pass
* @param array $options
2012-04-10 14:06:34 -04:00
*/
public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', $options = array())
2012-04-10 14:06:34 -04:00
{
if (isset($options[PDO::ATTR_PERSISTENT]) && $options[PDO::ATTR_PERSISTENT] == TRUE)
{
$this->conn = fbird_pconnect($dbpath, $user, $pass, 'utf-8', 0);
}
else
{
$this->conn = fbird_connect($dbpath, $user, $pass, 'utf-8', 0);
}
2012-04-10 14:06:34 -04:00
// Throw an exception to make this match other pdo classes
2014-03-17 19:34:48 -04:00
if ( ! is_resource($this->conn)) throw new PDOException(fbird_errmsg(), fbird_errcode());
// Load these classes here because this
// driver does not call the constructor
// of DB_PDO, which defines these two
// class variables for the other drivers
// Load the sql class
2012-04-10 14:06:34 -04:00
$class = __CLASS__."_sql";
$this->sql = new $class();
// Load the util class
$class = __CLASS__."_util";
$this->util = new $class($this);
2012-04-10 14:06:34 -04:00
}
// --------------------------------------------------------------------------
/**
* Empty a database table
*
* @param string $table
*/
public function truncate($table)
{
2012-11-07 08:42:34 -05:00
// Firebird lacks a truncate command
2012-04-10 14:06:34 -04:00
$sql = 'DELETE FROM "'.$table.'"';
$this->statement = $this->query($sql);
}
// --------------------------------------------------------------------------
/**
* Wrapper public function to better match PDO
*
* @param string $sql
* @return $this
* @throws PDOException
2012-04-10 14:06:34 -04:00
*/
public function query($sql)
{
2014-02-11 14:29:41 -05:00
if (empty($sql)) throw new PDOException("Query method requires an sql query!");
2012-04-10 14:06:34 -04:00
$this->statement_link = (isset($this->trans))
? fbird_query($this->trans, $sql)
: fbird_query($this->conn, $sql);
// Throw the error as a exception
2014-02-11 14:29:41 -05:00
$err_string = fbird_errmsg() . "Last query:" . $this->last_query;
2014-03-17 19:34:48 -04:00
if ($this->statement_link === FALSE) throw new PDOException($err_string, fbird_errcode());
$this->statement = new FireBird_Result($this->statement_link);
2012-04-10 14:06:34 -04:00
return $this->statement;
2012-04-10 14:06:34 -04:00
}
// --------------------------------------------------------------------------
/**
* Emulate PDO prepare
*
* @param string $query
* @param array $options
2012-04-10 14:06:34 -04:00
* @return $this
* @throws PDOException
2012-04-10 14:06:34 -04:00
*/
public function prepare($query, $options=NULL)
{
$this->statement_link = fbird_prepare($this->conn, $query);
// Throw the error as an exception
2014-03-17 19:34:48 -04:00
if ($this->statement_link === FALSE) throw new PDOException(fbird_errmsg(), fbird_errcode());
2012-04-10 14:06:34 -04:00
$this->statement = new FireBird_Result($this->statement_link);
2012-04-10 14:06:34 -04:00
return $this->statement;
2012-04-10 14:06:34 -04:00
}
// --------------------------------------------------------------------------
/**
* Start a database transaction
*
* @return bool
*/
public function beginTransaction()
{
2014-02-11 14:29:41 -05: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()
{
return fbird_commit($this->trans);
}
// --------------------------------------------------------------------------
/**
* Rollback a transaction
*
* @return bool
*/
public function rollBack()
{
return fbird_rollback($this->trans);
}
// --------------------------------------------------------------------------
/**
* Prepare and execute a query
*
* @param string $sql
* @param array $args
* @return resource
*/
public function prepare_execute($sql, $args)
{
$query = $this->prepare($sql);
// Set the statement in the class variable for easy later access
$this->statement_link =& $query;
2012-04-10 14:06:34 -04:00
return $query->execute($args);
}
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->quote
*
* @param string $str
* @param int $param_type
2012-04-10 14:06:34 -04:00
* @return string
*/
public function quote($str, $param_type = NULL)
{
if(is_numeric($str))
{
return $str;
}
return "'".str_replace("'", "''", $str)."'";
}
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
*
* @return array
*/
public function errorInfo()
{
$code = fbird_errcode();
$msg = fbird_errmsg();
return array(0, $code, $msg);
}
2012-04-18 11:42:19 -04:00
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->errorCode
*
* @return array
*/
public function errorCode()
{
return fbird_errcode();
}
2012-04-10 14:06:34 -04:00
// --------------------------------------------------------------------------
/**
* Bind a prepared query with arguments for executing
*
* @param string $sql
* @param array $params
* @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
return NULL;
2012-04-10 14:06:34 -04:00
}
// --------------------------------------------------------------------------
/**
* Create sql for batch insert
*
* @param string $table
* @param array $data
* @return string
*/
public function insert_batch($table, $data=array())
{
// This is not very applicable to the firebird database
return NULL;
}
2012-04-10 14:06:34 -04:00
}
// End of firebird_driver.php