-
Notifications
You must be signed in to change notification settings - Fork 345
/
trino.go
64 lines (61 loc) · 1.94 KB
/
trino.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
// Package trino defines and registers usql's Trino driver.
//
// See: https://github.com/trinodb/trino-go-client
package trino
import (
"context"
"io"
"regexp"
_ "github.com/trinodb/trino-go-client/trino" // DRIVER
"github.com/xo/usql/drivers"
"github.com/xo/usql/drivers/metadata"
infos "github.com/xo/usql/drivers/metadata/informationschema"
)
func init() {
endRE := regexp.MustCompile(`;?\s*$`)
newReader := func(db drivers.DB, opts ...metadata.ReaderOption) metadata.Reader {
ir := infos.New(
infos.WithPlaceholder(func(int) string { return "?" }),
infos.WithCustomClauses(map[infos.ClauseName]string{
infos.ColumnsColumnSize: "0",
infos.ColumnsNumericScale: "0",
infos.ColumnsNumericPrecRadix: "0",
infos.ColumnsCharOctetLength: "0",
}),
infos.WithFunctions(false),
infos.WithSequences(false),
infos.WithIndexes(false),
infos.WithConstraints(false),
infos.WithColumnPrivileges(false),
infos.WithUsagePrivileges(false),
)(db, opts...)
mr := &metaReader{
LoggingReader: metadata.NewLoggingReader(db, opts...),
}
return metadata.NewPluginReader(ir, mr)
}
drivers.Register("trino", drivers.Driver{
AllowMultilineComments: true,
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
},
Version: func(ctx context.Context, db drivers.DB) (string, error) {
var ver string
err := db.QueryRowContext(
ctx,
`SELECT node_version FROM system.runtime.nodes LIMIT 1`,
).Scan(&ver)
if err != nil {
return "", err
}
return "Trino " + ver, nil
},
NewMetadataReader: newReader,
NewMetadataWriter: func(db drivers.DB, w io.Writer, opts ...metadata.ReaderOption) metadata.Writer {
return metadata.NewDefaultWriter(newReader(db, opts...))(db, w)
},
Copy: drivers.CopyWithInsert(func(int) string { return "?" }),
})
}