Skip to content

Commit 34d1802

Browse files
committed
Migrate from pq to pgx v5 as default postgres driver
1 parent ac95c64 commit 34d1802

26 files changed

Lines changed: 647 additions & 223 deletions

Readme.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ This is inspired by [SQLC](https://github.com/sqlc-dev/sqlc) but pushes the idea
2121
* **Safe**: All the SQL queries use prepared statement to avoid injection
2222
* **Consistent**: Easy to use transaction API to rollback when an error occurs
2323

24-
## Status
24+
## Getting Started
2525

26-
This repostiroy is currently a WIP, features are still not complete and may likely change
26+
```sh
27+
# Install MangoSQL CLI
28+
go install github.com/kefniark/mango-sql/cmd/mangosql
2729

28-
Roadmap:
29-
* Handle custom queries for advanced relations (join, aggregations, ...)
30-
* Handle sql enums
31-
* Handle sql views
32-
* Support more types and custom types (cf ulid, ...)
33-
* Support more driver and database Mysql/MariaDB/Sqlite3
34-
* Write benchmark to compare performance with existing Golang ORM
30+
# Use mango to generate a DB Client (by default ./database/client.go)
31+
mangosql ./database/schema.sql
32+
33+
# Generated Go Code can be output somewhere else
34+
mangosql --output ./mydb/myclient.go --package mydb ./database/schema.sql
35+
```
3536

3637
## Example
3738

@@ -101,6 +102,18 @@ user.Save(db)
101102

102103
```
103104

105+
## Status
106+
107+
This repository is currently a WIP, features are still not complete and may likely change
108+
109+
Roadmap:
110+
* Handle custom queries for advanced relations (join, aggregations, ...)
111+
* Handle sql enums
112+
* Handle sql views
113+
* Support more types and custom types (cf ulid, ...)
114+
* Support more driver and database Mysql/MariaDB/Sqlite3
115+
* Write benchmark to compare performance with existing Golang ORM
116+
104117
## API
105118

106119
Here is the list of all the auto-generated methods for your tables:

cmd/mangosql/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Example: mangosql --output db/file.go db/schema.sql`,
3434
Value: "database",
3535
Usage: "Go Package",
3636
},
37+
&cli.StringFlag{
38+
Name: "driver",
39+
Value: "pgx",
40+
Usage: "SQL Driver",
41+
},
3742
},
3843
Action: func(ctx *cli.Context) error {
3944
if ctx.NArg() <= 0 {
@@ -45,6 +50,7 @@ Example: mangosql --output db/file.go db/schema.sql`,
4550
Src: name,
4651
Output: ctx.String("output"),
4752
Package: ctx.String("package"),
53+
Driver: ctx.String("driver"),
4854
})
4955
},
5056
}
@@ -58,6 +64,7 @@ type GenerateOptions struct {
5864
Src string
5965
Output string
6066
Package string
67+
Driver string
6168
}
6269

6370
func generate(opts GenerateOptions) error {
@@ -103,7 +110,7 @@ func generate(opts GenerateOptions) error {
103110
var b bytes.Buffer
104111
contents := bufio.NewWriter(&b)
105112

106-
if err = internal.Generate(schema, contents, opts.Package); err != nil {
113+
if err = internal.Generate(schema, contents, opts.Package, opts.Driver); err != nil {
107114
return err
108115
}
109116

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/gertd/go-pluralize v0.2.1
99
github.com/google/uuid v1.6.0
1010
github.com/iancoleman/strcase v0.3.0
11-
github.com/jackc/pgx v3.6.2+incompatible
1211
github.com/jackc/pgx/v5 v5.5.5
1312
github.com/jmoiron/sqlx v1.4.0
1413
github.com/lib/pq v1.10.9
@@ -32,12 +31,10 @@ require (
3231
github.com/davecgh/go-spew v1.1.1 // indirect
3332
github.com/dustin/go-humanize v1.0.1 // indirect
3433
github.com/getsentry/raven-go v0.2.0 // indirect
35-
github.com/gofrs/uuid v4.4.0+incompatible // indirect
3634
github.com/gogo/protobuf v1.3.2 // indirect
3735
github.com/golang/protobuf v1.5.4 // indirect
3836
github.com/google/go-cmp v0.6.0 // indirect
3937
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
40-
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
4138
github.com/jackc/pgpassfile v1.0.0 // indirect
4239
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
4340
github.com/jackc/puddle/v2 v2.2.1 // indirect

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqw
7979
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
8080
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
8181
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
82-
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
83-
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
8482
github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
8583
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
8684
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
@@ -134,14 +132,10 @@ github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbk
134132
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
135133
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI=
136134
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
137-
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 h1:vr3AYkKovP8uR8AvSGGUK1IDqRa5lAAvEkZG1LKaCRc=
138-
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
139135
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
140136
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
141137
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
142138
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
143-
github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o=
144-
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
145139
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
146140
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
147141
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
@@ -253,8 +247,6 @@ github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZ
253247
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
254248
github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
255249
github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
256-
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
257-
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
258250
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
259251
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
260252
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=

internal/generator.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,18 @@ var templates embed.FS
2222

2323
var plural = pluralize.NewClient()
2424

25-
func Generate(schema *SQLSchema, contents io.Writer, pkg string) error {
26-
headerTmpl, err := template.ParseFS(templates, "templates/header.tmpl")
25+
func Generate(schema *SQLSchema, contents io.Writer, pkg string, driver string) error {
26+
headerTmpl, err := template.ParseFS(templates, fmt.Sprintf("templates/header_%s.tmpl", driver))
2727
if err != nil {
2828
return err
2929
}
3030

31-
factoryTmpl, err := template.ParseFS(templates, "templates/factory.tmpl")
31+
factoryTmpl, err := template.ParseFS(templates, fmt.Sprintf("templates/factory_%s.tmpl", driver))
3232
if err != nil {
3333
return err
3434
}
3535

36-
// ctxConstTmpl, err := template.ParseFS(templates, "templates/ctx_const.tmpl")
37-
// if err != nil {
38-
// return err
39-
// }
40-
41-
ctxTmpl, err := template.ParseFS(templates, "templates/ctx.tmpl")
36+
ctxTmpl, err := template.ParseFS(templates, fmt.Sprintf("templates/ctx_%s.tmpl", driver))
4237
if err != nil {
4338
return err
4439
}

internal/templates/ctx_const.tmpl

Lines changed: 0 additions & 10 deletions
This file was deleted.

internal/templates/ctx_pgx.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const (
2+
{{ .Const }}
3+
)
4+
5+
type DBContext struct {
6+
db DB_PGX
7+
tx pgx.Tx
8+
}
9+

internal/templates/custom.tmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ type CustomQueries struct {
2525
return nil, err
2626
}
2727

28-
entries := []{{ .NameNormalized }}Model{}
29-
err = q.ctx.queryMany(&entries, sql, args...)
30-
return entries, err
28+
return QueryMany[{{ .NameNormalized }}Model](q.ctx, sql, args...)
3129
}
3230

3331
type {{ .NameNormalized }}Model struct {

0 commit comments

Comments
 (0)