diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-12-05 21:46:47 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-12-05 21:46:47 +0000 |
| commit | 959506c587362d53bdcc0a102966d01d9b41d296 (patch) | |
| tree | 4e104e23d02cbc6c82a2bcfbe746967fc1361a31 | |
| parent | d536d0535af4a3b88ec356a6d1b561d6942334f2 (diff) | |
Generate SQL for queries with paramsmain
4 files changed, 64 insertions, 15 deletions
diff --git a/examples/18-database/output/sqlite3/scripts/data/authorsByName.sql b/examples/18-database/output/sqlite3/scripts/data/authorsByName.sql new file mode 100644 index 0000000..e621470 --- /dev/null +++ b/examples/18-database/output/sqlite3/scripts/data/authorsByName.sql @@ -0,0 +1,8 @@ +-- Generated by hobgoblin. + +SELECT + id AS id, + name AS name +FROM authors +WHERE authors.name == ?1 +ORDER BY id; diff --git a/examples/18-database/output/sqlite3/test.py b/examples/18-database/output/sqlite3/test.py index c985de2..aea7ab3 100644 --- a/examples/18-database/output/sqlite3/test.py +++ b/examples/18-database/output/sqlite3/test.py @@ -57,6 +57,24 @@ class DataTestCase(unittest.TestCase): self.assertEqual(rows, [(2, "Bob")]) + def test_query_authors_by_name(self): + create_script = self._read_script("Data") + + connection = sqlite3.connect(":memory:", autocommit=False) + cursor = connection.cursor() + cursor.executescript(create_script) + connection.commit() + + cursor.execute("INSERT INTO authors VALUES (1, 'Alice')") + cursor.execute("INSERT INTO authors VALUES (2, 'Bob')") + cursor.execute("INSERT INTO authors VALUES (3, 'Carol')") + + select_query = self._read_script("authorsByName") + cursor.execute(select_query, ("Bob",)) + rows = cursor.fetchall() + + self.assertEqual(rows, [(2, "Bob")]) + def _read_script(self, name): sql_script_path = os.path.join(os.path.dirname(__file__), "scripts/data", name + ".sql") with open(sql_script_path, "rt", encoding="utf-8") as sql_script_file: diff --git a/examples/18-database/src/data.hob b/examples/18-database/src/data.hob index 9c813c3..b68aaae 100644 --- a/examples/18-database/src/data.hob +++ b/examples/18-database/src/data.hob @@ -18,7 +18,7 @@ query authorsByFixedId() { return Data.authors.where($(author) => author.id == 2); } -//query authorsByName(name: String) { -// return Data.authors -// .where($(author) => author.name == name); -//} +query authorsByName(name: String) { + return Data.authors + .where($(author) => author.name == name); +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java index 5aa8dc0..5f84508 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java @@ -11,6 +11,7 @@ import org.zwobble.hobgoblin.compiler.types.Type; import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; import org.zwobble.hobgoblin.compiler.util.Casing; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -59,10 +60,16 @@ public class SqlGenerator { TypedQueryDefinitionNode queryDefinition, TypesInfo typesInfo ) { + var variables = new HashMap<String, SqlReferent>(); + for (var paramIndex = 0; paramIndex < queryDefinition.params().size(); paramIndex++) { + var param = queryDefinition.params().get(paramIndex); + variables.put(param.name(), new SqlReferent.Param(paramIndex + 1)); + } + for (var statement : queryDefinition.statements()) { switch (statement) { case TypedReturnNode return_ -> { - return generateSelectFromExpression(return_.expression(), typesInfo); + return generateSelectFromExpression(return_.expression(), variables, typesInfo); } } } @@ -72,6 +79,7 @@ public class SqlGenerator { private SqlSelectStatement generateSelectFromExpression( TypedExpressionNode expression, + Map<String, SqlReferent> variables, TypesInfo typesInfo ) { if (expression.type() instanceof TypeLevelValueType typeLevelValueType && typeLevelValueType.value() instanceof Table table) { @@ -84,7 +92,7 @@ public class SqlGenerator { } case TypedCallNode call -> { - yield generateSelectFromCall(call, typesInfo); + yield generateSelectFromCall(call, variables, typesInfo); } case TypedFunctionExpressionNode _ -> { @@ -105,12 +113,20 @@ public class SqlGenerator { }; } - private SqlSelectStatement generateSelectFromCall(TypedCallNode call, TypesInfo typesInfo) { + private SqlSelectStatement generateSelectFromCall( + TypedCallNode call, + Map<String, SqlReferent> variables, + TypesInfo typesInfo + ) { if (!(call.receiver() instanceof TypedMemberAccessNode memberAccess)) { throw new UnsupportedOperationException("TODO"); } - var baseSelect = generateSelectFromExpression(memberAccess.receiver(), typesInfo); + var baseSelect = generateSelectFromExpression( + memberAccess.receiver(), + variables, + typesInfo + ); if (memberAccess.memberName().equals("where")) { // TODO: handle existing WHERE @@ -120,17 +136,18 @@ public class SqlGenerator { throw new UnsupportedOperationException("TODO"); } + var newVariables = new HashMap<>(variables); + newVariables.put( + functionExpression.params().getFirst().name(), + new SqlReferent.Relation(baseSelect.from()) + ); + return new SqlSelectStatement( baseSelect.columns(), baseSelect.from(), Optional.of(generateExpressionFromExpression( functionExpression.body(), - Map.ofEntries( - Map.entry( - functionExpression.params().getFirst().name(), - new SqlReferent.Relation(baseSelect.from()) - ) - ), + newVariables, typesInfo )), baseSelect.orderBy() @@ -254,6 +271,13 @@ public class SqlGenerator { } private sealed interface SqlReferent { + record Param(int number) implements SqlReferent { + @Override + public SqlExpression toExpression() { + return new SqlParamRef(number); + } + } + record Relation(SqlIdentifier name) implements SqlReferent { @Override public SqlExpression toExpression() { @@ -262,6 +286,5 @@ public class SqlGenerator { } SqlExpression toExpression(); - } } |
