diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-12-05 21:37:21 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-12-05 21:37:21 +0000 |
| commit | 5a646ae3ae92b9756e005b38e40f9fd8476cdc7c (patch) | |
| tree | 5a1edc8c98d8d3e19f04cab0c8e92122a8aba1b4 | |
| parent | c2c7d9d6ecdbd5ab361d90e420e4460c8a68a7dc (diff) | |
Add param refs to SQL AST
6 files changed, 99 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/sql/ast.hob b/hobgoblin/src/output/lang/sql/ast.hob index 399a023..48a2d55 100644 --- a/hobgoblin/src/output/lang/sql/ast.hob +++ b/hobgoblin/src/output/lang/sql/ast.hob @@ -54,6 +54,7 @@ sum SqlExpression { variant SqlBinaryOperation; variant SqlColumnReference; variant SqlIntLiteral; + variant SqlParamRef; variant SqlRef; } @@ -76,6 +77,10 @@ struct SqlIntLiteral { field value: Int64; } +struct SqlParamRef { + field number: Int32; +} + struct SqlRef { field name: SqlIdentifier; } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java index 04c7869..4e0b71b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java @@ -187,6 +187,10 @@ public class SqlWriter { this.writeIntLiteral(intLiteral); } + case SqlParamRef paramRef -> { + this.writeParamRef(paramRef); + } + case SqlRef ref -> { this.writeRef(ref); } @@ -219,6 +223,11 @@ public class SqlWriter { this.writer.write(Long.toString(intLiteral.value())); } + private void writeParamRef(SqlParamRef paramRef) throws IOException { + this.writer.write("?"); + this.writer.write(Integer.toString(paramRef.number())); + } + private void writeRef(SqlRef ref) throws IOException { this.writeIdentifier(ref.name()); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java index 10b4e93..6075ebc 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression imports -public sealed interface SqlExpression permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef { +public sealed interface SqlExpression permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlParamRef.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlParamRef.java new file mode 100644 index 0000000..f2203dd --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlParamRef.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef imports + +public record SqlParamRef( + int number +) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.Builder(0); + } + + public record Builder( + int number + ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef build() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef(number); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.Builder withNumber(int number) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.Builder(number); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java index afbf80a..964f3c1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java @@ -259,6 +259,17 @@ public class SqlWriterTests { } @Test + public void paramRef() throws IOException { + var sql = SqlParamRef.arbitrary() + .withNumber(42) + .build(); + + var string = write(writer -> writer.writeExpression(sql)); + + assertThat(string, equalTo("?42")); + } + + @Test public void ref() throws IOException { var sql = ref("a"); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlParamRefMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlParamRefMatcher.java new file mode 100644 index 0000000..edcddbd --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlParamRefMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher imports + +public final class SqlParamRefMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher isSqlParamRef() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef>> submatchers; + + private SqlParamRefMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef>> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher withNumber(org.zwobble.precisely.Matcher<? super java.lang.Integer> number) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("number", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRef::number, (org.zwobble.precisely.Matcher) number)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlParamRefMatcher body +} |
