WIP
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
## Installing
|
## Installing
|
||||||
|
|
||||||
```
|
```
|
||||||
go install git.crumpington.com/lib/go/sqlgen/cmd/sqlgen@latest
|
go install git.crumpington.com/app/sqlgen/cmd/sqlgen@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|||||||
39
gen.go.tmpl
39
gen.go.tmpl
@@ -22,16 +22,16 @@ type {{.Type}} struct {
|
|||||||
{{.Name}} {{.Type}}{{end}}
|
{{.Name}} {{.Type}}{{end}}
|
||||||
}
|
}
|
||||||
|
|
||||||
const {{.Type}}_SelectQuery = "{{.SelectQuery}}"
|
const {{.LowerType}}_SelectQuery = "{{.SelectQuery}}"
|
||||||
|
|
||||||
{{if not .NoInsert -}}
|
{{if not .NoInsert -}}
|
||||||
|
|
||||||
func {{.Type}}_Insert(
|
func {{.LowerType}}_Insert(
|
||||||
tx TX,
|
tx TX,
|
||||||
row *{{.Type}},
|
row *{{.Type}},
|
||||||
) (err error) {
|
) (err error) {
|
||||||
{{.Type}}_Sanitize(row)
|
{{.LowerType}}_Sanitize(row)
|
||||||
if err = {{.Type}}_Validate(row); err != nil {
|
if err = {{.LowerType}}_Validate(row); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,12 +45,12 @@ func {{.Type}}_Insert(
|
|||||||
|
|
||||||
{{if .UpdateCols -}}
|
{{if .UpdateCols -}}
|
||||||
|
|
||||||
func {{.Type}}_Update(
|
func {{.LowerType}}_Update(
|
||||||
tx TX,
|
tx TX,
|
||||||
row *{{.Type}},
|
row *{{.Type}},
|
||||||
) (err error) {
|
) (err error) {
|
||||||
{{.Type}}_Sanitize(row)
|
{{.LowerType}}_Sanitize(row)
|
||||||
if err = {{.Type}}_Validate(row); err != nil {
|
if err = {{.LowerType}}_Validate(row); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,12 +76,12 @@ func {{.Type}}_Update(
|
|||||||
|
|
||||||
{{if .UpdateFullCols -}}
|
{{if .UpdateFullCols -}}
|
||||||
|
|
||||||
func {{.Type}}_UpdateFull(
|
func {{.LowerType}}_UpdateFull(
|
||||||
tx TX,
|
tx TX,
|
||||||
row *{{.Type}},
|
row *{{.Type}},
|
||||||
) (err error) {
|
) (err error) {
|
||||||
{{.Type}}_Sanitize(row)
|
{{.LowerType}}_Sanitize(row)
|
||||||
if err = {{.Type}}_Validate(row); err != nil {
|
if err = {{.LowerType}}_Validate(row); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +110,7 @@ func {{.Type}}_UpdateFull(
|
|||||||
|
|
||||||
{{if not .NoDelete -}}
|
{{if not .NoDelete -}}
|
||||||
|
|
||||||
func {{.Type}}_Delete(
|
func {{.LowerType}}_Delete(
|
||||||
tx TX,
|
tx TX,
|
||||||
{{.PKFunctionArgs -}}
|
{{.PKFunctionArgs -}}
|
||||||
) (err error) {
|
) (err error) {
|
||||||
@@ -135,7 +135,7 @@ func {{.Type}}_Delete(
|
|||||||
|
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
||||||
func {{.Type}}_Get(
|
func {{.LowerType}}_Get(
|
||||||
tx TX,
|
tx TX,
|
||||||
{{.PKFunctionArgs -}}
|
{{.PKFunctionArgs -}}
|
||||||
) (
|
) (
|
||||||
@@ -151,7 +151,7 @@ func {{.Type}}_Get(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func {{.Type}}_GetWhere(
|
func {{.LowerType}}_GetWhere(
|
||||||
tx TX,
|
tx TX,
|
||||||
query string,
|
query string,
|
||||||
args ...any,
|
args ...any,
|
||||||
@@ -169,12 +169,12 @@ func {{.Type}}_GetWhere(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
func {{.Type}}_Iterate(
|
func {{.LowerType}}_Iterate(
|
||||||
tx TX,
|
tx TX,
|
||||||
query string,
|
query string,
|
||||||
args ...any,
|
args ...any,
|
||||||
) (
|
) (
|
||||||
iter.Seq2[*{{.Type}}, error],
|
iter.Seq2[{{.Type}}, error],
|
||||||
) {
|
) {
|
||||||
rows, err := tx.Query(query, args...)
|
rows, err := tx.Query(query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,16 +186,19 @@ func {{.Type}}_Iterate(
|
|||||||
return func(yield func(*{{.Type}}, error) bool) {
|
return func(yield func(*{{.Type}}, error) bool) {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
row := &{{.Type}}{}
|
row := {{.Type}}{}
|
||||||
err := rows.Scan({{.ScanArgs}})
|
err := rows.Scan({{.ScanArgs}})
|
||||||
if !yield(row, err) {
|
if !yield(row, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err != rows.Err() ; err != nil {
|
||||||
|
yield({{.Type}}{}, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func {{.Type}}_List(
|
func {{.LowerType}}_List(
|
||||||
tx TX,
|
tx TX,
|
||||||
query string,
|
query string,
|
||||||
args ...any,
|
args ...any,
|
||||||
@@ -203,7 +206,7 @@ func {{.Type}}_List(
|
|||||||
l []*{{.Type}},
|
l []*{{.Type}},
|
||||||
err error,
|
err error,
|
||||||
) {
|
) {
|
||||||
for row, err := range {{.Type}}_Iterate(tx, query, args...) {
|
for row, err := range {{.LowerType}}_Iterate(tx, query, args...) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
7
parse.go
7
parse.go
@@ -52,9 +52,10 @@ func parseTable(driver string, schema *schema, tokens []string) ([]string, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
table := &table{
|
table := &table{
|
||||||
driver: driver,
|
driver: driver,
|
||||||
Name: tokens[0],
|
Name: tokens[0],
|
||||||
Type: tokens[2],
|
Type: tokens[2],
|
||||||
|
LowerType: strings.ToLower(tokens[2][:1]) + tokens[2][1:],
|
||||||
}
|
}
|
||||||
schema.Tables = append(schema.Tables, table)
|
schema.Tables = append(schema.Tables, table)
|
||||||
|
|
||||||
|
|||||||
15
schema.go
15
schema.go
@@ -10,13 +10,14 @@ type schema struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type table struct {
|
type table struct {
|
||||||
driver string //
|
driver string //
|
||||||
Name string // Name in SQL
|
Name string // Name in SQL
|
||||||
Type string // Go type
|
Type string // Go type
|
||||||
NoInsert bool
|
LowerType string // Go type, lowercase.
|
||||||
NoUpdate bool
|
NoInsert bool
|
||||||
NoDelete bool
|
NoUpdate bool
|
||||||
Columns []*column
|
NoDelete bool
|
||||||
|
Columns []*column
|
||||||
}
|
}
|
||||||
|
|
||||||
type column struct {
|
type column struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user