/** * Convert a LazyRecord schema to action. * * This is used for generating an Action View without CRUD type. */ public static function convertSchemaToAction(SchemaInterface $schema, BaseModel $record = null) { $columns = $schema->getColumns(true); $action = new BaseRecordAction(array(), $record); // no actual record is null $action->resetParams(); $action->initParamsFromColumns($columns, $record); return $action; }
public function buildForeignKeys(SchemaInterface $schema) { return array(); // FIXME $sqls = array(); if ($this->driver->type == 'sqlite') { return $sqls; } foreach ($schema->relations as $rel) { switch ($rel['type']) { case Relationship::BELONGS_TO: case Relationship::HAS_MANY: case Relationship::HAS_ONE: if (isset($rel['self_column']) && $rel['self_column'] != 'id') { $n = $rel['self_column']; $column = $schema->getColumn($n); if ($column->isa == "str") { continue; } $fSchema = new $rel['foreign_schema'](); $constraint = new Constraint(); $constraint->foreignKey($rel['self_column']); $constraint->reference($fSchema->getTable(), (array) $rel['foreign_column']); // $constraint->onUpdate('CASCADE'); // $constraint->onDelete('CASCADE'); $sqls[] = $query->toSql($this->driver, new ArgumentArray()); } } } return $sqls; }
public function buildIndex(SchemaInterface $schema) { // build single column index $sqls = array(); foreach ($schema->columns as $name => $column) { if ($column->index) { $table = $schema->getTable(); $indexName = is_string($column->index) ? $column->index : 'idx_' . $table . '_' . $name; $query = new CreateIndexQuery($indexName); $query->on($table, [$name]); if ($column->index_using) { $query->using($column->index_using); } $sqls[] = $query->toSql($this->driver, new ArgumentArray()); } } if ($queries = $schema->getIndexQueries()) { foreach ($queries as $query) { $sqls[] = $query->toSql($this->driver, new ArgumentArray()); } } return $sqls; }
public function dropTable(SchemaInterface $schema) { return 'DROP TABLE IF EXISTS ' . $this->driver->quoteIdentifier($schema->getTable()) . ';'; }
/** * compare two schemas. * * @param Schema $before schema before * @param Schema $after new schema */ public function compare(SchemaInterface $before, SchemaInterface $after) { $diff = array(); $beforeColumns = $before ? $before->getColumns() : array(); $afterColumns = $after ? $after->getColumns() : array(); $columnKeys = array_unique(array_merge(array_keys($beforeColumns), array_keys($afterColumns))); foreach ($columnKeys as $key) { // If schema and db has the same column, we then compare the column definitions if (isset($beforeColumns[$key]) && isset($afterColumns[$key])) { $bc = $beforeColumns[$key]; $ac = $afterColumns[$key]; $afterc = $afterColumns[$key]; $d = new ColumnDiff($key, 'M', $bc, $ac); // Compare the type info if (strtolower($bc->type) !== strtolower($ac->type)) { $d->appendDetail(new AttributeDiff('type', strtolower($bc->buildTypeName($this->driver)), strtolower($ac->buildTypeName($this->driver)))); } if ($bc->length !== $ac->length) { $d->appendDetail(new AttributeDiff('length', $bc->buildTypeName($this->driver), $ac->buildTypeName($this->driver))); } if ($bc->decimals !== $ac->decimals) { $d->appendDetail(new AttributeDiff('decimals', $bc->buildTypeName($this->driver), $ac->buildTypeName($this->driver))); } if ($bc->primary !== $ac->primary) { $d->appendDetail(new AttributeDiff('primary', $bc->primary, $ac->primary)); } // we only compare unsigned when: // driver is MySQL or the column is not a primary key if ($this->driver instanceof MySQLDriver) { if (!$ac->primary && !$bc->primary) { if ($bc->unsigned != $ac->unsigned) { $d->appendDetail(new AttributeDiff('unsigned', $bc->unsigned, $ac->unsigned)); } } } if ($bc->notNull != $ac->notNull) { $d->appendDetail(new AttributeDiff('notNull', $bc->notNull, $ac->notNull)); } // They are the same column, let's compare these attributes $attributes = array('default'); foreach ($attributes as $n) { // Closure are meaningless $aval = $ac->{$n}; $bval = $bc->{$n}; if ($aval instanceof Closure || $bval instanceof Closure) { continue; } if ($aval === null && $bval === null || $aval === false && $bval === false) { continue; } if (is_array($aval)) { $aval = new Raw($aval[0]); } if (is_array($bval)) { $bval = new Raw($bval[0]); } if ($aval instanceof Raw && $bval instanceof Raw && $aval->compare($bval) != 0) { $d->appendDetail(new AttributeDiff($n, $aval, $bval)); } elseif (is_scalar($aval) && is_scalar($bval) && $aval != $bval) { $d->appendDetail(new AttributeDiff($n, $aval, $bval)); } } if (count($d->details) > 0) { $diff[] = $d; } } elseif (isset($beforeColumns[$key]) && !isset($afterColumns[$key])) { // flag: - $diff[] = new ColumnDiff($key, 'D', $beforeColumns[$key], null); } elseif (isset($afterColumns[$key]) && !isset($beforeColumns[$key])) { // flag: + $diff[] = new ColumnDiff($key, 'A', null, $afterColumns[$key]); } } return $diff; }