From e2f02b6a09548e74304cbc6e32b6ce720d6dba84 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 16 Oct 2026 09:11:52 +0100 Subject: Add primary key constraint to SQL AST --- .../compiler/output/lang/sql/SqlWriterTests.java | 58 ++++++++++++++++++++++ .../sql/ast/SqlCreateTableStatementMatcher.java | 6 +++ .../sql/ast/SqlPrimaryKeyConstraintMatcher.java | 41 +++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlPrimaryKeyConstraintMatcher.java (limited to 'src/test/java') 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 5edf06c..2d8c7d3 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 @@ -5,6 +5,7 @@ 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 java.io.IOException; import java.io.StringWriter; @@ -14,6 +15,8 @@ import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; public class SqlWriterTests { + // == Create table statements == + @Test public void createTableStatement() throws IOException { var sql = SqlCreateTableStatement.arbitrary() @@ -38,6 +41,61 @@ public class SqlWriterTests { )); } + @Test + public void createTableStatementWithTableConstraint() throws IOException { + var sql = SqlCreateTableStatement.arbitrary() + .withName(id("authors")) + .addColumn(SqlColumnDefinition.arbitrary() + .withName(id("id")) + .withTypeName(id("INT")) + ) + .addColumn(SqlColumnDefinition.arbitrary() + .withName(id("name")) + .withTypeName(id("TEXT")) + ) + .addTableConstraint(SqlPrimaryKeyConstraint.arbitrary() + .addColumn(id("id")) + ) + .build(); + + var string = write(writer -> writer.writeStatement(sql)); + + assertThat(string, equalTo(""" + CREATE TABLE authors ( + id INT, + name TEXT, + PRIMARY KEY (id) + );""" + )); + } + + // == Table constraints == + + @Test + public void primaryKeyConstraintWithSingleColumn() throws IOException { + var sql = SqlPrimaryKeyConstraint.arbitrary() + .addColumn(id("id")) + .build(); + + var string = write(writer -> writer.writeTableConstraint(sql)); + + assertThat(string, equalTo("PRIMARY KEY (id)")); + } + + @Test + public void primaryKeyConstraintWithMultipleColumns() throws IOException { + var sql = SqlPrimaryKeyConstraint.arbitrary() + .addColumn(id("a")) + .addColumn(id("b")) + .build(); + + var string = write(writer -> writer.writeTableConstraint(sql)); + + assertThat(string, equalTo("PRIMARY KEY (a, b)")); + } + + // == Test utilities == + private String write(SqlWriter.Write write) throws IOException { return write(write, Optional.empty()); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlCreateTableStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlCreateTableStatementMatcher.java index d6c8d57..d5024ef 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlCreateTableStatementMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlCreateTableStatementMatcher.java @@ -42,6 +42,12 @@ public final class SqlCreateTableStatementMatcher implements org.zwobble.precise return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatementMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatementMatcher withTableConstraints(org.zwobble.precisely.Matcher> tableConstraints) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("tableConstraints", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatement::tableConstraints, (org.zwobble.precisely.Matcher) tableConstraints)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatementMatcher(submatchers); + } + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatementMatcher body // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatementMatcher body } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlPrimaryKeyConstraintMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlPrimaryKeyConstraintMatcher.java new file mode 100644 index 0000000..500896b --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlPrimaryKeyConstraintMatcher.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.SqlPrimaryKeyConstraintMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher imports + +public final class SqlPrimaryKeyConstraintMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher isSqlPrimaryKeyConstraint() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private SqlPrimaryKeyConstraintMatcher( + 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.SqlPrimaryKeyConstraint.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher withColumns(org.zwobble.precisely.Matcher> columns) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("columns", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraint::columns, (org.zwobble.precisely.Matcher) columns)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraintMatcher body +} -- cgit v1.2.3