-
Notifications
You must be signed in to change notification settings - Fork 153
/
vertica.go
165 lines (150 loc) · 3.95 KB
/
vertica.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
package sql
import (
"database/sql"
"fmt"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/values"
)
type VerticaRowReader struct {
Cursor *sql.Rows
columns []interface{}
columnTypes []flux.ColType
columnNames []string
}
func (m *VerticaRowReader) Next() bool {
next := m.Cursor.Next()
if next {
columnNames, err := m.Cursor.Columns()
if err != nil {
return false
}
m.columns = make([]interface{}, len(columnNames))
columnPointers := make([]interface{}, len(columnNames))
for i := 0; i < len(columnNames); i++ {
columnPointers[i] = &m.columns[i]
}
if err := m.Cursor.Scan(columnPointers...); err != nil {
return false
}
}
return next
}
func (m *VerticaRowReader) GetNextRow() ([]values.Value, error) {
row := make([]values.Value, len(m.columns))
for i, col := range m.columns {
switch col := col.(type) {
case int:
row[i] = values.NewInt(int64(col))
case uint:
row[i] = values.NewUInt(uint64(col))
case bool, int64, uint64, float64, string:
row[i] = values.New(col)
case time.Time:
row[i] = values.NewTime(values.ConvertTime(col))
case []uint8:
switch m.columnTypes[i] {
case flux.TInt:
newInt, err := UInt8ToInt64(col)
if err != nil {
return nil, err
}
row[i] = values.NewInt(newInt)
case flux.TFloat:
newFloat, err := UInt8ToFloat(col)
if err != nil {
return nil, err
}
row[i] = values.NewFloat(newFloat)
case flux.TTime:
t, err := time.Parse(layout, string(col))
if err != nil {
fmt.Print(err)
}
row[i] = values.NewTime(values.ConvertTime(t))
default:
row[i] = values.NewString(string(col))
}
case nil:
row[i] = values.NewNull(flux.SemanticType(m.columnTypes[i]))
default:
execute.PanicUnknownType(flux.TInvalid)
}
}
return row, nil
}
func (m *VerticaRowReader) InitColumnNames(n []string) {
m.columnNames = n
}
func (m *VerticaRowReader) InitColumnTypes(types []*sql.ColumnType) {
stringTypes := make([]flux.ColType, len(types))
for i := 0; i < len(types); i++ {
switch types[i].DatabaseTypeName() {
case "INT", "INTEGER", "BIGINT", "SMALLINT", "TINYINT", "INT2", "INT4", "INT8", "SERIAL2", "SERIAL4", "SERIAL8":
stringTypes[i] = flux.TInt
case "FLOAT", "FLOAT4", "FLOAT8":
stringTypes[i] = flux.TFloat
case "DATE", "TIME", "TIMESTAMP":
stringTypes[i] = flux.TTime
case "BOOL":
stringTypes[i] = flux.TBool
case "TEXT", "VARCHAR", "VARBINARY":
stringTypes[i] = flux.TString
default:
stringTypes[i] = flux.TString
}
}
m.columnTypes = stringTypes
}
func (m *VerticaRowReader) ColumnNames() []string {
return m.columnNames
}
func (m *VerticaRowReader) ColumnTypes() []flux.ColType {
return m.columnTypes
}
func (m *VerticaRowReader) SetColumns(i []interface{}) {
m.columns = i
}
func (m *VerticaRowReader) Close() error {
if err := m.Cursor.Err(); err != nil {
return err
}
return m.Cursor.Close()
}
func NewVerticaRowReader(r *sql.Rows) (execute.RowReader, error) {
reader := &VerticaRowReader{
Cursor: r,
}
cols, err := r.Columns()
if err != nil {
return nil, err
}
reader.InitColumnNames(cols)
types, err := r.ColumnTypes()
if err != nil {
return nil, err
}
reader.InitColumnTypes(types)
return reader, nil
}
// VerticaTranslateColumn translates flux colTypes into their corresponding Vertica column type
func VerticaColumnTranslateFunc() translationFunc {
c := map[string]string{
flux.TFloat.String(): "FLOAT",
flux.TInt.String(): "INTEGER",
flux.TUInt.String(): "INTEGER",
flux.TString.String(): "VARCHAR",
flux.TTime.String(): "TIMESTAMP",
flux.TBool.String(): "BOOL",
}
return func(f flux.ColType, colName string) (string, error) {
s, found := c[f.String()]
if !found {
return "", errors.Newf(codes.Internal, "Vertica does not support column type %s", f.String())
}
return doubleQuote(colName) + " " + s, nil
}
}