good morning!!!!

Skip to content
Snippets Groups Projects
Commit 6b3eea05 authored by Peter Kieltyka's avatar Peter Kieltyka
Browse files

Merge branch 'issue-79' into v2

parents 1ffab3d6 bc354737
Branches
Tags
No related merge requests found
...@@ -847,14 +847,62 @@ func TestFibonacci(t *testing.T) { ...@@ -847,14 +847,62 @@ func TestFibonacci(t *testing.T) {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
} }
// Find() with no arguments.
res = col.Find() res = col.Find()
total, err = res.Count() total, err = res.Count()
if total != 6 { if total != 6 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
} }
// Find() with empty db.Cond.
res1 := col.Find(db.Cond{})
total, err = res1.Count()
if total != 6 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
}
// Find() with explicit IS NULL
res2 := col.Find(db.Cond{"input IS": nil})
total, err = res2.Count()
if total != 0 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
}
// Find() with implicit IS NULL
res2a := col.Find(db.Cond{"input": nil})
total, err = res2a.Count()
if total != 0 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
}
// Find() with explicit = NULL
res2b := col.Find(db.Cond{"input =": nil})
total, err = res2b.Count()
if total != 0 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
}
// Find() with implicit IN
res3 := col.Find(db.Cond{"input": []int{1, 2, 3, 4}})
total, err = res3.Count()
if total != 3 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
}
// Find() with implicit NOT IN
res3a := col.Find(db.Cond{"input NOT IN": []int{1, 2, 3, 4}})
total, err = res3a.Count()
if total != 3 {
t.Fatalf(`%s: Unexpected count %d.`, wrapper, total)
}
var items []fibonacci var items []fibonacci
err = res.All(&items) err = res.All(&items)
...@@ -862,6 +910,10 @@ func TestFibonacci(t *testing.T) { ...@@ -862,6 +910,10 @@ func TestFibonacci(t *testing.T) {
t.Fatalf(`%s: %q`, wrapper, err) t.Fatalf(`%s: %q`, wrapper, err)
} }
if len(items) != 6 {
t.Fatalf(`Waiting for 6 items.`)
}
for _, item := range items { for _, item := range items {
switch item.Input { switch item.Input {
case 0: case 0:
......
...@@ -11,6 +11,9 @@ import ( ...@@ -11,6 +11,9 @@ import (
var ( var (
sqlPlaceholder = sqlgen.RawValue(`?`) sqlPlaceholder = sqlgen.RawValue(`?`)
sqlNull = sqlgen.RawValue(`NULL`) sqlNull = sqlgen.RawValue(`NULL`)
sqlIsOperator = `IS`
sqlInOperator = `IN`
sqlDefaultOperator = `=`
) )
type TemplateWithUtils struct { type TemplateWithUtils struct {
...@@ -122,8 +125,6 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen ...@@ -122,8 +125,6 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen
if len(chunks) > 1 { if len(chunks) > 1 {
columnValue.Operator = chunks[1] columnValue.Operator = chunks[1]
} else {
columnValue.Operator = tu.DefaultOperator
} }
switch value := value.(type) { switch value := value.(type) {
...@@ -143,14 +144,19 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen ...@@ -143,14 +144,19 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen
default: default:
v := tu.ToInterfaceArguments(value) v := tu.ToInterfaceArguments(value)
l := len(v) if v == nil {
if v == nil || l == 0 {
// Nil value given. // Nil value given.
columnValue.Value = sqlNull columnValue.Value = sqlNull
if columnValue.Operator == "" {
columnValue.Operator = sqlIsOperator
}
} else { } else {
if l > 1 { if len(v) > 1 {
// Array value given. // Array value given.
columnValue.Value = sqlgen.RawValue(fmt.Sprintf(`(?%s)`, strings.Repeat(`, ?`, len(v)-1))) columnValue.Value = sqlgen.RawValue(fmt.Sprintf(`(?%s)`, strings.Repeat(`, ?`, len(v)-1)))
if columnValue.Operator == "" {
columnValue.Operator = sqlInOperator
}
} else { } else {
// Single value given. // Single value given.
columnValue.Value = sqlPlaceholder columnValue.Value = sqlPlaceholder
...@@ -159,6 +165,15 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen ...@@ -159,6 +165,15 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen
} }
} }
// Using guessed operator if no operator was given.
if columnValue.Operator == "" {
if tu.DefaultOperator != "" {
columnValue.Operator = tu.DefaultOperator
} else {
columnValue.Operator = sqlDefaultOperator
}
}
ToColumnValues.ColumnValues = append(ToColumnValues.ColumnValues, &columnValue) ToColumnValues.ColumnValues = append(ToColumnValues.ColumnValues, &columnValue)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment