示例#1
0
 /**
  * generate create table queries
  * @throws IncorrectMethodUsageException if table() was not called first
  */
 protected final function create()
 {
     if ($this->table === null) {
         throw new IncorrectMethodUsageException('Wrong use of method create(). Use method table() first.');
     }
     $this->table->addPrimary($this->primaryKey);
     $this->tables[count($this->queries)] = $this->table;
     $queryBuilder = $this->adapter->getQueryBuilder();
     $queries = $queryBuilder->createTable($this->table);
     $this->queries = array_merge($this->queries, $queries);
     $this->table = null;
 }
示例#2
0
 private function createNewTable(Table $table, $tmpTableName)
 {
     if (is_null($this->adapter)) {
         throw new PhoenixException('Missing adapter');
     }
     $oldColumns = $this->adapter->tableInfo($table->getName());
     $columns = array_merge($oldColumns, $table->getColumnsToChange());
     $newTable = new Table($table->getName());
     $columnNames = [];
     foreach ($columns as $column) {
         $columnNames[] = $column->getName();
         if ($column->isAutoincrement()) {
             $newTable->addPrimary($column);
             continue;
         }
         $newTable->addColumn($column);
     }
     $queries = $this->createTable($newTable);
     $queries[] = 'INSERT INTO ' . $this->escapeString($newTable->getName()) . ' (' . implode(',', $this->escapeArray($columnNames)) . ') SELECT ' . implode(',', $this->escapeArray(array_keys($oldColumns))) . ' FROM ' . $this->escapeString($tmpTableName);
     return $queries;
 }
示例#3
0
 private function createTableCharset(Table $table)
 {
     $tableCharset = $this->createCharset($table->getCharset(), $table->getCollation());
     return $tableCharset ? ' DEFAULT' . $tableCharset : '';
 }
示例#4
0
 protected function dropKeyQuery(Table $table, $key)
 {
     return 'ALTER TABLE ' . $this->escapeString($table->getName()) . ' DROP ' . $key . ';';
 }
示例#5
0
 public function testGetters()
 {
     $table = new Table('test');
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addColumn(new Column('title', 'string')));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addColumn(new Column('total', 'int')));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addColumn(new Column('bodytext', 'text')));
     $columns = $table->getColumns();
     $this->assertCount(3, $columns);
     foreach ($columns as $column) {
         $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Column', $column);
     }
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Column', $table->getColumn('title'));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addIndex(new Index('title', 'title', 'unique')));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addIndex(new Index(['title', 'alias'], 'title_alias')));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addIndex(new Index(['bodytext'], 'bodytext', 'fulltext')));
     $indexes = $table->getIndexes();
     $this->assertCount(3, $indexes);
     foreach ($indexes as $index) {
         $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Index', $index);
     }
     $this->setExpectedException('\\Exception', 'Column "unknown_column" not found');
     $table->getColumn('unknown_column');
 }
示例#6
0
 public function testChangeAddedColumn()
 {
     $table = new Table('with_change_added_column');
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->addColumn(new Column('old_name', 'integer')));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->changeColumn('old_name', new Column('new_name', 'string')));
     $queryBuilder = new SqliteQueryBuilder();
     $expectedQueries = ['ALTER TABLE "with_change_added_column" ADD COLUMN "new_name" varchar(255) NOT NULL;'];
     $this->assertEquals($expectedQueries, $queryBuilder->alterTable($table));
 }
示例#7
0
 protected function createEnumSetColumn(Column $column, Table $table)
 {
     return sprintf($this->remapType($column), $table->getName(), $column->getName());
 }
示例#8
0
 public function testChangeColumn()
 {
     $table = new Table('with_columns_to_change');
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->changeColumn('old_name', new Column('new_name', 'integer')));
     $this->assertInstanceOf('\\Phoenix\\Database\\Element\\Table', $table->changeColumn('no_name_change', new Column('no_name_change', 'integer')));
     $queryBuilder = new PgsqlQueryBuilder();
     $expectedQueries = ['ALTER TABLE "with_columns_to_change" RENAME COLUMN "old_name" TO "new_name";', 'ALTER TABLE "with_columns_to_change" ALTER COLUMN "new_name" TYPE int4 USING new_name::integer;', 'ALTER TABLE "with_columns_to_change" ALTER COLUMN "no_name_change" TYPE int4 USING no_name_change::integer;'];
     $this->assertEquals($expectedQueries, $queryBuilder->alterTable($table));
 }