Add beginning of table builder classes

This commit is contained in:
Timothy Warren 2014-04-07 18:28:53 -04:00
parent 2af3b0be9f
commit 9011678dc3
7 changed files with 560 additions and 2 deletions

View File

@ -15,6 +15,10 @@
namespace Query\Driver; namespace Query\Driver;
use Query\Table\Table_Builder;
// --------------------------------------------------------------------------
/** /**
* Base Database class * Base Database class
* *
@ -88,7 +92,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
$this->$sub = new $class($this); $this->$sub = new $class($this);
} }
$this->table = new \Query\Table\Table_Builder('', array(), $this); $this->table = new Table_Builder('', array(), $this);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -0,0 +1,98 @@
<?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;
/**
* Base class for table builder component classes`
*/
abstract class Abstract_Table {
/**
* Valid options for the current class
* @var array
*/
protected $valid_options;
// --------------------------------------------------------------------------
// ! Abstract Methods
// --------------------------------------------------------------------------
/**
* String representation of the column/index
*/
abstract public function __toString();
// --------------------------------------------------------------------------
// ! Concrete methods
// --------------------------------------------------------------------------
/**
* Set options for the current column
*
* @param array $options
* return \Query\Table_Column
*/
public function set_options(Array $options)
{
$type = end(explode('_', get_class($this)));
foreach($options as $option => $value)
{
if ( ! in_array($option, $this->valid_options))
{
throw new \InvalidArgumentException("{$option} is not a valid {$type}");
}
$func = "set_{$option}";
(method_exists($this, "set_{$option}"))
? $this->$func($value)
: $this->__set($option, $value);
}
return $this;
}
// --------------------------------------------------------------------------
/**
* Getters
* @param mixed $name
* @return mixed
*/
public function __get($name)
{
if ( ! isset($this->$name)) return NULL;
return $this->$name;
}
// --------------------------------------------------------------------------
/**
* Setters
* @param mixed $name
* @param mixed $val
* @return \Query\Table_Column
*/
public function __set($name, $val)
{
$this->$name = $val;
return $this;
}
}
// End of abstract_table.php

View File

