OpenSQLManager/sys/db/db_pdo.php

356 lines
7.5 KiB
PHP
Raw Normal View History

2012-02-28 14:58:52 -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-02-28 14:58:52 -05:00
*/
// --------------------------------------------------------------------------
/**
* Base Database class
*
* Extends PDO to simplify cross-database issues
2012-03-28 11:23:08 -04:00
*
* @abstract
2012-02-28 14:58:52 -05:00
*/
abstract class DB_PDO extends PDO {
public $manip;
protected $statement;
/**
* PDO constructor wrapper
*/
2012-02-28 14:58:52 -05:00
public function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array())
{
parent::__construct($dsn, $username, $password, $driver_options);
2012-04-10 10:47:37 -04:00
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2012-02-28 14:58:52 -05:00
}
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
// -------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
/**
* Simplifies prepared statements for database queries
*
* @param string $sql
* @param array $data
* @return mixed PDOStatement / FALSE
*/
public function prepare_query($sql, $data)
{
// Prepare the sql
$query = $this->prepare($sql);
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
if( ! (is_object($query) || is_resource($query)))
{
$this->get_last_error();
return FALSE;
}
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
// Set the statement in the class variable for easy later access
$this->statement =& $query;
2012-03-28 11:23:08 -04:00
if( ! (is_array($data) || is_object($data)))
2012-02-28 14:58:52 -05:00
{
trigger_error("Invalid data argument");
return FALSE;
}
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
// Bind the parameters
foreach($data as $k => $value)
{
if(is_numeric($k))
{
$k++;
}
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
$res = $query->bindValue($k, $value);
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
if( ! $res)
{
trigger_error("Parameter not successfully bound");
return FALSE;
}
}
2012-03-28 11:23:08 -04:00
return $query;
2012-02-28 14:58:52 -05:00
}
// -------------------------------------------------------------------------
/**
* Create and execute a prepared statement with the provided parameters
*
* @param string $sql
* @param array $params
* @return PDOStatement
*/
public function prepare_execute($sql, $params)
2012-03-28 11:23:08 -04:00
{
$this->statement = $this->prepare_query($sql, $params);
2012-02-28 14:58:52 -05:00
$this->statement->execute();
return $this->statement;
}
// -------------------------------------------------------------------------
/**
* Retreives the data from a select query
*
* @param PDOStatement $statement
* @return array
*/
public function get_query_data($statement)
{
2012-03-09 11:56:14 -05:00
$this->statement =& $statement;
2012-02-28 14:58:52 -05:00
// Execute the query
$this->statement->execute();
// Return the data array fetched
return $this->statement->fetchAll(PDO::FETCH_ASSOC);
}
// -------------------------------------------------------------------------
/**
* Returns number of rows affected by an INSERT, UPDATE, DELETE type query
*
* @param PDOStatement $statement
* @return int
*/
public function affected_rows($statement='')
2012-02-28 14:58:52 -05:00
{
if ( ! empty($statement))
2012-03-28 11:23:08 -04:00
{
$this->statement = $statement;
}
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
// Return number of rows affected
return $this->statement->rowCount();
2012-02-28 14:58:52 -05:00
}
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
// --------------------------------------------------------------------------
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
/**
* Return the last error for the current database connection
*
* @return string
*/
public function get_last_error()
{
$info = $this->errorInfo();
2012-03-28 11:23:08 -04:00
2012-02-28 14:58:52 -05:00
echo "Error: <pre>{$info[0]}:{$info[1]}\n{$info[2]}</pre>";
}
// --------------------------------------------------------------------------
/**
* Surrounds the string with the databases identifier escape characters
*
2012-03-09 08:04:47 -05:00
* @param mixed $ident
* @return string
*/
public function quote_ident($ident)
{
2012-03-09 08:04:47 -05:00
if (is_array($ident))
{
2012-03-09 08:26:16 -05:00
return array_map(array($this, 'quote_ident'), $ident);
2012-03-09 08:04:47 -05:00
}
// Split each identifier by the period
$hiers = explode('.', $ident);
return '"'.implode('"."', $hiers).'"';
}
2012-02-28 14:58:52 -05:00
// -------------------------------------------------------------------------
/**
* Deletes all the rows from a table. Does the same as the truncate
* method if the database does not support 'TRUNCATE';
*
* @param string $table
* @return mixed
*/
public function empty_table($table)
{
$sql = 'DELETE FROM '.$this->quote_ident($table);
return $this->query($sql);
}
// -------------------------------------------------------------------------
2012-02-28 14:58:52 -05:00
/**
* Return schemas for databases that list them
*
* @return array
2012-02-28 14:58:52 -05:00
*/
public function get_schemas()
{
return FALSE;
}
2012-04-09 16:11:14 -04:00
// -------------------------------------------------------------------------
/**
* Method to simplify retreiving db results for meta-data queries
*
* @param string $sql
2012-04-09 16:45:59 -04:00
* @param bool $filtered_index
* @return mixed
2012-04-09 16:11:14 -04:00
*/
2012-04-09 16:45:59 -04:00
protected function driver_query($sql, $filtered_index=TRUE)
2012-04-09 16:11:14 -04:00
{
2012-04-09 16:45:59 -04:00
if ($sql === FALSE)
2012-04-09 16:11:14 -04:00
{
2012-04-09 16:45:59 -04:00
return FALSE;
2012-04-09 16:11:14 -04:00
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
$res = $this->query($sql);
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
$flag = ($filtered_index) ? PDO::FETCH_NUM : PDO::FETCH_ASSOC;
$all = $res->fetchAll($flag);
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
return ($filtered_index) ? db_filter($all, 0) : $all;
2012-04-09 16:11:14 -04:00
}
2012-04-10 10:47:37 -04:00
// -------------------------------------------------------------------------
2012-04-10 10:47:37 -04:00
2012-02-28 14:58:52 -05:00
/**
* Return list of tables for the current database
2012-03-28 11:23:08 -04:00
*
2012-02-28 14:58:52 -05:00
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_tables()
{
return $this->driver_query($this->sql->table_list());
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-02-28 14:58:52 -05:00
2012-04-03 11:23:53 -04:00
/**
* Return list of dbs for the current connection, if possible
*
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_dbs()
{
return $this->driver_query($this->sql->db_list());
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-04-03 11:23:53 -04:00
/**
* Return list of views for the current database
*
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_views()
{
return $this->driver_query($this->sql->view_list());
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-04-06 16:10:16 -04:00
/**
* Return list of sequences for the current database, if they exist
*
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_sequences()
{
return $this->driver_query($this->sql->sequence_list());
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-04-06 16:10:16 -04:00
2012-04-06 19:22:52 -04:00
/**
* Return list of function for the current database
*
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_functions()
{
return $this->driver_query($this->sql->function_list(), FALSE);
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-04-06 19:22:52 -04:00
2012-04-06 21:07:49 -04:00
/**
* Return list of stored procedures for the current database
*
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_procedures()
{
return $this->driver_query($this->sql->procedure_list(), FALSE);
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-04-06 21:07:49 -04:00
2012-04-06 19:22:52 -04:00
/**
* Return list of triggers for the current database
*
* @return array
*/
2012-04-09 16:45:59 -04:00
public function get_triggers()
{
return $this->driver_query($this->sql->trigger_list(), FALSE);
}
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
// -------------------------------------------------------------------------
2012-04-10 10:47:37 -04:00
2012-04-09 16:45:59 -04:00
/**
* Retreives an array of non-user-created tables for
* the connection/database
*
* @return array
*/
public function get_system_tables()
{
return $this->driver_query($this->sql->system_table_list());
}
// -------------------------------------------------------------------------
// ! Abstract public functions to override in child classes
// -------------------------------------------------------------------------
2012-04-06 19:22:52 -04:00
2012-02-28 14:58:52 -05:00
/**
* Empty the passed table
2012-03-28 11:23:08 -04:00
*
2012-02-28 14:58:52 -05:00
* @param string $table
2012-03-28 11:23:08 -04:00
*
2012-02-28 14:58:52 -05:00
* @return void
*/
abstract public function truncate($table);
/**
* Return the number of rows for the last SELECT query
2012-03-28 11:23:08 -04:00
*
2012-02-28 14:58:52 -05:00
* @return int
*/
abstract public function num_rows();
2012-04-09 15:24:10 -04:00
/**
* Connect to a different database
*
* @param string $name
* @return void
*/
abstract public function switch_db($name);
2012-02-28 14:58:52 -05:00
}
// End of db_pdo.php