* Db/Database.php - Added createTable() to create table from meta-information * Db/MySQLDB.php - Overridden _columnDef() to handle specialties for createTable() * Db/Table.php - Added meta-information to create() table from it (uses Database::createTable()) * Db/MySQLTable.php - Overridden _createOptions() to handle specialties for Table::create()
/trunk/Db/MySQLDB.php |
---|
63,6 → 63,20 |
} |
/** |
* (non-PHPdoc) |
* @see \PointedEars\PHPX\Db\Database::_columnDef() |
*/ |
protected function _columnDef (array $data, $columnName) |
{ |
$standard_Def = parent::_columnDef($data, $columnName); |
$mySQL_def = $standardDef |
. (isset($data['format']) && $data['format'] ? " COLUMN_FORMAT {$data['format']}" : '') |
. (isset($data['storage']) && $data['storage'] ? " STORAGE {$data['storage']}" : ''); |
return $mySQL_def; |
} |
/** |
* Creates this MySQL database |
* |
* @param string $dsn |
/trunk/Db/MySQLTable.php |
---|
0,0 → 1,38 |
<?php |
namespace PointedEars\PHPX\Db; |
/** |
* @author |
* Copyright (c) 2013 Thomas 'PointedEars' Lahn |
*/ |
class MySQLTable extends Table |
{ |
/** |
* Engine for this table. |
* |
* @var string |
* @see Table::create() |
*/ |
protected static $_engine; |
/** |
* (non-PHPdoc) |
* @see \PointedEars\PHPX\Db\Table::_createOptions() |
*/ |
protected function _createOptions () |
{ |
$options = parent::_createOptions(); |
$class = \get_class($this); |
foreach (array('engine') as $option) |
{ |
if ($class::${"_$option"}) |
{ |
$options[$option] = $class::${"_$option"}; |
} |
} |
return $options; |
} |
} |
/trunk/Db/Database.php |
---|
230,7 → 230,7 |
public function create ($dsn, $username = null, $password = null, |
array $options = null, $dbspec = null, $force = false) |
{ |
$connection = new PDO($dsn, |
$connection = new \PDO($dsn, |
$username !== null ? $username : $this->_username, |
$password !== null ? $password : $this->_password, |
$options !== null ? $options : $this->_options); |
244,6 → 244,81 |
} |
/** |
* Maps column meta-information to a column definition. |
* |
* Should be overwritten and called by inheriting classes. |
* |
* @todo |
* @param array $value |
* @param string $column_name |
* @return string |
*/ |
protected function _columnDef (array $data, $column_name) |
{ |
$def = (isset($data['unsigned']) && $data['unsigned'] ? 'UNSIGNED ' : '') |
. $data['type'] |
. (isset($data['not_null']) && $data['not_null'] ? ' NOT NULL' : ' NULL') |
. (isset($data['default']) && $data['default'] ? " DEFAULT {$data['default']}" : '') |
. (isset($data['auto_inc']) && $data['auto_inc'] ? ' AUTO_INCREMENT' : '') |
. (isset($data['unique']) && $data['unique'] ? ' UNIQUE KEY' : '') |
. (isset($data['primary']) && $data['primary'] ? ' PRIMARY KEY' : '') |
. (isset($data['comment']) && $data['comment'] ? " COMMENT '{$data['comment']}'" : ''); |
return $this->escapeName($column_name) . ' ' . $def; |
} |
/** |
* Creates a database table according to the specified parameters |
* |
* @todo |
* @param string $name |
* @param array $columns |
* @param array $options = null |
* @return bool |
* @see PDOStatement::execute() |
*/ |
public function createTable ($name, array $columns, array $options = null) |
{ |
$class = \get_class($this); |
$query = 'CREATE TABLE ' |
. $this->escapeName($name) |
. '(' |
. array_map(array($this, '_columnDef'), $columns, array_keys($columns)) . ')'; |
$stmt = $this->prepare($query); |
/* DEBUG */ |
if (defined('DEBUG') && DEBUG > 1) |
{ |
debug(array( |
'query' => $query, |
)); |
} |
$success =& $this->_lastSuccess; |
$success = $stmt->execute(); |
$errorInfo =& $this->_lastError; |
$errorInfo = $stmt->errorInfo(); |
$this->_resetLastInsertId(); |
$result =& $this->_lastResult; |
$result = $stmt->fetchAll(); |
if (defined('DEBUG') && DEBUG > 1) |
{ |
debug(array( |
'_lastSuccess' => $success, |
'_lastError' => $errorInfo, |
'_lastResult' => $result |
)); |
} |
return $success; |
} |
/** |
* Initiates a transaction |
* |
* @return bool |
/trunk/Db/Table.php |
---|
22,8 → 22,30 |
protected static $_name = ''; |
/** |
* Columns definition |
* @var array |
* @see Table::create() |
*/ |
protected static $_columns; |
/** |
* Indexes definition |
* @var array |
* @see Table::create() |
*/ |
protected static $_indexes; |
/** |
* Constraints definition |
* @var array |
* @see Table::create() |
*/ |
protected static $_constraints; |
/** |
* Database of the table |
* @var Database|string |
* @see Table::create() |
*/ |
protected static $_database; |
165,6 → 187,40 |
} |
/** |
* Returns the <var>options</var> array for {@link Database::createTable} |
* |
* Should be called and overridden by inheriting classes. |
* |
* @return array |
*/ |
protected function _createOptions () |
{ |
$options = array(); |
foreach (array('indexes', 'constraints') as $option) |
{ |
if ($class::${"_$option"}) |
{ |
$options[$option] = $class::${"_$option"}; |
} |
} |
return $options; |
} |
/** |
* Creates the table for this model |
* |
* @return bool |
*/ |
public function create () |
{ |
$class = \get_class($this); |
return $this->database->createTable( |
$class::$_name, $class::$_columns, $this->_createOptions()); |
} |
/** |
* Initiates a transaction |
* |
* @return bool |