Finished select and insert commands

This commit is contained in:
Patrick O'brien 2016-02-23 22:38:24 +10:00
parent 8e9fdf2258
commit 5d679aa0bc
8 changed files with 289 additions and 21 deletions

29
cmds/delete.go Normal file
View file

@ -0,0 +1,29 @@
package cmds
import (
"os"
"github.com/spf13/cobra"
)
func init() {
SQLBoiler.AddCommand(deleteCmd)
deleteCmd.Run = deleteRun
}
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Generate delete statement helpers from table definitions",
}
func deleteRun(cmd *cobra.Command, args []string) {
out := generateDeletes()
for _, v := range out {
os.Stdout.Write(v)
}
}
func generateDeletes() [][]byte {
return [][]byte{}
}

92
cmds/insert.go Normal file
View file

@ -0,0 +1,92 @@
package cmds
import (
"bytes"
"fmt"
"go/format"
"os"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
"github.com/spf13/cobra"
)
func init() {
SQLBoiler.AddCommand(insertCmd)
insertCmd.Run = insertRun
}
var insertCmd = &cobra.Command{
Use: "insert",
Short: "Generate insert statement helpers from table definitions",
}
func insertRun(cmd *cobra.Command, args []string) {
out := generateInserts()
for _, v := range out {
os.Stdout.Write(v)
}
}
func generateInserts() [][]byte {
t, err := template.New("insert.tpl").Funcs(template.FuncMap{
"makeGoColName": makeGoColName,
"makeDBColName": makeDBColName,
"makeGoInsertParamNames": makeGoInsertParamNames,
"makeGoInsertParamFlags": makeGoInsertParamFlags,
}).ParseFiles("templates/insert.tpl")
if err != nil {
errorQuit(err)
}
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
errorQuit(err)
}
out, err := format.Source(buf.Bytes())
if err != nil {
errorQuit(err)
}
outputs = append(outputs, out)
}
return outputs
}
// makeGoInsertParamNames takes a [][]DBData and returns a comma seperated
// list of parameter names for the insert statement
func makeGoInsertParamNames(data []dbdrivers.DBTable) string {
var paramNames string
for i := 0; i < len(data); i++ {
paramNames = paramNames + data[i].ColName
if len(data) != i+1 {
paramNames = paramNames + ", "
}
}
return paramNames
}
// makeGoInsertParamFlags takes a [][]DBData and returns a comma seperated
// list of parameter flags for the insert statement
func makeGoInsertParamFlags(data []dbdrivers.DBTable) string {
var paramFlags string
for i := 0; i < len(data); i++ {
paramFlags = fmt.Sprintf("%s$%d", paramFlags, i+1)
if len(data) != i+1 {
paramFlags = paramFlags + ", "
}
}
return paramFlags
}

View file

@ -1 +1,78 @@
package cmds
import (
"bytes"
"fmt"
"go/format"
"os"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
"github.com/spf13/cobra"
)
func init() {
SQLBoiler.AddCommand(selectCmd)
selectCmd.Run = selectRun
}
var selectCmd = &cobra.Command{
Use: "select",
Short: "Generate select statement helpers from table definitions",
}
func selectRun(cmd *cobra.Command, args []string) {
out := generateSelects()
for _, v := range out {
os.Stdout.Write(v)
}
}
func generateSelects() [][]byte {
t, err := template.New("select.tpl").Funcs(template.FuncMap{
"makeGoColName": makeGoColName,
"makeGoVarName": makeGoVarName,
"makeSelectParamNames": makeSelectParamNames,
}).ParseFiles("templates/select.tpl")
if err != nil {
errorQuit(err)
}
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
errorQuit(err)
}
out, err := format.Source(buf.Bytes())
if err != nil {
errorQuit(err)
}
outputs = append(outputs, out)
}
return outputs
}
func makeSelectParamNames(tableName string, data []dbdrivers.DBTable) string {
var paramNames string
for i := 0; i < len(data); i++ {
paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].ColName,
makeDBColName(tableName, data[i].ColName),
)
if len(data) != i+1 {
paramNames = paramNames + ", "
}
}
return paramNames
}

