Fix generation for placeholders before arrays

This commit is contained in:
Simon Binder 2021-04-03 22:23:37 +02:00
parent 9b6b5d1b69
commit 133b7c652f
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 29 additions and 9 deletions

View File

@ -1618,13 +1618,17 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
readsFrom: {config}).map(config.mapFromRow);
}
Selectable<String> typeConverterVar(SyncType? var1, List<SyncType?> var2) {
Selectable<String> typeConverterVar(SyncType? var1, List<SyncType?> var2,
{Expression<bool> pred = const CustomExpression('(TRUE)')}) {
var $arrayStartIndex = 2;
final generatedpred = $write(pred);
$arrayStartIndex += generatedpred.amountOfVariables;
final expandedvar2 = $expandVar($arrayStartIndex, var2.length);
$arrayStartIndex += var2.length;
return customSelect(
'SELECT config_key FROM config WHERE sync_state = ? OR sync_state_implicit IN ($expandedvar2)',
'SELECT config_key FROM config WHERE ${generatedpred.sql} AND(sync_state = ? OR sync_state_implicit IN ($expandedvar2))',
variables: [
...generatedpred.introducedVariables,
Variable<int?>(ConfigTable.$converter0.mapToSql(var1)),
for (var $ in var2)
Variable<int?>(ConfigTable.$converter1.mapToSql($))

View File

@ -57,7 +57,8 @@ FROM config WHERE config_key = ?;
readMultiple: SELECT * FROM config WHERE config_key IN ? ORDER BY $clause;
readDynamic ($predicate = TRUE): SELECT * FROM config WHERE $predicate;
typeConverterVar: SELECT config_key FROM config WHERE sync_state = ? OR sync_state_implicit IN ?;
typeConverterVar($pred = TRUE): SELECT config_key FROM config WHERE $pred AND
(sync_state = ? OR sync_state_implicit IN ?);
tableValued AS JsonResult:
SELECT "key", "value"

View File

@ -227,6 +227,9 @@ void main() {
await db.typeConverterVar(SyncType.locallyCreated,
[SyncType.locallyUpdated, SyncType.synchronized]).get();
verify(mock.runSelect(any, [0, 1, 2]));
verify(mock.runSelect(
'SELECT config_key FROM config WHERE (TRUE) AND(sync_state = ? '
'OR sync_state_implicit IN (?2, ?3))',
[0, 1, 2]));
});
}

View File

@ -1,3 +1,7 @@
## 4.2.2
- Fix generated code for queries using a Dart placeholder before an array variable
## 4.2.1
- Fix generated code for nullable columns in moor files

View File

@ -349,9 +349,21 @@ class QueryWriter {
void _writeExpandedDeclarations() {
var indexCounterWasDeclared = false;
final needsIndexCounter = query.variables.any((v) => v.isArray);
var needsIndexCounter = false;
var highestIndexBeforeArray = 0;
for (final variable in query.variables) {
// Variables use an explicit index, we need to know the start index at
// runtime (can be dynamic when placeholders or other arrays appear before
// this one)
if (variable.isArray) {
needsIndexCounter = true;
break;
}
highestIndexBeforeArray = max(highestIndexBeforeArray, variable.index);
}
void _writeIndexCounterIfNeeded() {
if (indexCounterWasDeclared || !needsIndexCounter) {
return; // already written or not necessary at all
@ -393,10 +405,6 @@ class QueryWriter {
// increase highest index for the next expanded element
_increaseIndexCounter('${element.dartParameterName}.length');
}
if (!indexCounterWasDeclared) {
highestIndexBeforeArray = max(highestIndexBeforeArray, element.index);
}
} else if (element is FoundDartPlaceholder) {
_writeIndexCounterIfNeeded();