summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hobgoblin/src/output/lang/sql/ast.hob6
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java17
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScript.java44
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java46
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java41
5 files changed, 149 insertions, 5 deletions
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<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement>(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<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement>(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<java.lang.Object> {
+ 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript>> submatchers;
+
+ private SqlScriptMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript>> 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.SqlScript.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher withStatements(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement>> statements) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript>>(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
+}