View file

@ -4,8 +4,15 @@ import (
"fmt"
"os"
"strings"
"github.com/pobri19/sqlboiler/dbdrivers"
)
type tplData struct {
TableName string
TableData []dbdrivers.DBTable
}
func errorQuit(err error) {
fmt.Println(fmt.Sprintf("Error: %s\n---\n", err))
structCmd.Help()
@ -26,6 +33,26 @@ func makeGoColName(name string) string {
return strings.Join(s, "")
}
func makeGoVarName(name string) string {
s := strings.Split(name, "_")
for i := 0; i < len(s); i++ {
if s[i] == "id" {
s[i] = "ID"
continue
}
// Skip first word Title for variable names
if i == 0 {
continue
}
s[i] = strings.Title(s[i])
}
return strings.Join(s, "")
}
func makeDBColName(tableName, colName string) string {
return tableName + "_" + colName
}

View file

@ -19,11 +19,6 @@ var structCmd = &cobra.Command{
Short: "Generate structs from table definitions",
}
type tplData struct {
TableName string
TableData interface{}
}
func structRun(cmd *cobra.Command, args []string) {
out := generateStructs()
@ -42,7 +37,7 @@ func generateStructs() [][]byte {
errorQuit(err)
}
var structOutputs [][]byte
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
@ -60,8 +55,8 @@ func generateStructs() [][]byte {
errorQuit(err)
}
structOutputs = append(structOutputs, out)
outputs = append(outputs, out)
}
return structOutputs
return outputs
}

29
cmds/update.go Normal file
View file

@ -0,0 +1,29 @@
package cmds
import (
"os"
"github.com/spf13/cobra"
)
func init() {
SQLBoiler.AddCommand(updateCmd)
updateCmd.Run = updateRun
}
var updateCmd = &cobra.Command{
Use: "update",
Short: "Generate update statement helpers from table definitions",
}
func updateRun(cmd *cobra.Command, args []string) {
out := generateUpdates()
for _, v := range out {
os.Stdout.Write(v)
}
}
func generateUpdates() [][]byte {
return [][]byte{}
}

20
templates/insert.tpl Normal file
View file

@ -0,0 +1,20 @@
{{- $tableName := .TableName -}}
func Insert{{makeGoColName $tableName}}(o *{{makeGoColName $tableName}}, db *sqlx.DB) (int, error) {
if o == nil {
return 0, errors.New("No {{makeGoColName $tableName}} provided for insertion")
}
var rowID int
err := db.QueryRow(`
INSERT INTO {{$tableName}}
({{makeGoInsertParamNames .TableData}})
VALUES({{makeGoInsertParamFlags .TableData}})
RETURNING id
`)
if err != nil {
return 0, fmt.Errorf("Unable to insert {{$tableName}}: %s", err)
}
return rowID, nil
}

View file

@ -1,19 +1,18 @@
func Insert{{makeGoColName $tableName}}(o *{{makeGoColName $tableName}}, db *sqlx.DB) (int, error) {
if o == nil {
return 0, errors.New("No {{objName}} provided for insertion")
{{- $tableName := .TableName -}}
func Select{{makeGoColName $tableName}}(id int, db *sqlx.DB) ({{makeGoColName $tableName}}, error) {
if id == 0 {
return nil, errors.New("No ID provided for {{makeGoColName $tableName}} select")
}
var rowID int
err := db.QueryRow(
`INSERT INTO {{tableName}}
({{makeGoInsertParamNames tableData}})
VALUES({{makeGoInsertParamFlags tableData}})
RETURNING id`
)
{{$varName := makeGoVarName $tableName}}
var {{$varName}} {{makeGoColName $tableName}}
err := db.Select(&{{$varName}}, `
SELECT {{makeSelectParamNames $tableName .TableData}}
WHERE id=$1
`, id)
if err != nil {
return 0, fmt.Errorf("Unable to insert {{objName}}: %s", err)
return nil, fmt.Errorf("Unable to select from {{$tableName}}: %s", err)
}
return rowID, nil
return {{$varName}}, nil
}