toSqlFragment method

String toSqlFragment()

Implementation

String toSqlFragment() {
  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:
      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)' : '';

  // 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';
}