OpenSQLManager/sys/db/drivers/firebird.php

554 lines
11 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
// --------------------------------------------------------------------------
/**
* 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
* @param array $params
* @return $this
*/
public function query($sql)
{
$this->count = 0;
2012-03-28 11:23:08 -04:00
2012-03-06 16:53:40 -05:00
if (isset($this->trans))
{
$this->statement_link = @fbird_query($this->trans, $sql);
2012-03-06 16:53:40 -05:00
}
else
{
$this->statement_link = @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
// --------------------------------------------------------------------------
/**
* List tables for the current database
2012-03-28 11:23:08 -04:00
*
* @return array
*/
public function get_tables()
2012-03-28 11:23:08 -04:00
{
$sql = <<<SQL
2012-03-28 11:23:08 -04:00
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
SQL;
$this->statement = $this->query($sql);
2012-03-28 11:23:08 -04:00
$tables = array();
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['RDB$RELATION_NAME'];
}
2012-03-28 11:23:08 -04:00
return $tables;
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
/**
* Get list of views for the current database
*
* @return array
*/
public function get_views()
{
// @todo Implement
return FALSE;
}
// --------------------------------------------------------------------------
2012-04-06 16:10:16 -04:00
/**
* Get list of sequences for the current database
*
* @return array
*/
public function get_sequences()
{
// @todo Implement
return FALSE;
}
// --------------------------------------------------------------------------
2012-04-03 11:23:53 -04:00
/**
* Not applicable to firebird
*
* @return FALSE
*/
public function get_dbs()
{
return FALSE;
}
// --------------------------------------------------------------------------
/**
* List system tables for the current database
2012-03-28 11:23:08 -04:00
*
* @return array
*/
public function get_system_tables()
{
$sql = <<<SQL
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
WHERE "RDB\$RELATION_NAME" LIKE 'RDB$%'
OR "RDB\$RELATION_NAME" LIKE 'MON$%';
SQL;
$this->statement = $this->query($sql);
$tables = array();
2012-03-15 14:04:45 -04:00
while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['RDB$RELATION_NAME'];
}
2012-03-28 11:23:08 -04:00
return $tables;
}
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
*/
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
*
* @param string $sql
* @param mixed $args
* @return FALSE
*/
public function prepare_query($sql, $args)
{
// You can't bind query statements before execution with
// the firebird database
return FALSE;
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
2012-03-28 11:23:08 -04:00
return '';
}
2012-03-28 11:23:08 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
/**
* Create an SQL backup file for the current database's data
*
* @param array $exclude
* @param bool $system_tables
* @return string
*/
public function backup_data($exclude=array(), $system_tables=FALSE)
{
// Determine which tables to use
if($system_tables == TRUE)
{
$tables = array_merge($this->get_system_tables(), $this->get_tables());
}
else
{
$tables = $this->get_tables();
}
2012-03-28 11:23:08 -04:00
// Filter out the tables you don't want
if( ! empty($exclude))
{
$tables = array_diff($tables, $exclude);
}
2012-03-28 11:23:08 -04:00
$output_sql = '';
2012-03-28 11:23:08 -04:00
// Get the data for each object
foreach($tables as $t)
{
$sql = 'SELECT * FROM "'.trim($t).'"';
$res = $this->query($sql);
$obj_res = $this->fetchAll(PDO::FETCH_ASSOC);
2012-03-28 11:23:08 -04:00
unset($res);
2012-03-28 11:23:08 -04:00
// Nab the column names by getting the keys of the first row
$columns = @array_keys($obj_res[0]);
2012-03-28 11:23:08 -04:00
$insert_rows = array();
2012-03-28 11:23:08 -04:00
// Create the insert statements
foreach($obj_res as $row)
{
$row = array_values($row);
2012-03-28 11:23:08 -04:00
// Quote values as needed by type
if(stripos($t, 'RDB$') === FALSE)
{
$row = array_map(array(&$this, 'quote'), $row);
$row = array_map('trim', $row);
}
2012-03-28 11:23:08 -04:00
$row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
2012-03-28 11:23:08 -04:00
unset($row);
2012-03-28 11:23:08 -04:00
$insert_rows[] = $row_string;
}
2012-03-28 11:23:08 -04:00
unset($obj_res);
2012-03-28 11:23:08 -04:00
$output_sql .= "\n\nSET TRANSACTION;\n".implode("\n", $insert_rows)."\nCOMMIT;";
}
2012-03-28 11:23:08 -04:00
return $output_sql;
}
}
2012-03-15 14:04:45 -04:00
// --------------------------------------------------------------------------
/**
* Firebird result class to emulate PDOStatement Class
*/
class Firebird_Result {
private $statement;
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
/**
* Create the object by passing the resource for
* the query
*
* @param resource $link
*/
public function __construct($link)
{
$this->statement = $link;
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
// --------------------------------------------------------------------------
/**
* Emulate PDO fetch public function
2012-03-28 11:23:08 -04:00
*
2012-03-15 14:04:45 -04:00
* @param int $fetch_style
* @return mixed
*/
public function fetch($fetch_style=PDO::FETCH_ASSOC, $statement=NULL)
{
if ( ! is_null($statement))
{
$this->statement = $statement;
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
switch($fetch_style)
{
case PDO::FETCH_OBJ:
return fbird_fetch_object($this->statement, IBASE_FETCH_BLOBS);
2012-03-15 14:04:45 -04:00
break;
case PDO::FETCH_NUM:
return fbird_fetch_row($this->statement, IBASE_FETCH_BLOBS);
2012-03-15 14:04:45 -04:00
break;
default:
return fbird_fetch_assoc($this->statement, IBASE_FETCH_BLOBS);
2012-03-15 14:04:45 -04:00
break;
}
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
// --------------------------------------------------------------------------
/**
* Emulate PDO fetchAll public function
2012-03-28 11:23:08 -04:00
*
2012-03-15 14:04:45 -04:00
* @param int $fetch_style
* @return mixed
*/
public function fetchAll($fetch_style=PDO::FETCH_ASSOC, $statement=NULL)
{
$all = array();
while($row = $this->fetch($fetch_style, $statement))
{
$all[] = $row;
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
$this->result = $all;
return $all;
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
/**
* Run a prepared statement query
2012-03-28 11:23:08 -04:00
*
2012-03-15 14:04:45 -04:00
* @param array $args
* @return bool
*/
public function execute($args)
{
//Add the prepared statement as the first parameter
array_unshift($args, $this->statement);
2012-03-28 11:23:08 -04:00
// Let php do all the hard stuff in converting
2012-03-15 14:04:45 -04:00
// the array of arguments into a list of arguments
2012-03-19 12:20:51 -04:00
$this->__construct(call_user_func_array('fbird_execute', $args));
return $this;
2012-03-15 14:04:45 -04:00
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
// --------------------------------------------------------------------------
/**
* Return the number of rows affected by the previous query
2012-03-28 11:23:08 -04:00
*
2012-03-15 14:04:45 -04:00
* @return int
*/
public function rowCount($statement="")
{
2012-04-06 16:10:16 -04:00
$statement = $statement OR $this->statement;
return fbird_affected_rows($statement);
2012-03-15 14:04:45 -04:00
}
2012-03-28 11:23:08 -04:00
2012-03-15 14:04:45 -04:00
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
*
* @return array
*/
public function errorInfo()
{
$code = fbird_errcode();
$msg = fbird_errmsg();
2012-03-15 14:04:45 -04:00
return array(0, $code, $msg);
}
}
// End of firebird-ibase.php