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
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-24 15:32:09 -04:00
|
|
|
namespace Query\Driver\Util;
|
|
|
|
use Query\Driver\Driver_Interface;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2014-04-02 17:08:50 -04:00
|
|
|
|
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
|
2014-03-31 16:01:58 -04:00
|
|
|
* @subpackage Drivers
|
2014-04-09 10:55:17 -04:00
|
|
|
* @method string quote_ident(string $sql)
|
|
|
|
* @method string quote_table(string $sql)
|
2012-04-18 15:53:06 -04:00
|
|
|
*/
|
2014-04-03 14:44:03 -04:00
|
|
|
abstract class Abstract_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
|
|
|
*
|
2014-04-10 15:54:43 -04:00
|
|
|
* @param Driver_Interface $conn
|
2012-04-18 15:53:06 -04:00
|
|
|
*/
|
2014-04-10 15:54:43 -04:00
|
|
|
public function __construct(Driver_Interface $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
|
|
|
/**
|
2014-04-24 13:42:01 -04:00
|
|
|
* Get the driver object for the current connection
|
2012-04-18 16:28:12 -04:00
|
|
|
*
|
2014-04-24 13:42:01 -04:00
|
|
|
* @return Driver_Interface
|
2012-04-18 16:28:12 -04:00
|
|
|
*/
|
2014-04-24 13:42:01 -04:00
|
|
|
public function get_driver()
|
2012-04-18 16:28:12 -04:00
|
|
|
{
|
2014-04-24 13:42:01 -04:00
|
|
|
return $this->conn;
|
2012-04-18 16:28:12 -04:00
|
|
|
}
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2012-04-18 15:53:06 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2014-04-24 11:31:03 -04:00
|
|
|
* Convenience public function to generate sql for creating a db table
|
2012-04-18 15:53:06 -04:00
|
|
|
*
|
2014-04-10 15:54:43 -04:00
|
|
|
* @deprecated Use the table builder class instead
|
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
|
2014-04-23 17:03:46 -04:00
|
|
|
* @param bool $if_not_exists
|
2012-04-18 15:53:06 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-23 17:03:46 -04:00
|
|
|
public function create_table($name, $fields, array $constraints=array(), $if_not_exists=TRUE)
|
2014-03-31 12:58:43 -04:00
|
|
|
{
|
2014-04-23 17:03:46 -04:00
|
|
|
$exists_str = ($if_not_exists) ? ' IF NOT EXISTS ' : ' ';
|
|
|
|
|
2014-03-31 12:58:43 -04:00
|
|
|
// Reorganize into an array indexed with column information
|
|
|
|
// Eg $column_array[$colname] = array(
|
|
|
|
// 'type' => ...,
|
|
|
|
// 'constraint' => ...,
|
|
|
|
// 'index' => ...,
|
|
|
|
// )
|
2014-04-24 13:08:26 -04:00
|
|
|
$column_array = \array_zipper(array(
|
|
|
|
'type' => $fields,
|
|
|
|
'constraint' => $constraints
|
|
|
|
));
|
2014-03-31 12:58:43 -04:00
|
|
|
|
2014-04-24 11:31:03 -04:00
|
|
|
// Join column definitions together
|
2014-03-31 12:58:43 -04:00
|
|
|
$columns = array();
|
|
|
|
foreach($column_array as $n => $props)
|
|
|
|
{
|
2014-04-24 13:42:01 -04:00
|
|
|
$str = $this->get_driver()->quote_ident($n);
|
2014-03-31 12:58:43 -04:00
|
|
|
$str .= (isset($props['type'])) ? " {$props['type']}" : "";
|
|
|
|
$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : "";
|
|
|
|
|
|
|
|
$columns[] = $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the sql for the creation of the table
|
2014-04-24 13:42:01 -04:00
|
|
|
$sql = 'CREATE TABLE'.$exists_str.$this->get_driver()->quote_table($name).' (';
|
2014-03-31 12:58:43 -04:00
|
|
|
$sql .= implode(', ', $columns);
|
|
|
|
$sql .= ')';
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2014-04-15 16:16:15 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
/**
|
2014-03-31 16:35:02 -04:00
|
|
|
* Drop the selected table
|
2012-04-18 15:53:06 -04:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-03-31 16:35:02 -04:00
|
|
|
public function delete_table($name)
|
|
|
|
{
|
2014-04-24 13:42:01 -04:00
|
|
|
return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name);
|
2014-03-31 16:35:02 -04:00
|
|
|
}
|
2014-04-15 16:16:15 -04:00
|
|
|
|
2014-04-24 13:42:01 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Abstract Methods
|
2014-04-15 16:16:15 -04:00
|
|
|
// --------------------------------------------------------------------------
|
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();
|
2014-04-15 16:16:15 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-03 14:44:03 -04:00
|
|
|
// End of abstract_util.php
|