Skip to content

Commit

Permalink
Merge pull request #53 from flycash/dev
Browse files Browse the repository at this point in the history
expression: RawExpr accepts arguments
  • Loading branch information
flycash authored Dec 9, 2021
2 parents 3f0fce2 + e67b8af commit 9e86e78
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
- [Force test and lint in pre-push](https://github.com/gotomicro/eql/pull/35)
- [Insert implementation ](https://github.com/gotomicro/eql/pull/38)
- [Delete implementation ](https://github.com/gotomicro/eql/pull/43)
- [having implementation ](https://github.com/gotomicro/eql/pull/45)
- [Having implementation ](https://github.com/gotomicro/eql/pull/45)
- [RawExpr accepts arguments ](https://github.com/gotomicro/eql/pull/53)

### Docs, Lint issues and Examples
- [Add examples and docs for Aggregate and Assign](https://github.com/gotomicro/eql/pull/50)
Expand Down
7 changes: 6 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (b *builder) parameter(arg interface{}) {
func (b *builder) buildExpr(expr Expr) error {
switch e := expr.(type) {
case RawExpr:
_, _ = b.buffer.WriteString(string(e))
b.buildRawExpr(e)
case Column:
cm, ok := b.meta.fieldMap[e.name]
if !ok {
Expand Down Expand Up @@ -121,6 +121,11 @@ func (b *builder) buildBinaryExpr(e binaryExpr) error {
return b.buildSubExpr(e.right)
}

func (b *builder) buildRawExpr(e RawExpr) {
_, _ = b.buffer.WriteString(e.raw)
b.args = append(b.args, e.args...)
}

func (b *builder) buildSubExpr(subExpr Expr) error {
switch r := subExpr.(type) {
case MathExpr:
Expand Down
33 changes: 31 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,39 @@ func ExampleDB_Insert() {
func ExampleDB_Select() {
db := New()
tm := &TestModel{}
query, _ := db.Select().From(tm).Build()
fmt.Printf("SQL: %s", query.SQL)
cases := []*Selector{
// case0: all columns are included
db.Select().From(tm),
// case1: only query specific columns
db.Select(Columns("Id", "Age")).From(tm),
// case2: using alias
db.Select(C("Id").As("my_id")).From(tm),
// case3: using aggregation function and alias
db.Select(Avg("Age").As("avg_age")).From(tm),
// case4: using raw expression
db.Select(Raw("COUNT(DISTINCT `age`) AS `age_cnt`")).From(tm),
}

for index, tc := range cases {
query, _ := tc.Build()
fmt.Printf("case%d:\n%s", index, query.string())
}
// Output:
// case0:
// SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model`;
// Args: []interface {}(nil)
// case1:
// SQL: SELECT `id`,`age` FROM `test_model`;
// Args: []interface {}(nil)
// case2:
// SQL: SELECT `id` AS `my_id` FROM `test_model`;
// Args: []interface {}(nil)
// case3:
// SQL: SELECT AVG(`age`) AS `avg_age` FROM `test_model`;
// Args: []interface {}(nil)
// case4:
// SQL: SELECT COUNT(DISTINCT `age`) AS `age_cnt` FROM `test_model`;
// Args: []interface {}(nil)
}

func ExampleDB_Update() {
Expand Down
14 changes: 10 additions & 4 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ type Expr interface {
}

// RawExpr uses string as Expr
type RawExpr string
type RawExpr struct {
raw string
args []interface{}
}

// Raw just take expr as Expr
func Raw(expr string) RawExpr {
return RawExpr(expr)
func Raw(expr string, args ...interface{}) RawExpr {
return RawExpr{
raw: expr,
args: args,
}
}

func (r RawExpr) expr() (string, error) {
return string(r), nil
return r.raw, nil
}

func (RawExpr) selected() {}
Expand Down
2 changes: 1 addition & 1 deletion predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (Predicate) expr() (string, error) {
// Not indicates "NOT"
func Not(p Predicate) Predicate {
return Predicate{
left: RawExpr(""),
left: Raw(""),
op: opNot,
right: p,
}
Expand Down
2 changes: 1 addition & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *Selector) buildSelectedList() error {
return err
}
case RawExpr:
_, _ = s.buffer.WriteString(string(expr))
s.buildRawExpr(expr)
}
}
return nil
Expand Down

0 comments on commit 9e86e78

Please sign in to comment.