good morning!!!!

Skip to content
Snippets Groups Projects
Commit d6db544a authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

PostgreSQL: Adding Constraint() interface.

parent b03f75b3
No related branches found
No related tags found
No related merge requests found
...@@ -83,6 +83,14 @@ func whereValues(term interface{}) (where sqlgen.Where, args []interface{}) { ...@@ -83,6 +83,14 @@ func whereValues(term interface{}) (where sqlgen.Where, args []interface{}) {
for _, kk := range k { for _, kk := range k {
where = append(where, kk) where = append(where, kk)
} }
case db.Constraint:
k, v := conditionValues(t.Constraint())
args = append(args, v...)
for _, kk := range k {
where = append(where, kk)
}
default:
panic(fmt.Sprintf(db.ErrUnknownConditionType.Error(), reflect.TypeOf(t)))
} }
return where, args return where, args
......
...@@ -85,13 +85,21 @@ type testValuesStruct struct { ...@@ -85,13 +85,21 @@ type testValuesStruct struct {
Time time.Duration `field:"_time"` Time time.Duration `field:"_time"`
} }
type ItemWithNonNumericKey struct { type ItemWithKey struct {
Code string `db:"code"` Code string `db:"code"`
UserID string `db:"user_id"` UserID string `db:"user_id"`
SomeVal string `db:"some_val"` SomeVal string `db:"some_val"`
} }
func (item *ItemWithNonNumericKey) SetID(keys ...interface{}) error { func (item ItemWithKey) Constraint() db.Cond {
cond := db.Cond{
"code": item.Code,
"user_id": item.UserID,
}
return cond
}
func (item *ItemWithKey) SetID(keys ...interface{}) error {
if len(keys) == 2 { if len(keys) == 2 {
item.Code = string(keys[0].([]byte)) item.Code = string(keys[0].([]byte))
item.UserID = string(keys[1].([]byte)) item.UserID = string(keys[1].([]byte))
...@@ -1336,13 +1344,13 @@ func TestNonNumericKey(t *testing.T) { ...@@ -1336,13 +1344,13 @@ func TestNonNumericKey(t *testing.T) {
n := rand.Intn(100000) n := rand.Intn(100000)
item := &ItemWithNonNumericKey{ item := ItemWithKey{
"ABCDEF", "ABCDEF",
strconv.Itoa(n), strconv.Itoa(n),
"Some value", "Some value",
} }
if id, err = nonNumericKeys.Append(item); err != nil { if id, err = nonNumericKeys.Append(&item); err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -1356,6 +1364,23 @@ func TestNonNumericKey(t *testing.T) { ...@@ -1356,6 +1364,23 @@ func TestNonNumericKey(t *testing.T) {
t.Fatal(`Keys must match.`) t.Fatal(`Keys must match.`)
} }
// Using constraint interface.
res := nonNumericKeys.Find(ItemWithKey{Code: item.Code, UserID: item.UserID})
var item2 ItemWithKey
if item2.SomeVal == item.SomeVal {
t.Fatal(`Values must be different before query.`)
}
if err := res.One(&item2); err != nil {
t.Fatal(err)
}
if item2.SomeVal != item.SomeVal {
t.Fatal(`Values must be equal after query.`)
}
} }
// Attempts to add many different datatypes to a single row in a collection, // Attempts to add many different datatypes to a single row in a collection,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment