2014-04-07 18:28:53 -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
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace Query\Table;
|
2014-04-10 15:54:43 -04:00
|
|
|
use Query\Driver\Driver_Interface;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2014-04-07 18:28:53 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class defining database / table creation methods
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @subpackage Table_Builder
|
|
|
|
*/
|
|
|
|
class Table_Builder {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the current table
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Driver for the current db
|
|
|
|
* @var Driver_Interface
|
|
|
|
*/
|
|
|
|
private $driver = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options for the current table
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $table_options = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Columns to be added/updated for the current table
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $columns = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indexes to be added/updated for the current table
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $indexes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Foreign keys to be added/updated for the current table
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $foreign_keys = array();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2014-04-15 16:16:15 -04:00
|
|
|
* @param string $name
|
|
|
|
* @param array $options
|
|
|
|
* @param Driver_Interface $driver
|
2014-04-07 18:28:53 -04:00
|
|
|
* @return Table_Builder
|
|
|
|
*/
|
2014-04-10 15:54:43 -04:00
|
|
|
public function __construct($name = '', $options = array(), Driver_Interface $driver = NULL)
|
2014-04-07 18:28:53 -04:00
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
$this->table_options = array_merge($this->table_options, $options);
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2014-04-10 15:54:43 -04:00
|
|
|
$this->set_driver($driver);
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2014-04-10 15:54:43 -04:00
|
|
|
if ($name !== '')
|
2014-04-07 18:28:53 -04:00
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
$this->name = (isset($this->driver)) ? $this->driver->prefix_table($name) : $name;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias to constructor
|
|
|
|
*
|
2014-04-15 16:16:15 -04:00
|
|
|
* @param string $name
|
|
|
|
* @param array $options
|
|
|
|
* @param Driver_Interface $driver
|
2014-04-10 15:54:43 -04:00
|
|
|
* @return Table_Builder
|
2014-04-07 18:28:53 -04:00
|
|
|
*/
|
2014-04-10 15:54:43 -04:00
|
|
|
public function __invoke($name = '', $options = array(), Driver_Interface $driver = NULL)
|
2014-04-07 18:28:53 -04:00
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this->__construct($name, $options, $driver);
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the reference to the current database driver
|
|
|
|
*
|
2014-04-15 16:16:15 -04:00
|
|
|
* @param Driver_Interface $driver
|
2014-04-10 15:54:43 -04:00
|
|
|
* @return Table_Builder
|
2014-04-07 18:28:53 -04:00
|
|
|
*/
|
2014-04-10 15:54:43 -04:00
|
|
|
public function set_driver(Driver_Interface $driver = NULL)
|
2014-04-07 18:28:53 -04:00
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
if ( ! is_null($driver))
|
|
|
|
{
|
|
|
|
$this->driver = $driver;
|
|
|
|
}
|
2014-04-07 18:28:53 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current DB Driver
|
|
|
|
*
|
2014-04-15 16:16:15 -04:00
|
|
|
* @return Driver_Interface
|
2014-04-07 18:28:53 -04:00
|
|
|
*/
|
|
|
|
public function get_driver()
|
|
|
|
{
|
|
|
|
return $this->driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Column Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a column to the current table
|
|
|
|
*
|
|
|
|
* @param string $column_name
|
|
|
|
* @param string $type
|
|
|
|
* @param array $options
|
2014-04-10 15:54:43 -04:00
|
|
|
* @return Table_Builder
|
2014-04-07 18:28:53 -04:00
|
|
|
*/
|
|
|
|
public function add_column($column_name, $type = NULL, $options = array())
|
|
|
|
{
|
|
|
|
$col = new Table_Column($column_name, $type, $options);
|
|
|
|
$this->columns[] = $col;
|
2014-04-10 15:54:43 -04:00
|
|
|
|
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Remove the specified column name from the current table
|
|
|
|
*
|
|
|
|
* @param string $column_name
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function remove_column($column_name)
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Rename the specified column on the current table
|
|
|
|
*
|
|
|
|
* @param string $old_name
|
|
|
|
* @param string $new_name
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function rename_column($old_name, $new_name)
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Change the specified column on the current table
|
|
|
|
*
|
|
|
|
* @param string $column_name
|
|
|
|
* @param string $new_column_type
|
|
|
|
* @param array $options
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function change_column($column_name, $new_column_type, $options = array())
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Determine whether the column currently exists on the current table
|
|
|
|
*
|
|
|
|
* @param string $column_name
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function has_column($column_name, $options = array())
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Index Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Add an index to the current table
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $options
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function add_index($columns, $options = array())
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
$col = new Table_Index($columns, $options);
|
|
|
|
$this->indexes[] = $col;
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Remove an index from the current table
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $options
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function remove_index($columns, $options = array())
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Remove an index by its name from the current table
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function remove_index_by_name($name)
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Check if the current table has an index on the specified columns
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function has_index($columns, $options = array())
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Foreign Key Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Add a foreign key to the current table
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @param string $referenced_table
|
|
|
|
* @param array $referenced_columns
|
|
|
|
* @param array $options
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function add_foreign_key($columns, $referenced_table, $referenced_columns = array('id'), $options = array())
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
$key = new Table_Foreign_Key($columns, $referenced_table, $referenced_columns, $options);
|
|
|
|
$this->foreign_keys[] = $key;
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2014-04-10 15:54:43 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Drop the foreign key from the current table
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @param string $constraint
|
|
|
|
* @return \Query\Table\Table_Builder
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function drop_foreign_key($columns, $constraint = NULL)
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-15 16:16:15 -04:00
|
|
|
return $this;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Determine whether the current table has the specified foreign key
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @param string $constraint
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function has_foreign_key($columns, $constraint = NULL)
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-22 14:02:54 -04:00
|
|
|
$keys = $this->get_driver()->get_fks($this->name);
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2014-04-22 14:02:54 -04:00
|
|
|
|
|
|
|
foreach($keys as $key)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Table-wide methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Check whether the current table exists
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function exists()
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
$tables = $this->driver->get_tables();
|
|
|
|
return in_array($this->name, $tables);
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Drop the current table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function drop()
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Rename the current table
|
|
|
|
*
|
|
|
|
* @param string $new_table_name
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function rename($new_table_name)
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Get the list of columns for the current table
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function get_columns()
|
|
|
|
{
|
2014-04-22 14:02:54 -04:00
|
|
|
return $this->driver->get_columns($this->name);
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Action methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Create the table from the previously set options
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function create()
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-10 15:54:43 -04:00
|
|
|
$this->reset();
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Update the current table with the changes made
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function update()
|
|
|
|
{
|
2014-06-09 17:02:14 -04:00
|
|
|
// @TODO: implement
|
2014-04-10 15:54:43 -04:00
|
|
|
$this->reset();
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Save the changes made to the table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
($this->exists())
|
|
|
|
? $this->update()
|
|
|
|
: $this->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:16:15 -04:00
|
|
|
/**
|
|
|
|
* Reset the state of the table builder
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-04-07 18:28:53 -04:00
|
|
|
public function reset()
|
|
|
|
{
|
2014-04-10 15:54:43 -04:00
|
|
|
$skip = array(
|
|
|
|
'driver' => 'driver'
|
|
|
|
);
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2014-04-10 15:54:43 -04:00
|
|
|
foreach($this as $key => $val)
|
|
|
|
{
|
|
|
|
if ( ! isset($skip[$key]))
|
|
|
|
{
|
|
|
|
$this->$key = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 18:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-04-22 14:02:54 -04:00
|
|
|
// End of table_builder.php
|