2012-04-18 15:53:06 -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-18 15:53:06 -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-18 15:53:06 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class defining database / table creation methods
|
2012-04-20 13:17:39 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @subpackage Query
|
2012-04-18 15:53:06 -04:00
|
|
|
*/
|
2012-04-27 16:25:46 -04:00
|
|
|
abstract class DB_Util {
|
2012-04-18 15:53:06 -04:00
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
/**
|
|
|
|
* Reference to the current connection object
|
|
|
|
*/
|
2012-04-18 15:53:06 -04:00
|
|
|
private $conn;
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2012-04-18 15:53:06 -04:00
|
|
|
/**
|
|
|
|
* Save a reference to the connection object for later use
|
2012-04-19 11:42:50 -04:00
|
|
|
*
|
2013-02-07 16:21:02 -05:00
|
|
|
* @param object $conn
|
2012-04-18 15:53:06 -04:00
|
|
|
*/
|
2013-02-07 16:21:02 -05:00
|
|
|
public function __construct($conn)
|
2012-04-18 15:53:06 -04:00
|
|
|
{
|
2014-03-31 12:58:43 -04:00
|
|
|
$this->conn = $conn;
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2012-04-18 16:28:12 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2012-04-18 16:28:12 -04:00
|
|
|
/**
|
|
|
|
* Enable calling driver methods
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param array $args
|
2014-02-07 16:53:01 -05:00
|
|
|
* @return mixed
|
2012-04-18 16:28:12 -04:00
|
|
|
*/
|
|
|
|
public function __call($method, $args)
|
|
|
|
{
|
2014-02-11 14:29:41 -05:00
|
|
|
return call_user_func_array(array($this->conn, $method), $args);
|
2012-04-18 16:28:12 -04:00
|
|
|
}
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2012-04-18 15:53:06 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Abstract Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2014-03-31 12:58:43 -04:00
|
|
|
* Convienience public function to generate sql for creating a db table
|
2012-04-18 15:53:06 -04:00
|
|
|
*
|
|
|
|
* @param string $name
|
2014-03-31 12:58:43 -04:00
|
|
|
* @param array $fields
|
2012-04-18 15:53:06 -04:00
|
|
|
* @param array $constraints
|
|
|
|
* @param array $indexes
|
2014-03-31 12:58:43 -04:00
|
|
|
*
|
2012-04-18 15:53:06 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-03-31 12:58:43 -04:00
|
|
|
public function create_table($name, $fields, array $constraints=array(), array $indexes=array())
|
|
|
|
{
|
|
|
|
$column_array = array();
|
|
|
|
|
|
|
|
// Reorganize into an array indexed with column information
|
|
|
|
// Eg $column_array[$colname] = array(
|
|
|
|
// 'type' => ...,
|
|
|
|
// 'constraint' => ...,
|
|
|
|
// 'index' => ...,
|
|
|
|
// )
|
|
|
|
foreach($fields as $colname => $type)
|
|
|
|
{
|
|
|
|
if(is_numeric($colname))
|
|
|
|
{
|
|
|
|
$colname = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$column_array[$colname] = array();
|
|
|
|
$column_array[$colname]['type'] = ($type !== $colname) ? $type : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! empty($constraints))
|
|
|
|
{
|
|
|
|
foreach($constraints as $col => $const)
|
|
|
|
{
|
|
|
|
$column_array[$col]['constraint'] = $const;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join column definitons together
|
|
|
|
$columns = array();
|
|
|
|
foreach($column_array as $n => $props)
|
|
|
|
{
|
|
|
|
$str = '"'.$n.'"';
|
|
|
|
$str .= (isset($props['type'])) ? " {$props['type']}" : "";
|
|
|
|
$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : "";
|
|
|
|
|
|
|
|
$columns[] = $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the sql for the creation of the table
|
|
|
|
$sql = 'CREATE TABLE "'.$name.'" (';
|
|
|
|
$sql .= implode(', ', $columns);
|
|
|
|
$sql .= ')';
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get database-specific sql to drop a table
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function delete_table($name);
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2012-04-18 15:53:06 -04:00
|
|
|
/**
|
|
|
|
* Return an SQL file with the database table structure
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function backup_structure();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an SQL file with the database data as insert statements
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function backup_data();
|
|
|
|
|
2012-12-18 16:19:52 -05:00
|
|
|
}
|
2014-02-07 16:53:01 -05:00
|
|
|
// End of db_util.php
|