Update query builder to make queries readable

* Finish DeleteAll for slice and query
* Fixed some formatting and comments
This commit is contained in:
Patrick O'brien 2016-08-09 20:19:42 +10:00
parent 3431919269
commit 99b292b1ee
12 changed files with 88 additions and 70 deletions

View file

@ -1 +1 @@
SELECT * FROM "q" ORDER BY a ASC,b DESC;
SELECT * FROM "q" ORDER BY a ASC, b DESC;

View file

@ -1 +1 @@
SELECT count(*) as ab, thing as bd,"stuff" FROM "t";
SELECT count(*) as ab, thing as bd, "stuff" FROM "t";

View file

@ -1 +1 @@
SELECT count(*) as ab, thing as bd,"stuff" FROM "a","b";
SELECT count(*) as ab, thing as bd, "stuff" FROM "a", "b";

View file

@ -1 +1 @@
SELECT "a"."happy" as "a.happy","r"."fun" as "r.fun","q" FROM happiness as a INNER JOIN rainbows r on a.id = r.happy_id;
SELECT "a"."happy" as "a.happy", "r"."fun" as "r.fun", "q" FROM happiness as a INNER JOIN rainbows r on a.id = r.happy_id;

View file

@ -1 +1 @@
SELECT * FROM "a" GROUP BY id,name HAVING id <> 1,length(name, 'utf8') > 5;
SELECT * FROM "a" GROUP BY id, name HAVING id <> 1, length(name, 'utf8') > 5;

1
boil/_fixtures/08.sql Normal file
View file

@ -0,0 +1 @@
DELETE FROM thing happy, upset as "sad", "fun", thing as stuff, "angry" as mad WHERE id=$1 and $thing=$2 OR stuff=$3;

1
boil/_fixtures/09.sql Normal file
View file

@ -0,0 +1 @@
DELETE FROM "thing" "happy" WHERE id=$1 and $thing=$2 AND stuff=$3;

View file

@ -47,21 +47,21 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
if hasSelectCols && hasJoins && !hasModFunc {
selectColsWithAs := writeAsStatements(q)
// Don't identQuoteSlice - writeAsStatements does this
buf.WriteString(strings.Join(selectColsWithAs, `,`))
buf.WriteString(strings.Join(selectColsWithAs, ", "))
} else if hasSelectCols {
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.selectCols), `,`))
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.selectCols), ", "))
} else if hasJoins {
selectColsWithStars := writeStars(q)
buf.WriteString(strings.Join(selectColsWithStars, `,`))
buf.WriteString(strings.Join(selectColsWithStars, ", "))
} else {
buf.WriteByte('*')
}
if hasModFunc {
buf.WriteString(")")
buf.WriteByte(')')
}
fmt.Fprintf(buf, " FROM %s", strings.Join(strmangle.IdentQuoteSlice(q.from), `,`))
fmt.Fprintf(buf, " FROM %s", strings.Join(strmangle.IdentQuoteSlice(q.from), ", "))
for _, j := range q.joins {
if j.kind != JoinInner {
@ -74,16 +74,16 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
buf.WriteString(where)
if len(q.groupBy) != 0 {
fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ","))
fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ", "))
}
if len(q.having) != 0 {
fmt.Fprintf(buf, " HAVING %s", strings.Join(q.having, ","))
fmt.Fprintf(buf, " HAVING %s", strings.Join(q.having, ", "))
}
if len(q.orderBy) != 0 {
buf.WriteString(" ORDER BY ")
buf.WriteString(strings.Join(q.orderBy, `,`))
buf.WriteString(strings.Join(q.orderBy, ", "))
}
if q.limit != 0 {
@ -148,7 +148,7 @@ func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) {
buf := &bytes.Buffer{}
buf.WriteString("DELETE FROM ")
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.from), ","))
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.from), ", "))
where, args := whereClause(q)
buf.WriteString(where)

View file

