Exemple #1
0
 public function addColumn(Table $table, Column $col)
 {
     $sql = sprintf("ALTER TABLE %s ADD %s %s", $this->conn->quoteIdentifier($table->getName()), $this->conn->quoteIdentifier($col->getName()), $this->getColumnDefinitionSql($col));
     $this->conn->execute($sql);
 }
 protected function getColumnDefinitionSql(Column $col)
 {
     $type = $this->getDatabaseType($col->getType());
     $limit = $col->getLimit();
     if ($col->isIdentity()) {
         $sql = 'SERIAL';
     } else {
         $sql = strtoupper($type['name']);
         if (array_key_exists('limit', $type)) {
             $sql .= sprintf('(%u)', $limit === NULL ? $type['limit'] : min($limit, $type['limit']));
         }
         if (!empty($type['unsigned']) || array_key_exists('unsigned', $type) && $col->isUnsigned()) {
             if ($col->isNullable()) {
                 $sql .= sprintf(' CHECK (%s IS NULL OR %s >= 0)', $this->conn->quoteIdentifier($col->getName()), $this->conn->quoteIdentifier($col->getName()));
             } else {
                 $sql .= sprintf(' CHECK (%s >= 0)', $this->conn->quoteIdentifier($col->getName()));
             }
         }
     }
     $sql .= $col->isNullable() ? ' NULL' : ' NOT NULL';
     if ($col->hasDefault()) {
         $sql .= $this->getDefaultValueSql($col->getDefault());
     }
     return $sql;
 }