introduce SqlDialect.escape DO NOT MERGE until Column.escapedName is fixed (currently hardcoded for mariadb)

This commit is contained in:
BananaMasterz 2023-07-24 16:32:35 +03:00
parent 72368c9171
commit 78d6064634
6 changed files with 19 additions and 13 deletions

View File

@ -393,8 +393,7 @@ String escapeIfNeeded(String s, [SqlDialect dialect = SqlDialect.sqlite]) {
isKeyword |= additionalMariaDBKeywords.contains(inUpperCase);
}
if (isKeyword || _notInKeyword.hasMatch(s)) {
return dialect == SqlDialect.mariadb ? '`$s`' : '"$s"';
}
if (isKeyword || _notInKeyword.hasMatch(s)) return dialect.escape(s);
return s;
}

View File

@ -45,7 +45,7 @@ abstract class Column<T extends Object> extends Expression<T> {
/// 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).
/// For performance reasons, we unconditionally escape names now.
String get escapedName => '`$name`'; // mariadb backtick escape DOT NOT MERGE
String get escapedName => '`$name`'; // mariadb backtick escape DOT NOT MERGE STILL
}
/// A column that stores int values.

View File

@ -88,7 +88,5 @@ class GenerationContext {
/// Turns [columnName] into a safe SQL identifier by wrapping it in double
/// quotes, or backticks depending on the dialect.
String identifier(String columnName) {
return dialect == SqlDialect.mariadb ? '`$columnName`' : '"$columnName"';
}
String identifier(String columnName) => dialect.escape(columnName);
}

View File

@ -99,6 +99,7 @@ enum SqlDialect {
integerType: 'INTEGER',
realType: 'REAL',
blobType: 'BLOB',
escapeChar: '"',
),
/// (currently unsupported)
@ -108,6 +109,7 @@ enum SqlDialect {
integerType: '',
blobType: '',
realType: '',
escapeChar: '',
),
/// PostgreSQL (currently supported in an experimental state)
@ -117,6 +119,7 @@ enum SqlDialect {
integerType: 'bigint',
blobType: 'bytea',
realType: 'float8',
escapeChar: '"',
),
/// MariaDB (currently supported in an experimental state)
@ -126,6 +129,7 @@ enum SqlDialect {
integerType: 'BIGINT',
blobType: 'BLOB',
realType: 'DOUBLE',
escapeChar: '`',
);
final String booleanType;
@ -133,6 +137,10 @@ enum SqlDialect {
final String integerType;
final String realType;
final String blobType;
final String escapeChar;
/// Escapes [identifier] by wrapping it in [escapeChar].
String escape(String identifier) => '$escapeChar$identifier$escapeChar';
const SqlDialect({
required this.booleanType,
@ -140,5 +148,6 @@ enum SqlDialect {
required this.integerType,
required this.realType,
required this.blobType,
required this.escapeChar,
});
}

View File

@ -148,12 +148,12 @@ extension NameWithAlias on ResultSetImplementation<dynamic, dynamic> {
/// for a table called users that has been aliased as "u".
String get tableWithAlias {
var dialect = attachedDatabase.executor.dialect;
var entityNameEscaped = dialect.escape(entityName);
var aliasedNameEscaped = dialect.escape(aliasedName);
if (aliasedName == entityName) {
return dialect == SqlDialect.mariadb ? '`$entityName`' : '"$entityName"';
return entityNameEscaped;
} else {
return dialect == SqlDialect.mariadb
? '`$entityName` `$aliasedName`'
: '"$entityName" "$aliasedName"';
return '$entityNameEscaped $aliasedNameEscaped';
}
}
}

View File

@ -75,9 +75,9 @@ Map<SqlDialect, String> defaultConstraints(DriftColumn column) {
if (column.sqlType == DriftSqlType.bool) {
final name = column.nameInSql;
dialectSpecificConstraints[SqlDialect.sqlite]!
.add('CHECK ("$name" IN (0, 1))');
.add('CHECK (${SqlDialect.sqlite.escape(name)} IN (0, 1))');
dialectSpecificConstraints[SqlDialect.mariadb]!
.add('CHECK (`$name` IN (0, 1))');
.add('CHECK (${SqlDialect.mariadb.escape(name)} IN (0, 1))');
}
for (final constraints in dialectSpecificConstraints.values) {