2012-03-05 15:15:27 -05:00
|
|
|
<?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
|
2012-03-05 15:15:27 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Firebird Database class
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-19 12:04:10 -04:00
|
|
|
* PDO-firebird isn't stable, so this is a wrapper of the fbird_ public functions.
|
2012-03-05 15:15:27 -05:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
/**
|
|
|
|
* Open the link to the database
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @param string $db
|
2012-03-28 11:23:08 -04:00
|
|
|
* @param string $user
|
2012-03-05 15:15:27 -05:00
|
|
|
* @param string $pass
|
|
|
|
*/
|
|
|
|
public function __construct($dbpath, $user='sysdba', $pass='masterkey')
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
$this->conn = @fbird_connect($dbpath, $user, $pass, 'utf-8');
|
2012-03-06 21:32:49 -05:00
|
|
|
|
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))
|
2012-03-06 21:32:49 -05:00
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
throw new PDOException(fbird_errmsg());
|
2012-03-07 15:21:44 -05:00
|
|
|
die();
|
2012-03-06 21:32:49 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
$class = __CLASS__."_sql";
|
|
|
|
$this->sql = new $class;
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2012-03-28 11:23:08 -04:00
|
|
|
* Close the link to the database and any existing results
|
2012-03-05 15:15:27 -05:00
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
@fbird_close();
|
|
|
|
@fbird_free_result($this->statement);
|
2012-03-05 15:15:27 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-09 15:24:10 -04:00
|
|
|
/**
|
|
|
|
* Doesn't apply to Firebird
|
|
|
|
*/
|
|
|
|
public function switch_db($name)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
/**
|
|
|
|
* Empty a database table
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @param string $table
|
|
|
|
*/
|
|
|
|
public function truncate($table)
|
|
|
|
{
|
|
|
|
// Firebird lacka a truncate command
|
|
|
|
$sql = 'DELETE FROM "'.$table.'"';
|
2012-03-12 16:09:12 -04:00
|
|
|
$this->statement = $this->query($sql);
|
2012-03-05 15:15:27 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05: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
|
|
|
{
|
2012-03-19 12:04:10 -04: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-05 15:15:27 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emulate PDO prepare
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function prepare($query, $options=NULL)
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
$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
|
|
|
{
|
2012-03-19 12:04:10 -04: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-05 15:15:27 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List tables for the current database
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_tables()
|
2012-03-28 11:23:08 -04:00
|
|
|
{
|
2012-03-05 15:15:27 -05:00
|
|
|
$sql = <<<SQL
|
2012-03-28 11:23:08 -04:00
|
|
|
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
|
2012-03-05 15:15:27 -05:00
|
|
|
WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
|
|
|
|
AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
|
2012-04-06 20:05:30 -04:00
|
|
|
AND "RDB\$VIEW_BLR" IS NOT NULL
|
2012-04-06 16:17:07 -04:00
|
|
|
ORDER BY "RDB\$RELATION_NAME" ASC
|
2012-03-05 15:15:27 -05:00
|
|
|
SQL;
|
|
|
|
|
|
|
|
$this->statement = $this->query($sql);
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05: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))
|
2012-03-05 15:15:27 -05:00
|
|
|
{
|
|
|
|
$tables[] = $row['RDB$RELATION_NAME'];
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
return $tables;
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-04 10:47:14 -04:00
|
|
|
/**
|
|
|
|
* Get list of views for the current database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_views()
|
|
|
|
{
|
2012-04-06 19:22:52 -04:00
|
|
|
$sql = <<<SQL
|
|
|
|
SELECT "RDB\$RELATION_NAME"
|
|
|
|
FROM "RDB\$RELATIONS"
|
|
|
|
WHERE "RDB\$VIEW_BLR" IS NOT NULL
|
|
|
|
AND ("RDB\$SYSTEM_FLAG" IS NULL OR "RDB\$SYSTEM_FLAG" = 0)
|
|
|
|
SQL;
|
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'RDB$RELATION_NAME');
|
2012-04-04 10:47:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-06 16:10:16 -04:00
|
|
|
/**
|
|
|
|
* Get list of sequences for the current database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_sequences()
|
2012-04-06 19:22:52 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
SELECT "RDB\$GENERATOR_NAME"
|
|
|
|
FROM "RDB\$GENERATORS"
|
2012-04-06 20:05:30 -04:00
|
|
|
WHERE "RDB\$SYSTEM_FLAG" = 0
|
2012-04-06 19:22:52 -04:00
|
|
|
SQL;
|
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'RDB$GENERATOR_NAME');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return list of custom functions for the current database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_functions()
|
2012-04-06 16:10:16 -04:00
|
|
|
{
|
2012-04-06 20:05:30 -04:00
|
|
|
$sql = <<<SQL
|
|
|
|
SELECT * FROM "RDB\$TRIGGERS"
|
|
|
|
WHERE "RDB\$SYSTEM_FLAG" = 0
|
|
|
|
SQL;
|
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
return $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-04-06 16:10:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-06 21:07:49 -04:00
|
|
|
/**
|
|
|
|
* Retrun list of stored procedures for the current database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_procedures()
|
|
|
|
{
|
|
|
|
$sql = 'SELECT * FROM "RDB$PROCEDURES"';
|
2012-04-09 10:34:57 -04:00
|
|
|
|
2012-04-06 21:07:49 -04:00
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
return $res->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-06 19:22:52 -04:00
|
|
|
/**
|
|
|
|
* Return list of triggers for the current database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_triggers()
|
|
|
|
{
|
2012-04-06 20:05:30 -04:00
|
|
|
$sql = <<<SQL
|
|
|
|
SELECT * FROM "RDB\$FUNCTIONS"
|
|
|
|
WHERE "RDB\$SYSTEM_FLAG" = 0
|
|
|
|
SQL;
|
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
return $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-04-06 19:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2012-04-03 11:23:53 -04:00
|
|
|
/**
|
|
|
|
* Not applicable to firebird
|
|
|
|
*
|
|
|
|
* @return FALSE
|
|
|
|
*/
|
|
|
|
public function get_dbs()
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
/**
|
|
|
|
* List system tables for the current database
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05: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))
|
2012-03-05 15:15:27 -05:00
|
|
|
{
|
|
|
|
$tables[] = $row['RDB$RELATION_NAME'];
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
return $tables;
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of rows returned for a SELECT query
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05: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();
|
2012-03-05 15:15:27 -05:00
|
|
|
|
|
|
|
return count($this->result);
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
/**
|
|
|
|
* Start a database transaction
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function beginTransaction()
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
if(($this->trans = fbird_trans($this->conn)) !== NULL)
|
2012-03-05 15:15:27 -05:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
/**
|
|
|
|
* Commit a database transaction
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function commit()
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
return fbird_commit($this->trans);
|
2012-03-05 15:15:27 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
/**
|
|
|
|
* Rollback a transaction
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function rollBack()
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
return fbird_rollback($this->trans);
|
2012-03-05 15:15:27 -05:00
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05: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
|
|
|
|
2012-03-05 15:15:27 -05: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);
|
2012-03-05 15:15:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to emulate PDO->quote
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-03-05 15:15:27 -05:00
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-06 20:05:30 -04:00
|
|
|
public function quote($str, $param_type = NULL)
|
2012-03-05 15:15:27 -05:00
|
|
|
{
|
|
|
|
if(is_numeric($str))
|
|
|
|
{
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "'".str_replace("'", "''", $str)."'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function errorInfo()
|
|
|
|
{
|
2012-03-19 12:04:10 -04:00
|
|
|
$code = fbird_errcode();
|
|
|
|
$msg = fbird_errmsg();
|
2012-03-05 15:15:27 -05:00
|
|
|
|
|
|
|
return array(0, $code, $msg);
|
|
|
|
}
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-03-05 15:15:27 -05: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)
|
2012-03-05 15:15:27 -05:00
|
|
|
{
|
|
|
|
// 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
|