Query/docs/files/core/abstract/abstract_util.php.txt

142 lines
3.3 KiB
Plaintext
Raw Normal View History

2014-04-15 16:13:18 -04:00
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
2014-04-24 15:32:28 -04:00
namespace Query\Driver\Util;
use Query\Driver\Driver_Interface;
// --------------------------------------------------------------------------
2014-04-15 16:13:18 -04:00
/**
* Abstract class defining database / table creation methods
*
* @package Query
* @subpackage Drivers
* @method string quote_ident(string $sql)
* @method string quote_table(string $sql)
*/
abstract class Abstract_Util {
/**
* Reference to the current connection object
*/
private $conn;
/**
* Save a reference to the connection object for later use
*
* @param Driver_Interface $conn
*/
public function __construct(Driver_Interface $conn)
{
$this->conn = $conn;
}
// --------------------------------------------------------------------------
/**
2014-04-24 14:54:08 -04:00
* Get the driver object for the current connection
2014-04-15 16:13:18 -04:00
*
2014-04-24 14:54:08 -04:00
* @return Driver_Interface
2014-04-15 16:13:18 -04:00
*/
2014-04-24 14:54:08 -04:00
public function get_driver()
2014-04-15 16:13:18 -04:00
{
2014-04-24 14:54:08 -04:00
return $this->conn;
2014-04-15 16:13:18 -04:00
}
// --------------------------------------------------------------------------
/**
2014-04-24 11:25:12 -04:00
* Convenience public function to generate sql for creating a db table
2014-04-15 16:13:18 -04:00
*
* @deprecated Use the table builder class instead
* @param string $name
* @param array $fields
* @param array $constraints
2014-04-24 11:25:12 -04:00
* @param bool $if_not_exists
2014-04-15 16:13:18 -04:00
* @return string
*/
2014-04-24 11:25:12 -04:00
public function create_table($name, $fields, array $constraints=array(), $if_not_exists=TRUE)
2014-04-15 16:13:18 -04:00
{
2014-04-24 11:25:12 -04:00
$exists_str = ($if_not_exists) ? ' IF NOT EXISTS ' : ' ';
2014-04-15 16:13:18 -04:00
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
// 'constraint' => ...,
// 'index' => ...,
// )
2014-04-24 14:54:08 -04:00
$column_array = \array_zipper(array(
'type' => $fields,
'constraint' => $constraints
));
2014-04-15 16:13:18 -04:00
2014-04-24 11:25:12 -04:00
// Join column definitions together
2014-04-15 16:13:18 -04:00
$columns = array();
foreach($column_array as $n => $props)
{
2014-04-24 14:54:08 -04:00
$str = $this->get_driver()->quote_ident($n);
2014-04-15 16:13:18 -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 14:54:08 -04:00
$sql = 'CREATE TABLE'.$exists_str.$this->get_driver()->quote_table($name).' (';
2014-04-15 16:13:18 -04:00
$sql .= implode(', ', $columns);
$sql .= ')';
return $sql;
}
// --------------------------------------------------------------------------
/**
* Drop the selected table
*
* @param string $name
* @return string
*/
public function delete_table($name)
{
2014-04-24 14:54:08 -04:00
return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name);
2014-04-15 16:13:18 -04:00
}
2014-04-24 14:54:08 -04:00
// --------------------------------------------------------------------------
// ! Abstract Methods
2014-04-15 16:13:18 -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();
}
// End of abstract_util.php