From 34e827b0b5d0c68a5c51c98868c4b3c4bf955583 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 25 Jun 2020 20:03:05 +0200 Subject: [PATCH] Escape column names in primary key (#661) --- .../src/runtime/query_builder/migration.dart | 2 +- moor/test/data/tables/todos.dart | 5 +- moor/test/data/tables/todos.g.dart | 52 +++---------------- moor/test/data_class_test.dart | 4 +- 4 files changed, 15 insertions(+), 48 deletions(-) diff --git a/moor/lib/src/runtime/query_builder/migration.dart b/moor/lib/src/runtime/query_builder/migration.dart index 75681ed3..7705fece 100644 --- a/moor/lib/src/runtime/query_builder/migration.dart +++ b/moor/lib/src/runtime/query_builder/migration.dart @@ -134,7 +134,7 @@ class Migrator { for (var i = 0; i < pkList.length; i++) { final column = pkList[i]; - context.buffer.write(column.$name); + context.buffer.write(escapeIfNeeded(column.$name)); if (i != pkList.length - 1) context.buffer.write(', '); } diff --git a/moor/test/data/tables/todos.dart b/moor/test/data/tables/todos.dart index 1d9e2dd6..405e0a27 100644 --- a/moor/test/data/tables/todos.dart +++ b/moor/test/data/tables/todos.dart @@ -63,9 +63,12 @@ class TableWithoutPK extends Table { text().map(const CustomConverter()).clientDefault(_uuid.v4)(); } -class PureDefaults extends Table with AutoIncrement { +class PureDefaults extends Table { // name after keyword to ensure it's escaped properly TextColumn get txt => text().named('insert').nullable()(); + + @override + Set get primaryKey => {txt}; } // example object used for custom mapping diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index 99a9740e..ba937eaf 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -1386,25 +1386,19 @@ class $TableWithoutPKTable extends TableWithoutPK } class PureDefault extends DataClass implements Insertable { - final int id; final String txt; - PureDefault({@required this.id, this.txt}); + PureDefault({this.txt}); factory PureDefault.fromData(Map data, GeneratedDatabase db, {String prefix}) { final effectivePrefix = prefix ?? ''; - final intType = db.typeSystem.forDartType(); final stringType = db.typeSystem.forDartType(); return PureDefault( - id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id']), txt: stringType.mapFromDatabaseResponse(data['${effectivePrefix}insert']), ); } @override Map toColumns(bool nullToAbsent) { final map = {}; - if (!nullToAbsent || id != null) { - map['id'] = Variable(id); - } if (!nullToAbsent || txt != null) { map['insert'] = Variable(txt); } @@ -1413,7 +1407,6 @@ class PureDefault extends DataClass implements Insertable { PureDefaultsCompanion toCompanion(bool nullToAbsent) { return PureDefaultsCompanion( - id: id == null && nullToAbsent ? const Value.absent() : Value(id), txt: txt == null && nullToAbsent ? const Value.absent() : Value(txt), ); } @@ -1422,7 +1415,6 @@ class PureDefault extends DataClass implements Insertable { {ValueSerializer serializer}) { serializer ??= moorRuntimeOptions.defaultSerializer; return PureDefault( - id: serializer.fromJson(json['id']), txt: serializer.fromJson(json['txt']), ); } @@ -1435,56 +1427,44 @@ class PureDefault extends DataClass implements Insertable { Map toJson({ValueSerializer serializer}) { serializer ??= moorRuntimeOptions.defaultSerializer; return { - 'id': serializer.toJson(id), 'txt': serializer.toJson(txt), }; } - PureDefault copyWith({int id, String txt}) => PureDefault( - id: id ?? this.id, + PureDefault copyWith({String txt}) => PureDefault( txt: txt ?? this.txt, ); @override String toString() { - return (StringBuffer('PureDefault(') - ..write('id: $id, ') - ..write('txt: $txt') - ..write(')')) + return (StringBuffer('PureDefault(')..write('txt: $txt')..write(')')) .toString(); } @override - int get hashCode => $mrjf($mrjc(id.hashCode, txt.hashCode)); + int get hashCode => $mrjf(txt.hashCode); @override bool operator ==(dynamic other) => - identical(this, other) || - (other is PureDefault && other.id == this.id && other.txt == this.txt); + identical(this, other) || (other is PureDefault && other.txt == this.txt); } class PureDefaultsCompanion extends UpdateCompanion { - final Value id; final Value txt; const PureDefaultsCompanion({ - this.id = const Value.absent(), this.txt = const Value.absent(), }); PureDefaultsCompanion.insert({ - this.id = const Value.absent(), this.txt = const Value.absent(), }); static Insertable custom({ - Expression id, Expression txt, }) { return RawValuesInsertable({ - if (id != null) 'id': id, if (txt != null) 'insert': txt, }); } - PureDefaultsCompanion copyWith({Value id, Value txt}) { + PureDefaultsCompanion copyWith({Value txt}) { return PureDefaultsCompanion( - id: id ?? this.id, txt: txt ?? this.txt, ); } @@ -1492,9 +1472,6 @@ class PureDefaultsCompanion extends UpdateCompanion { @override Map toColumns(bool nullToAbsent) { final map = {}; - if (id.present) { - map['id'] = Variable(id.value); - } if (txt.present) { map['insert'] = Variable(txt.value); } @@ -1504,7 +1481,6 @@ class PureDefaultsCompanion extends UpdateCompanion { @override String toString() { return (StringBuffer('PureDefaultsCompanion(') - ..write('id: $id, ') ..write('txt: $txt') ..write(')')) .toString(); @@ -1516,15 +1492,6 @@ class $PureDefaultsTable extends PureDefaults final GeneratedDatabase _db; final String _alias; $PureDefaultsTable(this._db, [this._alias]); - final VerificationMeta _idMeta = const VerificationMeta('id'); - GeneratedIntColumn _id; - @override - GeneratedIntColumn get id => _id ??= _constructId(); - GeneratedIntColumn _constructId() { - return GeneratedIntColumn('id', $tableName, false, - hasAutoIncrement: true, declaredAsPrimaryKey: true); - } - final VerificationMeta _txtMeta = const VerificationMeta('txt'); GeneratedTextColumn _txt; @override @@ -1538,7 +1505,7 @@ class $PureDefaultsTable extends PureDefaults } @override - List get $columns => [id, txt]; + List get $columns => [txt]; @override $PureDefaultsTable get asDslTable => this; @override @@ -1550,9 +1517,6 @@ class $PureDefaultsTable extends PureDefaults {bool isInserting = false}) { final context = VerificationContext(); final data = instance.toColumns(true); - if (data.containsKey('id')) { - context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta)); - } if (data.containsKey('insert')) { context.handle( _txtMeta, txt.isAcceptableOrUnknown(data['insert'], _txtMeta)); @@ -1561,7 +1525,7 @@ class $PureDefaultsTable extends PureDefaults } @override - Set get $primaryKey => {id}; + Set get $primaryKey => {txt}; @override PureDefault map(Map data, {String tablePrefix}) { final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null; diff --git a/moor/test/data_class_test.dart b/moor/test/data_class_test.dart index 25e44b55..54f2ee6b 100644 --- a/moor/test/data_class_test.dart +++ b/moor/test/data_class_test.dart @@ -92,10 +92,10 @@ void main() { }); test('data classes can be converted to companions with null to absent', () { - final entry = PureDefault(id: null, txt: null); + final entry = PureDefault(txt: null); expect(entry.toCompanion(false), - const PureDefaultsCompanion(id: Value(null), txt: Value(null))); + const PureDefaultsCompanion(txt: Value(null))); expect(entry.toCompanion(true), const PureDefaultsCompanion()); });