go/sqlgen/gen.go.tmpl

213 lines
3.3 KiB
Cheetah
Raw Normal View History

2024-11-11 05:36:55 +00:00
package {{.PackageName}}
import (
"database/sql"
"iter"
2024-11-18 16:01:17 +00:00
)
2024-11-11 05:36:55 +00:00
type TX interface {
Exec(query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
}
{{range .Schema.Tables}}
// ----------------------------------------------------------------------------
// Table: {{.Name}}
// ----------------------------------------------------------------------------
type {{.Type}} struct {
{{- range .Columns}}
{{.Name}} {{.Type}}{{end}}
}
const {{.Type}}_SelectQuery = "{{.SelectQuery}}"
{{if not .NoInsert -}}
func {{.Type}}_Insert(
tx TX,
row *{{.Type}},
) (err error) {
{{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil {
return err
}
_, err = tx.Exec("{{.InsertQuery}}", {{.InsertArgs}})
2024-11-19 15:20:18 +00:00
return err
2024-11-11 05:36:55 +00:00
}
{{- end}} {{/* if not .NoInsert */}}
{{if not .NoUpdate -}}
{{if .UpdateCols -}}
func {{.Type}}_Update(
tx TX,
row *{{.Type}},
2024-11-18 16:01:17 +00:00
) (err error) {
2024-11-11 05:36:55 +00:00
{{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil {
2024-11-18 16:01:17 +00:00
return err
2024-11-11 05:36:55 +00:00
}
result, err := tx.Exec("{{.UpdateQuery}}", {{.UpdateArgs}})
if err != nil {
2024-11-19 15:20:18 +00:00
return err
2024-11-11 05:36:55 +00:00
}
n, err := result.RowsAffected()
if err != nil {
panic(err)
}
2024-11-18 16:01:17 +00:00
switch n {
case 0:
2024-11-19 15:20:18 +00:00
return sql.ErrNoRows
2024-11-18 16:01:17 +00:00
case 1:
return nil
default:
2024-11-11 05:36:55 +00:00
panic("multiple rows updated")
}
}
{{- end}}
{{if .UpdateFullCols -}}
func {{.Type}}_UpdateFull(
tx TX,
row *{{.Type}},
2024-11-18 16:01:17 +00:00
) (err error) {
2024-11-11 05:36:55 +00:00
{{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil {
2024-11-18 16:01:17 +00:00
return err
2024-11-11 05:36:55 +00:00
}
result, err := tx.Exec("{{.UpdateFullQuery}}", {{.UpdateFullArgs}})
if err != nil {
2024-11-19 15:20:18 +00:00
return err
2024-11-11 05:36:55 +00:00
}
n, err := result.RowsAffected()
if err != nil {
panic(err)
}
2024-11-18 16:01:17 +00:00
switch n {
case 0:
2024-11-19 15:20:18 +00:00
return sql.ErrNoRows
2024-11-18 16:01:17 +00:00
case 1:
return nil
default:
2024-11-11 05:36:55 +00:00
panic("multiple rows updated")
}
}
{{- end}}
{{- end}} {{/* if not .NoUpdate */}}
{{if not .NoDelete -}}
func {{.Type}}_Delete(
tx TX,
{{.PKFunctionArgs -}}
2024-11-18 16:01:17 +00:00
) (err error) {
2024-11-11 05:36:55 +00:00
result, err := tx.Exec("{{.DeleteQuery}}", {{.DeleteArgs}})
2024-11-18 16:01:17 +00:00
if err != nil {
2024-11-19 15:20:18 +00:00
return err
2024-11-11 05:36:55 +00:00
}
n, err := result.RowsAffected()
if err != nil {
panic(err)
}
2024-11-18 16:01:17 +00:00
switch n {
case 0:
2024-11-19 15:20:18 +00:00
return sql.ErrNoRows
2024-11-18 16:01:17 +00:00
case 1:
return nil
default:
2024-11-11 05:36:55 +00:00
panic("multiple rows deleted")
}
}
{{- end}}
func {{.Type}}_Get(
tx TX,
{{.PKFunctionArgs -}}
) (
row *{{.Type}},
err error,
) {
row = &{{.Type}}{}
r := tx.QueryRow("{{.GetQuery}}", {{.DeleteArgs}})
2024-11-19 15:20:18 +00:00
err = r.Scan({{.ScanArgs}})
2024-11-11 05:36:55 +00:00
return
}
func {{.Type}}_GetWhere(
tx TX,
query string,
args ...any,
) (
row *{{.Type}},
err error,
) {
row = &{{.Type}}{}
r := tx.QueryRow(query, args...)
2024-11-19 15:20:18 +00:00
err = r.Scan({{.ScanArgs}})
2024-11-11 05:36:55 +00:00
return
}
func {{.Type}}_Iterate(
tx TX,
query string,
args ...any,
) (
iter.Seq2[*{{.Type}}, error],
) {
rows, err := tx.Query(query, args...)
if err != nil {
return func(yield func(*{{.Type}}, error) bool) {
yield(nil, err)
}
}
return func(yield func(*{{.Type}}, error) bool) {
defer rows.Close()
for rows.Next() {
row := &{{.Type}}{}
2024-11-19 15:20:18 +00:00
err := rows.Scan({{.ScanArgs}})
2024-11-11 05:36:55 +00:00
if !yield(row, err) {
return
}
}
}
}
func {{.Type}}_List(
tx TX,
query string,
args ...any,
) (
l []*{{.Type}},
err error,
) {
for row, err := range {{.Type}}_Iterate(tx, query, args...) {
if err != nil {
return nil, err
}
l = append(l, row)
}
return l, nil
}
{{end}} {{/* range .Schema.Tables */}}