2020-01-02 05:28:36 +00:00
|
|
|
package qgen
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-01-02 05:28:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MT struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
Type int
|
|
|
|
Contents string
|
2020-01-02 05:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func expectTokens(t *testing.T, whs []DBWhere, tokens ...MT) {
|
2022-02-21 03:32:53 +00:00
|
|
|
i := 0
|
|
|
|
for _, wh := range whs {
|
|
|
|
for _, expr := range wh.Expr {
|
|
|
|
if expr.Type != tokens[i].Type || expr.Contents != tokens[i].Contents {
|
|
|
|
t.Fatalf("token mismatch: %+v - %+v\n", expr, tokens[i])
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 05:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessWhere(t *testing.T) {
|
2022-02-21 03:32:53 +00:00
|
|
|
whs := processWhere("uid = ?")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenSub, "?"})
|
|
|
|
whs = processWhere("uid = 1")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "1"})
|
|
|
|
whs = processWhere("uid = 0")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "0"})
|
|
|
|
whs = processWhere("uid = '1'")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "1"})
|
|
|
|
whs = processWhere("uid = 't'")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "t"})
|
|
|
|
whs = processWhere("uid = ''")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, ""})
|
|
|
|
whs = processWhere("uid = '")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, ""})
|
2020-01-02 05:28:36 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
whs = processWhere("uid=?")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenSub, "?"})
|
|
|
|
whs = processWhere("uid=1")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "1"})
|
|
|
|
whs = processWhere("uid=0")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "0"})
|
|
|
|
whs = processWhere("uid=20")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "20"})
|
|
|
|
whs = processWhere("uid=uid+1")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenColumn, "uid"}, MT{TokenOp, "+"}, MT{TokenNumber, "1"})
|
|
|
|
whs = processWhere("uid='1'")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "1"})
|
|
|
|
whs = processWhere("uid='t'")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "t"})
|
2020-01-02 05:28:36 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
whs = processWhere("uid")
|
|
|
|
expectTokens(t, whs, MT{TokenColumn, "uid"})
|
2020-01-02 05:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMySQLBuildWhere(t *testing.T) {
|
2022-02-21 03:32:53 +00:00
|
|
|
a := &MysqlAdapter{Name: "mysql", Buffer: make(map[string]DBStmt)}
|
|
|
|
reap := func(wh, ex string) {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
a.buildWhere(wh, sb)
|
|
|
|
res := sb.String()
|
|
|
|
if res != ex {
|
|
|
|
t.Fatalf("build where mismatch: '%+v' - '%+v'\n", ex, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reap("uid = 0", " WHERE `uid`= 0 ")
|
|
|
|
reap("uid = '0'", " WHERE `uid`= '0'")
|
|
|
|
reap("uid=0", " WHERE `uid`= 0 ")
|
2020-01-02 05:28:36 +00:00
|
|
|
}
|