Esempio n. 1
0
 public function createTable(Table $table)
 {
     $options = ['engine' => 'InnoDB', 'collate' => 'utf8_unicode_ci'];
     $options = array_merge($options, array_change_key_case($table->getOptions(), CASE_LOWER));
     $pk = [];
     $sql = 'CREATE TABLE ' . $this->conn->quoteIdentifier($table->getName()) . ' ( ';
     foreach ($table->getPendingColumns() as $col) {
         $sql .= $this->conn->quoteIdentifier($col->getName()) . ' ' . $this->getColumnDefinitionSql($col) . ', ';
         if ($col->isPrimaryKey()) {
             $pk[] = $this->conn->quoteIdentifier($col->getName());
         }
     }
     $sql = rtrim($sql, ', ');
     if (!empty($pk)) {
         $sql .= sprintf(', PRIMARY KEY (%s)', implode(', ', $pk));
     }
     foreach ($table->getPendingIndexes() as $index) {
         $cols = array_map(function ($col) {
             return $this->conn->quoteIdentifier($col);
         }, $index->getColumns());
         $sql .= ', ' . $this->getIndexDefinitionSql($table, $index) . ' (' . implode(', ', $cols) . ')';
     }
     foreach ($table->getPendingForeignKeys() as $key) {
         $sql .= ', ' . $this->getForeignKeyDefinitionSql($table->getName(), $key);
     }
     $sql .= ')';
     foreach ($options as $k => $v) {
         $sql .= sprintf(' %s=%s', strtoupper($k), $v);
     }
     $stmt = $this->conn->prepare($sql);
     $stmt->execute();
 }
Esempio n. 2
0
 public function createTable(Table $table)
 {
     $pk = [];
     $sql = 'CREATE TABLE ' . $this->conn->quoteIdentifier($table->getName()) . ' ( ';
     foreach ($table->getPendingColumns() as $col) {
         $sql .= $this->conn->quoteIdentifier($col->getName()) . ' ' . $this->getColumnDefinitionSql($col) . ', ';
         if ($col->isPrimaryKey()) {
             $pk[] = $this->conn->quoteIdentifier($col->getName());
         }
     }
     $sql = rtrim($sql, ', ');
     if (!empty($pk)) {
         $sql .= sprintf(', PRIMARY KEY (%s)', implode(', ', $pk));
     }
     foreach ($table->getPendingForeignKeys() as $key) {
         $sql .= ', ' . $this->getForeignKeyDefinitionSql($table->getName(), $key);
     }
     $sql .= ')';
     $stmt = $this->conn->prepare($sql);
     $stmt->execute();
     foreach ($table->getPendingIndexes() as $index) {
         $this->addIndex($table, $index);
     }
 }