From 6f392b7a68dc47811928463ee4502c9a222a0657 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 16 Oct 2026 15:19:03 +0100 Subject: Add SQL scripts to SQL AST --- hobgoblin/src/output/lang/sql/ast.hob | 6 +++ .../compiler/output/lang/sql/SqlWriter.java | 17 ++++++++ .../compiler/output/lang/sql/ast/SqlScript.java | 44 +++++++++++++++++++++ .../compiler/output/lang/sql/SqlWriterTests.java | 46 +++++++++++++++++++--- .../output/lang/sql/ast/SqlScriptMatcher.java | 41 +++++++++++++++++++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScript.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java diff --git a/hobgoblin/src/output/lang/sql/ast.hob b/hobgoblin/src/output/lang/sql/ast.hob index f5dad7d..d70e7bc 100644 --- a/hobgoblin/src/output/lang/sql/ast.hob +++ b/hobgoblin/src/output/lang/sql/ast.hob @@ -1,3 +1,9 @@ +// == Scripts == + +struct SqlScript { + field statements: List[SqlStatement]; +} + // == Statements == sum SqlStatement { 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 b7d0caf..b6edc1b 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 @@ -37,6 +37,23 @@ public class SqlWriter { this.writer = writer; } + // == Scripts == + + public void writeScript(SqlScript script) throws IOException { + writer.writeHeader(); + + writeWithSeparator( + script.statements(), + this::writeStatement, + () -> { + writer.newLine(); + writer.newLine(); + } + ); + + writer.newLine(); + } + // == Statements == void writeStatement(SqlStatement statement) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScript.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScript.java new file mode 100644 index 0000000..4b91c2e --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScript.java @@ -0,0 +1,44 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript imports + +public record SqlScript( + java.util.List statements +) { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder(java.util.List.of()); + } + + public record Builder( + java.util.List statements + ) { + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript build() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript(statements); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder withStatements(java.util.List statements) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder(statements); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder addStatement(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement statement) { + var statements = new java.util.ArrayList(this.statements); + statements.add(statement); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder(statements); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder addStatement(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement.Builder statement) { + var statements = new java.util.ArrayList(this.statements); + statements.add(statement.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder(statements); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript 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 2d8c7d3..42b075f 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 @@ -2,10 +2,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.sql; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.output.CodeWriter; -import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnDefinition; -import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatement; -import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier; -import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraint; +import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.*; import java.io.IOException; import java.io.StringWriter; @@ -15,7 +12,46 @@ import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; public class SqlWriterTests { - // == Create table statements == + // == Scripts == + + @Test + public void script() throws IOException { + var sql = SqlScript.arbitrary() + .addStatement(SqlCreateTableStatement.arbitrary() + .withName(id("authors")) + .addColumn(SqlColumnDefinition.arbitrary() + .withName(id("id")) + .withTypeName(id("INT")) + ) + ) + .addStatement(SqlCreateTableStatement.arbitrary() + .withName(id("posts")) + .addColumn(SqlColumnDefinition.arbitrary() + .withName(id("name")) + .withTypeName(id("TEXT")) + ) + ) + .build(); + + var string = write(writer -> writer.writeScript(sql)); + + assertThat(string, equalTo(""" + -- Generated by hobgoblin. + + CREATE TABLE authors ( + id INT + ); + + CREATE TABLE posts ( + name TEXT + ); + """ + )); + } + + // == Statements == + + // === Create table statements === @Test public void createTableStatement() throws IOException { diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java new file mode 100644 index 0000000..6ce14b7 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.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.SqlScriptMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher imports + +public final class SqlScriptMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher isSqlScript() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private SqlScriptMatcher( + java.util.List> 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 toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher withStatements(org.zwobble.precisely.Matcher> statements) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("statements", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript::statements, (org.zwobble.precisely.Matcher) statements)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher body +} -- cgit v1.2.3