Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1df56bb

Browse files
author
José Carlos
authoredFeb 13, 2017
Merge pull request #335 from upper/issue-334
Keep the same order in db.Conf constraints
2 parents 0c07ed7 + 0feef1e commit 1df56bb

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed
 

‎db.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ package db // import "upper.io/db.v2"
7979
import (
8080
"fmt"
8181
"reflect"
82+
"sort"
8283
"time"
8384
)
8485

@@ -95,11 +96,13 @@ type Constraint interface {
9596
Value() interface{}
9697
}
9798

98-
// Constraints interface represents an array or constraints, like "a = 1, b =
99+
// Constraints interface represents an array of constraints, like "a = 1, b =
99100
// 2, c = 3".
100101
type Constraints interface {
101102
// Constraints returns an array of constraints.
102103
Constraints() []Constraint
104+
// Keys returns the map keys always in the same order.
105+
Keys() []interface{}
103106
}
104107

105108
// Compound represents an statement that has one or many sentences joined by by
@@ -197,17 +200,40 @@ type Cond map[interface{}]interface{}
197200
// Constraints returns each one of the Cond map records as a constraint.
198201
func (c Cond) Constraints() []Constraint {
199202
z := make([]Constraint, 0, len(c))
200-
for k, v := range c {
201-
z = append(z, NewConstraint(k, v))
203+
for _, k := range c.Keys() {
204+
z = append(z, NewConstraint(k, c[k]))
202205
}
203206
return z
204207
}
205208

209+
type condKeys []interface{}
210+
211+
func (ck condKeys) Len() int {
212+
return len(ck)
213+
}
214+
215+
func (ck condKeys) Less(i, j int) bool {
216+
return fmt.Sprintf("%v", ck[i]) < fmt.Sprintf("%v", ck[j])
217+
}
218+
219+
func (ck condKeys) Swap(i, j int) {
220+
ck[i], ck[j] = ck[j], ck[i]
221+
}
222+
223+
func (c Cond) Keys() []interface{} {
224+
keys := make(condKeys, 0, len(c))
225+
for k := range c {
226+
keys = append(keys, k)
227+
}
228+
sort.Sort(keys)
229+
return keys
230+
}
231+
206232
// Sentences return each one of the map records as a compound.
207233
func (c Cond) Sentences() []Compound {
208234
z := make([]Compound, 0, len(c))
209-
for k, v := range c {
210-
z = append(z, Cond{k: v})
235+
for _, k := range c.Keys() {
236+
z = append(z, Cond{k: c[k]})
211237
}
212238
return z
213239
}

‎lib/sqlbuilder/builder_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ func TestSelect(t *testing.T) {
179179
)
180180
}
181181

182+
{
183+
q := b.Select().From("artist").Where(
184+
db.Or(
185+
db.And(db.Cond{"a": 1, "b": 2, "c": 3}),
186+
db.And(db.Cond{"f": 6, "e": 5, "d": 4}),
187+
),
188+
)
189+
assert.Equal(
190+
`SELECT * FROM "artist" WHERE ((("a" = $1 AND "b" = $2 AND "c" = $3) OR ("d" = $4 AND "e" = $5 AND "f" = $6)))`,
191+
q.String(),
192+
)
193+
assert.Equal(
194+
[]interface{}{1, 2, 3, 4, 5, 6},
195+
q.Arguments(),
196+
)
197+
}
198+
182199
assert.Equal(
183200
`SELECT * FROM "artist" WHERE ((("id" = $1 OR "id" = $2 OR "id" IS NULL) OR ("name" = $3 OR "name" = $4)))`,
184201
b.Select().From("artist").Where(

0 commit comments

Comments
 (0)
Please sign in to comment.