Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Commit 7c4995a

Browse files
authored
add raw WhereClause support for condition (#11)
* add raw WhereClause support for condition * rename Raw* to WhereClare to distinguish between sqlbuilder whereclause and raw sql
1 parent 5631f95 commit 7c4995a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

condition/chain.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,10 @@ func (c Chain) Join(option sqlbuilder.JoinOption, table string, onExpr ...string
165165
func (c Chain) Build() []Condition {
166166
return c.conditions
167167
}
168+
169+
func (c Chain) WhereClause(whereClause *sqlbuilder.WhereClause) Chain {
170+
c.conditions = append(c.conditions, Condition{
171+
WhereClause: whereClause,
172+
})
173+
return c
174+
}

condition/condition.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type Condition struct {
6161

6262
// JoinCondition
6363
JoinCondition
64+
65+
WhereClause *sqlbuilder.WhereClause
6466
}
6567

6668
type JoinCondition struct {
@@ -116,6 +118,10 @@ func whereClause(conditions ...Condition) *sqlbuilder.WhereClause {
116118
if c.Skip {
117119
continue
118120
}
121+
if c.WhereClause != nil {
122+
clause.AddWhereClause(c.WhereClause)
123+
continue
124+
}
119125
if c.Or {
120126
if c.OrValuesFunc != nil {
121127
c.OrValues = c.OrValuesFunc()

condition/condition_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,37 @@ func TestWhereClause(t *testing.T) {
135135
fmt.Println(statement)
136136
fmt.Println(args)
137137
}
138+
139+
func TestRawWhereClause(t *testing.T) {
140+
sqlbuilder.DefaultFlavor = sqlbuilder.MySQL
141+
142+
rawWhereClause := sqlbuilder.NewWhereClause()
143+
144+
cond := sqlbuilder.NewCond()
145+
rawWhereClause.AddWhereExpr(cond.Args,
146+
cond.Or(
147+
cond.And(
148+
cond.EQ("a", 1),
149+
cond.EQ("b", 2),
150+
),
151+
cond.And(
152+
cond.EQ("c", 3),
153+
cond.EQ("d", 4),
154+
),
155+
))
156+
157+
cds := New(Condition{
158+
WhereClause: rawWhereClause,
159+
}, Condition{
160+
Field: "field_with_jzero",
161+
Value: 123,
162+
Operator: Equal,
163+
})
164+
165+
sb := sqlbuilder.NewSelectBuilder().Select("name", "age", "height").From("user")
166+
builder := Select(*sb, cds...)
167+
168+
sql, args := builder.Build()
169+
fmt.Println(sql)
170+
fmt.Println(args)
171+
}

0 commit comments

Comments
 (0)