Skip to content

Commit 57581d8

Browse files
committed
where func if
1 parent 2c1dd3b commit 57581d8

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

orm/query_where.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ func (q *Query[T]) WherePrimaryIfNotZero(val any) *Query[T] {
6464
}
6565
}
6666

67+
func (q *Query[T]) WhereFirstFieldIfNotZero(val any) *Query[T] {
68+
rval := reflect.ValueOf(val)
69+
if rval.IsZero() {
70+
return q
71+
} else {
72+
if rval.Kind() == reflect.Ptr {
73+
rval = rval.Elem()
74+
}
75+
if rval.Kind() != reflect.Struct {
76+
return q
77+
}
78+
if rval.NumField() == 0 {
79+
return q
80+
}
81+
return q.WherePrimary(rval.Field(0).Interface())
82+
}
83+
}
84+
6785
//short for OrWhere(primaryKey, vals...)
6886
func (q *Query[T]) OrWherePrimary(operator any, vals ...any) *Query[T] {
6987
//operator as vals
@@ -92,17 +110,25 @@ func (q *Query[T]) OrWhereBetween(column any, valLess, valGreat any) *Query[T] {
92110
})
93111
}
94112

95-
//"id=1"
96-
//&obj.id, 1
97-
//&obj.id, "=", 1
98-
func (q *Query[T]) WhereFunc(f func(where *Query[T]) *Query[T]) *Query[T] {
113+
func (q *Query[T]) WhereFunc(f func(wheref *Query[T]) *Query[T]) *Query[T] {
99114
return q.whereGroup(false, f)
100115
}
101116

102-
//"id=1"
103-
//&obj.id, 1
104-
//&obj.id, "=", 1
105-
func (q *Query[T]) OrWhereFunc(f func(where *Query[T]) *Query[T]) *Query[T] {
117+
func (q *Query[T]) OrWhereFunc(f func(wheref *Query[T]) *Query[T]) *Query[T] {
118+
return q.whereGroup(true, f)
119+
}
120+
121+
func (q *Query[T]) WhereFuncIf(ifval bool, f func(wheref *Query[T]) *Query[T]) *Query[T] {
122+
if ifval == false {
123+
return q
124+
}
125+
return q.whereGroup(false, f)
126+
}
127+
128+
func (q *Query[T]) OrWhereFuncIf(ifval bool, f func(wheref *Query[T]) *Query[T]) *Query[T] {
129+
if ifval == false {
130+
return q
131+
}
106132
return q.whereGroup(true, f)
107133
}
108134

0 commit comments

Comments
 (0)