summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-11-01 22:08:45 +0000
committerMichael Williamson <mike@zwobble.org>2026-11-01 22:08:45 +0000
commit66855f114b4aff76634bf77ac6336d7d4b4fb2f3 (patch)
tree6951c07c7fb93bfab294d8c4f7359e19be301139 /src/main/java
parent16ff9ae1529ea492354b5144d3ddd41b695e3999 (diff)
Add simple SQL SELECTs to AST
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java43
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java18
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlRef.java36
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java46
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlSelectStatement.java54
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlStatement.java2
6 files changed, 198 insertions, 1 deletions
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 ef1562e..890fbba 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
@@ -68,6 +68,10 @@ public class SqlWriter {
case SqlCreateTableStatement createTableStatement -> {
writeCreateTableStatement(createTableStatement);
}
+
+ case SqlSelectStatement selectStatement -> {
+ writeSelectStatement(selectStatement);
+ }
}
}
@@ -104,6 +108,31 @@ public class SqlWriter {
this.writeIdentifier(column.typeName());
}
+ private void writeSelectStatement(SqlSelectStatement selectStatement) throws IOException {
+ this.writer.write("SELECT");
+ this.writer.newLine();
+ this.writer.indent();
+
+ writeWithSeparator(
+ selectStatement.columns(),
+ column -> {
+ this.writeExpression(column.expression());
+ this.writer.write(" AS ");
+ this.writeIdentifier(column.alias());
+ },
+ () -> {
+ this.writer.write(",");
+ this.writer.newLine();
+ }
+ );
+
+ this.writer.newLine();
+ this.writer.dedent();
+ this.writer.write("FROM ");
+ this.writeIdentifier(selectStatement.from());
+ this.writer.write(";");
+ }
+
// == Table constraints ==
void writeTableConstraint(SqlTableConstraint tableConstraint) throws IOException {
@@ -124,6 +153,20 @@ public class SqlWriter {
this.writer.write(")");
}
+ // == Expressions ==
+
+ void writeExpression(SqlExpression expression) throws IOException {
+ switch (expression) {
+ case SqlRef ref -> {
+ this.writeRef(ref);
+ }
+ }
+ }
+
+ private void writeRef(SqlRef ref) throws IOException {
+ this.writeIdentifier(ref.name());
+ }
+
// == Identifiers ==
private void writeIdentifier(SqlIdentifier name) throws IOException {
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
new file mode 100644
index 0000000..37f5f93
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java
@@ -0,0 +1,18 @@
+// Generated by hobgoblin.
+
+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.SqlRef {
+ public interface Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression build();
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlRef.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlRef.java
new file mode 100644
index 0000000..156e6ab
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlRef.java
@@ -0,0 +1,36 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.sql.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef imports
+
+public record SqlRef(
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier name
+) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression {
+ public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build());
+ }
+
+ public record Builder(
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier name
+ ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef(name);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder(name);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.Builder name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder(name.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef body
+}
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
new file mode 100644
index 0000000..1db29b1
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.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.SqlResultColumn imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn imports
+
+public record SqlResultColumn(
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression expression,
+ 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.SqlRef.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build());
+ }
+
+ public record Builder(
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression expression,
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier alias
+ ) {
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn(expression, alias);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression expression) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(expression, alias);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder expression) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(expression.build(), alias);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder withAlias(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier alias) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(expression, alias);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder withAlias(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.Builder alias) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(expression, alias.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlSelectStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlSelectStatement.java
new file mode 100644
index 0000000..b5c889b
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlSelectStatement.java
@@ -0,0 +1,54 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.sql.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement imports
+
+public record SqlSelectStatement(
+ java.util.List<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn> columns,
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier from
+) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement {
+ public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build());
+ }
+
+ public record Builder(
+ java.util.List<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn> columns,
+ org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier from
+ ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement(columns, from);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder withColumns(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn> columns) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder(columns, from);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder addColumn(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn column) {
+ var columns = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn>(this.columns);
+ columns.add(column);
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder(columns, from);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder addColumn(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder column) {
+ var columns = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn>(this.columns);
+ columns.add(column.build());
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder(columns, from);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder withFrom(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier from) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder(columns, from);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder withFrom(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.Builder from) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder(columns, from.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlStatement.java
index 0f90dc1..a6d4ab2 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlStatement.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlStatement.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.SqlStatement imports
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement imports
-public sealed interface SqlStatement permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatement {
+public sealed interface SqlStatement permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatement, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatement {
public interface Builder {
public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement build();