diff --git a/boil/query.go b/boil/query.go index 78b1f89..e7f6e34 100644 --- a/boil/query.go +++ b/boil/query.go @@ -73,31 +73,21 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) { buf.WriteString(" FROM ") fmt.Fprintf(buf, `"%s"`, q.table) + where, args := whereClause(q) + buf.WriteString(where) + buf.WriteByte(';') - return buf, []interface{}{} + return buf, args } func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) { buf := &bytes.Buffer{} - var args []interface{} buf.WriteString("DELETE FROM ") fmt.Fprintf(buf, `"%s"`, q.table) - if len(q.where) > 0 { - buf.WriteString(" WHERE ") - for i := 0; i < len(q.where); i++ { - buf.WriteString(fmt.Sprintf("%s", q.where[i].clause)) - args = append(args, q.where[i].args...) - if i != len(q.where)-1 { - if q.where[i].orSeperator { - buf.WriteString(" OR ") - } else { - buf.WriteString(" AND ") - } - } - } - } + where, args := whereClause(q) + buf.WriteString(where) buf.WriteByte(';') @@ -203,3 +193,25 @@ func SetHaving(q *Query, clause string) { func SetLimit(q *Query, limit int) { q.limit = limit } + +func whereClause(q *Query) (string, []interface{}) { + buf := &bytes.Buffer{} + var args []interface{} + + if len(q.where) > 0 { + buf.WriteString(" WHERE ") + for i := 0; i < len(q.where); i++ { + buf.WriteString(fmt.Sprintf("%s", q.where[i].clause)) + args = append(args, q.where[i].args...) + if i != len(q.where)-1 { + if q.where[i].orSeperator { + buf.WriteString(" OR ") + } else { + buf.WriteString(" AND ") + } + } + } + } + + return buf.String(), args +} diff --git a/templates_test/find.tpl b/templates_test/find.tpl index 8e48959..c9aa2d3 100644 --- a/templates_test/find.tpl +++ b/templates_test/find.tpl @@ -24,10 +24,25 @@ func Test{{$tableNamePlural}}Find(t *testing.T) { for i := 0; i < len(j); i++ { j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}}) - // Compare saved objects from earlier to the found objects - if !reflect.DeepEqual(j[i], o[i]) { - t.Errorf("Expected j[%d] to match o[%d], got:\n\nj: #%v\n\no:#%v\n\n", i, i, j[i], o[i]) + {{range $key, $value := .Table.Columns}} + {{if eq $value.Type "null.Time"}} + if o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") { + t.Errorf("%d) Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006")) } + {{else if eq $value.Type "time.Time"}} + if o[i].{{titleCase $value.Name}}.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Format("02/01/2006") { + t.Errorf("%d) Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Format("02/01/2006")) + } + {{else if eq $value.Type "[]byte"}} + if !byteSliceEqual(o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) { + t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) + } + {{else}} + if j[i].{{titleCase $value.Name}} != o[i].{{titleCase $value.Name}} { + t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) + } + {{end}} + {{end}} } {{if hasPrimaryKey .Table.PKey}} diff --git a/templates_test/finishers.tpl b/templates_test/finishers.tpl index 0261da7..1787d42 100644 --- a/templates_test/finishers.tpl +++ b/templates_test/finishers.tpl @@ -88,7 +88,6 @@ func Test{{$tableNamePlural}}All(t *testing.T) { {{else if eq $value.Type "[]byte"}} if !byteSliceEqual(o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) { t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) - } {{else}} if j[i].{{titleCase $value.Name}} != o[i].{{titleCase $value.Name}} {