Skip to content

Commit 1c6cfc9

Browse files
committed
0 parents  commit 1c6cfc9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5025
-0
lines changed

.github/workflows/ci.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: go
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
jobs:
8+
test:
9+
name: test
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-go@v4
14+
with:
15+
go-version: '1.21'
16+
- run: make

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Riza, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: build test
2+
3+
build:
4+
go build ./...
5+
6+
test: bin/sqlc-gen-go.wasm
7+
go test ./...
8+
9+
all: bin/sqlc-gen-go bin/sqlc-gen-go.wasm
10+
11+
bin/sqlc-gen-go: bin go.mod go.sum $(wildcard **/*.go)
12+
cd plugin && go build -o ../bin/sqlc-gen-go ./main.go
13+
14+
bin/sqlc-gen-go.wasm: bin/sqlc-gen-go
15+
cd plugin && GOOS=wasip1 GOARCH=wasm go build -o ../bin/sqlc-gen-go.wasm main.go
16+
17+
bin:
18+
mkdir -p bin

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Usage
2+
3+
> [!IMPORTANT]
4+
> This repository is read-only. It contains a working Go codegen plugin extracted from https://github.com/sqlc-dev/sqlc which you can fork and modify to meet your needs.
5+
6+
```yaml
7+
version: '2'
8+
plugins:
9+
- name: golang
10+
wasm:
11+
url: "https://example.com"
12+
sha256: ""
13+
sql:
14+
- schema: schema.sql
15+
queries: query.sql
16+
engine: postgresql
17+
codegen:
18+
- plugin: golang
19+
out: db
20+
options:
21+
package: db
22+
sql_package: pgx/v5
23+
```

go.mod

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module github.com/sqlc-dev/sqlc-gen-go
2+
3+
go 1.21.3
4+
5+
require (
6+
github.com/fatih/structtag v1.2.0
7+
github.com/google/go-cmp v0.5.9
8+
github.com/jinzhu/inflection v1.0.0
9+
github.com/sqlc-dev/plugin-sdk-go v1.23.0
10+
)
11+
12+
require (
13+
github.com/golang/protobuf v1.5.3 // indirect
14+
golang.org/x/net v0.14.0 // indirect
15+
golang.org/x/sys v0.11.0 // indirect
16+
golang.org/x/text v0.12.0 // indirect
17+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
18+
google.golang.org/grpc v1.59.0 // indirect
19+
google.golang.org/protobuf v1.31.0 // indirect
20+
)

go.sum

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
2+
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
3+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
4+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
5+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
6+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
8+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
10+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
11+
github.com/sqlc-dev/plugin-sdk-go v1.23.0 h1:iSeJhnXPlbDXlbzUEebw/DxsGzE9rdDJArl8Hvt0RMM=
12+
github.com/sqlc-dev/plugin-sdk-go v1.23.0/go.mod h1:I1r4THOfyETD+LI2gogN2LX8wCjwUZrgy/NU4In3llA=
13+
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
14+
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
15+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
16+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
18+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
19+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
20+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
21+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
22+
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
23+
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
24+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
25+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
26+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
27+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

internal/debug/debug.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package debug
2+
3+
var Active bool

internal/driver.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package golang
2+
3+
type SQLDriver string
4+
5+
const (
6+
SQLPackagePGXV4 string = "pgx/v4"
7+
SQLPackagePGXV5 string = "pgx/v5"
8+
SQLPackageStandard string = "database/sql"
9+
)
10+
11+
const (
12+
SQLDriverPGXV4 SQLDriver = "github.com/jackc/pgx/v4"
13+
SQLDriverPGXV5 = "github.com/jackc/pgx/v5"
14+
SQLDriverLibPQ = "github.com/lib/pq"
15+
SQLDriverGoSQLDriverMySQL = "github.com/go-sql-driver/mysql"
16+
)
17+
18+
func parseDriver(sqlPackage string) SQLDriver {
19+
switch sqlPackage {
20+
case SQLPackagePGXV4:
21+
return SQLDriverPGXV4
22+
case SQLPackagePGXV5:
23+
return SQLDriverPGXV5
24+
default:
25+
return SQLDriverLibPQ
26+
}
27+
}
28+
29+
func (d SQLDriver) IsPGX() bool {
30+
return d == SQLDriverPGXV4 || d == SQLDriverPGXV5
31+
}
32+
33+
func (d SQLDriver) IsGoSQLDriverMySQL() bool {
34+
return d == SQLDriverGoSQLDriverMySQL
35+
}
36+
37+
func (d SQLDriver) Package() string {
38+
switch d {
39+
case SQLDriverPGXV4:
40+
return SQLPackagePGXV4
41+
case SQLDriverPGXV5:
42+
return SQLPackagePGXV5
43+
default:
44+
return SQLPackageStandard
45+
}
46+
}

internal/endtoend/testdata/authors/go/db.go

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/authors/go/models.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/authors/go/query.sql.go

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING *;
16+
17+
-- name: DeleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = $1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '2'
2+
plugins:
3+
- name: golang
4+
wasm:
5+
url: file://../../../../bin/sqlc-gen-go.wasm
6+
sha256: 841f3b108a637b18782f7a74a4ea3e0829e572ff8287f78effa02e17f389656c
7+
sql:
8+
- schema: schema.sql
9+
queries: query.sql
10+
engine: postgresql
11+
codegen:
12+
- plugin: golang
13+
out: go
14+
options:
15+
package: querytest
16+
sql_package: pgx/v5

0 commit comments

Comments
 (0)