*/
public function buildCreateTable($name, $def)
{
+ $def = $this->validateDef($name, $def);
$def = $this->filterDef($def);
$sql = array();
*/
function appendForeignKeyDef(array &$sql, $name, array $def)
{
+ if (count($def) != 2) {
+ throw new Exception("Invalid foreign key def for $name: " . var_export($def, true));
+ }
list($refTable, $map) = $def;
$srcCols = array_keys($map);
$refCols = array_values($map);
* @return array of SQL statements
*/
- function buildEnsureTable($tableName, $def)
+ function buildEnsureTable($tableName, array $def)
{
try {
$old = $this->getTableDef($tableName);
// Filter the DB-independent table definition to match the current
// database engine's features and limitations.
+ $def = $this->validateDef($tableName, $def);
$def = $this->filterDef($def);
$statements = array();
* with plugins written for 0.9.x.
*
* @param string $tableName
- * @param array $defs
+ * @param array $defs: array of ColumnDef objects
* @return array
*/
- function oldToNew($tableName, $defs)
+ protected function oldToNew($tableName, array $defs)
{
$table = array();
$prefixes = array(
'big',
);
foreach ($defs as $cd) {
- $cd->addToTableDef($table);
$column = array();
$column['type'] = $cd->type;
foreach ($prefixes as $prefix) {
if (!$cd->nullable) {
$column['not null'] = true;
}
- if ($cd->autoincrement) {
+ if ($cd->auto_increment) {
$column['type'] = 'serial';
}
if ($cd->default) {
return $tableDef;
}
+ /**
+ * Validate a table definition array, checking for basic structure.
+ *
+ * If necessary, converts from an old-style array of ColumnDef objects.
+ *
+ * @param string $tableName
+ * @param array $def: table definition array
+ * @return array validated table definition array
+ *
+ * @throws Exception on wildly invalid input
+ */
+ function validateDef($tableName, array $def)
+ {
+ if (count($def) && $def[0] instanceof ColumnDef) {
+ $def = $this->oldToNew($tableName, $def);
+ }
+
+ // A few quick checks :D
+ if (!isset($def['fields'])) {
+ throw new Exception("Invalid table definition for $tableName: no fields.");
+ }
+
+ return $def;
+ }
+
function isNumericType($type)
{
$type = strtolower($type);