Refactor Where clause, add to insert builder
* Fix Find template comparison, replace DeepEqual
This commit is contained in:
parent
5fc40c9226
commit
0bb847c0da
3 changed files with 46 additions and 20 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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}}
|
||||
|
|
|
@ -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}} {
|
||||
|
|
Loading…
Reference in a new issue