public function create(MidataTable $table) { // columns $ddlColumn = MidataDDL::factory($table->adapter(), MidataDDL::COLUMN); $tableName = $table->name(); $sql = "CREATE TABLE `{$tableName}`("; $name = $table->column("name"); foreach ($table->columns() as $column) { $column = $table->column($column); $inline = $ddlColumn->inline($column); $sql = "{$sql}\n {$inline},"; } // constraints $ddlConstraint = MidataDDL::factory($table->adapter(), MidataDDL::CONSTRAINT); foreach ($table->constraints() as $constraint) { $constraint = $table->constraint($constraint); $inline = $ddlConstraint->inline($constraint); $sql = "{$sql}\n {$inline},"; } // indexes $ddlIndex = MidataDDL::factory($table->adapter(), MidataDDL::INDEX); foreach ($table->indexes() as $index) { $index = $table->index($index); $inline = $ddlIndex->inline($index); $sql = "{$sql}\n {$inline},"; } $sql = trim($sql, ','); $sql .= "\n)"; return "{$sql};"; }
private function dropPrimaryKey(MidataConstraint $primaryKey) { $tableName = $primaryKey->tableName(); $adapter = $this->adapter(); $sql = ""; if ($adapter->isMysql()) { $table = $adapter->table($tableName); $primaryKey = $table->primaryKey(); if (count($primaryKey) == 1) { $column = $table->column($primaryKey[0]); if ($column->sequence()) { // prubujemy usunac klucz podstawowy z kolumny ktora // jest oznaczona jako auto_increment, wiec zanim // usuniemy klucz musimy usunac rowniez auto_increment // z kolumny $ddlColumn = MidataDDL::factory($adapter, MidataDDL::COLUMN); $column->sequence(false); $alter = $ddlColumn->alter($column); $sql .= "\n{$alter}"; } } } $sql .= "\nALTER TABLE `{$tableName}` DROP PRIMARY KEY"; return "{$sql};"; }
private function ddlBuilder($statement) { if (!isset($this->ddlBuilders[$statement])) { $this->ddlBuilders[$statement] = MidataDDL::factory($this->adapter('source'), $statement); } return $this->ddlBuilders[$statement]; }
/** * Return instace of DDL (Data Definition Builder) for specificed type of * object. * * @param string $statement column|trigger|foreignKey|view|table * @return \Midata\DDL */ public function ddl($statement) { return MidataDDL::factory($this, $statement); }