public function testEquals() { $table = new Table("foo", [new Column()]); $table->columns[0]->name = "foo"; $tableCopy = clone $table; $this->assertFalse($table === $tableCopy); $this->assertTrue($table->equals($tableCopy)); $tableCopy->name = "bar"; $this->assertFalse($table->equals($tableCopy)); $tableCopy->name = $table->name; $tableCopy->columns[0]->name = "baz"; $this->assertFalse($table->equals($tableCopy)); }
/** * Create or update a table in a database. * * @param string $database The name of the database. * @param string $schema The name of the schema. * @param Table $table The Table object representing what the table should become. * @param bool $force If false, the driver should not perform possibly destructive changes. * @return bool True if operations were performed successfully. */ public function setTable($database, $schema, Table $table, $force = false) { /* We either create or alter depending on whether the table exists. */ $curTable = $this->getTable($database, $schema, $table->name); if ($database = $this->interpretDatabase($database, $schema)) { $tableSQL = sprintf("%s.%s", $this->quoteIdentifier($database), $this->quoteIdentifier($table->name)); } else { $tableSQL = $this->quoteIdentifier($table->name); } if ($curTable === false) { $cols = []; foreach ($table->columns as $column) { $cols[] = $this->columnToSQL($column); } $sql = sprintf("CREATE TABLE %s (\n%s\n) ENGINE=InnoDB DEFAULT CHARSET=utf8", $tableSQL, implode(",\n", $cols)); } else { $sql = []; list($add, $drop, $change) = Table::columnDiff($curTable, $table); foreach ($drop as $dropCol) { $sql[] = sprintf("DROP COLUMN %s", $this->quoteIdentifier($dropCol->name)); } foreach ($add as $addCol) { $sql[] = sprintf("ADD COLUMN %s", $this->columnToSQL($addCol)); } foreach ($change as $changedCols) { list($oldCol, $newCol) = $changedCols; $sql[] = sprintf("MODIFY %s", $this->columnToSQL($newCol)); } if (empty($sql)) { return true; } $sql = sprintf("ALTER TABLE %s %s", $tableSQL, implode(",", $sql)); } return $this->queryRunner->exec($sql, false) !== false; }