Skip to content

Commit e90a1bc

Browse files
authored
refactor: add build method in condition (#170)
Signed-off-by: ZhangJian He <[email protected]>
1 parent 7698a66 commit e90a1bc

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

opengemini/query_builder.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (q *QueryBuilder) Build() *Query {
7171
if i > 0 {
7272
commandBuilder.WriteString(", ")
7373
}
74-
commandBuilder.WriteString(q.buildExpression(expr))
74+
commandBuilder.WriteString(expr.build())
7575
}
7676
} else {
7777
commandBuilder.WriteString("SELECT *")
@@ -90,7 +90,7 @@ func (q *QueryBuilder) Build() *Query {
9090
// Build the WHERE part
9191
if q.where != nil {
9292
commandBuilder.WriteString(" WHERE ")
93-
commandBuilder.WriteString(q.buildCondition(q.where))
93+
commandBuilder.WriteString(q.where.build())
9494
}
9595

9696
// Build the GROUP BY part
@@ -100,7 +100,7 @@ func (q *QueryBuilder) Build() *Query {
100100
if i > 0 {
101101
commandBuilder.WriteString(", ")
102102
}
103-
commandBuilder.WriteString(q.buildExpression(expr))
103+
commandBuilder.WriteString(expr.build())
104104
}
105105
}
106106

@@ -130,22 +130,3 @@ func (q *QueryBuilder) Build() *Query {
130130
Command: commandBuilder.String(),
131131
}
132132
}
133-
134-
func (q *QueryBuilder) buildExpression(expr Expression) string {
135-
return expr.build()
136-
}
137-
138-
func (q *QueryBuilder) buildCondition(cond Condition) string {
139-
switch c := cond.(type) {
140-
case *ComparisonCondition:
141-
return fmt.Sprintf(`"%s" %s '%v'`, c.Column, c.Operator, c.Value)
142-
case *CompositeCondition:
143-
var parts []string
144-
for _, condition := range c.Conditions {
145-
parts = append(parts, q.buildCondition(condition))
146-
}
147-
return fmt.Sprintf("(%s)", strings.Join(parts, fmt.Sprintf(" %s ", c.LogicalOperator)))
148-
default:
149-
return ""
150-
}
151-
}

opengemini/query_builder_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestQueryBuilderSelectWhereCondition(t *testing.T) {
6161

6262
query := qb.From("h2o_feet").Where(condition).Build()
6363

64-
expectedQuery := `SELECT * FROM "h2o_feet" WHERE "water_level" > '8'`
64+
expectedQuery := `SELECT * FROM "h2o_feet" WHERE "water_level" > 8`
6565

6666
require.Equal(t, expectedQuery, query.Command)
6767
}
@@ -79,7 +79,7 @@ func TestQueryBuilderSelectWithComplexWhereCondition(t *testing.T) {
7979

8080
query := qb.Select(NewFieldExpression("water_level")).From("h2o_feet").Where(finalCondition).Build()
8181

82-
expectedQuery := `SELECT "water_level" FROM "h2o_feet" WHERE ("location" <> 'santa_monica' AND ("water_level" < '-0.57' OR "water_level" > '9.95'))`
82+
expectedQuery := `SELECT "water_level" FROM "h2o_feet" WHERE ("location" <> 'santa_monica' AND ("water_level" < -0.57 OR "water_level" > 9.95))`
8383

8484
require.Equal(t, expectedQuery, query.Command)
8585
}

opengemini/query_condition.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
package opengemini
22

3-
type Condition interface{}
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type Condition interface {
9+
build() string
10+
}
411

512
type ComparisonCondition struct {
613
Column string
714
Operator ComparisonOperator
815
Value interface{}
916
}
1017

18+
func (c *ComparisonCondition) build() string {
19+
switch c.Value.(type) {
20+
case string:
21+
return fmt.Sprintf(`"%s" %s '%v'`, c.Column, c.Operator, c.Value)
22+
default:
23+
return fmt.Sprintf(`"%s" %s %v`, c.Column, c.Operator, c.Value)
24+
}
25+
}
26+
1127
func NewComparisonCondition(column string, operator ComparisonOperator, value interface{}) *ComparisonCondition {
1228
return &ComparisonCondition{
1329
Column: column,
@@ -21,6 +37,14 @@ type CompositeCondition struct {
2137
Conditions []Condition
2238
}
2339

40+
func (c *CompositeCondition) build() string {
41+
var parts []string
42+
for _, condition := range c.Conditions {
43+
parts = append(parts, condition.build())
44+
}
45+
return fmt.Sprintf("(%s)", strings.Join(parts, fmt.Sprintf(" %s ", c.LogicalOperator)))
46+
}
47+
2448
func NewCompositeCondition(logicalOperator LogicalOperator, conditions ...Condition) *CompositeCondition {
2549
return &CompositeCondition{
2650
LogicalOperator: logicalOperator,

0 commit comments

Comments
 (0)