mirror of https://github.com/AMT-Cheif/drift.git
Merge branch 'more-plurals' into develop
This commit is contained in:
commit
08241bfdd2
|
@ -1,5 +1,9 @@
|
|||
## 2.14.0-dev
|
||||
|
||||
- __Breaking change__: The name of the generated row class derived from the name
|
||||
of the Dart table name now supports more forms of plurals.
|
||||
For instance, a table without a `@DataClassName` annotation named `Categories`
|
||||
would now generate a `Category` class instead of `Categorie`.
|
||||
- Don't generate `const` row classes when they are extending a class which
|
||||
isn't const.
|
||||
|
||||
|
|
|
@ -14,15 +14,20 @@ String dataClassNameForClassName(String tableName) {
|
|||
// they're storing (users, products, ...). We try to find the singular word
|
||||
// from the table name.
|
||||
|
||||
// todo we might want to implement some edge cases according to
|
||||
// https://en.wikipedia.org/wiki/English_plurals
|
||||
|
||||
if (tableName.endsWith('s')) {
|
||||
return tableName.substring(0, tableName.length - 1);
|
||||
if (tableName.endsWith('ss') ||
|
||||
tableName.endsWith('us') ||
|
||||
tableName.endsWith('sses')) {
|
||||
return tableName;
|
||||
} else if (tableName.endsWith('ies')) {
|
||||
return '${tableName.substring(0, tableName.length - 3)}y';
|
||||
} else {
|
||||
return tableName.substring(0, tableName.length - 1);
|
||||
}
|
||||
} else {
|
||||
// Default behavior if the table name is not a valid plural.
|
||||
return '${tableName}Data';
|
||||
}
|
||||
|
||||
// Default behavior if the table name is not a valid plural.
|
||||
return '${tableName}Data';
|
||||
}
|
||||
|
||||
CustomParentClass? parseCustomParentClass(
|
||||
|
|
|
@ -93,6 +93,22 @@ class InvalidConstraints extends Table {
|
|||
IntColumn get a => integer().autoIncrement().customConstraint('foo')();
|
||||
IntColumn get b => integer().customConstraint('a').customConstraint('b')();
|
||||
}
|
||||
|
||||
class Properties extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
}
|
||||
|
||||
class Cats extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
}
|
||||
|
||||
class Messages extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
}
|
||||
|
||||
class Pianos extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
}
|
||||
''',
|
||||
});
|
||||
});
|
||||
|
@ -142,6 +158,35 @@ class InvalidConstraints extends Table {
|
|||
});
|
||||
});
|
||||
|
||||
group("class name inflection", () {
|
||||
test("singularizes table name that end in -ies", () async {
|
||||
final result = await findTable('Properties');
|
||||
final table = result!.result as DriftTable;
|
||||
|
||||
expect(table.nameOfRowClass, 'Property');
|
||||
});
|
||||
|
||||
test("singularizes table name that end in -s", () async {
|
||||
final result = await findTable('Cats');
|
||||
final table = result!.result as DriftTable;
|
||||
|
||||
expect(table.nameOfRowClass, 'Cat');
|
||||
});
|
||||
|
||||
test("singularizes table name that end in -es", () async {
|
||||
final result = await findTable('Messages');
|
||||
final table = result!.result as DriftTable;
|
||||
|
||||
expect(table.nameOfRowClass, 'Message');
|
||||
});
|
||||
|
||||
test("singularizes table name that end in -os", () async {
|
||||
final result = await findTable('Pianos');
|
||||
final table = result!.result as DriftTable;
|
||||
|
||||
expect(table.nameOfRowClass, 'Piano');
|
||||
});
|
||||
});
|
||||
group('Columns', () {
|
||||
test('should use field name if no name has been set explicitly', () async {
|
||||
final result = await findTable('Users');
|
||||
|
|
|
@ -473,8 +473,8 @@ class TodoEntriesCompanion extends UpdateCompanion<TodoEntry> {
|
|||
|
||||
class TextEntries extends Table
|
||||
with
|
||||
TableInfo<TextEntries, TextEntrie>,
|
||||
VirtualTableInfo<TextEntries, TextEntrie> {
|
||||
TableInfo<TextEntries, TextEntry>,
|
||||
VirtualTableInfo<TextEntries, TextEntry> {
|
||||
@override
|
||||
final GeneratedDatabase attachedDatabase;
|
||||
final String? _alias;
|
||||
|
@ -494,7 +494,7 @@ class TextEntries extends Table
|
|||
String get actualTableName => $name;
|
||||
static const String $name = 'text_entries';
|
||||
@override
|
||||
VerificationContext validateIntegrity(Insertable<TextEntrie> instance,
|
||||
VerificationContext validateIntegrity(Insertable<TextEntry> instance,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
final data = instance.toColumns(true);
|
||||
|
@ -512,9 +512,9 @@ class TextEntries extends Table
|
|||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => const {};
|
||||
@override
|
||||
TextEntrie map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
TextEntry map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
return TextEntrie(
|
||||
return TextEntry(
|
||||
description: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}description'])!,
|
||||
);
|
||||
|
@ -532,9 +532,9 @@ class TextEntries extends Table
|
|||
'fts5(description, content=todo_entries, content_rowid=id)';
|
||||
}
|
||||
|
||||
class TextEntrie extends DataClass implements Insertable<TextEntrie> {
|
||||
class TextEntry extends DataClass implements Insertable<TextEntry> {
|
||||
final String description;
|
||||
const TextEntrie({required this.description});
|
||||
const TextEntry({required this.description});
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
|
@ -548,10 +548,10 @@ class TextEntrie extends DataClass implements Insertable<TextEntrie> {
|
|||
);
|
||||
}
|
||||
|
||||
factory TextEntrie.fromJson(Map<String, dynamic> json,
|
||||
factory TextEntry.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return TextEntrie(
|
||||
return TextEntry(
|
||||
description: serializer.fromJson<String>(json['description']),
|
||||
);
|
||||
}
|
||||
|
@ -563,12 +563,12 @@ class TextEntrie extends DataClass implements Insertable<TextEntrie> {
|
|||
};
|
||||
}
|
||||
|
||||
TextEntrie copyWith({String? description}) => TextEntrie(
|
||||
TextEntry copyWith({String? description}) => TextEntry(
|
||||
description: description ?? this.description,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('TextEntrie(')
|
||||
return (StringBuffer('TextEntry(')
|
||||
..write('description: $description')
|
||||
..write(')'))
|
||||
.toString();
|
||||
|
@ -579,10 +579,10 @@ class TextEntrie extends DataClass implements Insertable<TextEntrie> {
|
|||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is TextEntrie && other.description == this.description);
|
||||
(other is TextEntry && other.description == this.description);
|
||||
}
|
||||
|
||||
class TextEntriesCompanion extends UpdateCompanion<TextEntrie> {
|
||||
class TextEntriesCompanion extends UpdateCompanion<TextEntry> {
|
||||
final Value<String> description;
|
||||
final Value<int> rowid;
|
||||
const TextEntriesCompanion({
|
||||
|
@ -593,7 +593,7 @@ class TextEntriesCompanion extends UpdateCompanion<TextEntrie> {
|
|||
required String description,
|
||||
this.rowid = const Value.absent(),
|
||||
}) : description = Value(description);
|
||||
static Insertable<TextEntrie> custom({
|
||||
static Insertable<TextEntry> custom({
|
||||
Expression<String>? description,
|
||||
Expression<int>? rowid,
|
||||
}) {
|
||||
|
|
|
@ -9,7 +9,7 @@ environment:
|
|||
|
||||
dependencies:
|
||||
drift: ^2.0.2+1
|
||||
sqlcipher_flutter_libs: ^0.5.1
|
||||
sqlcipher_flutter_libs: ^0.6.0
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path_provider: ^2.0.11
|
||||
|
|
|
@ -14,7 +14,7 @@ void main() {
|
|||
}
|
||||
|
||||
class _DatabaseSampleState extends State<_DatabaseSample> {
|
||||
List<Entrie> allItems = [];
|
||||
List<Entry> allItems = [];
|
||||
TextEditingController editController = TextEditingController();
|
||||
final database = MyDatabase(Platform.createDatabaseConnection('sample'));
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
part of 'database.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
class Entries extends Table with TableInfo<Entries, Entrie> {
|
||||
class Entries extends Table with TableInfo<Entries, Entry> {
|
||||
@override
|
||||
final GeneratedDatabase attachedDatabase;
|
||||
final String? _alias;
|
||||
|
@ -28,7 +28,7 @@ class Entries extends Table with TableInfo<Entries, Entrie> {
|
|||
String get actualTableName => $name;
|
||||
static const String $name = 'entries';
|
||||
@override
|
||||
VerificationContext validateIntegrity(Insertable<Entrie> instance,
|
||||
VerificationContext validateIntegrity(Insertable<Entry> instance,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
final data = instance.toColumns(true);
|
||||
|
@ -47,9 +47,9 @@ class Entries extends Table with TableInfo<Entries, Entrie> {
|
|||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
Entrie map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
Entry map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
return Entrie(
|
||||
return Entry(
|
||||
id: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.int, data['${effectivePrefix}id'])!,
|
||||
value: attachedDatabase.typeMapping
|
||||
|
@ -66,10 +66,10 @@ class Entries extends Table with TableInfo<Entries, Entrie> {
|
|||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class Entrie extends DataClass implements Insertable<Entrie> {
|
||||
class Entry extends DataClass implements Insertable<Entry> {
|
||||
final int id;
|
||||
final String value;
|
||||
const Entrie({required this.id, required this.value});
|
||||
const Entry({required this.id, required this.value});
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
|
@ -85,10 +85,10 @@ class Entrie extends DataClass implements Insertable<Entrie> {
|
|||
);
|
||||
}
|
||||
|
||||
factory Entrie.fromJson(Map<String, dynamic> json,
|
||||
factory Entry.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return Entrie(
|
||||
return Entry(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
value: serializer.fromJson<String>(json['text']),
|
||||
);
|
||||
|
@ -102,13 +102,13 @@ class Entrie extends DataClass implements Insertable<Entrie> {
|
|||
};
|
||||
}
|
||||
|
||||
Entrie copyWith({int? id, String? value}) => Entrie(
|
||||
Entry copyWith({int? id, String? value}) => Entry(
|
||||
id: id ?? this.id,
|
||||
value: value ?? this.value,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('Entrie(')
|
||||
return (StringBuffer('Entry(')
|
||||
..write('id: $id, ')
|
||||
..write('value: $value')
|
||||
..write(')'))
|
||||
|
@ -120,10 +120,10 @@ class Entrie extends DataClass implements Insertable<Entrie> {
|
|||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is Entrie && other.id == this.id && other.value == this.value);
|
||||
(other is Entry && other.id == this.id && other.value == this.value);
|
||||
}
|
||||
|
||||
class EntriesCompanion extends UpdateCompanion<Entrie> {
|
||||
class EntriesCompanion extends UpdateCompanion<Entry> {
|
||||
final Value<int> id;
|
||||
final Value<String> value;
|
||||
const EntriesCompanion({
|
||||
|
@ -134,7 +134,7 @@ class EntriesCompanion extends UpdateCompanion<Entrie> {
|
|||
this.id = const Value.absent(),
|
||||
required String value,
|
||||
}) : value = Value(value);
|
||||
static Insertable<Entrie> custom({
|
||||
static Insertable<Entry> custom({
|
||||
Expression<int>? id,
|
||||
Expression<String>? value,
|
||||
}) {
|
||||
|
@ -176,7 +176,7 @@ class EntriesCompanion extends UpdateCompanion<Entrie> {
|
|||
abstract class _$MyDatabase extends GeneratedDatabase {
|
||||
_$MyDatabase(QueryExecutor e) : super(e);
|
||||
late final Entries entries = Entries(this);
|
||||
Selectable<Entrie> allEntries() {
|
||||
Selectable<Entry> allEntries() {
|
||||
return customSelect('SELECT * FROM entries', variables: [], readsFrom: {
|
||||
entries,
|
||||
}).asyncMap(entries.mapFromRow);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
part of 'database.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
class Entries extends Table with TableInfo<Entries, Entrie> {
|
||||
class Entries extends Table with TableInfo<Entries, Entry> {
|
||||
@override
|
||||
final GeneratedDatabase attachedDatabase;
|
||||
final String? _alias;
|
||||
|
@ -28,7 +28,7 @@ class Entries extends Table with TableInfo<Entries, Entrie> {
|
|||
String get actualTableName => $name;
|
||||
static const String $name = 'entries';
|
||||
@override
|
||||
VerificationContext validateIntegrity(Insertable<Entrie> instance,
|
||||
VerificationContext validateIntegrity(Insertable<Entry> instance,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
final data = instance.toColumns(true);
|
||||
|
@ -47,9 +47,9 @@ class Entries extends Table with TableInfo<Entries, Entrie> {
|
|||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
Entrie map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
Entry map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
return Entrie(
|
||||
return Entry(
|
||||
id: attachedDatabase.typeMapping
|
||||
.read(DriftSqlType.int, data['${effectivePrefix}id'])!,
|
||||
value: attachedDatabase.typeMapping
|
||||
|
@ -66,10 +66,10 @@ class Entries extends Table with TableInfo<Entries, Entrie> {
|
|||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class Entrie extends DataClass implements Insertable<Entrie> {
|
||||
class Entry extends DataClass implements Insertable<Entry> {
|
||||
final int id;
|
||||
final String value;
|
||||
const Entrie({required this.id, required this.value});
|
||||
const Entry({required this.id, required this.value});
|
||||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
|
@ -85,10 +85,10 @@ class Entrie extends DataClass implements Insertable<Entrie> {
|
|||
);
|
||||
}
|
||||
|
||||
factory Entrie.fromJson(Map<String, dynamic> json,
|
||||
factory Entry.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return Entrie(
|
||||
return Entry(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
value: serializer.fromJson<String>(json['text']),
|
||||
);
|
||||
|
@ -102,13 +102,13 @@ class Entrie extends DataClass implements Insertable<Entrie> {
|
|||
};
|
||||
}
|
||||
|
||||
Entrie copyWith({int? id, String? value}) => Entrie(
|
||||
Entry copyWith({int? id, String? value}) => Entry(
|
||||
id: id ?? this.id,
|
||||
value: value ?? this.value,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('Entrie(')
|
||||
return (StringBuffer('Entry(')
|
||||
..write('id: $id, ')
|
||||
..write('value: $value')
|
||||
..write(')'))
|
||||
|
@ -120,10 +120,10 @@ class Entrie extends DataClass implements Insertable<Entrie> {
|
|||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is Entrie && other.id == this.id && other.value == this.value);
|
||||
(other is Entry && other.id == this.id && other.value == this.value);
|
||||
}
|
||||
|
||||
class EntriesCompanion extends UpdateCompanion<Entrie> {
|
||||
class EntriesCompanion extends UpdateCompanion<Entry> {
|
||||
final Value<int> id;
|
||||
final Value<String> value;
|
||||
const EntriesCompanion({
|
||||
|
@ -134,7 +134,7 @@ class EntriesCompanion extends UpdateCompanion<Entrie> {
|
|||
this.id = const Value.absent(),
|
||||
required String value,
|
||||
}) : value = Value(value);
|
||||
static Insertable<Entrie> custom({
|
||||
static Insertable<Entry> custom({
|
||||
Expression<int>? id,
|
||||
Expression<String>? value,
|
||||
}) {
|
||||
|
@ -176,7 +176,7 @@ class EntriesCompanion extends UpdateCompanion<Entrie> {
|
|||
abstract class _$MyDatabase extends GeneratedDatabase {
|
||||
_$MyDatabase(QueryExecutor e) : super(e);
|
||||
late final Entries entries = Entries(this);
|
||||
Selectable<Entrie> allEntries() {
|
||||
Selectable<Entry> allEntries() {
|
||||
return customSelect('SELECT * FROM entries', variables: [], readsFrom: {
|
||||
entries,
|
||||
}).asyncMap(entries.mapFromRow);
|
||||
|
|
Loading…
Reference in New Issue