-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstmt.go
More file actions
90 lines (82 loc) · 1.8 KB
/
stmt.go
File metadata and controls
90 lines (82 loc) · 1.8 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package kiviksql
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"github.com/pingcap/parser/ast"
)
type stmt struct {
ast ast.StmtNode
}
func (s *stmt) Close() error {
return nil
}
func (s *stmt) Exec(_ []driver.Value) (driver.Result, error) {
return nil, nil
}
func (s *stmt) NumInput() int {
return 0
}
func (s *stmt) Query(_ []driver.Value) (driver.Rows, error) {
switch t := s.ast.(type) {
case *ast.SelectStmt:
_, err := mangoQuery(t)
return nil, err
default:
return nil, errors.New("Unsupported query")
}
}
func mangoQuery(sel *ast.SelectStmt) (map[string]interface{}, error) {
where := sel.Where
if where == nil {
return nil, nil
}
switch t := where.(type) {
case *ast.BinaryOperationExpr:
op, err := mangoOp(t.Op)
if err != nil {
return nil, err
}
var column, value string
L := t.L
R := t.R
// Ensure column name is on left
if _, ok := R.(*ast.ColumnNameExpr); ok {
L, R = R, L
}
if cn, ok := L.(*ast.ColumnNameExpr); ok {
column = cn.Name.String()
} else {
buf := &bytes.Buffer{}
where.Format(buf)
return nil, fmt.Errorf("no column name found in WHERE expression '%s'", buf.String())
}
if vl, ok := R.(ast.ValueExpr); ok {
v, err := json.Marshal(vl.GetValue())
if err != nil {
return nil, err
}
value = string(v)
} else {
buf := &bytes.Buffer{}
where.Format(buf)
return nil, fmt.Errorf("no value found in WHERE expression '%s'", buf.String())
}
return map[string]interface{}{
column: map[string]interface{}{
op: value,
},
}, nil
// fmt.Printf("XXXX: %s %s %v (%T)\n", column, op, value, R)
}
fmt.Println(where.Text())
buf := &bytes.Buffer{}
where.Format(buf)
fmt.Println(buf.String())
tp := where.GetType()
fmt.Println(tp)
fmt.Printf("where type %T", where)
return nil, nil
}