@ -15,6 +15,8 @@
namespace Query; namespace Query;
use \Query\Driver\Driver_Interface;
/** /**
* Convienience class for creating sql queries - also the class that * Convienience class for creating sql queries - also the class that
* instantiates the specific db driver * instantiates the specific db driver
@ -180,7 +182,7 @@ class Query_Builder implements Query_Builder_Interface {
* *
* @param \Query\Driver\Driver_Interface $db * @param \Query\Driver\Driver_Interface $db
*/ */
public function __construct(\Query\Driver\Driver_Interface $db) public function __construct(Driver_Interface $db)
{ {
$this->db = $db; $this->db = $db;

293
core/table_builder.php Normal file
View File

@ -0,0 +1,293 @@
<?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;
/**
* 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
*
* @param string $name
* @param array $options
* @param Driver_Interface $driver
* @return Table_Builder
*/
public function __construct($name, $options = array(), \Query\Driver\Driver_Interface $driver = NULL)
{
$this->name = $name;
if ( ! empty($options))
{
$this->table_options = array_merge($this->table_options, $options);
}
if ( ! is_null($driver))
{
$this->driver = $driver;
}
return $this;
}
// --------------------------------------------------------------------------
/**
* Alias to constructor
*
* @param string $name
* @param array $options
* @param \Query\Driver_Interface $driver
*/
public function __invoke($name, $options = array(), \Query\Driver\Driver_Interface $driver = NULL)
{
$this->__construct($name, $options, $driver);
}
// --------------------------------------------------------------------------
/**
* Set the reference to the current database driver
*
* @param \Query\Driver_Interface $driver
* @return \Query\Table_Builder
*/
public function set_driver(\Query\Driver_Interface $driver)
{
$this->driver = $driver;
return $this;
}
// --------------------------------------------------------------------------
/**
* Get the current DB Driver
*
* @return \Query\Driver_Interface
*/
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
*/
public function add_column($column_name, $type = NULL, $options = array())
{
$col = new Table_Column($column_name, $type, $options);
$this->columns[] = $col;
}
// --------------------------------------------------------------------------
public function remove_column($column_name)
{
}
// --------------------------------------------------------------------------
public function rename_column($old_name, $new_name)
{
}
// --------------------------------------------------------------------------
public function change_column($column_name, $new_column_type, $options = array())
{
}
// --------------------------------------------------------------------------
public function has_column($column_name, $options = array())
{
}
// --------------------------------------------------------------------------
// ! Index Methods
// --------------------------------------------------------------------------
public function add_index($columns, $options = array())
{
}
// --------------------------------------------------------------------------
public function remove_index($columns, $options = array())
{
}
// --------------------------------------------------------------------------
public function remove_index_by_name($name)
{
}
// --------------------------------------------------------------------------
public function has_index($columns, $options = array())
{
}
// --------------------------------------------------------------------------
// ! Foreign Key Methods
// --------------------------------------------------------------------------
public function add_foreign_key($columns, $referenced_table, $referenced_columns = array('id'), $options = array())
{
}
// --------------------------------------------------------------------------
public function drop_foreign_key($columns, $constraint = NULL)
{
}
// --------------------------------------------------------------------------
public function has_foreign_key($columns, $constraint = NULL)
{
}
// --------------------------------------------------------------------------
// ! Table-wide methods
// --------------------------------------------------------------------------
public function exists()
{
}
// --------------------------------------------------------------------------
public function drop()
{
}
// --------------------------------------------------------------------------
public function rename($new_table_name)
{
}
// --------------------------------------------------------------------------
public function get_columns()
{
}
// --------------------------------------------------------------------------
// ! Action methods
// --------------------------------------------------------------------------
public function create()
{
}
// --------------------------------------------------------------------------
public function update()
{
}
// --------------------------------------------------------------------------
public function save()
{
($this->exists())
? $this->update()
: $this->create();
$this->reset();
}
// --------------------------------------------------------------------------
public function reset()
{
}
}
// End of table_bulider.php

78
core/table_column.php Normal file
View File

@ -0,0 +1,78 @@
<?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;
/**
* Class representing a column when creating a table
*/
class Table_Column extends Abstract_Table {
/**
* The name of the column
* @var string
*/
protected $name;
/**
* The type of the column
* @var string
*/
protected $type;
/**
* Valid column options
* @var type array
*/
protected $valid_options = array(
'limit',
'length',
'default',
'null',
'precision',
'scale',
'after',
'update',
'comment'
);
// --------------------------------------------------------------------------
/**
* Set the attributes for the column
*
* @param string $name
* @param [string] $type
* @param [array] $options
*/
public function __construct($name, $type = NULL, $options = array())
{
}
// --------------------------------------------------------------------------
/**
* Return the string to create the column
*
* @return string
*/
public function __toString()
{
}
}
// End of table_column.php

View File

@ -0,0 +1,41 @@
<?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;
/**
* Class representing a foreign key
*/
class Table_Foreign_Key extends Abstract_Table {
/**
* Valid options for a foreign key
* @var type array
*/
protected $valid_options = array(
'delete',
'update',
'constraint'
);
/**
* String representation of the foreign key
*/
public function __toString()
{
}
}
// End of table_foreign_key.php

42
core/table_index.php Normal file
View File

@ -0,0 +1,42 @@
<?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;
/**
* Class representing indicies when creating a table
*/
class Table_Index extends Abstract_Table {
/**
* Valid options for a table index
* @var array
*/
protected $valid_options = array(
'type',
'unique',
'name'
);
/**
* Return the string representation of the current index
*/
public function __toString()
{
}
}
// End of table_index.php