diff --git a/docs/content/en/docs/Advanced Features/builder_options.md b/docs/content/en/docs/Advanced Features/builder_options.md index f9550854..5a8bc1f1 100644 --- a/docs/content/en/docs/Advanced Features/builder_options.md +++ b/docs/content/en/docs/Advanced Features/builder_options.md @@ -6,7 +6,7 @@ description: >- The `moor_generator` package has some options that control how the code is generated. Note that, in most cases, the default settings -should be sufficient. +should be sufficient. See the section on recommended settings below. To use the options, create a `build.yaml` file in the root of your project (e.g. next to your `pubspec.yaml`): @@ -38,4 +38,17 @@ At the moment, moor supports these options: * `skip_verification_code`: Generated tables contain a significant chunk of code to verify integrity of inserted data and report detailed errors when the integrity is violated. If you're only using inserts with SQL, or don't need this functionality, enabling this flag can help to reduce the amount - generated code. \ No newline at end of file + generated code. +* `use_data_class_name_for_companions`: By default, the name for [companion classes]({{< relref "../Getting started/writing_queries.md#updates-and-deletes" >}}) + is based on the table name (e.g. a `@DataClassName('Users') class UsersTable extends Table` would generate + a `UsersTableCompanion`). With this option, the name is based on the data class (so `UsersCompanion` in + this case). + +## Recommended options + +In general, we recommend not enabling these options unless you need to. There are two exceptions though: + +- `compact_query_methods`: We recommend enabling this flag because it generates less code and it will + be the only option in the next breaking upgrade. +- `skip_verification_code`: You can remove a significant portion of generated code with this option. The + downside is that error messages when inserting invalid data will be less specific. \ No newline at end of file diff --git a/moor_generator/CHANGELOG.md b/moor_generator/CHANGELOG.md index e87a68bb..7a028e37 100644 --- a/moor_generator/CHANGELOG.md +++ b/moor_generator/CHANGELOG.md @@ -2,6 +2,8 @@ - Accept inheritance in table definitions (e.g. if an abstract class declared as `IntColumn get foo => integer()()`, tables inheriting from that class will also have a `foo` column) +- New `use_data_class_name_for_companions` option that will make the name of the companion + based on the data class name (uses table name by default). ## 2.0.1 diff --git a/moor_generator/lib/src/backends/build/options.dart b/moor_generator/lib/src/backends/build/options.dart index 2b3976c4..6a596599 100644 --- a/moor_generator/lib/src/backends/build/options.dart +++ b/moor_generator/lib/src/backends/build/options.dart @@ -5,12 +5,14 @@ class MoorOptions { final bool overrideHashAndEqualsInResultSets; final bool compactQueryMethods; final bool skipVerificationCode; + final bool useDataClassNameForCompanions; - MoorOptions( - this.generateFromJsonStringConstructor, - this.overrideHashAndEqualsInResultSets, - this.compactQueryMethods, - this.skipVerificationCode); + const MoorOptions( + {this.generateFromJsonStringConstructor = false, + this.overrideHashAndEqualsInResultSets = false, + this.compactQueryMethods = false, + this.skipVerificationCode = false, + this.useDataClassNameForCompanions = false}); factory MoorOptions.fromBuilder(Map config) { final writeFromString = @@ -25,13 +27,15 @@ class MoorOptions { final skipVerificationCode = config['skip_verification_code'] as bool ?? false; - return MoorOptions(writeFromString, overrideInResultSets, - compactQueryMethods, skipVerificationCode); - } + final dataClassNamesForCompanions = + config['use_data_class_name_for_companions'] as bool ?? false; - const MoorOptions.defaults() - : generateFromJsonStringConstructor = false, - overrideHashAndEqualsInResultSets = false, - compactQueryMethods = false, - skipVerificationCode = false; + return MoorOptions( + generateFromJsonStringConstructor: writeFromString, + overrideHashAndEqualsInResultSets: overrideInResultSets, + compactQueryMethods: compactQueryMethods, + skipVerificationCode: skipVerificationCode, + useDataClassNameForCompanions: dataClassNamesForCompanions, + ); + } } diff --git a/moor_generator/lib/src/model/specified_table.dart b/moor_generator/lib/src/model/specified_table.dart index 0aad3099..5baa55d7 100644 --- a/moor_generator/lib/src/model/specified_table.dart +++ b/moor_generator/lib/src/model/specified_table.dart @@ -1,4 +1,5 @@ import 'package:moor_generator/src/analyzer/sql_queries/meta/declarations.dart'; +import 'package:moor_generator/src/backends/build/moor_builder.dart'; import 'package:moor_generator/src/model/specified_column.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:moor_generator/src/model/used_type_converter.dart'; @@ -48,7 +49,11 @@ class SpecifiedTable { return name; } - String get updateCompanionName => _updateCompanionName(_baseName); + String getNameForCompanionClass(MoorOptions options) { + final baseName = + options.useDataClassNameForCompanions ? dartTypeName : _baseName; + return '${baseName}Companion'; + } /// The set of primary keys, if they have been explicitly defined by /// overriding `primaryKey` in the table class. `null` if the primary key has @@ -104,5 +109,3 @@ class SpecifiedTable { String _dbFieldName(String className) => ReCase(className).camelCase; String tableInfoNameForTableClass(String className) => '\$${className}Table'; - -String _updateCompanionName(String className) => '${className}Companion'; diff --git a/moor_generator/lib/src/writer/tables/data_class_writer.dart b/moor_generator/lib/src/writer/tables/data_class_writer.dart index 2790e4e5..e35b76b7 100644 --- a/moor_generator/lib/src/writer/tables/data_class_writer.dart +++ b/moor_generator/lib/src/writer/tables/data_class_writer.dart @@ -219,7 +219,7 @@ class DataClassWriter { void _writeCompanionOverride() { // TableCompanion createCompanion(bool nullToAbsent) - final companionClass = table.updateCompanionName; + final companionClass = table.getNameForCompanionClass(scope.options); _buffer.write('@override\n' '$companionClass createCompanion(bool nullToAbsent) {\n' 'return $companionClass('); diff --git a/moor_generator/lib/src/writer/tables/table_writer.dart b/moor_generator/lib/src/writer/tables/table_writer.dart index afc5577d..3c10165a 100644 --- a/moor_generator/lib/src/writer/tables/table_writer.dart +++ b/moor_generator/lib/src/writer/tables/table_writer.dart @@ -98,7 +98,7 @@ class TableWriter { // Map entityToSql(covariant UpdateCompanion instance) _buffer ..write('@override\nMap entityToSql(' - '${table.updateCompanionName} d) {\n') + '${table.getNameForCompanionClass(scope.options)} d) {\n') ..write('final map = {};'); for (var column in table.columns) { @@ -204,7 +204,8 @@ class TableWriter { _buffer ..write('@override\nVerificationContext validateIntegrity' - '(${table.updateCompanionName} d, {bool isInserting = false}) {\n') + '(${table.getNameForCompanionClass(scope.options)} d, ' + '{bool isInserting = false}) {\n') ..write('final context = VerificationContext();\n'); for (var column in table.columns) { diff --git a/moor_generator/lib/src/writer/tables/update_companion_writer.dart b/moor_generator/lib/src/writer/tables/update_companion_writer.dart index 7b81b69b..c3a3d1db 100644 --- a/moor_generator/lib/src/writer/tables/update_companion_writer.dart +++ b/moor_generator/lib/src/writer/tables/update_companion_writer.dart @@ -13,7 +13,7 @@ class UpdateCompanionWriter { } void write() { - _buffer.write('class ${table.updateCompanionName} ' + _buffer.write('class ${table.getNameForCompanionClass(scope.options)} ' 'extends UpdateCompanion<${table.dartTypeName}> {\n'); _writeFields(); _writeConstructor(); @@ -31,7 +31,7 @@ class UpdateCompanionWriter { } void _writeConstructor() { - _buffer.write('const ${table.updateCompanionName}({'); + _buffer.write('const ${table.getNameForCompanionClass(scope.options)}({'); for (var column in table.columns) { _buffer.write('this.${column.dartGetterName} = const Value.absent(),'); @@ -48,7 +48,7 @@ class UpdateCompanionWriter { // can't be constant because we use initializers (this.a = Value(a)). // for a parameter a which is only potentially constant. - _buffer.write('${table.updateCompanionName}.insert({'); + _buffer.write('${table.getNameForCompanionClass(scope.options)}.insert({'); // Say we had two required columns a and c, and an optional column b. // .insert({ @@ -87,7 +87,9 @@ class UpdateCompanionWriter { } void _writeCopyWith() { - _buffer.write('${table.updateCompanionName} copyWith({'); + _buffer + ..write(table.getNameForCompanionClass(scope.options)) + ..write(' copyWith({'); var first = true; for (var column in table.columns) { if (!first) { @@ -99,7 +101,7 @@ class UpdateCompanionWriter { _buffer ..write('}) {\n') // - ..write('return ${table.updateCompanionName}('); + ..write('return ${table.getNameForCompanionClass(scope.options)}('); for (var column in table.columns) { final name = column.dartGetterName; _buffer.write('$name: $name ?? this.$name,'); diff --git a/moor_generator/lib/src/writer/writer.dart b/moor_generator/lib/src/writer/writer.dart index 54773ff2..85fcda5a 100644 --- a/moor_generator/lib/src/writer/writer.dart +++ b/moor_generator/lib/src/writer/writer.dart @@ -50,6 +50,8 @@ class Scope extends _Node { final DartScope scope; final Writer writer; + MoorOptions get options => writer.options; + Scope({@required Scope parent, Writer writer}) : scope = parent?.scope?.nextLevel ?? DartScope.library, writer = writer ?? parent?.writer, diff --git a/moor_generator/test/model/specified_table_test.dart b/moor_generator/test/model/specified_table_test.dart new file mode 100644 index 00000000..00f1befe --- /dev/null +++ b/moor_generator/test/model/specified_table_test.dart @@ -0,0 +1,19 @@ +import 'package:moor_generator/src/backends/build/moor_builder.dart'; +import 'package:moor_generator/src/model/specified_table.dart'; +import 'package:test/test.dart'; + +void main() { + test('companion names', () { + final table = + SpecifiedTable(overriddenName: 'GoogleUser', dartTypeName: 'User'); + + expect(table.getNameForCompanionClass(const MoorOptions()), + 'GoogleUserCompanion'); + + expect( + table.getNameForCompanionClass( + const MoorOptions(useDataClassNameForCompanions: true)), + 'UserCompanion', + ); + }); +}