forked from xormplus/xorm
/
pk.go
98 lines (83 loc) · 2.88 KB
/
pk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package statements
import (
"fmt"
"reflect"
"github.com/MasterYang7/xorm/schemas"
"github.com/xormplus/builder"
)
var (
ptrPkType = reflect.TypeOf(&schemas.PK{})
pkType = reflect.TypeOf(schemas.PK{})
stringType = reflect.TypeOf("")
intType = reflect.TypeOf(int64(0))
uintType = reflect.TypeOf(uint64(0))
)
// ErrIDConditionWithNoTable represents an error there is no reference table with an ID condition
type ErrIDConditionWithNoTable struct {
ID schemas.PK
}
func (err ErrIDConditionWithNoTable) Error() string {
return fmt.Sprintf("ID condition %#v need reference table", err.ID)
}
// IsIDConditionWithNoTableErr return true if the err is ErrIDConditionWithNoTable
func IsIDConditionWithNoTableErr(err error) bool {
_, ok := err.(ErrIDConditionWithNoTable)
return ok
}
// ID generate "where id = ? " statement or for composite key "where key1 = ? and key2 = ?"
func (statement *Statement) ID(id interface{}) *Statement {
switch t := id.(type) {
case *schemas.PK:
statement.idParam = *t
case schemas.PK:
statement.idParam = t
case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
statement.idParam = schemas.PK{id}
default:
idValue := reflect.ValueOf(id)
idType := idValue.Type()
switch idType.Kind() {
case reflect.String:
statement.idParam = schemas.PK{idValue.Convert(stringType).Interface()}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
statement.idParam = schemas.PK{idValue.Convert(intType).Interface()}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
statement.idParam = schemas.PK{idValue.Convert(uintType).Interface()}
case reflect.Slice:
if idType.ConvertibleTo(pkType) {
statement.idParam = idValue.Convert(pkType).Interface().(schemas.PK)
}
case reflect.Ptr:
if idType.ConvertibleTo(ptrPkType) {
statement.idParam = idValue.Convert(ptrPkType).Elem().Interface().(schemas.PK)
}
}
}
if statement.idParam == nil {
statement.LastError = fmt.Errorf("ID param %#v is not supported", id)
}
return statement
}
// ProcessIDParam handles the process of id condition
func (statement *Statement) ProcessIDParam() error {
if statement.idParam == nil {
return nil
}
if statement.RefTable == nil {
return ErrIDConditionWithNoTable{statement.idParam}
}
if len(statement.RefTable.PrimaryKeys) != len(statement.idParam) {
return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d",
len(statement.RefTable.PrimaryKeys),
len(statement.idParam),
)
}
for i, col := range statement.RefTable.PKColumns() {
var colName = statement.colName(col, statement.TableName())
statement.cond = statement.cond.And(builder.Eq{colName: statement.idParam[i]})
}
return nil
}