This commit is contained in:
Lunny Xiao 2021-07-23 14:50:34 +08:00
parent 38042fbc8c
commit ddb51cfe8e
2 changed files with 18 additions and 17 deletions

View File

@ -45,7 +45,7 @@ func TestSetExpr(t *testing.T) {
assert.EqualValues(t, 1, cnt) assert.EqualValues(t, 1, cnt)
var not = "NOT" var not = "NOT"
if testEngine.Dialect().URI().DBType == schemas.MSSQL { if testEngine.Dialect().URI().DBType == schemas.MSSQL || testEngine.Dialect().URI().DBType == schemas.DAMENG {
not = "~" not = "~"
} }
cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr)) cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))
@ -54,9 +54,9 @@ func TestSetExpr(t *testing.T) {
tableName := testEngine.TableName(new(UserExprIssue), true) tableName := testEngine.TableName(new(UserExprIssue), true)
cnt, err = testEngine.SetExpr("issue_id", cnt, err = testEngine.SetExpr("issue_id",
builder.Select("id"). builder.Select(testEngine.Quote("id")).
From(tableName). From(testEngine.Quote(tableName)).
Where(builder.Eq{"id": issue.Id})). Where(builder.Eq{testEngine.Quote("id"): issue.Id})).
ID(1). ID(1).
Update(new(UserExpr)) Update(new(UserExpr))
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -37,49 +37,50 @@ func TestBuilder(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
var cond Condition var cond Condition
has, err := testEngine.Where(builder.Eq{"col_name": "col1"}).Get(&cond) var q = testEngine.Quote
has, err := testEngine.Where(builder.Eq{q("col_name"): "col1"}).Get(&cond)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist") assert.Equal(t, true, has, "records should exist")
has, err = testEngine.Where(builder.Eq{"col_name": "col1"}. has, err = testEngine.Where(builder.Eq{q("col_name"): "col1"}.
And(builder.Eq{"op": OpEqual})). And(builder.Eq{q("op"): OpEqual})).
NoAutoCondition(). NoAutoCondition().
Get(&cond) Get(&cond)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist") assert.Equal(t, true, has, "records should exist")
has, err = testEngine.Where(builder.Eq{"col_name": "col1", "op": OpEqual, "value": "1"}). has, err = testEngine.Where(builder.Eq{q("col_name"): "col1", q("op"): OpEqual, q("value"): "1"}).
NoAutoCondition(). NoAutoCondition().
Get(&cond) Get(&cond)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist") assert.Equal(t, true, has, "records should exist")
has, err = testEngine.Where(builder.Eq{"col_name": "col1"}. has, err = testEngine.Where(builder.Eq{q("col_name"): "col1"}.
And(builder.Neq{"op": OpEqual})). And(builder.Neq{q("op"): OpEqual})).
NoAutoCondition(). NoAutoCondition().
Get(&cond) Get(&cond)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, false, has, "records should not exist") assert.Equal(t, false, has, "records should not exist")
var conds []Condition var conds []Condition
err = testEngine.Where(builder.Eq{"col_name": "col1"}. err = testEngine.Where(builder.Eq{q("col_name"): "col1"}.
And(builder.Eq{"op": OpEqual})). And(builder.Eq{q("op"): OpEqual})).
Find(&conds) Find(&conds)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist") assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0) conds = make([]Condition, 0)
err = testEngine.Where(builder.Like{"col_name", "col"}).Find(&conds) err = testEngine.Where(builder.Like{q("col_name"), "col"}).Find(&conds)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist") assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0) conds = make([]Condition, 0)
err = testEngine.Where(builder.Expr("col_name = ?", "col1")).Find(&conds) err = testEngine.Where(builder.Expr(q("col_name")+" = ?", "col1")).Find(&conds)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist") assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0) conds = make([]Condition, 0)
err = testEngine.Where(builder.In("col_name", "col1", "col2")).Find(&conds) err = testEngine.Where(builder.In(q("col_name"), "col1", "col2")).Find(&conds)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist") assert.EqualValues(t, 1, len(conds), "records should exist")
@ -91,8 +92,8 @@ func TestBuilder(t *testing.T) {
// complex condtions // complex condtions
var where = builder.NewCond() var where = builder.NewCond()
if true { if true {
where = where.And(builder.Eq{"col_name": "col1"}) where = where.And(builder.Eq{q("col_name"): "col1"})
where = where.Or(builder.And(builder.In("col_name", "col1", "col2"), builder.Expr("col_name = ?", "col1"))) where = where.Or(builder.And(builder.In(q("col_name"), "col1", "col2"), builder.Expr(q("col_name")+" = ?", "col1")))
} }
conds = make([]Condition, 0) conds = make([]Condition, 0)