-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsqlbackend.go
48 lines (39 loc) · 1.1 KB
/
pgsqlbackend.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
package sqlbackend
import (
"context"
"github.com/stackql/psql-wire/pkg/sqldata"
)
type QueryCallback func(context.Context, string) (sqldata.ISQLResultStream, error)
type ISQLBackend interface {
HandleSimpleQuery(context.Context, string) (sqldata.ISQLResultStream, error)
SplitCompoundQuery(string) ([]string, error)
}
type SimpleSQLBackend struct {
simpleCallback QueryCallback
}
func (sb *SimpleSQLBackend) HandleSimpleQuery(ctx context.Context, query string) (sqldata.ISQLResultStream, error) {
return sb.simpleCallback(ctx, query)
}
func (sb *SimpleSQLBackend) SplitCompoundQuery(s string) ([]string, error) {
res := []string{}
var beg int
var inDoubleQuotes bool
for i := 0; i < len(s); i++ {
if s[i] == ';' && !inDoubleQuotes {
res = append(res, s[beg:i])
beg = i + 1
} else if s[i] == '"' {
if !inDoubleQuotes {
inDoubleQuotes = true
} else if i > 0 && s[i-1] != '\\' {
inDoubleQuotes = false
}
}
}
return append(res, s[beg:]), nil
}
func NewSimpleSQLBackend(simpleCallback QueryCallback) ISQLBackend {
return &SimpleSQLBackend{
simpleCallback: simpleCallback,
}
}