public function buildTable(ActiveRecord $model) { $schema = $model->getClassSchema(); $params = array(); foreach ($model->__calculateSaveDownRows() as $p => $parameter) { $auto_increment = false; $type = "varchar(200)"; $auto_increment_possible = false; if (isset($schema[$parameter])) { $psuedo_type = $schema[$parameter]['type']; switch (strtolower($psuedo_type)) { case 'int': case 'integer': $length = isset($schema[$parameter]['length']) ? $schema[$parameter]['length'] : 10; $type = "INT"; $auto_increment_possible = true; break; case 'string': $length = isset($schema[$parameter]['length']) ? $schema[$parameter]['length'] : 200; $type = "VARCHAR({$length})"; break; case 'date': case 'datetime': $type = 'TIME'; break; case 'enum': $type = "ENUM('" . implode("', '", $schema[$parameter]['options']) . "')"; break; case 'text': $type = "TEXT"; break; case 'blob': $type = 'BLOB'; break; case "decimal": $type = "DECIMAL(" . implode(",", $schema[$parameter]['options']) . ")"; break; case "uuid": $type = "VARCHAR(" . strlen(UUID::v4()) . ")"; break; case "md5": $type = "VARCHAR(" . strlen(md5("test")) . ")"; break; case "sha1": $type = "VARCHAR(" . strlen(sha1("test")) . ")"; break; } } if ($p == 0) { // First param always primary key if possible if ($auto_increment_possible) { $primary_key = $parameter; if (!$model instanceof VersionedActiveRecord) { $auto_increment = true; } } } if ($auto_increment) { $type = 'SERIAL'; } $nullability = $schema[$parameter]['nullable'] ? "NULL" : "NOT NULL"; $params[] = " " . trim("\"{$parameter}\" {$type} {$nullability}"); } // Disable auto-increment if this object is versioned. if ($model instanceof VersionedActiveRecord) { if (isset($primary_key)) { $params[] = " PRIMARY KEY (\"{$primary_key}\", \"sequence\")"; } } else { if (isset($primary_key)) { $params[] = " PRIMARY KEY (\"{$primary_key}\")"; } } $query = "CREATE TABLE IF NOT EXISTS \"{$model->getTableName()}\"\n"; $query .= "(\n"; $query .= implode(",\n", $params) . "\n"; $query .= ");\n"; // Log it. if (DatabaseLayer::getInstance()->getLogger() instanceof Logger) { DatabaseLayer::getInstance()->getLogger()->addInfo("Creating table {$model->getTableName()}\n\n{$query}"); } $this->query($query); }
public function buildTable(ActiveRecord $model) { $schema = $model->getClassSchema(); $params = array(); foreach ($model->__calculateSaveDownRows() as $p => $parameter) { $auto_increment = false; $type = "varchar(200)"; $auto_increment_possible = false; if (isset($schema[$parameter])) { $psuedo_type = $schema[$parameter]['type']; switch (strtolower($psuedo_type)) { case 'int': case 'integer': $type = "INTEGER"; $auto_increment_possible = true; break; case 'date': case 'datetime': case 'enum': case 'string': case 'text': case 'uuid': case 'md5': case 'sha1': $type = "TEXT"; break; case 'blob': $type = 'BLOB'; break; } } $is_primary_key = false; if ($p == 0) { // First param always primary key if possible if ($auto_increment_possible) { $is_primary_key = "PRIMARY KEY"; if (!$model instanceof VersionedActiveRecord) { $auto_increment = true; } } } if ($auto_increment && !$model instanceof VersionedActiveRecord) { $auto_increment_sql = 'AUTOINCREMENT'; } else { $auto_increment_sql = false; } $nullability = $schema[$parameter]['nullable'] ? "NULL" : "NOT NULL"; $nullability = $is_primary_key ? '' : $nullability; $is_primary_key = !$model instanceof VersionedActiveRecord ? $is_primary_key : null; $params[] = " " . trim("`{$parameter}` {$type} {$is_primary_key} {$auto_increment_sql} {$nullability}"); } $query = "CREATE TABLE IF NOT EXISTS `{$model->getTableName()}`\n"; $query .= "(\n"; $query .= implode(",\n", $params) . "\n"; $query .= ")\n"; $this->query($query); // Log it. if (DatabaseLayer::getInstance()->getLogger() instanceof Logger) { DatabaseLayer::getInstance()->getLogger()->addInfo("Creating table {$model->getTableName()}\n\n{$query}"); } }