This commit is contained in:
jdl
2026-06-25 13:21:19 +02:00
parent 9f4c86f39f
commit ad59b98aef
4 changed files with 34 additions and 29 deletions

View File

@@ -3,7 +3,7 @@
## Installing
```
go install git.crumpington.com/lib/go/sqlgen/cmd/sqlgen@latest
go install git.crumpington.com/app/sqlgen/cmd/sqlgen@latest
```
## Usage

View File

@@ -22,16 +22,16 @@ type {{.Type}} struct {
{{.Name}} {{.Type}}{{end}}
}
const {{.Type}}_SelectQuery = "{{.SelectQuery}}"
const {{.LowerType}}_SelectQuery = "{{.SelectQuery}}"
{{if not .NoInsert -}}
func {{.Type}}_Insert(
func {{.LowerType}}_Insert(
tx TX,
row *{{.Type}},
) (err error) {
{{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil {
{{.LowerType}}_Sanitize(row)
if err = {{.LowerType}}_Validate(row); err != nil {
return err
}
@@ -45,12 +45,12 @@ func {{.Type}}_Insert(
{{if .UpdateCols -}}
func {{.Type}}_Update(
func {{.LowerType}}_Update(
tx TX,
row *{{.Type}},
) (err error) {
{{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil {
{{.LowerType}}_Sanitize(row)
if err = {{.LowerType}}_Validate(row); err != nil {
return err
}
@@ -76,12 +76,12 @@ func {{.Type}}_Update(
{{if .UpdateFullCols -}}
func {{.Type}}_UpdateFull(
func {{.LowerType}}_UpdateFull(
tx TX,
row *{{.Type}},
) (err error) {
{{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil {
{{.LowerType}}_Sanitize(row)
if err = {{.LowerType}}_Validate(row); err != nil {
return err
}
@@ -110,7 +110,7 @@ func {{.Type}}_UpdateFull(
{{if not .NoDelete -}}
func {{.Type}}_Delete(
func {{.LowerType}}_Delete(
tx TX,
{{.PKFunctionArgs -}}
) (err error) {
@@ -135,7 +135,7 @@ func {{.Type}}_Delete(
{{- end}}
func {{.Type}}_Get(
func {{.LowerType}}_Get(
tx TX,
{{.PKFunctionArgs -}}
) (
@@ -151,7 +151,7 @@ func {{.Type}}_Get(
}
func {{.Type}}_GetWhere(
func {{.LowerType}}_GetWhere(
tx TX,
query string,
args ...any,
@@ -169,12 +169,12 @@ func {{.Type}}_GetWhere(
func {{.Type}}_Iterate(
func {{.LowerType}}_Iterate(
tx TX,
query string,
args ...any,
) (
iter.Seq2[*{{.Type}}, error],
iter.Seq2[{{.Type}}, error],
) {
rows, err := tx.Query(query, args...)
if err != nil {
@@ -186,16 +186,19 @@ func {{.Type}}_Iterate(
return func(yield func(*{{.Type}}, error) bool) {
defer rows.Close()
for rows.Next() {
row := &{{.Type}}{}
row := {{.Type}}{}
err := rows.Scan({{.ScanArgs}})
if !yield(row, err) {
return
}
}
if err != rows.Err() ; err != nil {
yield({{.Type}}{}, err)
}
}
}
func {{.Type}}_List(
func {{.LowerType}}_List(
tx TX,
query string,
args ...any,
@@ -203,7 +206,7 @@ func {{.Type}}_List(
l []*{{.Type}},
err error,
) {
for row, err := range {{.Type}}_Iterate(tx, query, args...) {
for row, err := range {{.LowerType}}_Iterate(tx, query, args...) {
if err != nil {
return nil, err
}

View File

@@ -52,9 +52,10 @@ func parseTable(driver string, schema *schema, tokens []string) ([]string, error
}
table := &table{
driver: driver,
Name: tokens[0],
Type: tokens[2],
driver: driver,
Name: tokens[0],
Type: tokens[2],
LowerType: strings.ToLower(tokens[2][:1]) + tokens[2][1:],
}
schema.Tables = append(schema.Tables, table)

View File

@@ -10,13 +10,14 @@ type schema struct {
}
type table struct {
driver string //
Name string // Name in SQL
Type string // Go type
NoInsert bool
NoUpdate bool
NoDelete bool
Columns []*column
driver string //
Name string // Name in SQL
Type string // Go type
LowerType string // Go type, lowercase.
NoInsert bool
NoUpdate bool
NoDelete bool
Columns []*column
}
type column struct {