diff options
9 files changed, 125 insertions, 4 deletions
diff --git a/hobgoblin/src/output/lang/sql/ast.hob b/hobgoblin/src/output/lang/sql/ast.hob index c10699b..219edae 100644 --- a/hobgoblin/src/output/lang/sql/ast.hob +++ b/hobgoblin/src/output/lang/sql/ast.hob @@ -51,6 +51,7 @@ struct SqlPrimaryKeyConstraint { sum SqlExpression { variant SqlBinaryOperation; + variant SqlColumnReference; variant SqlIntLiteral; variant SqlRef; } @@ -61,6 +62,11 @@ struct SqlBinaryOperation { field right: SqlExpression; } +struct SqlColumnReference { + field relationName: SqlIdentifier; + field columnName: SqlIdentifier; +} + enum SqlBinaryOperator { variant equals; } 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 bc806c6..6fb2813 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 @@ -173,6 +173,10 @@ public class SqlWriter { this.writeBinaryOperation(binaryOperation); } + case SqlColumnReference columnReference -> { + this.writeColumnReference(columnReference); + } + case SqlIntLiteral intLiteral -> { this.writeIntLiteral(intLiteral); } @@ -193,6 +197,12 @@ public class SqlWriter { this.writeExpression(binaryOperation.right()); } + private void writeColumnReference(SqlColumnReference columnReference) throws IOException { + this.writeIdentifier(columnReference.relationName()); + this.writer.write("."); + this.writeIdentifier(columnReference.columnName()); + } + private void writeBinaryOperator(SqlBinaryOperator operator) throws IOException { this.writer.write(switch (operator) { case EQUALS -> "=="; diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java index 8bdde89..7b5daad 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java @@ -11,7 +11,7 @@ public record SqlBinaryOperation( org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression right ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression { public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator.EQUALS, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator.EQUALS, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.arbitrary().build()); } public record Builder( diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlColumnReference.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlColumnReference.java new file mode 100644 index 0000000..1b8fdbe --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlColumnReference.java @@ -0,0 +1,46 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference imports + +public record SqlColumnReference( + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier relationName, + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier columnName +) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier relationName, + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier columnName + ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference build() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference(relationName, columnName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder withRelationName(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier relationName) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder(relationName, columnName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder withRelationName(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.Builder relationName) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder(relationName.build(), columnName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder withColumnName(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier columnName) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder(relationName, columnName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder withColumnName(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.Builder columnName) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder(relationName, columnName.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference body +} 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 5e1387b..10b4e93 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.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.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/SqlOrderingTerm.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java index 80ca2e6..7710d5c 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java @@ -9,7 +9,7 @@ public record SqlOrderingTerm( org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression expression ) { public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTerm.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTerm.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTerm.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.arbitrary().build()); } public record Builder( diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java index 26516b0..f897709 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java @@ -10,7 +10,7 @@ public record SqlResultColumn( org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier alias ) { public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build()); } public record Builder( 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 4f9771a..2a96c4b 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 @@ -215,6 +215,18 @@ public class SqlWriterTests { } @Test + public void columnReference() throws IOException { + var sql = SqlColumnReference.arbitrary() + .withRelationName(id("authors")) + .withColumnName(id("name")) + .build(); + + var string = write(writer -> writer.writeExpression(sql)); + + assertThat(string, equalTo("authors.name")); + } + + @Test public void intLiteral() throws IOException { var sql = SqlIntLiteral.arbitrary() .withValue(42) diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlColumnReferenceMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlColumnReferenceMatcher.java new file mode 100644 index 0000000..35916c0 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlColumnReferenceMatcher.java @@ -0,0 +1,47 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher imports + +public final class SqlColumnReferenceMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher isSqlColumnReference() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference>> submatchers; + + private SqlColumnReferenceMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference>> 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.SqlColumnReference.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher withRelationName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier> relationName) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("relationName", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference::relationName, (org.zwobble.precisely.Matcher) relationName)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher withColumnName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier> columnName) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("columnName", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReference::columnName, (org.zwobble.precisely.Matcher) columnName)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnReferenceMatcher body +} |