@ -44,6 +44,22 @@ func TestBuildQuery(t *testing.T) {
groupBy: []string{"id", "name"},
having: []string{"id <> 1", "length(name, 'utf8') > 5"},
}, nil},
{&Query{
delete: true,
from: []string{"thing happy", `upset as "sad"`, "fun", "thing as stuff", `"angry" as mad`},
where: []where{
where{clause: "id=$1 and $thing=$2", orSeparator: true, args: []interface{}{}},
where{clause: "stuff=$3", args: []interface{}{}},
},
}, nil},
{&Query{
delete: true,
from: []string{`"thing" "happy"`},
where: []where{
where{clause: "id=$1 and $thing=$2", orSeparator: false, args: []interface{}{}},
where{clause: "stuff=$3", args: []interface{}{}},
},
}, nil},
}
for i, test := range tests {

View file

@ -184,7 +184,7 @@ func TestBind_InnerJoinSelect(t *testing.T) {
ret := sqlmock.NewRows([]string{"fun.id", "h.id"})
ret.AddRow(driver.Value(int64(10)), driver.Value(int64(11)))
ret.AddRow(driver.Value(int64(12)), driver.Value(int64(13)))
mock.ExpectQuery(`SELECT "fun"."id" as "fun.id","h"."id" as "h.id" FROM "fun" INNER JOIN happy as h on fun.happy_id = h.id;`).WillReturnRows(ret)
mock.ExpectQuery(`SELECT "fun"."id" as "fun.id", "h"."id" as "h.id" FROM "fun" INNER JOIN happy as h on fun.happy_id = h.id;`).WillReturnRows(ret)
SetExecutor(query, db)
err = query.Bind(&testResults)

View file

@ -1,5 +1,15 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
// 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
}
// One returns a single {{$varNameSingular}} record from the query.
func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
o := &{{$tableNameSingular}}{}
@ -14,14 +24,14 @@ 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))
}
// AllP returns all {{$tableNameSingular}} records from the query, and panics on error.
func (q {{$varNameSingular}}Query) AllP() {{$tableNameSingular}}Slice {
o, err := q.All()
if err != nil {
panic(boil.WrapErr(err))
}
return o
return o
}
// All returns all {{$tableNameSingular}} records from the query.
@ -36,14 +46,14 @@ func (q {{$varNameSingular}}Query) All() ({{$tableNameSingular}}Slice, error) {
return o, nil
}
// AllP returns all {{$tableNameSingular}} records from the query, and panics on error.
func (q {{$varNameSingular}}Query) AllP() {{$tableNameSingular}}Slice {
o, err := q.All()
if err != nil {
panic(boil.WrapErr(err))
}
// 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 o
return c
}
// Count returns the count of all {{$tableNameSingular}} records in the query.
@ -60,14 +70,14 @@ 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()
// Exists checks if the row exists in the table, and panics on error.
func (q {{$varNameSingular}}Query) ExistsP() bool {
e, err := q.Exists()
if err != nil {
panic(boil.WrapErr(err))
}
return c
return e
}
// Exists checks if the row exists in the table.
@ -84,13 +94,3 @@ func (q {{$varNameSingular}}Query) Exists() (bool, error) {
return count > 0, nil
}
// Exists checks if the row exists in the table, and panics on error.
func (q {{$varNameSingular}}Query) ExistsP() bool {
e, err := q.Exists()
if err != nil {
panic(boil.WrapErr(err))
}
return e
}

View file

@ -1,5 +1,14 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
// DeleteP deletes a single {{$tableNameSingular}} record with an executor.
// DeleteP will match against the primary key column to find the record to delete.
// Panics on error.
func (o *{{$tableNameSingular}}) DeleteP(exec boil.Executor) {
if err := o.Delete(exec); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteG deletes a single {{$tableNameSingular}} record.
// DeleteG will match against the primary key column to find the record to delete.
func (o *{{$tableNameSingular}}) DeleteG() error {
@ -44,24 +53,22 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error {
return nil
}
// DeleteP deletes a single {{$tableNameSingular}} record with an executor.
// DeleteP will match against the primary key column to find the record to delete.
// Panics on error.
func (o *{{$tableNameSingular}}) DeleteP(exec boil.Executor) {
if err := o.Delete(exec); err != nil {
panic(boil.WrapErr(err))
}
// DeleteAllP deletes all rows, and panics on error.
func (q {{$varNameSingular}}Query) DeleteAllP() {
if err := q.DeleteAll(); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteAll deletes all rows.
func (o {{$varNameSingular}}Query) DeleteAll() error {
if o.Query == nil {
// DeleteAll deletes all matching rows.
func (q {{$varNameSingular}}Query) DeleteAll() error {
if q.Query == nil {
return errors.New("{{.PkgName}}: no {{$varNameSingular}}Query provided for delete all")
}
boil.SetDelete(o.Query)
boil.SetDelete(q.Query)
_, err := boil.ExecQuery(o.Query)
_, err := boil.ExecQuery(q.Query)
if err != nil {
return fmt.Errorf("{{.PkgName}}: unable to delete all from {{.Table.Name}}: %s", err)
}
@ -69,13 +76,6 @@ 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, and panics on error.
func (o {{$tableNameSingular}}Slice) DeleteAllGP() {
if err := o.DeleteAllG(); err != nil {
@ -91,7 +91,14 @@ func (o {{$tableNameSingular}}Slice) DeleteAllG() error {
return o.DeleteAll(boil.GetDB())
}
// DeleteAll deletes all rows in the slice with an executor.
// DeleteAllP deletes all rows in the slice, using an executor, and panics on error.
func (o {{$tableNameSingular}}Slice) DeleteAllP(exec boil.Executor) {
if err := o.DeleteAll(exec); err != nil {
panic(boil.WrapErr(err))
}
}
// DeleteAll deletes all rows in the slice, using an executor.
func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error {
if o == nil {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
@ -124,10 +131,3 @@ func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error {
return nil
}
// DeleteAllP deletes all rows in the slice with an executor, and panics on error.
func (o {{$tableNameSingular}}Slice) DeleteAllP(exec boil.Executor) {
if err := o.DeleteAll(exec); err != nil {
panic(boil.WrapErr(err))
}
}