@@ -79,6 +79,7 @@ package db // import "upper.io/db.v2"
79
79
import (
80
80
"fmt"
81
81
"reflect"
82
+ "sort"
82
83
"time"
83
84
)
84
85
@@ -95,11 +96,13 @@ type Constraint interface {
95
96
Value () interface {}
96
97
}
97
98
98
- // Constraints interface represents an array or constraints, like "a = 1, b =
99
+ // Constraints interface represents an array of constraints, like "a = 1, b =
99
100
// 2, c = 3".
100
101
type Constraints interface {
101
102
// Constraints returns an array of constraints.
102
103
Constraints () []Constraint
104
+ // Keys returns the map keys always in the same order.
105
+ Keys () []interface {}
103
106
}
104
107
105
108
// Compound represents an statement that has one or many sentences joined by by
@@ -197,17 +200,40 @@ type Cond map[interface{}]interface{}
197
200
// Constraints returns each one of the Cond map records as a constraint.
198
201
func (c Cond ) Constraints () []Constraint {
199
202
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 ] ))
202
205
}
203
206
return z
204
207
}
205
208
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
+
206
232
// Sentences return each one of the map records as a compound.
207
233
func (c Cond ) Sentences () []Compound {
208
234
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 ] })
211
237
}
212
238
return z
213
239
}
0 commit comments