Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ name: CI
on: [push, pull_request]

jobs:
test-go:
name: Test Go
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: './go.mod'

- name: Run tests
run: go test ./...

test-java:
needs: [test-go]

name: Test Java
runs-on: ubuntu-latest

Expand Down
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ A WASM plugin for SQLC allowing the generation of Java code.

> [!NOTE]
> Only the `PostgreSQL` engine is supported currently. Support for `MySQL` is planned.

## Configuration Values

| Name | Type | Required | Description |
|--------------------------|---------|----------|------------------------------------------------------------------------------------------------------------------------------------------|
| ------------------------ | ------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `package` | string | yes | The name of the package where the generated files will be located |
| `query_parameter_limit` | integer | no | not yet implemented |
| `indent_char` | string | no | The character to use to indent the code. Defaults to space `" "` |
Expand All @@ -19,29 +19,31 @@ A WASM plugin for SQLC allowing the generation of Java code.
## Usage

`sqlc.yaml`

```yaml
version: '2'
version: "2"
plugins:
- name: java
wasm:
url: TODO
sha256: TODO
- name: java
wasm:
url: TODO
sha256: TODO
sql:
- schema: src/main/resources/postgresql/schema.sql
queries: src/main/resources/postgresql/queries.sql
engine: postgresql
codegen:
- out: src/main/java/com/example/postgresql
plugin: java
options:
package: com.example.postgresql
- schema: src/main/resources/postgresql/schema.sql
queries: src/main/resources/postgresql/queries.sql
engine: postgresql
codegen:
- out: src/main/java/com/example/postgresql
plugin: java
options:
package: com.example.postgresql
```

## Building From Source

Building the plugin is very simple, just clone the repository and run the following command within the `plugin` directory:
Building the plugin is very simple, just clone the repository and run the following command:

```bash
GOOS=wasip1 GOARCH=wasm go build -o ../sqlc-gen-java.wasm
GOOS=wasip1 GOARCH=wasm go build -o sqlc-gen-java.wasm plugin/main.go
```

A file `sqlc-gen-java.wasm` will be created in the repository root - you can then move it to your sqlc-enabled project
Expand All @@ -51,13 +53,13 @@ You should ensure that the `sha256` value in your `sqlc.yaml` is correct for thi

## Planned Features

- `sqlc.embed()` support
- `MySQL` support
- `SQLite` support
- Improved parameter naming
- First-class support for `bytes`, `blob`, `bytea` datatypes

**Tentative:**

- r2dbc support
- Support for PostgreSQL enum types
- copyfrom support where possible [ref](https://www.baeldung.com/jdbc-batch-processing)
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require github.com/sqlc-dev/plugin-sdk-go v1.23.0

require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/iancoleman/strcase v0.3.0
github.com/jinzhu/inflection v1.0.0
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/sqlc-dev/plugin-sdk-go v1.23.0 h1:iSeJhnXPlbDXlbzUEebw/DxsGzE9rdDJArl8Hvt0RMM=
github.com/sqlc-dev/plugin-sdk-go v1.23.0/go.mod h1:I1r4THOfyETD+LI2gogN2LX8wCjwUZrgy/NU4In3llA=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
Expand Down
36 changes: 31 additions & 5 deletions internal/codegen/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codegen

import (
"fmt"
"github.com/tandemdude/sqlc-gen-java/internal/core"
"os"
"strings"
Expand All @@ -25,11 +26,36 @@ func (b *IndentStringBuilder) WriteIndentedString(level int, s string) int {
return count
}

func writeSqlcHeader(sb *IndentStringBuilder) {
func (b *IndentStringBuilder) writeSqlcHeader() {
sqlcVersion := os.Getenv("SQLC_VERSION")

sb.WriteString("// Code generated by sqlc. DO NOT EDIT.\n")
sb.WriteString("// versions:\n")
sb.WriteString("// sqlc " + sqlcVersion + "\n")
sb.WriteString("// sqlc-gen-java " + core.PluginVersion + "\n")
b.WriteString("// Code generated by sqlc. DO NOT EDIT.\n")
b.WriteString("// versions:\n")
b.WriteString("// sqlc " + sqlcVersion + "\n")
b.WriteString("// sqlc-gen-java " + core.PluginVersion + "\n")
}

func (b *IndentStringBuilder) writeQueriesBoilerplate(nonNullAnnotation, nullableAnnotation string) {
methodTypes := [][]string{
{"Integer", "Int"},
{"Long", "Long"},
{"Float", "Float"},
{"Double", "Double"},
{"Boolean", "Boolean"},
}

for _, methodType := range methodTypes {
b.WriteIndentedString(1, fmt.Sprintf(
"private static %s%s get%s(%sResultSet rs, int col) throws SQLException {\n",
nullableAnnotation,
methodType[0],
methodType[1],
nonNullAnnotation,
))
b.WriteIndentedString(2, fmt.Sprintf(
"var colVal = rs.get%s(col); return rs.wasNull() ? null : colVal;\n",
methodType[1],
))
b.WriteIndentedString(1, "}\n")
}
}
72 changes: 72 additions & 0 deletions internal/codegen/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package codegen

import (
"fmt"
"slices"
"strings"

"github.com/iancoleman/strcase"
"github.com/tandemdude/sqlc-gen-java/internal/core"
)

func BuildModelFile(config core.Config, name string, model []core.QueryReturn) (string, []byte, error) {
imports := make([]string, 0)

var nonNullAnnotation string
if config.NonNullAnnotation != "" {
imports = append(imports, config.NonNullAnnotation)
nonNullAnnotation = "@" + config.NonNullAnnotation[strings.LastIndex(config.NonNullAnnotation, ".")+1:] + " "
}
var nullableAnnotation string
if config.NullableAnnotation != "" {
imports = append(imports, config.NullableAnnotation)
nullableAnnotation = "@" + config.NullableAnnotation[strings.LastIndex(config.NullableAnnotation, ".")+1:] + " "
}

header := NewIndentStringBuilder(config.IndentChar, config.CharsPerIndentLevel)
header.writeSqlcHeader()
header.WriteString("\n")
header.WriteString("package " + config.Package + ".models;\n")
header.WriteString("\n")

body := NewIndentStringBuilder(config.IndentChar, config.CharsPerIndentLevel)
body.WriteString("\n")
body.WriteString("public record " + strcase.ToCamel(name) + "(\n")
for i, ret := range model {
imp, jt, err := core.ResolveImportAndType(ret.JavaType.Type)
if err != nil {
return "", nil, err
}
imports = append(imports, imp)

if ret.JavaType.IsList {
imports = append(imports, "java.util.List")
jt = "List<" + jt + ">"
}

annotation := nonNullAnnotation
if ret.JavaType.IsNullable {
annotation = nullableAnnotation
}

body.WriteIndentedString(1, annotation+jt+" "+strcase.ToLowerCamel(ret.Name))
if i != len(model)-1 {
body.WriteString(",\n")
}
}
body.WriteString("\n")
body.WriteString(") {}\n")

// sort alphabetically and remove duplicate imports
slices.Sort(imports)
imports = slices.Compact(imports)
for _, imp := range imports {
if imp == "" {
continue
}

header.WriteString("import " + imp + ";\n")
}

return fmt.Sprintf("models/%s.java", strcase.ToCamel(name)), []byte(header.String() + body.String()), nil
}
Loading