Skip to content

Commit

Permalink
add function for query return
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Apr 17, 2024
1 parent ddfd08a commit 5572e43
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: go

on:
push:
tags:
- "v*"

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable
# More assembly might b
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 7 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
builds:
- id: wasm
main: ./plugin
goos: ["wasip1"]
goarch: ["wasm"]
archives:
- format: binary
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test: bin/sqlc-gen-go.wasm

all: bin/sqlc-gen-go bin/sqlc-gen-go.wasm

bin/sqlc-gen-go: bin go.mod go.sum $(wildcard **/*.go)
bin/sqlc-gen-go: bin go.mod go.sum $(wildcard **/*.go) $(wildcard internal/templates/**/*.tmpl)
cd plugin && go build -o ../bin/sqlc-gen-go ./main.go

bin/sqlc-gen-go.wasm: bin/sqlc-gen-go
Expand Down
18 changes: 15 additions & 3 deletions internal/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{if eq .Cmd ":one"}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}} {{if ne .Arg.Pair "" }},{{end}} {{ .Ret.Name }}OptionalInitFunc func(*{{.Ret.DefineType}})) ({{.Ret.DefineType}}, error) {
{{- template "queryCodeStdExec" . }}
{{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }}
var {{.Ret.Name}} {{.Ret.Type}}
{{- end}}
if {{ .Ret.Name }}OptionalInitFunc != nil {
{{ .Ret.Name }}OptionalInitFunc(&{{.Ret.ReturnName}})
}

err := row.Scan({{.Ret.Scan}})
return {{.Ret.ReturnName}}, err
}
Expand All @@ -35,7 +39,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{if eq .Cmd ":many"}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}} {{if ne .Arg.Pair "" }},{{end}} {{ .Ret.Name }}OptionalInitFunc func(*{{.Ret.DefineType}}), {{ .Ret.Name }}OptionalFilterFunc func({{.Ret.DefineType}}) bool) ([]{{.Ret.DefineType}}, error) {
{{- template "queryCodeStdExec" . }}
if err != nil {
return nil, err
Expand All @@ -47,10 +51,18 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
var items []{{.Ret.DefineType}}
{{end -}}
for rows.Next() {
var {{.Ret.Name}} {{.Ret.Type}}
var {{.Ret.Name}} {{.Ret.Type}}
if {{ .Ret.Name }}OptionalInitFunc != nil {
{{ .Ret.Name }}OptionalInitFunc(&{{.Ret.ReturnName}})
}

if err := rows.Scan({{.Ret.Scan}}); err != nil {
return nil, err
}

if {{ .Ret.Name }}OptionalFilterFunc != nil && !{{ .Ret.Name }}OptionalFilterFunc({{.Ret.ReturnName}}) {
continue
}
items = append(items, {{.Ret.ReturnName}})
}
if err := rows.Close(); err != nil {
Expand Down

0 comments on commit 5572e43

Please sign in to comment.