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); isKeyword |= additionalMariaDBKeywords.contains(inUpperCase);
} }
if (isKeyword || _notInKeyword.hasMatch(s)) { if (isKeyword || _notInKeyword.hasMatch(s)) return dialect.escape(s);
return dialect == SqlDialect.mariadb ? '`$s`' : '"$s"';
}
return 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 /// 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 => '`$name`'; // mariadb backtick escape DOT NOT MERGE String get escapedName => '`$name`'; // mariadb backtick escape DOT NOT MERGE STILL
} }
/// A column that stores int values. /// 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 /// Turns [columnName] into a safe SQL identifier by wrapping it in double
/// quotes, or backticks depending on the dialect. /// quotes, or backticks depending on the dialect.
String identifier(String columnName) { String identifier(String columnName) => dialect.escape(columnName);
return dialect == SqlDialect.mariadb ? '`$columnName`' : '"$columnName"';
}
} }

View File

@ -99,6 +99,7 @@ enum SqlDialect {
integerType: 'INTEGER', integerType: 'INTEGER',
realType: 'REAL', realType: 'REAL',
blobType: 'BLOB', blobType: 'BLOB',
escapeChar: '"',
), ),
/// (currently unsupported) /// (currently unsupported)
@ -108,6 +109,7 @@ enum SqlDialect {
integerType: '', integerType: '',
blobType: '', blobType: '',
realType: '', realType: '',
escapeChar: '',
), ),
/// PostgreSQL (currently supported in an experimental state) /// PostgreSQL (currently supported in an experimental state)
@ -117,6 +119,7 @@ enum SqlDialect {
integerType: 'bigint', integerType: 'bigint',
blobType: 'bytea', blobType: 'bytea',
realType: 'float8', realType: 'float8',
escapeChar: '"',
), ),
/// MariaDB (currently supported in an experimental state) /// MariaDB (currently supported in an experimental state)
@ -126,6 +129,7 @@ enum SqlDialect {
integerType: 'BIGINT', integerType: 'BIGINT',
blobType: 'BLOB', blobType: 'BLOB',
realType: 'DOUBLE', realType: 'DOUBLE',
escapeChar: '`',
); );
final String booleanType; final String booleanType;
@ -133,6 +137,10 @@ enum SqlDialect {
final String integerType; final String integerType;
final String realType; final String realType;
final String blobType; final String blobType;
final String escapeChar;
/// Escapes [identifier] by wrapping it in [escapeChar].
String escape(String identifier) => '$escapeChar$identifier$escapeChar';
const SqlDialect({ const SqlDialect({
required this.booleanType, required this.booleanType,
@ -140,5 +148,6 @@ enum SqlDialect {
required this.integerType, required this.integerType,
required this.realType, required this.realType,
required this.blobType, 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". /// for a table called users that has been aliased as "u".
String get tableWithAlias { String get tableWithAlias {
var dialect = attachedDatabase.executor.dialect; var dialect = attachedDatabase.executor.dialect;
var entityNameEscaped = dialect.escape(entityName);
var aliasedNameEscaped = dialect.escape(aliasedName);
if (aliasedName == entityName) { if (aliasedName == entityName) {
return dialect == SqlDialect.mariadb ? '`$entityName`' : '"$entityName"'; return entityNameEscaped;
} else { } else {
return dialect == SqlDialect.mariadb return '$entityNameEscaped $aliasedNameEscaped';
? '`$entityName` `$aliasedName`'
: '"$entityName" "$aliasedName"';
} }
} }
} }

View File

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