Cake\Database\Schema\SqliteSchema::columnSql PHP Method

columnSql() public method

{@inheritDoc}
public columnSql ( Table $table, $name )
$table Table
    public function columnSql(Table $table, $name)
    {
        $data = $table->column($name);
        $typeMap = ['uuid' => ' CHAR(36)', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' BLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'json' => ' TEXT'];
        $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';
            }
        }
        if (isset($typeMap[$data['type']])) {
            $out .= $typeMap[$data['type']];
        }
        if ($data['type'] === 'text' && $data['length'] !== Table::LENGTH_TINY) {
            $out .= ' TEXT';
        }
        if ($data['type'] === 'string' || $data['type'] === 'text' && $data['length'] === Table::LENGTH_TINY) {
            $out .= ' VARCHAR';
            if (isset($data['length'])) {
                $out .= '(' . (int) $data['length'] . ')';
            }
        }
        if ($data['type'] === 'integer') {
            $out .= ' INTEGER';
            if (isset($data['length']) && [$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 && $data['type'] === 'timestamp') {
            $out .= ' DEFAULT NULL';
        }
        if (isset($data['default'])) {
            $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
        }
        return $out;
    }

Usage Example

Example #1
0
 /**
  * Test generating a bigint column that is a primary key.
  *
  * @return void
  */
 public function testColumnSqlPrimaryKeyBigInt()
 {
     $driver = $this->_getMockedDriver();
     $schema = new SqliteSchema($driver);
     $table = new Table('articles');
     $table->addColumn('id', ['type' => 'biginteger', 'null' => false])->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
     $result = $schema->columnSql($table, 'id');
     $this->assertEquals($result, '"id" BIGINT NOT NULL');
     $result = $schema->constraintSql($table, 'primary');
     $this->assertEquals('CONSTRAINT "primary" PRIMARY KEY ("id")', $result, 'Bigint primary keys are not special.');
 }