Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  *
  * @param bool $quoted Quote name.
  */
 public function getName($quoted = false)
 {
     $name = $this->name;
     if (empty($this->name)) {
         $name = $this->table->getName() . '_foreign_' . $this->column . '_' . uniqid();
     }
     if (strlen($name) > 64) {
         //Many dbs has limitations on identifier length
         $name = md5($name);
     }
     return $quoted ? $this->table->driver()->identifier($name) : $name;
 }
Ejemplo n.º 2
0
 /**
  * @param AbstractTable $table
  * @param RecordSchema  $record
  */
 public function __construct(AbstractTable $table, RecordSchema $record)
 {
     $altered = [];
     foreach ($table->alteredColumns() as $column) {
         $altered[] = $column->getName();
     }
     parent::__construct(\Spiral\interpolate('Passive table "{database}"."{table}" ({record}), were altered, columns: {columns}', ['database' => $record->getDatabase(), 'table' => $table->getName(), 'record' => $record, 'columns' => join(', ', $altered)]));
 }
Ejemplo n.º 3
0
 /**
  * Index sql creation syntax.
  *
  * @param bool $includeTable Include table ON statement (not required for inline index
  *                           creation).
  * @return string
  */
 public function sqlStatement($includeTable = true)
 {
     $statement = [];
     $statement[] = $this->type . ($this->type == self::UNIQUE ? ' INDEX' : '');
     $statement[] = $this->getName(true);
     if ($includeTable) {
         $statement[] = 'ON ' . $this->table->getName(true);
     }
     $statement[] = '(' . join(', ', array_map([$this->table->driver(), 'identifier'], $this->columns)) . ')';
     return join(' ', $statement);
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function dropForeign(AbstractTable $table, AbstractReference $foreign)
 {
     $this->run("ALTER TABLE {$table->getName(true)} DROP FOREIGN KEY {$foreign->getName(true)}");
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * Copy table data to another location.
  *
  * @see http://stackoverflow.com/questions/4007014/alter-column-in-sqlite
  * @param AbstractTable $temporary
  * @param array         $mapping Association between old and new columns (quoted).
  */
 private function copyData(AbstractTable $temporary, array $mapping)
 {
     $this->logger()->debug("Copying table data from {source} to {table} using mapping ({columns}) => ({target}).", ['source' => $this->driver->identifier($this->initial->getName()), 'table' => $temporary->getName(true), 'columns' => join(', ', $mapping), 'target' => join(', ', array_keys($mapping))]);
     $query = \Spiral\interpolate("INSERT INTO {table} ({target}) SELECT {columns} FROM {source}", ['source' => $this->driver->identifier($this->initial->getName()), 'table' => $temporary->getName(true), 'columns' => join(', ', $mapping), 'target' => join(', ', array_keys($mapping))]);
     //Let's go
     $this->driver->statement($query);
 }
Ejemplo n.º 6
0
 /**
  * Get statement needed to create table.
  *
  * @param AbstractTable $table
  * @return string
  */
 protected function createStatement(AbstractTable $table)
 {
     $statement = ["CREATE TABLE {$table->getName(true)} ("];
     $innerStatement = [];
     //Columns
     foreach ($table->getColumns() as $column) {
         $innerStatement[] = $column->sqlStatement();
     }
     //Primary key
     if (!empty($table->getPrimaryKeys())) {
         $primaryKeys = array_map([$this, 'quote'], $table->getPrimaryKeys());
         $innerStatement[] = 'PRIMARY KEY (' . join(', ', $primaryKeys) . ')';
     }
     //Constraints and foreign keys
     foreach ($table->getForeigns() as $reference) {
         $innerStatement[] = $reference->sqlStatement();
     }
     $statement[] = "    " . join(",\n    ", $innerStatement);
     $statement[] = ')';
     return join("\n", $statement);
 }
Ejemplo n.º 7
0
 /**
  * Table aliases associated with given table schema.
  *
  * @param AbstractTable $table
  * @return string
  * @throws SchemaException
  */
 public function tableAlias(AbstractTable $table)
 {
     foreach ($this->aliases as $item) {
         if ($item['schema'] === $table) {
             return $item['table'];
         }
     }
     throw new SchemaException("Unable to resolve table alias for table '{$table->getName()}'");
 }
Ejemplo n.º 8
0
 /**
  * @param AbstractTable $table
  * @param ColumnSchema  $initial
  * @param ColumnSchema  $column
  */
 private function renameColumn(AbstractTable $table, ColumnSchema $initial, ColumnSchema $column)
 {
     $statement = \Spiral\interpolate('ALTER TABLE {table} RENAME COLUMN {column} TO {name}', ['table' => $table->getName(true), 'column' => $initial->getName(true), 'name' => $column->getName(true)]);
     $this->run($statement);
 }