toSqlFragment method
Implementation
String toSqlFragment({String? tableName}) {
String type;
switch (columnType) {
case ColumnType.bigint:
case ColumnType.integer:
case ColumnType.timestampWithoutTimeZone: // Stored as epoch milliseconds
case ColumnType.boolean: // SQLite uses INTEGER (0/1) for booleans
type = 'INTEGER';
case ColumnType.doublePrecision:
type = 'REAL';
case ColumnType.uuid: // Storing UUIDs as BLOB for efficiency
case ColumnType.bytea:
case ColumnType.jsonb:
type = 'BLOB';
case ColumnType.text:
case ColumnType.json:
case ColumnType.vector:
case ColumnType.halfvec:
case ColumnType.sparsevec:
case ColumnType.bit:
case ColumnType.geography:
case ColumnType.geographyLineString:
case ColumnType.geographyPolygon:
case ColumnType.geographyGeometryCollection:
type = 'TEXT';
case ColumnType.unknown:
throw const FormatException('Unknown column type');
}
var nullable = isNullable ? '' : ' NOT NULL';
var defaultSql = columnType.getSqliteColumnDefault(columnDefault);
var defaultValue = defaultSql != null ? ' DEFAULT ($defaultSql)' : '';
if (columnDefault == defaultIntSerial && !isPrimary) {
final columnLocation = tableName == null
? 'Column "$name"'
: 'Column "$name" on table "$tableName"';
throw MigrationUnsupportedByDialectException(
dialect: DatabaseDialect.sqlite.name,
reason:
'$columnLocation uses "$defaultIntSerial" as its default value, '
'but "${DatabaseDialect.sqlite.name}" only supports auto-increment '
'on the "$defaultPrimaryKeyName" column.',
);
}
// The id column is special.
if (isPrimary) {
if (isNullable) {
throw const FormatException('The id column must be non-nullable');
}
// SQLite "INTEGER PRIMARY KEY" is an alias for ROWID.
if (type == 'INTEGER') {
defaultValue = '';
}
type = '$type PRIMARY KEY';
nullable = '';
}
return '"$name" $type$nullable$defaultValue';
}