-
Notifications
You must be signed in to change notification settings - Fork 5
/
mapping.go
169 lines (156 loc) · 4.8 KB
/
mapping.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
package main
import (
"fmt"
"log"
"math"
"net"
"strings"
"time"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"go.riyazali.net/sqlite"
"google.golang.org/protobuf/types/known/timestamppb"
)
type SQLiteColumn struct {
Name string
Type string
}
type SQLiteColumns []SQLiteColumn
func (s SQLiteColumns) DeclarationString() string {
var out []string
for _, c := range s {
out = append(out, fmt.Sprintf("\"%s\" %s", c.Name, c.Type))
}
return strings.Join(out, ", ")
}
// getPluginOperator converts a sqlite.ConstraintOp to a QualOperator
func getPluginOperator(op sqlite.ConstraintOp) *QualOperator {
log.Println("[TRACE] getPluginOperator start", op)
defer log.Println("[TRACE] getPluginOperator end", op)
cost := &QualOperator{
Op: "NOOP",
Cost: math.MaxFloat64,
}
switch op {
case sqlite.INDEX_CONSTRAINT_EQ:
cost.Op = "="
cost.Cost = 1
case sqlite.INDEX_CONSTRAINT_GT:
cost.Op = ">"
cost.Cost = 10
case sqlite.INDEX_CONSTRAINT_GE:
cost.Op = ">="
cost.Cost = 10
case sqlite.INDEX_CONSTRAINT_LE:
cost.Op = "<="
cost.Cost = 10
case sqlite.INDEX_CONSTRAINT_LT:
cost.Op = "<"
cost.Cost = 10
// we should extend this to include LIKE, GLOB, REGEXP, MATCH and others
}
return cost
}
// getSQLiteColumnsFromTableSchema converts a proto.TableSchema to a SQLiteColumns
// which can be used to create a SQLite table
func getSQLiteColumnsFromTableSchema(ts *proto.TableSchema) SQLiteColumns {
cols := ts.Columns
var out SQLiteColumns
for _, col := range cols {
out = append(out, SQLiteColumn{Name: col.Name, Type: getMappedType(col.Type)})
}
return out
}
// getMappedType converts a proto.ColumnType to a SQLite type
func getMappedType(in proto.ColumnType) string {
switch in {
case proto.ColumnType_BOOL, proto.ColumnType_INT:
return "INT"
case proto.ColumnType_DOUBLE:
return "FLOAT"
default:
// everything else is a string as far as SQLite is concerned
return "TEXT"
}
}
// getMappedQualValue converts a sqlite.Value to a proto.QualValue
// based on the type of the column definition of the qual
func getMappedQualValue(v sqlite.Value, qual *Qual) (*proto.QualValue, error) {
log.Println("[DEBUG] getMappedQualValue", v, qual)
defer log.Println("[DEBUG] end getMappedQualValue", v, qual)
switch v.Type() {
case sqlite.SQLITE_INTEGER:
return getMappedIntValue(v.Int64(), qual)
case sqlite.SQLITE_TEXT:
return getMappedStringValue(v.Text(), qual)
case sqlite.SQLITE_FLOAT:
return &proto.QualValue{Value: &proto.QualValue_DoubleValue{DoubleValue: v.Float()}}, nil
case sqlite.SQLITE_NULL:
return &proto.QualValue{Value: nil}, nil
default:
// default to a string
return &proto.QualValue{Value: &proto.QualValue_StringValue{StringValue: v.Text()}}, nil
}
}
// getMappedStringValue converts a string to a proto.QualValue
// based on the type of the column definition of the qual
func getMappedStringValue(v string, q *Qual) (*proto.QualValue, error) {
log.Println("[DEBUG] getMappedStringValue", v, q)
defer log.Println("[DEBUG] end getMappedStringValue", v, q)
switch q.ColumnDefinition.GetType() {
case proto.ColumnType_IPADDR, proto.ColumnType_INET:
ip := net.ParseIP(v)
if ip != nil {
return &proto.QualValue{
Value: &proto.QualValue_InetValue{
InetValue: &proto.Inet{
Addr: ip.String(),
},
},
}, nil
}
return nil, fmt.Errorf("could not parse '%s' as IP ADDR", v)
case proto.ColumnType_CIDR:
_, _, err := net.ParseCIDR(v)
if err == nil {
return nil, err
}
return &proto.QualValue{
Value: &proto.QualValue_InetValue{
InetValue: &proto.Inet{
Cidr: v,
},
},
}, nil
case proto.ColumnType_LTREE:
return &proto.QualValue{Value: &proto.QualValue_LtreeValue{LtreeValue: v}}, nil
case proto.ColumnType_JSON:
return &proto.QualValue{Value: &proto.QualValue_JsonbValue{JsonbValue: v}}, nil
case proto.ColumnType_DATETIME, proto.ColumnType_TIMESTAMP:
timestamp, err := time.Parse(SQLITE_TIMESTAMP_FORMAT, v)
if err != nil {
// try parsing with the DATEONLY format
timestamp, err = time.Parse(SQLITE_DATEONLY_FORMAT, v)
if err != nil {
return nil, err
}
}
return &proto.QualValue{
Value: &proto.QualValue_TimestampValue{
TimestampValue: timestamppb.New(timestamp),
},
}, nil
}
return &proto.QualValue{Value: &proto.QualValue_StringValue{StringValue: v}}, nil
}
// getMappedIntValue converts an int64 to a proto.QualValue
// based on the type of the column definition of the qual
func getMappedIntValue(v int64, q *Qual) (*proto.QualValue, error) {
log.Println("[DEBUG] getMappedIntValue", v, q)
defer log.Println("[DEBUG] end getMappedIntValue", v, q)
switch q.ColumnDefinition.GetType() {
case proto.ColumnType_BOOL:
return &proto.QualValue{Value: &proto.QualValue_BoolValue{BoolValue: v != 0}}, nil
default:
return &proto.QualValue{Value: &proto.QualValue_Int64Value{Int64Value: v}}, nil
}
}