Added comments to all tpl statements
* Added new statements * Modified existing statements * (TODO: FINISH STATEMENT IMPLEMENTATIONS)
This commit is contained in:
parent
d58e986c56
commit
403951940f
4 changed files with 130 additions and 24 deletions
|
@ -1,12 +1,13 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
func Delete{{makeGoColName $tableName}}(id int, db *sqlx.DB) error {
|
||||
// {{makeGoColName $tableName}}Delete deletes a single record.
|
||||
func {{makeGoColName $tableName}}Delete(db *sqlx.DB, id int) error {
|
||||
if id == nil {
|
||||
return nil, errors.New("No ID provided for {{makeGoColName $tableName}} delete")
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} delete")
|
||||
}
|
||||
|
||||
err := db.Exec("DELETE FROM {{$tableName}} WHERE id=$1", id)
|
||||
if err != nil {
|
||||
return errors.New("Unable to delete from {{$tableName}}: %s", err)
|
||||
return errors.New("model: unable to delete from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
func Insert{{makeGoColName $tableName}}(o *{{makeGoColName $tableName}}, db *sqlx.DB) (int, error) {
|
||||
// {{makeGoColName $tableName}}Insert inserts a single record.
|
||||
func {{makeGoColName $tableName}}Insert(db *sqlx.DB, o *{{makeGoColName $tableName}}) (int, error) {
|
||||
if o == nil {
|
||||
return 0, errors.New("No {{makeGoColName $tableName}} provided for insertion")
|
||||
return 0, errors.New("model: no {{$tableName}} provided for insertion")
|
||||
}
|
||||
|
||||
var rowID int
|
||||
err := db.QueryRow(`
|
||||
INSERT INTO {{$tableName}}
|
||||
({{makeGoInsertParamNames .TableData}})
|
||||
VALUES({{makeGoInsertParamFlags .TableData}})
|
||||
RETURNING id
|
||||
`)
|
||||
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 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return rowID, nil
|
||||
|
|
|
@ -1,17 +1,125 @@
|
|||
{{- $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")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} {{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `
|
||||
SELECT {{makeSelectParamNames $tableName .TableData}}
|
||||
WHERE id=$1
|
||||
`, id)
|
||||
// {{makeGoColName $tableName}}All retrieves all records.
|
||||
func {{makeGoColName $tableName}}All(db *sqlx.DB) ([]*{{makeGoColName $tableName}}, error) {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to select from {{$tableName}}: %s", err)
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}AllBy retrieves all records with the specified column values.
|
||||
func {{makeGoColName $tableName}}AllBy(db *sqlx.DB, columns map[string]interface{}) ([]*{{makeGoColName $tableName}}, error) {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}FieldsAll retrieves the specified columns for all records.
|
||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{makeGoColName $tableName}}FieldsAll(db *sqlx.DB, results interface{}) error {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}FieldsAllBy retrieves the specified columns
|
||||
// for all records with the specified column values.
|
||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{makeGoColName $tableName}}FieldsAllBy(db *sqlx.DB, columns map[string]interface{}, results interface{}) error {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}Find retrieves a single record by ID.
|
||||
func {{makeGoColName $tableName}}Find(db *sqlx.DB, id int) (*{{makeGoColName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}FindBy retrieves a single record with the specified column values.
|
||||
func {{makeGoColName $tableName}}FindBy(db *sqlx.DB, columns map[string]interface{}) (*{{makeGoColName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{makeSelectParamNames $tableName .TableData}} WHERE %s=$1`, column), value)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}FieldsFind retrieves the specified columns for a single record by ID.
|
||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{makeGoColName $tableName}}FieldsFind(db *sqlx.DB, id int, results interface{}) (*{{makeGoColName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
||||
|
||||
// {{makeGoColName $tableName}}FieldsFindBy retrieves the specified columns
|
||||
// for a single record with the specified column values.
|
||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{makeGoColName $tableName}}FieldsFindBy(db *sqlx.DB, columns map[string]interface{}, results interface{}) (*{{makeGoColName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}} is an object representing the database table.
|
||||
type {{makeGoColName $tableName}} struct {
|
||||
{{range $key, $value := .TableData -}}
|
||||
{{makeGoColName $value.ColName}} {{$value.ColType}} `db:"{{makeDBColName $tableName $value.ColName}}",json:"{{$value.ColName}}"`
|
||||
{{makeGoColName $value.ColName}} {{$value.ColType}} `db:"{{makeDBColName $tableName $value.ColName}}" json:"{{$value.ColName}}"`
|
||||
{{end -}}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue