-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.go
170 lines (149 loc) · 4.14 KB
/
database.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
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/jackc/pgx"
_ "github.com/jackc/pgx/stdlib"
"github.com/cube2222/octosql/octosql"
"github.com/cube2222/octosql/physical"
"github.com/cube2222/octosql/plugins"
)
type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
Schema string `yaml:"schema"`
}
func (c *Config) Validate() error {
return nil
}
type testLogger struct {
}
func (t *testLogger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {
log.Printf("%s %s %+v", level, msg, data)
}
func connect(config *Config) (*pgx.ConnPool, error) {
pgxConfig := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: config.Host,
Port: uint16(config.Port),
User: config.User,
Database: config.Database,
Password: config.Password,
TLSConfig: nil,
},
MaxConnections: 128,
}
if os.Getenv("OCTOSQL_POSTGRES_QUERY_LOGGING") == "1" {
pgxConfig.ConnConfig.Logger = &testLogger{}
}
db, err := pgx.NewConnPool(pgxConfig)
if err != nil {
return nil, fmt.Errorf("couldn't open database: %w", err)
}
return db, nil
}
func Creator(ctx context.Context, configUntyped plugins.ConfigDecoder) (physical.Database, error) {
var cfg Config
if err := configUntyped.Decode(&cfg); err != nil {
return nil, err
}
return &Database{
Config: &cfg,
}, nil
}
type Database struct {
Config *Config
}
func (d *Database) ListTables(ctx context.Context) ([]string, error) {
panic("implement me")
}
func (d *Database) GetTable(ctx context.Context, name string, options map[string]string) (physical.DatasourceImplementation, physical.Schema, error) {
db, err := connect(d.Config)
if err != nil {
return nil, physical.Schema{}, fmt.Errorf("couldn't connect to database: %w", err)
}
if d.Config.Schema == "" {
return nil, physical.Schema{}, fmt.Errorf("`schema` configuration setting is required")
}
rows, err := db.QueryEx(ctx, "SELECT column_name, udt_name, is_nullable FROM information_schema.columns WHERE table_name = $1 AND table_schema = $2 ORDER BY ordinal_position", nil, name, d.Config.Schema)
if err != nil {
return nil, physical.Schema{}, fmt.Errorf("couldn't describe table: %w", err)
}
var descriptions [][]string
for rows.Next() {
desc := make([]string, 3)
if err := rows.Scan(&desc[0], &desc[1], &desc[2]); err != nil {
return nil, physical.Schema{}, fmt.Errorf("couldn't scan table description: %w", err)
}
descriptions = append(descriptions, desc)
}
if len(descriptions) == 0 {
return nil, physical.Schema{}, fmt.Errorf("table %s does not exist", name)
}
fields := make([]physical.SchemaField, 0, len(descriptions))
for i := range descriptions {
t, ok := getOctoSQLType(descriptions[i][1])
if !ok {
continue
}
if descriptions[i][2] == "YES" {
t = octosql.TypeSum(t, octosql.Null)
}
fields = append(fields, physical.SchemaField{
Name: descriptions[i][0],
Type: t,
})
}
return &impl{
config: d.Config,
table: name,
},
physical.Schema{
Fields: fields,
TimeField: -1,
NoRetractions: true,
},
nil
}
func getOctoSQLType(typename string) (octosql.Type, bool) {
if strings.HasPrefix(typename, "_") {
elementType, ok := getOctoSQLType(typename[1:])
if !ok {
return octosql.Type{}, false
}
return octosql.Type{
TypeID: octosql.TypeIDList,
List: struct {
Element *octosql.Type
}{Element: &elementType},
}, true
}
switch typename {
case "int", "int2", "int4", "int8":
return octosql.Int, true
case "text", "character", "varchar", "bpchar":
return octosql.String, true
case "real", "numeric", "float4", "float8":
return octosql.Float, true
case "bool", "boolean":
return octosql.Boolean, true
case "timestamptz", "timestamp", "timetz", "time":
return octosql.Time, true
case "jsonb":
// TODO: Handle me better.
return octosql.String, true
case "bytea":
// TODO: Handle me better.
return octosql.String, true
default:
log.Printf("unsupported postgres field type '%s' - ignoring column", typename)
return octosql.Null, false
// TODO: Support more types.
}
}