OpenSQLManager/sys/db/drivers/firebird/firebird_driver.php

257 lines
5.2 KiB
PHP
Raw Normal View History

<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
2012-03-28 11:23:08 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Firebird Database class
2012-03-28 11:23:08 -04:00
*
* PDO-firebird isn't stable, so this is a wrapper of the fbird_ public functions.
*/
class firebird extends DB_PDO {
2012-03-15 14:04:45 -04:00
protected $statement, $statement_link, $trans, $count, $result, $conn;
2012-03-28 11:23:08 -04:00
/**
* Open the link to the database
2012-03-28 11:23:08 -04:00
*
* @param string $db
2012-03-28 11:23:08 -04:00
* @param string $user
* @param string $pass
*/
public function __construct($dbpath, $user='sysdba', $pass='masterkey')
{
$this->conn = @fbird_connect($dbpath, $user, $pass, 'utf-8');
2012-03-07 11:26:45 -05:00
// Throw an exception to make this match other pdo classes
2012-03-08 12:37:33 -05:00
if ( ! is_resource($this->conn))
{
throw new PDOException(fbird_errmsg());
die();
}
2012-03-28 11:23:08 -04:00
$class = __CLASS__."_sql";
$this->sql = new $class;
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
/**
2012-03-28 11:23:08 -04:00
* Close the link to the database and any existing results
*/
public function __destruct()
{
@fbird_close();
@fbird_free_result($this->statement);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-04-09 15:24:10 -04:00
/**
* Doesn't apply to Firebird
*/
public function switch_db($name)
{
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Empty a database table
2012-03-28 11:23:08 -04:00
*
* @param string $table
*/
public function truncate($table)
{
// Firebird lacka a truncate command
$sql = 'DELETE FROM "'.$table.'"';
$this->statement = $this->query($sql);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Wrapper public function to better match PDO
*
* @param string $sql
* @return $this
*/
public function query($sql)
{
$this->count = 0;
2012-03-28 11:23:08 -04:00
2012-04-09 10:34:57 -04:00
$this->statement_link = (isset($this->trans))
? @fbird_query($this->trans, $sql)
: @fbird_query($this->conn, $sql);
2012-03-07 11:26:45 -05:00
// Throw the error as a exception
2012-03-15 14:04:45 -04:00
if ($this->statement_link === FALSE)
2012-03-07 11:26:45 -05:00
{
throw new PDOException(fbird_errmsg());
2012-03-08 13:29:30 -05:00
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
return new FireBird_Result($this->statement_link);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
/**
* Emulate PDO prepare
*
* @param string $query
* @return $this
*/
public function prepare($query, $options=NULL)
{
$this->statement_link = @fbird_prepare($this->conn, $query);
2012-03-07 11:26:45 -05:00
// Throw the error as an exception
2012-03-15 14:04:45 -04:00
if ($this->statement_link === FALSE)
2012-03-07 11:26:45 -05:00
{
throw new PDOException(fbird_errmsg());
2012-03-08 13:29:30 -05:00
}
2012-03-07 11:26:45 -05:00
2012-03-15 14:04:45 -04:00
return new FireBird_Result($this->statement_link);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
2012-03-28 11:23:08 -04:00
*
* @return int
*/
public function num_rows()
{
// @todo: Redo this similar to the codeigniter driver
if(isset($this->result))
{
return count($this->result);
}
//Fetch all the rows for the result
2012-03-15 14:04:45 -04:00
$this->result = $this->statement->fetchAll();
return count($this->result);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Start a database transaction
2012-03-28 11:23:08 -04:00
*
* @return bool
*/
public function beginTransaction()
{
if(($this->trans = fbird_trans($this->conn)) !== NULL)
{
return TRUE;
}
return FALSE;
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Commit a database transaction
2012-03-28 11:23:08 -04:00
*
* @return bool
*/
public function commit()
{
return fbird_commit($this->trans);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Rollback a transaction
2012-03-28 11:23:08 -04:00
*
* @return bool
*/
public function rollBack()
{
return fbird_rollback($this->trans);
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Prepare and execute a query
*
* @param string $sql
* @param array $args
* @return resource
*/
public function prepare_execute($sql, $args)
{
$query = $this->prepare($sql);
2012-03-28 11:23:08 -04:00
// Set the statement in the class variable for easy later access
$this->statement =& $query;
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
return $query->execute($args);
}
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->quote
2012-03-28 11:23:08 -04:00
*
* @param string $str
* @return string
*/
2012-04-06 20:05:30 -04:00
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-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Bind a prepared query with arguments for executing
*
* @return FALSE
*/
2012-04-06 20:05:30 -04:00
public function prepare_query($sql, $params)
{
// You can't bind query statements before execution with
// the firebird database
return FALSE;
}
}
2012-04-09 15:41:48 -04:00
// End of firebird_driver.php