Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in查询功能的实现 #102

Merged
merged 8 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func (b *builder) buildExpr(expr Expr) error {
if err := b.buildBinaryExpr(binaryExpr(e)); err != nil {
return err
}
case Ins:
if err := b.buildIns(e); err != nil {
return err
}
case nil:
default:
return errors.New("unsupported expr")
Expand Down Expand Up @@ -247,3 +251,16 @@ func (b *builder) buildSubExpr(subExpr Expr) error {
}
return nil
}

func (b *builder) buildIns(is Ins) error {
b.buffer.WriteByte('(')
for idx, inVal := range is.ins {
if idx > 0 {
b.buffer.WriteByte(',')
}
b.args = append(b.args, inVal)
b.buffer.WriteByte('?')
}
b.buffer.WriteByte(')')
return nil
}
27 changes: 27 additions & 0 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,30 @@ func Columns(cs ...string) columns {
cs: cs,
}
}

func (c Column) In(ins ...any) Predicate {
return Predicate{
left: c,
op: opIn,
right: Ins{
ins: ins,
},
}
}
func (c Column) NotIn(ins ...any) Predicate {
flycash marked this conversation as resolved.
Show resolved Hide resolved
return Predicate{
left: c,
op: opNotIN,
right: Ins{
ins: ins,
},
}
}

type Ins struct {
flycash marked this conversation as resolved.
Show resolved Hide resolved
ins []any
flycash marked this conversation as resolved.
Show resolved Hide resolved
}

func (i Ins) expr() (string, error) {
panic("implement me")
}
8 changes: 5 additions & 3 deletions predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ var (
// opMinus = op{symbol:"-", text: "-"}
opMulti = op{symbol: "*", text: "*"}
// opDiv = op{symbol:"/", text: "/"}
opAnd = op{symbol: "AND", text: " AND "}
opOr = op{symbol: "OR", text: " OR "}
opNot = op{symbol: "NOT", text: "NOT "}
opAnd = op{symbol: "AND", text: " AND "}
opOr = op{symbol: "OR", text: " OR "}
opNot = op{symbol: "NOT", text: "NOT "}
opIn = op{symbol: "IN", text: " IN "}
opNotIN = op{symbol: "NOT IN", text: " NOT IN "}
)

// Predicate will be used in Where Or Having
Expand Down
12 changes: 12 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ func TestSelectable(t *testing.T) {
builder: NewSelector[TestModel](db).Select(Columns("Id"), Columns("FirstName"), Avg("Age").As("avg_age")).From(&TestModel{}).GroupBy("FirstName").Having(C("Invalid").LT(20)),
wantErr: errs.NewInvalidFieldError("Invalid"),
},
{
flycash marked this conversation as resolved.
Show resolved Hide resolved
name: "in",
builder: NewSelector[TestModel](db).Select(Columns("Id")).From(&TestModel{}).Where(C("Id").In(1, 2, 3)),
wantSql: "SELECT `id` FROM `test_model` WHERE `id` IN (?,?,?);",
wantArgs: []interface{}{1, 2, 3},
},
{
name: "not in",
builder: NewSelector[TestModel](db).Select(Columns("Id")).From(&TestModel{}).Where(C("Id").NotIn(1, 2, 3)),
wantSql: "SELECT `id` FROM `test_model` WHERE `id` NOT IN (?,?,?);",
wantArgs: []interface{}{1, 2, 3},
},
flycash marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tc := range testCases {
Expand Down