Optionally disable verification code (#173)

This commit is contained in:
Simon Binder 2019-10-05 21:41:20 +02:00
parent ed03bff4c2
commit 718af051ff
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 39 additions and 11 deletions

View File

@ -34,4 +34,8 @@ At the moment, moor supports these options:
methods returning a `Stream` or a `Future`. As the `Selectable` class contains its own methods
to convert it to a `Stream` and `Future`, the two later methods only exist for backwards
compatibility. When this flag is enabled, moor won't write them at all. This will be the only
option in moor 3.0
option in moor 3.0
* `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.

View File

@ -34,11 +34,19 @@ class VerificationResult {
/// Used internally by moor for integrity checks.
class VerificationContext {
final Map<VerificationMeta, VerificationResult> _errors = {};
final Map<VerificationMeta, VerificationResult> _errors;
/// Used internally by moor
bool get dataValid => _errors.isEmpty;
/// Creates a verification context, which stores the individual integrity
/// check results. Used by generated code.
VerificationContext() : _errors = {};
/// Constructs a verification context that can't be used to report error. This
/// is used internally by moor if integrity checks have been disabled.
const VerificationContext.notEnabled() : _errors = const {};
/// Used internally by moor when inserting
void handle(VerificationMeta meta, VerificationResult result) {
if (!result.success) {

View File

@ -45,7 +45,11 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> on Table {
/// Validates that the given entity can be inserted into this table, meaning
/// that it respects all constraints (nullability, text length, etc.).
VerificationContext validateIntegrity(covariant UpdateCompanion<D> instance,
{bool isInserting = false});
{bool isInserting = false}) {
// default behavior when users chose to not verify the integrity (build time
// option)
return const VerificationContext.notEnabled();
}
/// Maps the given update companion to a [Map] that can be inserted into sql.
/// The keys should represent the column name in sql, the values the

View File

@ -4,9 +4,13 @@ class MoorOptions {
final bool generateFromJsonStringConstructor;
final bool overrideHashAndEqualsInResultSets;
final bool compactQueryMethods;
final bool skipVerificationCode;
MoorOptions(this.generateFromJsonStringConstructor,
this.overrideHashAndEqualsInResultSets, this.compactQueryMethods);
MoorOptions(
this.generateFromJsonStringConstructor,
this.overrideHashAndEqualsInResultSets,
this.compactQueryMethods,
this.skipVerificationCode);
factory MoorOptions.fromBuilder(Map<String, dynamic> config) {
final writeFromString =
@ -18,12 +22,16 @@ class MoorOptions {
final compactQueryMethods =
config['compact_query_methods'] as bool ?? false;
return MoorOptions(
writeFromString, overrideInResultSets, compactQueryMethods);
final skipVerificationCode =
config['skip_verification_code'] as bool ?? false;
return MoorOptions(writeFromString, overrideInResultSets,
compactQueryMethods, skipVerificationCode);
}
const MoorOptions.defaults()
: generateFromJsonStringConstructor = false,
overrideHashAndEqualsInResultSets = false,
compactQueryMethods = false;
compactQueryMethods = false,
skipVerificationCode = false;
}

View File

@ -192,12 +192,16 @@ class TableWriter {
void _writeColumnVerificationMeta(SpecifiedColumn column) {
// final VerificationMeta _targetDateMeta = const VerificationMeta('targetDate');
_buffer
..write('final VerificationMeta ${_fieldNameForColumnMeta(column)} = ')
..write("const VerificationMeta('${column.dartGetterName}');\n");
if (!scope.writer.options.skipVerificationCode) {
_buffer
..write('final VerificationMeta ${_fieldNameForColumnMeta(column)} = ')
..write("const VerificationMeta('${column.dartGetterName}');\n");
}
}
void _writeValidityCheckMethod() {
if (scope.writer.options.skipVerificationCode) return;
_buffer
..write('@override\nVerificationContext validateIntegrity'
'(${table.updateCompanionName} d, {bool isInserting = false}) {\n')