forked from chuckpreslar/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_visitor.go
More file actions
52 lines (43 loc) · 1.25 KB
/
postgres_visitor.go
File metadata and controls
52 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package codex
import (
"strconv"
)
type PostgresVisitor struct {
*ToSqlVisitor
Bindings int // Number of bindings used in parameterization
}
var _ VisitorInterface = (*PostgresVisitor)(nil)
// creates PostgresVisitor with PostgresCollector
func NewPostgresVisitor() *PostgresVisitor {
// can not use NewToSqlVisitor() because PostgresCollector needed instead of Collector.
return &PostgresVisitor{NewToSqlVisitor(NewPostgresCollector()), 0}
}
func (v *PostgresVisitor) Accept(o interface{}) (string, []interface{}, error) {
err := v.Visit(o, v)
return v.String(), v.Args(), err
}
// TODO obsolete
func (v *PostgresVisitor) VisitBinding(o *BindingNode, visitor VisitorInterface) (err error) {
v.Bindings += 1
v.AppendSqlByte('$')
v.AppendSqlStr(strconv.Itoa(v.Bindings))
return
}
func (v *PostgresVisitor) VisitLike(o *LikeNode, visitor VisitorInterface) (err error) {
err = visitor.Visit(o.Left, visitor)
if err != nil {
return
}
visitor.AppendSqlStr(" ILIKE ")
err = visitor.Visit(o.Right, visitor)
return
}
func (v *PostgresVisitor) VisitUnlike(o *UnlikeNode, visitor VisitorInterface) (err error) {
err = visitor.Visit(o.Left, visitor)
if err != nil {
return
}
visitor.AppendSqlStr(" NOT ILIKE ")
err = visitor.Visit(o.Right, visitor)
return
}