Fix drift tests

This commit is contained in:
Simon Binder 2023-08-02 17:38:09 +02:00
parent bb7e808487
commit 7678971d36
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
7 changed files with 34 additions and 13 deletions

View File

@ -36,7 +36,7 @@ abstract class Column<T extends Object> extends Expression<T> {
/// The (unescaped) name of this column. /// The (unescaped) name of this column.
/// ///
/// Use [escapedName] to access a name that's escaped in double quotes if /// Use [escapedNameFor] to access a name that's escaped in double quotes if
/// needed. /// needed.
String get name; String get name;
@ -45,8 +45,14 @@ abstract class Column<T extends Object> extends Expression<T> {
/// In the past, this getter only used to add double-quotes when that is /// In the past, this getter only used to add double-quotes when that is
/// really needed (for instance because [name] is also a reserved keyword). /// really needed (for instance because [name] is also a reserved keyword).
/// For performance reasons, we unconditionally escape names now. /// For performance reasons, we unconditionally escape names now.
String get escapedName => @Deprecated('Use escapedNameFor with the current dialect')
'`$name`'; // mariadb backtick escape DOT NOT MERGE STILL String get escapedName => '"$name"';
/// [name], but wrapped in double quotes or the DBMS-specific escape
/// identifier.
String escapedNameFor(SqlDialect dialect) {
return dialect.escape(name);
}
} }
/// A column that stores int values. /// A column that stores int values.

View File

@ -157,7 +157,7 @@ abstract class Expression<D extends Object> implements FunctionParameter {
/// For an "is in" comparison with values, use [isIn]. /// For an "is in" comparison with values, use [isIn].
Expression<bool> isInExp(List<Expression<D>> expressions) { Expression<bool> isInExp(List<Expression<D>> expressions) {
if (expressions.isEmpty) { if (expressions.isEmpty) {
return Constant(true); return Constant(false);
} }
return _InExpression(this, expressions, false); return _InExpression(this, expressions, false);

View File

@ -251,7 +251,7 @@ class Migrator {
expressionsForSelect.add(expression); expressionsForSelect.add(expression);
if (!first) context.buffer.write(', '); if (!first) context.buffer.write(', ');
context.buffer.write(column.escapedName); context.buffer.write(column.escapedNameFor(dialect));
first = false; first = false;
} }
} }
@ -348,7 +348,7 @@ class Migrator {
for (var i = 0; i < pkList.length; i++) { for (var i = 0; i < pkList.length; i++) {
final column = pkList[i]; final column = pkList[i];
context.buffer.write(column.escapedName); context.buffer.write(column.escapedNameFor(context.dialect));
if (i != pkList.length - 1) context.buffer.write(', '); if (i != pkList.length - 1) context.buffer.write(', ');
} }
@ -362,7 +362,7 @@ class Migrator {
for (var i = 0; i < uqList.length; i++) { for (var i = 0; i < uqList.length; i++) {
final column = uqList[i]; final column = uqList[i];
context.buffer.write(column.escapedName); context.buffer.write(column.escapedNameFor(context.dialect));
if (i != uqList.length - 1) context.buffer.write(', '); if (i != uqList.length - 1) context.buffer.write(', ');
} }
@ -418,7 +418,9 @@ class Migrator {
await _issueQueryByDialect(stmts); await _issueQueryByDialect(stmts);
} else if (view.query != null) { } else if (view.query != null) {
final context = GenerationContext.fromDb(_db, supportsVariables: false); final context = GenerationContext.fromDb(_db, supportsVariables: false);
final columnNames = view.$columns.map((e) => e.escapedName).join(', '); final columnNames = view.$columns
.map((e) => e.escapedNameFor(context.dialect))
.join(', ');
context.generatingForView = view.entityName; context.generatingForView = view.entityName;
context.buffer.write('CREATE VIEW IF NOT EXISTS ' context.buffer.write('CREATE VIEW IF NOT EXISTS '
@ -495,7 +497,7 @@ class Migrator {
context.buffer context.buffer
..write('ALTER TABLE ${context.identifier(table.aliasedName)} ') ..write('ALTER TABLE ${context.identifier(table.aliasedName)} ')
..write('RENAME COLUMN ${context.identifier(oldName)} ') ..write('RENAME COLUMN ${context.identifier(oldName)} ')
..write('TO ${column.escapedName};'); ..write('TO ${column.escapedNameFor(context.dialect)};');
return _issueCustomQuery(context.sql); return _issueCustomQuery(context.sql);
} }

View File

@ -134,10 +134,21 @@ enum SqlDialect {
supportsIndexedParameters: false, supportsIndexedParameters: false,
); );
/// The type to use in `CAST`s and column definitions to store booleans.
final String booleanType; final String booleanType;
/// The type to use in `CAST`s and column definitions to store strings.
final String textType; final String textType;
/// The type to use in `CAST`s and column definitions to store 64-bit
/// integers.
final String integerType; final String integerType;
/// The type to use in `CAST`s and column definitions to store doubles.
final String realType; final String realType;
/// The type to use in `CAST`s and column definitions to store blobs (as
/// a [Uint8List] in Dart).
final String blobType; final String blobType;
/// The character used to wrap identifiers to distinguish them from keywords. /// The character used to wrap identifiers to distinguish them from keywords.

View File

@ -115,6 +115,7 @@ class GeneratedColumn<T extends Object> extends Column<T> {
/// buffer. /// buffer.
void writeColumnDefinition(GenerationContext into) { void writeColumnDefinition(GenerationContext into) {
final isSerial = into.dialect == SqlDialect.postgres && hasAutoIncrement; final isSerial = into.dialect == SqlDialect.postgres && hasAutoIncrement;
final escapedName = escapedNameFor(into.dialect);
if (isSerial) { if (isSerial) {
into.buffer.write('$escapedName bigserial PRIMARY KEY NOT NULL'); into.buffer.write('$escapedName bigserial PRIMARY KEY NOT NULL');
@ -177,7 +178,8 @@ class GeneratedColumn<T extends Object> extends Column<T> {
..write(context.identifier(tableName)) ..write(context.identifier(tableName))
..write('.'); ..write('.');
} }
context.buffer.write(ignoreEscape ? $name : escapedName); context.buffer
.write(ignoreEscape ? $name : escapedNameFor(context.dialect));
} }
} }

View File

@ -291,7 +291,7 @@ class InsertStatement<T extends Table, D> {
// Writing the escaped name directly because it should not have a table // Writing the escaped name directly because it should not have a table
// name in front of it. // name in front of it.
ctx.buffer.write(target.escapedName); ctx.buffer.write(target.escapedNameFor(ctx.dialect));
first = false; first = false;
} }

View File

@ -73,8 +73,8 @@ void main() {
// it and replace it with the direct constant (since nothing can be a // it and replace it with the direct constant (since nothing can be a
// member of the empty set). sqlite3 seems to do the same thing, as // member of the empty set). sqlite3 seems to do the same thing, as
// `NULL IN ()` is `0` and not `NULL`. // `NULL IN ()` is `0` and not `NULL`.
expect(innerExpression.isIn([]), generates('FALSE')); expect(innerExpression.isIn([]), generates('0'));
expect(innerExpression.isNotIn([]), generates('TRUE')); expect(innerExpression.isNotIn([]), generates('1'));
}); });
}); });
} }