From 5380a18450d0ba0822df83318d58b22e3d7d166d Mon Sep 17 00:00:00 2001 From: Daniel Brauner Date: Wed, 6 Apr 2022 20:14:13 +0200 Subject: [PATCH 1/2] Minor bug fixes --- drift_dev/lib/src/analyzer/sql_queries/nested_queries.dart | 7 +++++++ sqlparser/lib/utils/node_to_text.dart | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/drift_dev/lib/src/analyzer/sql_queries/nested_queries.dart b/drift_dev/lib/src/analyzer/sql_queries/nested_queries.dart index aa4cfdab..56062d1b 100644 --- a/drift_dev/lib/src/analyzer/sql_queries/nested_queries.dart +++ b/drift_dev/lib/src/analyzer/sql_queries/nested_queries.dart @@ -83,6 +83,13 @@ class NestedQueryAnalyzer extends RecursiveVisitor<_AnalyzerState, void> { super.visitDriftSpecificNode(e, arg); } + @override + void visitVariable(Variable e, _AnalyzerState arg) { + arg.actualAndAddedVariables.add(e); + + super.visitVariable(e, arg); + } + @override void visitReference(Reference e, _AnalyzerState arg) { final resultEntity = e.resultEntity; diff --git a/sqlparser/lib/utils/node_to_text.dart b/sqlparser/lib/utils/node_to_text.dart index d7d69b5a..bc257f51 100644 --- a/sqlparser/lib/utils/node_to_text.dart +++ b/sqlparser/lib/utils/node_to_text.dart @@ -727,7 +727,6 @@ class NodeSqlBuilder extends AstVisitor { InsertMode.insertOrFail: TokenType.fail, InsertMode.insertOrIgnore: TokenType.ignore, }[mode]!); - visitNullable(e.returning, arg); } _keyword(TokenType.into); From 3c75fbee9d739b61a017d71f252abfb59736b4e5 Mon Sep 17 00:00:00 2001 From: Daniel Brauner Date: Wed, 6 Apr 2022 22:07:28 +0200 Subject: [PATCH 2/2] New test cases --- .../writer/queries/query_writer_test.dart | 37 ++++++++++++++----- sqlparser/test/utils/node_to_text_test.dart | 4 ++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/drift_dev/test/writer/queries/query_writer_test.dart b/drift_dev/test/writer/queries/query_writer_test.dart index ed9c28f1..ca38f901 100644 --- a/drift_dev/test/writer/queries/query_writer_test.dart +++ b/drift_dev/test/writer/queries/query_writer_test.dart @@ -160,7 +160,11 @@ void main() { c TEXT ); - query: SELECT a, LIST(SELECT b, c FROM tbl WHERE a = :a AND b = :b) FROM tbl WHERE a = :a; + query: + SELECT + parent.a, + LIST(SELECT b, c FROM tbl WHERE a = :a OR a = parent.a AND b = :b) + FROM tbl AS parent WHERE parent.a = :a; ''', }); }); @@ -204,17 +208,32 @@ void main() { ); }); - test('with the new query generator', () { + test('should generate correct queries with variables', () { return _runTest( const MoorOptions.defaults(newSqlCodeGeneration: true), [ - contains('SELECT a FROM tbl WHERE a = ?1'), - contains('SELECT b, c FROM tbl WHERE a = ?1 AND b = ?2'), - contains('nestedQuery0: await'), - contains('variables: [Variable(a), Variable(b)]'), - contains('b: row.read(\'b\')'), - contains('c: row.read(\'c\')'), - contains('class QueryNestedQuery0'), + contains( + r'SELECT parent.a, parent.a AS "\$n_0" FROM tbl AS parent WHERE parent.a = ?1', + ), + contains( + r'[Variable(a)]', + ), + contains( + r'SELECT b, c FROM tbl WHERE a = ?1 OR a = ?2 AND b = ?3', + ), + contains( + r"[Variable(a), Variable(row.read('\$n_0')), Variable(b)]", + ), + ], + ); + }); + + test('should generate correct data class', () { + return _runTest( + const MoorOptions.defaults(newSqlCodeGeneration: true), + [ + contains('QueryNestedQuery0({this.b,this.c,})'), + contains('QueryResult({this.a,required this.nestedQuery0,})'), ], ); }); diff --git a/sqlparser/test/utils/node_to_text_test.dart b/sqlparser/test/utils/node_to_text_test.dart index cce7d726..599cb78c 100644 --- a/sqlparser/test/utils/node_to_text_test.dart +++ b/sqlparser/test/utils/node_to_text_test.dart @@ -304,6 +304,10 @@ CREATE UNIQUE INDEX my_idx ON t1 (c1, c2, c3) WHERE c1 < c3; testFormat('INSERT INTO foo DEFAULT VALUES RETURNING *'); }); + test('with returning and insert mode', () { + testFormat('INSERT OR IGNORE INTO foo DEFAULT VALUES RETURNING *'); + }); + test('upsert - do nothing', () { testFormat( 'INSERT OR REPLACE INTO foo DEFAULT VALUES ON CONFLICT DO NOTHING');