Fix tests and generation in drift package

This commit is contained in:
Simon Binder 2021-10-08 22:40:58 +02:00
parent 3a56de1830
commit 7358f545ac
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
35 changed files with 200 additions and 160 deletions

21
drift/build.yaml Normal file
View File

@ -0,0 +1,21 @@
targets:
$default:
builders:
drift_dev:
options:
override_hash_and_equals_in_result_sets: true
use_column_name_as_json_key_when_defined_in_moor_file: true
generate_connect_constructor: true
compact_query_methods: true
write_from_json_string_constructor: true
raw_result_set_data: true
apply_converters_on_variables: true
generate_values_in_copy_with: true
named_parameters: true
new_sql_code_generation: true
scoped_dart_components: true
sqlite:
version: "3.35"
modules:
- json1
- fts5

View File

@ -6,7 +6,7 @@ homepage: https://drift.simonbinder.eu/
issue_tracker: https://github.com/simolus3/moor/issues
environment:
sdk: '>=2.13.0 <3.0.0'
sdk: '>=2.14.0 <3.0.0'
dependencies:
async: ^2.5.0

View File

@ -68,7 +68,7 @@ class Config extends DataClass implements Insertable<Config> {
factory Config.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return Config(
configKey: serializer.fromJson<String>(json['config_key']),
configValue: serializer.fromJson<String?>(json['config_value']),
@ -83,7 +83,7 @@ class Config extends DataClass implements Insertable<Config> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'config_key': serializer.toJson<String>(configKey),
'config_value': serializer.toJson<String?>(configValue),
@ -117,10 +117,8 @@ class Config extends DataClass implements Insertable<Config> {
}
@override
int get hashCode => $mrjf($mrjc(
configKey.hashCode,
$mrjc(configValue.hashCode,
$mrjc(syncState.hashCode, syncStateImplicit.hashCode))));
int get hashCode =>
Object.hash(configKey, configValue, syncState, syncStateImplicit);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -322,7 +320,7 @@ class WithDefault extends DataClass implements Insertable<WithDefault> {
factory WithDefault.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return WithDefault(
a: serializer.fromJson<String?>(json['a']),
b: serializer.fromJson<int?>(json['b']),
@ -335,7 +333,7 @@ class WithDefault extends DataClass implements Insertable<WithDefault> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'a': serializer.toJson<String?>(a),
'b': serializer.toJson<int?>(b),
@ -359,7 +357,7 @@ class WithDefault extends DataClass implements Insertable<WithDefault> {
}
@override
int get hashCode => $mrjf($mrjc(a.hashCode, b.hashCode));
int get hashCode => Object.hash(a, b);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -601,7 +599,7 @@ class WithConstraint extends DataClass implements Insertable<WithConstraint> {
factory WithConstraint.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return WithConstraint(
a: serializer.fromJson<String?>(json['a']),
b: serializer.fromJson<int>(json['b']),
@ -615,7 +613,7 @@ class WithConstraint extends DataClass implements Insertable<WithConstraint> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'a': serializer.toJson<String?>(a),
'b': serializer.toJson<int>(b),
@ -643,7 +641,7 @@ class WithConstraint extends DataClass implements Insertable<WithConstraint> {
}
@override
int get hashCode => $mrjf($mrjc(a.hashCode, $mrjc(b.hashCode, c.hashCode)));
int get hashCode => Object.hash(a, b, c);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -832,7 +830,7 @@ class MytableData extends DataClass implements Insertable<MytableData> {
factory MytableData.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return MytableData(
someid: serializer.fromJson<int>(json['someid']),
sometext: serializer.fromJson<String?>(json['sometext']),
@ -847,7 +845,7 @@ class MytableData extends DataClass implements Insertable<MytableData> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'someid': serializer.toJson<int>(someid),
'sometext': serializer.toJson<String?>(sometext),
@ -879,10 +877,7 @@ class MytableData extends DataClass implements Insertable<MytableData> {
}
@override
int get hashCode => $mrjf($mrjc(
someid.hashCode,
$mrjc(
sometext.hashCode, $mrjc(isInserting.hashCode, somedate.hashCode))));
int get hashCode => Object.hash(someid, sometext, isInserting, somedate);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1079,7 +1074,7 @@ class EMail extends DataClass implements Insertable<EMail> {
factory EMail.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return EMail(
sender: serializer.fromJson<String>(json['sender']),
title: serializer.fromJson<String>(json['title']),
@ -1092,7 +1087,7 @@ class EMail extends DataClass implements Insertable<EMail> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'sender': serializer.toJson<String>(sender),
'title': serializer.toJson<String>(title),
@ -1116,8 +1111,7 @@ class EMail extends DataClass implements Insertable<EMail> {
}
@override
int get hashCode =>
$mrjf($mrjc(sender.hashCode, $mrjc(title.hashCode, body.hashCode)));
int get hashCode => Object.hash(sender, title, body);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1289,7 +1283,7 @@ class WeirdData extends DataClass implements Insertable<WeirdData> {
factory WeirdData.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return WeirdData(
sqlClass: serializer.fromJson<int>(json['class']),
textColumn: serializer.fromJson<String>(json['text']),
@ -1302,7 +1296,7 @@ class WeirdData extends DataClass implements Insertable<WeirdData> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'class': serializer.toJson<int>(sqlClass),
'text': serializer.toJson<String>(textColumn),
@ -1323,7 +1317,7 @@ class WeirdData extends DataClass implements Insertable<WeirdData> {
}
@override
int get hashCode => $mrjf($mrjc(sqlClass.hashCode, textColumn.hashCode));
int get hashCode => Object.hash(sqlClass, textColumn);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1469,7 +1463,7 @@ class MyViewData extends DataClass {
}
factory MyViewData.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return MyViewData(
configKey: serializer.fromJson<String>(json['configKey']),
configValue: serializer.fromJson<String?>(json['configValue']),
@ -1485,7 +1479,7 @@ class MyViewData extends DataClass {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'configKey': serializer.toJson<String>(configKey),
'configValue': serializer.toJson<String?>(configValue),
@ -1519,10 +1513,8 @@ class MyViewData extends DataClass {
}
@override
int get hashCode => $mrjf($mrjc(
configKey.hashCode,
$mrjc(configValue.hashCode,
$mrjc(syncState.hashCode, syncStateImplicit.hashCode))));
int get hashCode =>
Object.hash(configKey, configValue, syncState, syncStateImplicit);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1825,7 +1817,7 @@ class JsonResult extends CustomResultSet {
this.value,
}) : super(row);
@override
int get hashCode => $mrjf($mrjc(key.hashCode, value.hashCode));
int get hashCode => Object.hash(key, value);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1853,7 +1845,7 @@ class MultipleResult extends CustomResultSet {
this.c,
}) : super(row);
@override
int get hashCode => $mrjf($mrjc(a.hashCode, $mrjc(b.hashCode, c.hashCode)));
int get hashCode => Object.hash(a, b, c);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1887,12 +1879,8 @@ class ReadRowIdResult extends CustomResultSet {
this.syncStateImplicit,
}) : super(row);
@override
int get hashCode => $mrjf($mrjc(
rowid.hashCode,
$mrjc(
configKey.hashCode,
$mrjc(configValue.hashCode,
$mrjc(syncState.hashCode, syncStateImplicit.hashCode)))));
int get hashCode =>
Object.hash(rowid, configKey, configValue, syncState, syncStateImplicit);
@override
bool operator ==(Object other) =>
identical(this, other) ||

View File

@ -69,7 +69,7 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
factory TodoEntry.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return TodoEntry(
id: serializer.fromJson<int>(json['id']),
title: serializer.fromJson<String?>(json['title']),
@ -85,7 +85,7 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<int>(id),
'title': serializer.toJson<String?>(title),
@ -121,12 +121,7 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
}
@override
int get hashCode => $mrjf($mrjc(
id.hashCode,
$mrjc(
title.hashCode,
$mrjc(content.hashCode,
$mrjc(targetDate.hashCode, category.hashCode)))));
int get hashCode => Object.hash(id, title, content, targetDate, category);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -345,7 +340,7 @@ class Category extends DataClass implements Insertable<Category> {
factory Category.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return Category(
id: serializer.fromJson<int>(json['id']),
description: serializer.fromJson<String>(json['description']),
@ -359,7 +354,7 @@ class Category extends DataClass implements Insertable<Category> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<int>(id),
'description': serializer.toJson<String>(description),
@ -385,8 +380,7 @@ class Category extends DataClass implements Insertable<Category> {
}
@override
int get hashCode =>
$mrjf($mrjc(id.hashCode, $mrjc(description.hashCode, priority.hashCode)));
int get hashCode => Object.hash(id, description, priority);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -577,7 +571,7 @@ class User extends DataClass implements Insertable<User> {
factory User.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return User(
id: serializer.fromJson<int>(json['id']),
name: serializer.fromJson<String>(json['name']),
@ -592,7 +586,7 @@ class User extends DataClass implements Insertable<User> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<int>(id),
'name': serializer.toJson<String>(name),
@ -628,12 +622,8 @@ class User extends DataClass implements Insertable<User> {
}
@override
int get hashCode => $mrjf($mrjc(
id.hashCode,
$mrjc(
name.hashCode,
$mrjc(isAwesome.hashCode,
$mrjc(profilePicture.hashCode, creationTime.hashCode)))));
int get hashCode =>
Object.hash(id, name, isAwesome, profilePicture, creationTime);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -854,7 +844,7 @@ class SharedTodo extends DataClass implements Insertable<SharedTodo> {
factory SharedTodo.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return SharedTodo(
todo: serializer.fromJson<int>(json['todo']),
user: serializer.fromJson<int>(json['user']),
@ -867,7 +857,7 @@ class SharedTodo extends DataClass implements Insertable<SharedTodo> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'todo': serializer.toJson<int>(todo),
'user': serializer.toJson<int>(user),
@ -888,7 +878,7 @@ class SharedTodo extends DataClass implements Insertable<SharedTodo> {
}
@override
int get hashCode => $mrjf($mrjc(todo.hashCode, user.hashCode));
int get hashCode => Object.hash(todo, user);
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -1168,7 +1158,7 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
factory PureDefault.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return PureDefault(
txt: serializer.fromJson<String?>(json['txt']),
);
@ -1180,7 +1170,7 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'txt': serializer.toJson<String?>(txt),
};
@ -1199,7 +1189,7 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
}
@override
int get hashCode => $mrjf(txt.hashCode);
int get hashCode => txt.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) || (other is PureDefault && other.txt == this.txt);
@ -1395,16 +1385,8 @@ class AllTodosWithCategoryResult extends CustomResultSet {
required this.catDesc,
}) : super(row);
@override
int get hashCode => $mrjf($mrjc(
id.hashCode,
$mrjc(
title.hashCode,
$mrjc(
content.hashCode,
$mrjc(
targetDate.hashCode,
$mrjc(category.hashCode,
$mrjc(catId.hashCode, catDesc.hashCode)))))));
int get hashCode =>
Object.hash(id, title, content, targetDate, category, catId, catDesc);
@override
bool operator ==(Object other) =>
identical(this, other) ||

View File

@ -17,9 +17,9 @@ builders:
build_extensions: {".moor": [".temp.dart", ".dart_in_moor"]}
build_to: cache
auto_apply: dependents
applies_builders: ["moor_generator|moor_cleanup"]
applies_builders: ["drift_dev|cleanup"]
moor_generator:
drift_dev:
import: "package:drift_dev/integrations/build.dart"
builder_factories: ["moorBuilder"]
build_extensions: {".dart": [".moor.g.part"]}
@ -37,6 +37,6 @@ builders:
required_inputs: [".dart_in_moor"]
post_process_builders:
moor_cleanup:
cleanup:
import: "package:drift_dev/integrations/build.dart"
builder_factory: "moorCleanup"

View File

@ -2,10 +2,11 @@ import 'package:build/build.dart';
import 'package:drift_dev/src/backends/build/moor_builder.dart';
import 'package:drift_dev/src/backends/build/preprocess_builder.dart';
Builder moorBuilder(BuilderOptions options) => MoorSharedPartBuilder(options);
Builder moorBuilder(BuilderOptions options) =>
MoorSharedPartBuilder(options, isForNewDriftPackage: true);
Builder moorBuilderNotShared(BuilderOptions options) =>
MoorPartBuilder(options);
MoorPartBuilder(options, isForNewDriftPackage: true);
Builder preparingBuilder(BuilderOptions options) => PreprocessBuilder();

View File

@ -17,7 +17,7 @@ class UseMoorParser {
.whereType<DartType>();
if (tablesOrNull == null) {
step.reportError(ErrorInDartCode(
message: 'Could not read tables from @UseMoor annotation! \n'
message: 'Could not read tables from @DriftDatabase annotation! \n'
'Please make sure that all table classes exist.',
affectedElement: element,
));

View File

@ -18,6 +18,7 @@ final _flags = Resource(() => _BuilderFlags());
mixin MoorBuilder on Builder {
MoorOptions get options;
bool get isForNewDriftPackage;
Writer createWriter({bool nnbd = false}) {
return Writer(options, generationOptions: GenerationOptions(nnbd: nnbd));
@ -66,12 +67,18 @@ class MoorSharedPartBuilder extends SharedPartBuilder with MoorBuilder {
@override
final MoorOptions options;
MoorSharedPartBuilder._(List<Generator> generators, String name, this.options)
@override
final bool isForNewDriftPackage;
MoorSharedPartBuilder._(List<Generator> generators, String name, this.options,
this.isForNewDriftPackage)
: super(generators, name);
factory MoorSharedPartBuilder(BuilderOptions options) {
factory MoorSharedPartBuilder(BuilderOptions options,
{bool isForNewDriftPackage = false}) {
return _createBuilder(options, (generators, parsedOptions) {
return MoorSharedPartBuilder._(generators, 'moor', parsedOptions);
return MoorSharedPartBuilder._(
generators, 'moor', parsedOptions, isForNewDriftPackage);
});
}
@ -94,12 +101,18 @@ class MoorPartBuilder extends PartBuilder with MoorBuilder {
@override
final MoorOptions options;
MoorPartBuilder._(List<Generator> generators, String extension, this.options)
@override
final bool isForNewDriftPackage;
MoorPartBuilder._(List<Generator> generators, String extension, this.options,
this.isForNewDriftPackage)
: super(generators, extension);
factory MoorPartBuilder(BuilderOptions options) {
factory MoorPartBuilder(BuilderOptions options,
{bool isForNewDriftPackage = false}) {
return _createBuilder(options, (generators, parsedOptions) {
return MoorPartBuilder._(generators, '.moor.dart', parsedOptions);
return MoorPartBuilder._(
generators, '.moor.dart', parsedOptions, isForNewDriftPackage);
});
}
}

View File

@ -273,6 +273,7 @@ class _Moor2DriftDartRewriter extends GeneralizingAstVisitor<void> {
for (final annotation in element.metadata) {
final value = annotation.computeConstantValue();
if (value == null) return;
final type = value.type;
if (type is! InterfaceType) continue;

View File

@ -17,6 +17,10 @@ class DataClassWriter {
String get serializerType => scope.nullableType('ValueSerializer');
String get _runtimeOptions => scope.generationOptions.writeForMoorPackage
? 'moorRuntimeOptions'
: 'driftRuntimeOptions';
void write() {
_buffer.write('class ${table.dartTypeName} extends DataClass ');
if (isInsertable) {
@ -109,7 +113,7 @@ class DataClassWriter {
..write('factory $dataClassName.fromJson('
'Map<String, dynamic> json, {$serializerType serializer}'
') {\n')
..write('serializer ??= moorRuntimeOptions.defaultSerializer;\n')
..write('serializer ??= $_runtimeOptions.defaultSerializer;\n')
..write('return $dataClassName(');
for (final column in table.columns) {
@ -135,7 +139,7 @@ class DataClassWriter {
void _writeToJson() {
_buffer.write('@override Map<String, dynamic> toJson('
'{$serializerType serializer}) {\n'
'serializer ??= moorRuntimeOptions.defaultSerializer;\n'
'serializer ??= $_runtimeOptions.defaultSerializer;\n'
'return <String, dynamic>{\n');
for (final column in table.columns) {

View File

@ -1,7 +1,6 @@
const _hashCombine = '\$mrjc';
const _hashFinish = '\$mrjf';
class HashCodeWriter {
static const int _maxArgsToObjectHash = 20;
const HashCodeWriter();
/// Writes an expression to calculate a hash code of an object that consists
@ -10,22 +9,20 @@ class HashCodeWriter {
if (fields.isEmpty) {
into.write('identityHashCode(this)');
} else if (fields.length == 1) {
into.write('$_hashFinish(${fields.last}.hashCode)');
into.write('${fields[0]}.hashCode');
} else {
into.write('$_hashFinish(');
_writeInner(fields, into, 0);
into.write(')');
}
}
final needsHashAll = fields.length > _maxArgsToObjectHash;
/// recursively writes a "combine(a, combine(b, c)))" expression
void _writeInner(List<String> fields, StringBuffer into, int index) {
if (index == fields.length - 1) {
into.write('${fields.last}.hashCode');
} else {
into.write('$_hashCombine(${fields[index]}.hashCode, ');
_writeInner(fields, into, index + 1);
into.write(')');
into.write(needsHashAll ? 'Object.hashAll([' : 'Object.hash(');
var first = true;
for (final field in fields) {
if (!first) into.write(', ');
into.write(field);
first = false;
}
into.write(needsHashAll ? '])' : ')');
}
}
}

View File

@ -117,11 +117,16 @@ class GenerationOptions {
/// Whether companions should be generated.
final bool writeCompanions;
/// Whether we're generating code for the old moor package instead of the
/// new `drift` package.
final bool writeForMoorPackage;
const GenerationOptions({
this.forSchema,
this.nnbd = false,
this.writeDataClasses = true,
this.writeCompanions = true,
this.writeForMoorPackage = false,
});
/// Whether, instead of generating the full database code, we're only

View File

@ -31,7 +31,7 @@ dependencies:
# Dart analysis
analyzer: "^2.1.0"
analyzer_plugin: ^0.7.0
analyzer_plugin: ^0.8.0
source_span: ^1.5.5
package_config: ^2.0.0
pub_semver: ^2.0.0
@ -50,6 +50,8 @@ dev_dependencies:
build_runner: ^2.0.0
build_test: ^2.0.0
json_serializable: ^5.0.0
# Used to test the migration from moor to drift
moor: any
executables:
drift_dev:
@ -57,5 +59,7 @@ executables:
dependency_overrides:
drift:
path: ../drift
moor:
path: ../moor
sqlparser:
path: ../sqlparser

View File

@ -15,13 +15,13 @@ void main() {
setUpAll(() {
backend = TestBackend({
AssetId.parse('test_lib|lib/entry.dart'): r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Foos extends Table {
IntColumn get id => integer().autoIncrement()();
}
@UseMoor(include: {'db.moor'}, tables: [Foos])
@DriftDatabase(include: {'db.moor'}, tables: [Foos])
class Database {}
''',
AssetId.parse('test_lib|lib/db.moor'): r'''

View File

@ -13,7 +13,7 @@ void main() {
setUpAll(() {
state = TestState.withContent({
'a|lib/invalid_no_unnamed_constructor.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class RowClass {
RowClass.create();
@ -22,7 +22,7 @@ class RowClass {
class TableClass extends Table {}
''',
'a|lib/invalid_no_named_constructor.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class RowClass {
RowClass();
@ -32,7 +32,7 @@ class RowClass {
class TableClass extends Table {}
''',
'a|lib/mismatching_type.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class RowClass {
RowClass(int x);
@ -43,7 +43,7 @@ class TableClass extends Table {
}
''',
'a|lib/mismatching_nullability.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class RowClass {
RowClass(int x);
@ -54,7 +54,7 @@ class TableClass extends Table {
}
''',
'a|lib/mismatching_type_converter.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class MyConverter extends TypeConverter<int, String> {
const MyConverter();
@ -76,7 +76,7 @@ class TableClass extends Table {
''',
'a|lib/generic.dart': '''
//@dart=2.13
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
typedef StringRow = GenericRow<String>;
typedef IntRow = GenericRow<int>;
@ -98,7 +98,7 @@ class IntTable extends Table {
''',
'a|lib/blob.dart': '''
// @dart=2.13
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
@UseRowClass(Cls)
class Tbl extends Table {

View File

@ -13,7 +13,7 @@ void main() {
setUpAll(() async {
state = TestState.withContent({
'foo|lib/main.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
enum Fruits {
apple, orange, banana

View File

@ -7,7 +7,7 @@ void main() {
test('can define abstract tables', () async {
final test = TestState.withContent({
'a|lib/main.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
abstract class CategoriesTable extends Table {
IntColumn get id => integer()();
@ -35,7 +35,7 @@ class SocietiesCategoriesTable extends CategoriesTable {
String get tableName => 'societies_categories';
}
@UseMoor(tables: [OccurrenceCategoriesTable, SocietiesCategoriesTable])
@DriftDatabase(tables: [OccurrenceCategoriesTable, SocietiesCategoriesTable])
class Database {}
''',
});

View File

@ -20,7 +20,7 @@ void main() {
setUpAll(() {
backend = TestBackend({
AssetId.parse('test_lib|lib/main.dart'): r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class TableWithCustomName extends Table {
@override String get tableName => 'my-fancy-table';
@ -95,13 +95,13 @@ void main() {
}
''',
AssetId.parse('test_lib|lib/invalid_reference.dart'): '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Foo extends Table {
IntColumn get id => integer().autoIncrement()();
}
@UseMoor(tables: [Foo, DoesNotExist])
@DriftDatabase(tables: [Foo, DoesNotExist])
class Database {}
''',
});
@ -310,7 +310,7 @@ void main() {
(e) => e.message,
'message',
allOf(
contains('Could not read tables from @UseMoor annotation!'),
contains('Could not read tables from @DriftDatabase annotation!'),
contains('Please make sure that all table classes exist.'),
),
),

View File

@ -9,14 +9,14 @@ void main() {
test('supports inheritance for daos', () async {
final state = TestState.withContent({
'a|lib/database.dart': r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Products extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
}
@UseMoor(tables: [Products], daos: [ProductsDao])
@DriftDatabase(tables: [Products], daos: [ProductsDao])
class MyDatabase {}
abstract class BaseDao<T extends Table, D >
@ -35,7 +35,7 @@ abstract class BaseProductsDao extends BaseDao<Products, Product> {
BaseProductsDao(MyDatabase db): super(db, db.products);
}
@UseDao(tables: [ProductTable])
@DriftAccessor(tables: [ProductTable])
class ProductsDao extends BaseProductsDao with _$ProductDaoMixin {
ProductsDao(MyDatabase db): super(db);
}

View File

@ -8,18 +8,18 @@ void main() {
test("tables imported in two ways aren't duplicated", () async {
final state = TestState.withContent({
'foo|lib/main.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
import 'table.dart';
@UseMoor(tables: [Users], include: {'file.moor'})
@DriftDatabase(tables: [Users], include: {'file.moor'})
class MyDatabase {}
''',
'foo|lib/file.moor': '''
import 'table.dart';
''',
'foo|lib/table.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();

View File

@ -14,7 +14,7 @@ void main() {
setUpAll(() {
state = TestState.withContent({
'test_lib|lib/database.dart': r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
import 'another.dart'; // so that the resolver picks it up
@ -27,7 +27,7 @@ class UsedLanguages extends Table {
Set<Column> get primaryKey => {language, library};
}
@UseMoor(
@DriftDatabase(
tables: [UsedLanguages],
include: {'package:test_lib/tables.moor'},
queries: {
@ -54,7 +54,7 @@ joinTest: SELECT * FROM reference_test r
INNER JOIN libraries l ON l.id = r.library;
''',
'test_lib|lib/another.dart': r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class ProgrammingLanguages extends Table {
IntColumn get id => integer().autoIncrement()();

View File

@ -68,7 +68,7 @@ void main() {
);
''',
'foo|lib/enum.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
enum Fruits {
apple, orange, banane

View File

@ -9,7 +9,7 @@ void main() {
test('reports an error when importing a part file into .moor', () async {
final state = TestState.withContent({
'a|lib/base.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
part 'tables.dart';
''',

View File

@ -8,7 +8,7 @@ void main() {
test('moor files can import original dart source', () async {
final state = TestState.withContent({
'a|lib/base.dart': r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
part 'base.g.dart';
@ -30,7 +30,7 @@ class Units extends Table {
IntColumn get id => integer().autoIncrement()();
}
@UseMoor(include: {'customizedSQL.moor'})
@DriftDatabase(include: {'customizedSQL.moor'})
class AppDatabase extends _$AppDatabase {
AppDatabase()
: super(FlutterQueryExecutor.inDatabaseFolder(

View File

@ -8,12 +8,12 @@ void main() {
test('analyzes views referencing Dart tables', () async {
final state = TestState.withContent({
'a|lib/db.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
import 'dart:io';
import 'entities/person.dart';
@UseMoor(tables: [Persons], include: {'query.moor'})
@DriftDatabase(tables: [Persons], include: {'query.moor'})
class MyDatabase {
MyDatabase() : super(_openConnection());
@override
@ -33,7 +33,7 @@ SELECT id, name, last_name, '...' AS full_name
FROM persons;
''',
'a|lib/entities/person.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Persons extends Table {
IntColumn get id => integer().autoIncrement()();

View File

@ -8,15 +8,15 @@ void main() {
test('gracefully handles daos with invalid types', () async {
final state = TestState.withContent({
'foo|lib/bar.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Foos extends Table {
IntColumn get id => integer().autoIncrement()();
}
@UseMoor() class Db {}
@DriftDatabase() class Db {}
@UseDao(tables: [Foos, Db])
@DriftAccessor(tables: [Foos, Db])
class Dao extends DatabaseAccessor<Db> {}
''',
});

View File

@ -29,9 +29,9 @@ import 'b.moor';
test('in a dart file', () async {
final state = TestState.withContent({
'foo|lib/a.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
@UseMoor(include: {'b.moor'})
@DriftDatabase(include: {'b.moor'})
class Database {
}

View File

@ -27,9 +27,9 @@ void main() {
'foo|lib/a.dart': '''
// @dart = 2.1
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
@UseMoor(tables: [])
@DriftDatabase(tables: [])
class Database {}
''',
},

View File

@ -24,7 +24,7 @@ CREATE TABLE foo (
);
''',
'foo|converter.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class MyConverter extends TypeConverter<DateTime, int> {
const MyConverter();
@ -72,7 +72,7 @@ CREATE TABLE foo (
import 'converter.dart';
''',
'foo|converter.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class MyConverter extends TypeConverter<DateTime, int> {
const MyConverter();

View File

@ -24,9 +24,9 @@ BEGIN
END;
''',
'foo|lib/main.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
@UseMoor(include: {'a.moor'})
@DriftDatabase(include: {'a.moor'})
class MyDatabase {}
'''
});
@ -80,9 +80,9 @@ CREATE TABLE unaffected_on_update (
);
''',
'foo|lib/main.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
@UseMoor(include: {'a.moor'})
@DriftDatabase(include: {'a.moor'})
class MyDatabase {}
'''
});

View File

@ -43,7 +43,7 @@ END;
CREATE INDEX groups_name ON "groups"(name);
''',
'foo|lib/main.dart': '''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
@ -60,7 +60,7 @@ class SettingsConverter extends TypeConverter<Settings, String> {
Settings mapToDart(String db) => Settings();
}
@UseMoor(include: {'a.moor'}, tables: [Users])
@DriftDatabase(include: {'a.moor'}, tables: [Users])
class Database {}
''',
}, options: const MoorOptions.defaults(modules: [SqlModule.fts5]));

View File

@ -10,7 +10,7 @@ import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';
const _testInput = r'''
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
part 'main.moor.dart';
@ -19,7 +19,7 @@ class Users extends Table {
TextColumn get name => text()();
}
@UseMoor(
@DriftDatabase(
tables: [Users],
queries: {
'someQuery': 'SELECT 1 AS foo, 2 AS bar;',

View File

@ -1,4 +1,6 @@
//@dart=2.9
import 'package:charcode/ascii.dart';
import 'package:drift_dev/src/writer/utils/hash_code.dart';
import 'package:test/test.dart';
@ -12,13 +14,20 @@ void main() {
test('hash code for a single field', () {
final buffer = StringBuffer();
const HashCodeWriter().writeHashCode(['a'], buffer);
expect(buffer.toString(), r'$mrjf(a.hashCode)');
expect(buffer.toString(), r'a.hashCode');
});
test('hash code for multiple fields', () {
final buffer = StringBuffer();
const HashCodeWriter().writeHashCode(['a', 'b', 'c'], buffer);
expect(buffer.toString(), r'Object.hash(a, b, c)');
});
test('hash code for lots of fields', () {
final buffer = StringBuffer();
const HashCodeWriter().writeHashCode(
List.generate(26, (index) => String.fromCharCode($a + index)), buffer);
expect(buffer.toString(),
r'$mrjf($mrjc(a.hashCode, $mrjc(b.hashCode, c.hashCode)))');
r'Object.hashAll([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z])');
});
}

View File

@ -1 +1,16 @@
export 'package:drift_dev/integrations/build.dart';
// ignore_for_file: implementation_imports
import 'package:build/build.dart';
import 'package:drift_dev/src/backends/build/moor_builder.dart';
import 'package:drift_dev/src/backends/build/preprocess_builder.dart';
Builder moorBuilder(BuilderOptions options) =>
MoorSharedPartBuilder(options, isForNewDriftPackage: false);
Builder moorBuilderNotShared(BuilderOptions options) =>
MoorPartBuilder(options, isForNewDriftPackage: false);
Builder preparingBuilder(BuilderOptions options) => PreprocessBuilder();
PostProcessBuilder moorCleanup(BuilderOptions options) {
return const FileDeletingBuilder(['.temp.dart']);
}