/**
  * Generate the SQL fragment for a single column in a table.
  *
  * @param TableSchema $table The table instance the column is in.
  * @param string $name The name of the column.
  * @return string SQL fragment.
  */
 public function columnSql(TableSchema $table, $name)
 {
     $data = $table->column($name);
     $out = $this->_driver->quoteIdentifier($name);
     $typeMap = ['boolean' => ' BOOLEAN', 'binary' => ' BYTEA', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' TIMESTAMP', 'timestamp' => ' TIMESTAMP', 'uuid' => ' UUID'];
     if (isset($typeMap[$data['type']])) {
         $out .= $typeMap[$data['type']];
     }
     if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
         $type = $data['type'] === 'integer' ? ' INTEGER' : ' BIGINT';
         if ([$name] === $table->primaryKey() || $data['autoIncrement'] === true) {
             $type = $data['type'] === 'integer' ? ' SERIAL' : ' BIGSERIAL';
             unset($data['null'], $data['default']);
         }
         $out .= $type;
     }
     if ($data['type'] === 'string') {
         $isFixed = !empty($data['fixed']);
         $type = ' VARCHAR';
         if ($isFixed) {
             $type = ' CHAR';
         }
         $out .= $type;
         if (isset($data['length']) && $data['length'] != 36) {
             $out .= '(' . (int) $data['length'] . ')';
         }
     }
     if ($data['type'] === 'float' && isset($data['precision'])) {
         $out .= '(' . (int) $data['precision'] . ')';
     }
     if ($data['type'] === 'decimal' && (isset($data['length']) || isset($data['precision']))) {
         $out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
     }
     if (isset($data['null']) && $data['null'] === false) {
         $out .= ' NOT NULL';
     }
     if (isset($data['null']) && $data['null'] === true) {
         $out .= ' DEFAULT NULL';
         unset($data['default']);
     }
     if (isset($data['default']) && $data['type'] !== 'timestamp') {
         $defaultValue = $data['default'];
         if ($data['type'] === 'boolean') {
             $defaultValue = (bool) $defaultValue;
         }
         $out .= ' DEFAULT ' . $this->_driver->schemaValue($defaultValue);
     }
     return $out;
 }
Exemple #2
0
 /**
  * Generate the SQL fragment for a single column in a table.
  *
  * @param TableSchema $table The table instance the column is in.
  * @param string $name The name of the column.
  * @return string SQL fragment.
  */
 public function columnSql(TableSchema $table, $name)
 {
     $data = $table->column($name);
     $out = $this->_driver->quoteIdentifier($name);
     $typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' LONGBLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'uuid' => ' CHAR(36)'];
     $specialMap = ['string' => true];
     if (isset($typeMap[$data['type']])) {
         $out .= $typeMap[$data['type']];
     }
     if (isset($specialMap[$data['type']])) {
         switch ($data['type']) {
             case 'string':
                 $out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
                 if (!isset($data['length'])) {
                     $data['length'] = 255;
                 }
                 break;
         }
     }
     $hasLength = ['integer', 'string'];
     if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
         $out .= '(' . (int) $data['length'] . ')';
     }
     $hasPrecision = ['float', 'decimal'];
     if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
         $out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
     }
     $hasUnsigned = ['float', 'decimal', 'integer', 'biginteger'];
     if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
         $out .= ' UNSIGNED';
     }
     if (isset($data['null']) && $data['null'] === false) {
         $out .= ' NOT NULL';
     }
     $addAutoIncrement = [$name] == (array) $table->primaryKey() && !$table->hasAutoIncrement();
     if (in_array($data['type'], ['integer', 'biginteger']) && ($data['autoIncrement'] === true || $addAutoIncrement)) {
         $out .= ' AUTO_INCREMENT';
     }
     if (isset($data['null']) && $data['null'] === true) {
         $out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
         unset($data['default']);
     }
     if (isset($data['default']) && !in_array($data['type'], ['timestamp', 'datetime'])) {
         $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
         unset($data['default']);
     }
     if (isset($data['default']) && in_array($data['type'], ['timestamp', 'datetime']) && strtolower($data['default']) === 'current_timestamp') {
         $out .= ' DEFAULT CURRENT_TIMESTAMP';
         unset($data['default']);
     }
     if (isset($data['comment']) && $data['comment'] !== '') {
         $out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
     }
     return $out;
 }
 /**
  * Generate the SQL to truncate a table.
  *
  * @param TableSchema $table Table instance.
  * @return array SQL statements to truncate a table.
  */
 public function truncateTableSql(TableSchema $table)
 {
     $name = $this->_driver->quoteIdentifier($table->name());
     $queries = [sprintf('DELETE FROM %s', $name)];
     // Restart identity sequences
     $pk = $table->primaryKey();
     if (count($pk) === 1) {
         $column = $table->column($pk[0]);
         if (in_array($column['type'], ['integer', 'biginteger'])) {
             $queries[] = sprintf('DBCC CHECKIDENT(%s, RESEED, 0)', $name);
         }
     }
     return $queries;
 }
Exemple #4
0
 /**
  * Generate the SQL fragment for a single column in a table.
  *
  * @param TableSchema $table The table instance the column is in.
  * @param string $name The name of the column.
  * @return string SQL fragment.
  * @throws Exception
  */
 public function columnSql(TableSchema $table, $name)
 {
     $data = $table->column($name);
     $typeMap = ['uuid' => ' CHAR(36)', 'string' => ' VARCHAR', 'integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' BLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP'];
     if (!isset($typeMap[$data['type']])) {
         throw new Exception(sprintf('Unknown column type for "%s"', $name));
     }
     $out = $this->_driver->quoteIdentifier($name);
     $hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];
     if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
         if ($data['type'] !== 'integer' || [$name] !== (array) $table->primaryKey()) {
             $out .= ' UNSIGNED';
         }
     }
     $out .= $typeMap[$data['type']];
     $hasLength = ['integer', 'string'];
     if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
         if ($data['type'] !== 'integer' || [$name] !== (array) $table->primaryKey()) {
             $out .= '(' . (int) $data['length'] . ')';
         }
     }
     $hasPrecision = ['float', 'decimal'];
     if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
         $out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
     }
     if (isset($data['null']) && $data['null'] === false) {
         $out .= ' NOT NULL';
     }
     if ($data['type'] === 'integer' && [$name] === (array) $table->primaryKey()) {
         $out .= ' PRIMARY KEY AUTOINCREMENT';
     }
     if (isset($data['null']) && $data['null'] === true) {
         $out .= ' DEFAULT NULL';
         unset($data['default']);
     }
     if (isset($data['default'])) {
         $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
     }
     return $out;
 }