/** * Turn a column model into SQL for alter or create. * * @param Column $column The column to convert. * @return string A partial chunk of SQL. */ private function columnToSQL($column) { $sql = [self::quoteIdentifier($column->name)]; if ($column->type instanceof String) { if ($column->type->length < 255) { $type = $column->type->variable ? 'VAR' : ''; $type .= $column->type->binary ? 'BINARY' : 'CHAR'; $type .= "({$column->type->length})"; } elseif ($column->type->length <= 255) { $type = $column->type->binary ? 'TINYBLOB' : 'TINYTEXT'; } elseif ($column->type->length <= 65535) { $type = $column->type->binary ? 'BLOB' : 'TEXT'; } elseif ($column->type->length <= pow(2, 24) - 1) { $type = $column->type->binary ? 'MEDIUMBLOB' : 'MEDIUMTEXT'; } else { $type = $column->type->binary ? 'LONGBLOB' : 'LONGTEXT'; } } elseif ($column->type instanceof Integer) { $typeMap = array_flip(self::$intSizeMap); if (isset($typeMap[$column->type->size])) { $type = $typeMap[$column->type->size]; } else { $type = $column->type->size > 4 ? 'BIGINT' : 'INT'; } } elseif ($column->type instanceof Float) { $type = $column->type->size > 4 ? 'DOUBLE' : 'FLOAT'; } elseif ($column->type instanceof Decimal) { $type = "DECIMAL({$column->type->precision}, {$column->type->scale})"; } elseif ($column->type instanceof DateTime) { if ($column->type->date && $column->type->time) { $type = $column->type->zone ? "TIMESTAMP" : "DATETIME"; } elseif ($column->type->date) { $type = "DATE"; } elseif ($column->type->time) { $type = "TIME"; } } elseif ($column->type instanceof Boolean) { /* MySQL doesn't directly support boolean */ $type = "TINYINT(1) UNSIGNED"; } elseif ($column->type instanceof Enum) { $values = array_map([$this->queryRunner, 'quote'], $column->type->values); $type = "ENUM(" . implode(",", $values) . ")"; } else { throw new DriverException("Unsupported type: " . get_class($column->type)); } $sql[] = $type; if ($column->primary) { $sql[] = "PRIMARY KEY"; } if ($column->sequence) { $sql[] = "AUTO_INCREMENT"; } if (!$column->null) { $sql[] = "NOT NULL"; } if ($column->default !== null) { $default = $this->queryRunner->quote($column->default); $sql[] = "DEFAULT {$default}"; } return implode(" ", $sql); }
public function testQuote() { $runner = new PDORunner(new PDO('sqlite::memory:')); $this->assertEquals('foo\'\'bar', $runner->quote('foo\'bar')); }