diff --git a/README.md b/README.md index 360f886..fc85441 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/gen.go.tmpl b/gen.go.tmpl index 9eea406..b3976e7 100644 --- a/gen.go.tmpl +++ b/gen.go.tmpl @@ -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 } diff --git a/parse.go b/parse.go index 1d974f6..1fd60fe 100644 --- a/parse.go +++ b/parse.go @@ -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) diff --git a/schema.go b/schema.go index ba8516f..9e93dce 100644 --- a/schema.go +++ b/schema.go @@ -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 {