forked from sqle/sqle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
221 lines (177 loc) · 4.36 KB
/
engine.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package sqle
import (
gosql "database/sql"
"database/sql/driver"
"errors"
"fmt"
"gopkg.in/sqle/sqle.v0/sql"
"gopkg.in/sqle/sqle.v0/sql/analyzer"
"gopkg.in/sqle/sqle.v0/sql/expression"
"gopkg.in/sqle/sqle.v0/sql/parse"
)
var (
ErrNotSupported = errors.New("feature not supported yet")
)
const (
DriverName = "sqle"
)
func init() {
gosql.Register(DriverName, defaultDriver)
}
type drv struct{}
var defaultDriver = &drv{}
func (d *drv) Open(name string) (driver.Conn, error) {
if name != "" {
return nil, fmt.Errorf("data source not found: %s", name)
}
e := DefaultEngine
return &session{Engine: e}, nil
}
// DefaultEngine is the default Engine instance, used when opening a connection
// to gitql:// when using database/sql.
var DefaultEngine = New()
// Engine is a SQL engine.
// It implements the standard database/sql/driver/Driver interface, so it can
// be registered as a database/sql driver.
type Engine struct {
Catalog *sql.Catalog
Analyzer *analyzer.Analyzer
}
// New creates a new Engine.
func New() *Engine {
c := sql.NewCatalog()
err := expression.RegisterDefaults(c)
if err != nil {
panic(err)
}
a := analyzer.New(c)
return &Engine{c, a}
}
// Open creates a new session for the engine and returns
// it as a driver.Conn.
//
// Name parameter is ignored.
func (e *Engine) Open(name string) (driver.Conn, error) {
return &session{Engine: e}, nil
}
// Query executes a query without attaching to any session.
func (e *Engine) Query(query string) (sql.Schema, sql.RowIter, error) {
parsed, err := parse.Parse(query)
if err != nil {
return nil, nil, err
}
analyzed, err := e.Analyzer.Analyze(parsed)
if err != nil {
return nil, nil, err
}
iter, err := analyzed.RowIter()
if err != nil {
return nil, nil, err
}
return analyzed.Schema(), iter, nil
}
func (e *Engine) AddDatabase(db sql.Database) {
e.Catalog.Databases = append(e.Catalog.Databases, db)
e.Analyzer.CurrentDatabase = db.Name()
}
// Session represents a SQL session.
// It implements the standard database/sql/driver/Conn interface.
type session struct {
*Engine
closed bool
//TODO: Current database
}
// Prepare returns a prepared statement, bound to this connection.
// Placeholders are not supported yet.
func (s *session) Prepare(query string) (driver.Stmt, error) {
if err := s.checkOpen(); err != nil {
return nil, err
}
return &stmt{session: s, query: query}, nil
}
// Close closes the session.
func (s *session) Close() error {
if err := s.checkOpen(); err != nil {
return err
}
s.closed = true
return nil
}
// Begin starts and returns a new transaction.
func (s *session) Begin() (driver.Tx, error) {
return nil, fmt.Errorf("transactions not supported")
}
func (s *session) checkOpen() error {
if s.closed {
return driver.ErrBadConn
}
return nil
}
type stmt struct {
*session
query string
closed bool
}
// Close closes the statement.
func (s *stmt) Close() error {
if err := s.checkOpen(); err != nil {
return err
}
s.closed = true
return nil
}
// NumInput returns the number of placeholder parameters.
// Always returns -1 since placeholders are not supported yet.
func (s *stmt) NumInput() int {
return -1
}
// Exec executes a query that doesn't return rows, such as an INSERT or UPDATE.
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return nil, ErrNotSupported
}
// Query executes a query that may return rows, such as a SELECT.
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
if len(args) > 0 {
return nil, ErrNotSupported
}
schema, iter, err := s.session.Engine.Query(s.query)
if err != nil {
return nil, err
}
return &rows{schema: schema, iter: iter}, nil
}
func (s *stmt) checkOpen() error {
if s.closed {
return driver.ErrBadConn
}
return nil
}
type rows struct {
schema sql.Schema
iter sql.RowIter
}
// Columns returns the names of the columns.
func (rs *rows) Columns() []string {
c := make([]string, len(rs.schema))
for i := 0; i < len(rs.schema); i++ {
c[i] = rs.schema[i].Name
}
return c
}
// Close closes the rows iterator.
func (rs *rows) Close() error {
return rs.iter.Close()
}
// Next populates the given array with the next row values.
// Returns io.EOF when there are no more values.
func (rs *rows) Next(dest []driver.Value) error {
r, err := rs.iter.Next()
if err != nil {
return err
}
for i := range dest {
f := rs.schema[i]
dest[i] = f.Type.Native(r[i])
}
return nil
}