Skip to content

Commit

Permalink
refactor: add build method in expression
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <[email protected]>
  • Loading branch information
shoothzj committed Aug 23, 2024
1 parent 32645ab commit 8d6a9ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
21 changes: 1 addition & 20 deletions opengemini/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,7 @@ func (q *QueryBuilder) Build() *Query {
}

func (q *QueryBuilder) buildExpression(expr Expression) string {
switch e := expr.(type) {
case *FieldExpression:
return `"` + e.Field + `"`
case *FunctionExpression:
var args []string
for _, arg := range e.Arguments {
args = append(args, q.buildExpression(arg))
}
return fmt.Sprintf("%s(%s)", e.Function, strings.Join(args, ", "))
case *ConstantExpression:
return fmt.Sprintf("%v", e.Value)
case *ArithmeticExpression:
var operandStrings []string
for _, operand := range e.Operands {
operandStrings = append(operandStrings, q.buildExpression(operand))
}
return fmt.Sprintf("(%s)", strings.Join(operandStrings, fmt.Sprintf(" %s ", e.Operator)))
default:
return ""
}
return expr.build()
}

func (q *QueryBuilder) buildCondition(cond Condition) string {
Expand Down
33 changes: 32 additions & 1 deletion opengemini/query_expression.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package opengemini

type Expression interface{}
import (
"fmt"
"strings"
)

type Expression interface {
build() string
}

type ConstantExpression struct {
Value interface{}
}

func (c *ConstantExpression) build() string {
return fmt.Sprintf("%v", c.Value)
}

func NewConstantExpression(value interface{}) *ConstantExpression {
return &ConstantExpression{
Value: value,
Expand All @@ -18,6 +29,10 @@ type FieldExpression struct {
Field string
}

func (f *FieldExpression) build() string {
return `"` + f.Field + `"`
}

func NewFieldExpression(field string) *FieldExpression {
return &FieldExpression{
Field: field,
Expand All @@ -29,6 +44,14 @@ type FunctionExpression struct {
Arguments []Expression
}

func (f *FunctionExpression) build() string {
var args []string
for _, arg := range f.Arguments {
args = append(args, arg.build())
}
return fmt.Sprintf("%s(%s)", f.Function, strings.Join(args, ", "))
}

func NewFunctionExpression(function FunctionEnum, arguments ...Expression) *FunctionExpression {
return &FunctionExpression{
Function: function,
Expand All @@ -41,6 +64,14 @@ type ArithmeticExpression struct {
Operands []Expression
}

func (a *ArithmeticExpression) build() string {
var operandStrings []string
for _, operand := range a.Operands {
operandStrings = append(operandStrings, operand.build())
}
return fmt.Sprintf("(%s)", strings.Join(operandStrings, fmt.Sprintf(" %s ", a.Operator)))
}

func NewArithmeticExpression(operator ArithmeticOperator, operands ...Expression) *ArithmeticExpression {
return &ArithmeticExpression{
Operator: operator,
Expand Down

0 comments on commit 8d6a9ea

Please sign in to comment.