Added panic functions

* Fixed linter errors
* Added lots of missing comments
* Fixed broken boil tests
* Skipped unfinished functions instead of error
This commit is contained in:
Patrick O'brien 2016-07-16 21:22:57 +10:00
parent a559738d59
commit f45d137f5f
12 changed files with 228 additions and 5 deletions

View file

@ -4,16 +4,19 @@ type boilErr struct {
error
}
// WrapErr wraps err in a boilErr
func WrapErr(err error) error {
return boilErr{
error: err,
}
}
// Error returns the underlying error string
func (e boilErr) Error() string {
return e.error.Error()
}
// IsBoilErr checks if err is a boilErr
func IsBoilErr(err error) bool {
_, ok := err.(boilErr)
return ok

View file

@ -304,7 +304,7 @@ func TestSelectNames(t *testing.T) {
}
result := SelectNames(o)
if result != `id, TestHello, head_size` {
if result != `"id", "TestHello", "head_size"` {
t.Error("Result was wrong, got:", result)
}
}
@ -320,7 +320,7 @@ func TestWhereClause(t *testing.T) {
result := WhereClause(columns)
if result != `id=$1 AND name=$2 AND date=$3` {
if result != `"id"=$1 AND "name"=$2 AND "date"=$3` {
t.Error("Result was wrong, got:", result)
}
}

View file

@ -128,68 +128,84 @@ func ExecQueryAll(q *Query) (*sql.Rows, error) {
return q.executor.Query(qs, args...)
}
// SetCount on the query.
func SetCount(q *Query) {
q.count = true
}
// SetDelete on the query.
func SetDelete(q *Query) {
q.delete = true
}
// SetUpdate on the query.
func SetUpdate(q *Query, cols map[string]interface{}) {
q.update = cols
}
// SetExecutor on the query.
func SetExecutor(q *Query, exec Executor) {
q.executor = exec
}
// SetSelect on the query.
func SetSelect(q *Query, columns ...string) {
q.selectCols = append(q.selectCols, columns...)
}
// Select returns the select columns in the query.
func Select(q *Query) []string {
cols := make([]string, len(q.selectCols))
copy(cols, q.selectCols)
return cols
}
// SetTable on the query.
func SetTable(q *Query, table string) {
q.table = table
}
// SetInnerJoin on the query.
func SetInnerJoin(q *Query, on string, args ...interface{}) {
q.innerJoins = append(q.innerJoins, join{on: on, args: args})
}
// SetOuterJoin on the query.
func SetOuterJoin(q *Query, on string, args ...interface{}) {
q.outerJoins = append(q.outerJoins, join{on: on, args: args})
}
// SetLeftOuterJoin on the query.
func SetLeftOuterJoin(q *Query, on string, args ...interface{}) {
q.leftOuterJoins = append(q.leftOuterJoins, join{on: on, args: args})
}
// SetRightOuterJoin on the query.
func SetRightOuterJoin(q *Query, on string, args ...interface{}) {
q.rightOuterJoins = append(q.rightOuterJoins, join{on: on, args: args})
}
// SetWhere on the query.
func SetWhere(q *Query, clause string, args ...interface{}) {
q.where = append(q.where, where{clause: clause, args: args})
}
// SetGroupBy on the query.
func SetGroupBy(q *Query, clause string) {
q.groupBy = append(q.groupBy, clause)
}
// SetOrderBy on the query.
func SetOrderBy(q *Query, clause string) {
q.orderBy = append(q.orderBy, clause)
}
// SetHaving on the query.
func SetHaving(q *Query, clause string) {
q.having = append(q.having, clause)
}
// SetLimit on the query.
func SetLimit(q *Query, limit int) {
q.limit = limit
}

View file

@ -43,6 +43,15 @@ func (q *Query) Bind(obj interface{}) error {
return nil
}
// Bind executes the query and inserts the
// result into the passed in object pointer.
// It panics on error.
func (q *Query) BindP(obj interface{}) {
if err := q.Bind(obj); err != nil {
panic(WrapErr(err))
}
}
// BindOne inserts the returned row columns into the
// passed in object pointer
func BindOne(row *sql.Row, selectCols []string, obj interface{}) error {

View file

@ -8,15 +8,19 @@ import (
)
func TestBind(t *testing.T) {
t.Errorf("Not implemented")
t.Skip("Not implemented")
}
func TestBindP(t *testing.T) {
t.Skip("Not implemented")
}
func TestBindOne(t *testing.T) {
t.Errorf("Not implemented")
t.Skip("Not implemented")
}
func TestBindAll(t *testing.T) {
t.Errorf("Not implemented")
t.Skip("Not implemented")
}
func TestGetStructValues(t *testing.T) {

View file

@ -15,6 +15,16 @@ func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
return o, nil
}
// OneP returns a single {{$varNameSingular}} record from the query, and panics on error.
func (q {{$varNameSingular}}Query) OneP() (*{{$tableNameSingular}}) {
o, err := q.One()
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// All returns all {{$tableNameSingular}} records from the query.
func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
var o {{$varNameSingular}}Slice
@ -33,6 +43,16 @@ func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
return o, nil
}
// AllP returns all {{$tableNameSingular}} records from the query, and panics on error.
func (q {{$varNameSingular}}Query) AllP() {{$varNameSingular}}Slice {
o, err := q.All()
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// Count returns the count of all {{$tableNameSingular}} records in the query.
func (q {{$varNameSingular}}Query) Count() (int64, error) {
var count int64
@ -46,3 +66,13 @@ func (q {{$varNameSingular}}Query) Count() (int64, error) {
return count, nil
}
// CountP returns the count of all {{$tableNameSingular}} records in the query, and panics on error.
func (q {{$varNameSingular}}Query) CountP() int64 {
c, err := q.Count()
if err != nil {
panic(boil.WrapErr(err))
}
return c
}

View file

@ -8,6 +8,16 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.
return {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(boil.GetDB(), selectCols...)
}
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key. Panics on error.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}P(selectCols ...string) *{{$rel.ForeignTable.NameGo}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(boil.GetDB(), selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}X(exec boil.Executor, selectCols ...string) (*{{$rel.ForeignTable.NameGo}}, error) {
{{$rel.Function.Varname}} := &{{$rel.ForeignTable.NameGo}}{}
@ -26,5 +36,14 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.
return {{$rel.Function.Varname}}, nil
}
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key. Panics on error.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}XP(exec boil.Executor, selectCols ...string) *{{$rel.ForeignTable.NameGo}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(exec, selectCol..)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
{{end -}}
{{- end -}}

View file

@ -10,6 +10,17 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
return {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(boil.GetDB(), selectCols...)
}
// {{$rel.Function.Name}}P panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}P(selectCols ...string) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(boil.GetDB(), selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// {{$rel.Function.Name}}X retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}X(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
@ -47,5 +58,16 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
return ret, nil
}
// {{$rel.Function.Name}}XP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}XP(exec boil.Executor, selectCols ...string) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(exec, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
{{end -}}{{- /* range relationships */ -}}
{{- end -}}{{- /* outer if join table */ -}}

View file

@ -9,6 +9,17 @@ func {{$tableNameSingular}}Find({{$pkArgs}}, selectCols ...string) (*{{$tableNam
return {{$tableNameSingular}}FindX(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
}
// {{$tableNameSingular}}FindP retrieves a single record by ID, and panics on error.
func {{$tableNameSingular}}FindP({{$pkArgs}}, selectCols ...string) *{{$tableNameSingular}} {
o, err := {{$tableNameSingular}}FindX(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// {{$tableNameSingular}}FindX retrieves a single record by ID with an executor.
func {{$tableNameSingular}}FindX(exec boil.Executor, {{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
@ -28,3 +39,13 @@ func {{$tableNameSingular}}FindX(exec boil.Executor, {{$pkArgs}}, selectCols ...
return {{$varNameSingular}}, nil
}
// {{$tableNameSingular}}FindXP retrieves a single record by ID with an executor, and panics on error.
func {{$tableNameSingular}}FindXP(exec boil.Executor, {{$pkArgs}}, selectCols ...string) *{{$tableNameSingular}} {
o, err := {{$tableNameSingular}}FindX(exec, {{$pkNames | join ", "}}, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}

View file

@ -5,6 +5,13 @@ func (o *{{$tableNameSingular}}) Insert(whitelist ... string) error {
return o.InsertX(boil.GetDB(), whitelist...)
}
// InsertP a single record, and panics on error.
func (o *{{$tableNameSingular}}) InsertP(whitelist ... string) {
if err := o.InsertX(boil.GetDB(), whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
// InsertX a single record using an executor.
func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... string) error {
if o == nil {
@ -74,6 +81,13 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
return nil
}
// InsertXP a single record using an executor, and panics on error.
func (o *{{$tableNameSingular}}) InsertXP(exec boil.Executor, whitelist ... string) {
if err := o.InsertX(exec, whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
// generateInsertColumns generates the whitelist columns and return columns for an insert statement
func (o *{{$tableNameSingular}}) generateInsertColumns(whitelist ...string) ([]string, []string) {
var wl []string

View file

@ -10,16 +10,41 @@ func (o *{{$tableNameSingular}}) Update(whitelist ...string) error {
return o.UpdateX(boil.GetDB(), whitelist...)
}
// Update a single {{$tableNameSingular}} record.
// UpdateP takes a whitelist of column names that should be updated.
// The primary key will be used to find the record to update.
// Panics on error.
func (o *{{$tableNameSingular}}) UpdateP(whitelist ...string) {
if err := o.UpdateX(boil.GetDB(), whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
// UpdateX uses an executor to update the {{$tableNameSingular}}.
func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... string) error {
return o.UpdateAtX(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
}
// UpdateXP uses an executor to update the {{$tableNameSingular}}, and panics on error.
func (o *{{$tableNameSingular}}) UpdateXP(exec boil.Executor, whitelist ... string) {
err := o.UpdateAtX(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
if err != nil {
panic(boil.WrapErr(err))
}
}
// UpdateAt updates the {{$tableNameSingular}} using the primary key to find the row to update.
func (o *{{$tableNameSingular}}) UpdateAt({{$pkArgs}}, whitelist ...string) error {
return o.UpdateAtX(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...)
}
// UpdateAtP updates the {{$tableNameSingular}} using the primary key to find the row to update. Panics on error.
func (o *{{$tableNameSingular}}) UpdateAtP({{$pkArgs}}, whitelist ...string) {
if err := o.UpdateAtX(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
// UpdateAtX uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{$pkArgs}}, whitelist ...string) error {
if err := o.doBeforeUpdateHooks(); err != nil {
@ -57,6 +82,15 @@ func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{$pkArgs}}, whit
return nil
}
// UpdateAtXP uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
// Panics on error.
func (o *{{$tableNameSingular}}) UpdateAtXP(exec boil.Executor, {{$pkArgs}}, whitelist ...string) {
if err := o.UpdateAtX(exec, {{$pkNames | join ", "}}, whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
// UpdateAll updates all rows with matching column names.
func (q {{$varNameSingular}}Query) UpdateAll(cols M) error {
boil.SetUpdate(q.Query, cols)
@ -68,6 +102,13 @@ func (q {{$varNameSingular}}Query) UpdateAll(cols M) error {
return nil
}
// UpdateAllP updates all rows with matching column names, and panics on error.
func (q {{$varNameSingular}}Query) UpdateAllP(cols M) {
if err := q.UpdateAll(cols); err != nil {
panic(boil.WrapErr(err))
}
}
// generateUpdateColumns generates the whitelist columns for an update statement
func (o *{{$tableNameSingular}}) generateUpdateColumns(whitelist ...string) []string {
if len(whitelist) != 0 {

View file

@ -10,6 +10,17 @@ func (o *{{$tableNameSingular}}) Delete() error {
return o.DeleteX(boil.GetDB())
}
// DeleteP deletes a single {{$tableNameSingular}} record.
// DeleteP will match against the primary key column to find the record to delete.
// Panics on error.
func (o *{{$tableNameSingular}}) DeleteP() {
if err := o.Delete(); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteX deletes a single {{$tableNameSingular}} record with an executor.
// DeleteX will match against the primary key column to find the record to delete.
func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
if o == nil {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion")
@ -33,6 +44,16 @@ func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
return nil
}
// DeleteXP deletes a single {{$tableNameSingular}} record with an executor.
// DeleteXP will match against the primary key column to find the record to delete.
// Panics on error.
func (o *{{$tableNameSingular}}) DeleteXP(exec boil.Executor) {
if err := o.DeleteX(exec); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteAll deletes all rows.
func (o {{$varNameSingular}}Query) DeleteAll() error {
if o.Query == nil {
return errors.New("{{.PkgName}}: no {{$varNameSingular}}Query provided for delete all")
@ -48,6 +69,14 @@ func (o {{$varNameSingular}}Query) DeleteAll() error {
return nil
}
// DeleteAllP deletes all rows, and panics on error.
func (o {{$varNameSingular}}Query) DeleteAllP() {
if err := o.DeleteAll(); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteAll deletes all rows in the slice.
func (o {{$varNameSingular}}Slice) DeleteAll() error {
if o == nil {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
@ -55,6 +84,14 @@ func (o {{$varNameSingular}}Slice) DeleteAll() error {
return o.DeleteAllX(boil.GetDB())
}
// DeleteAll deletes all rows in the slice.
func (o {{$varNameSingular}}Slice) DeleteAllP() {
if err := o.DeleteAll(); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteAllX deletes all rows in the slice with an executor.
func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error {
if o == nil {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
@ -83,3 +120,10 @@ func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error {
return nil
}
// DeleteAllXP deletes all rows in the slice with an executor, and panics on error.
func (o {{$varNameSingular}}Slice) DeleteAllXP(exec boil.Executor) {
if err := o.DeleteAllX(exec); err != nil {
panic(boil.WrapErr(err))
}
}