forked from turbot/steampipe-postgres-fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
224 lines (195 loc) · 5.56 KB
/
helpers.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
/*
#cgo linux LDFLAGS: -Wl,-unresolved-symbols=ignore-all
#cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
#include "fdw_helpers.h"
*/
import "C"
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"regexp"
"strings"
"time"
"unsafe"
"github.com/turbot/go-kit/helpers"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
typeHelpers "github.com/turbot/go-kit/types"
"github.com/turbot/steampipe-postgres-fdw/types"
)
// convert a C string list into a go array
func CStringListToGoArray(values *C.List) []string {
ensureUnique := map[string]bool{}
targets := []string{}
for it := C.list_head(values); it != nil; it = C.lnext(values, it) {
val := C.cellGetValue(it)
s := C.GoString(C.valueString(val))
if !ensureUnique[s] {
targets = append(targets, s)
ensureUnique[s] = true
}
}
return targets
}
// HACK: env vars do not all get copied into the Go env vars so explicitly copy them
func SetEnvVars() {
var penv **C.char = C.environ
s := C.GoString(*C.environ)
for s != "" {
idx := strings.Index(s, "=")
key := s[:idx]
value := s[idx+1:]
os.Setenv(key, value)
penv = C.incStringPointer(penv)
s = C.GoString(*penv)
}
}
func GetFTableOptions(id types.Oid) types.Options {
// TODO - We need a sanitized form of the table name, e.g. all lowercase
f := C.GetForeignTable(C.Oid(id))
tmp := getOptions(f.options)
return tmp
}
func getOptions(opts *C.List) types.Options {
m := make(types.Options)
for it := C.list_head(opts); it != nil; it = C.lnext(opts, it) {
el := C.cellGetDef(it)
name := C.GoString(el.defname)
val := C.GoString(C.defGetString(el))
m[name] = val
}
return m
}
func BuildRelation(rel C.Relation) *types.Relation {
r := &types.Relation{
ID: types.Oid(rel.rd_id),
IsValid: fdwBool(rel.rd_isvalid),
Attr: buildTupleDesc(rel.rd_att),
Namespace: getNamespace(rel),
}
return r
}
func getNamespace(rel C.Relation) string {
schema := C.get_namespace_name(C.fdw_relationGetNamespace(rel))
return C.GoString(schema)
}
func fdwBool(b C.bool) bool {
return bool(b)
}
func fdwString(p unsafe.Pointer, n int) string {
b := C.GoBytes(p, C.int(n))
i := bytes.IndexByte(b, 0)
if i < 0 {
i = len(b)
}
return string(b[:i])
}
func buildTupleDesc(desc C.TupleDesc) *types.TupleDesc {
if desc == nil {
return nil
}
d := &types.TupleDesc{
TypeID: types.Oid(desc.tdtypeid),
TypeMod: int(desc.tdtypmod),
//HasOid: fdwBool(desc.tdhasoid),
Attrs: make([]types.Attr, 0, int(desc.natts)),
}
for i := 0; i < cap(d.Attrs); i++ {
p := C.fdw_tupleDescAttr(desc, C.int(i))
d.Attrs = append(d.Attrs, buildAttr(p))
}
return d
}
const nameLen = C.NAMEDATALEN
func buildAttr(attr *C.FormData_pg_attribute) (out types.Attr) {
out.Name = fdwString(unsafe.Pointer(&attr.attname.data[0]), nameLen)
out.Type = types.Oid(attr.atttypid)
out.Dimensions = int(attr.attndims)
out.NotNull = fdwBool(attr.attnotnull)
out.Dropped = fdwBool(attr.attisdropped)
return
}
// convert a value from C StringInfo buffer into a C Datum
func ValToDatum(val interface{}, cinfo *C.ConversionInfo, buffer C.StringInfo) (res C.Datum, err error) {
defer func() {
if r := recover(); r != nil {
err = helpers.ToError(r)
}
}()
// init an empty return result
datum := C.fdw_cStringGetDatum(C.CString(""))
// write value into C buffer
if err := valToBuffer(val, cinfo.atttypoid, buffer); err != nil {
return datum, err
}
if buffer.len >= 0 {
if cinfo.atttypoid == C.BYTEAOID ||
cinfo.atttypoid == C.TEXTOID ||
cinfo.atttypoid == C.VARCHAROID {
// Special case, since the value is already a byte string.
datum = C.fdw_pointerGetDatum(unsafe.Pointer(C.cstring_to_text_with_len(buffer.data, buffer.len)))
} else {
datum = C.InputFunctionCall(cinfo.attinfunc,
buffer.data,
cinfo.attioparam,
cinfo.atttypmod)
}
}
return datum, nil
}
// write the value into the C StringInfo buffer
func valToBuffer(val interface{}, oid C.Oid, buffer C.StringInfo) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
var valueString string
// handle json explicitly
if oid == C.JSONBOID {
valueString, err = jsonValueString(val)
if err != nil {
return err
}
} else {
valueString = typeHelpers.ToString(val)
}
C.resetStringInfo(buffer)
C.appendBinaryStringInfo(buffer, C.CString(valueString), C.int(len(valueString)))
return
}
func jsonValueString(val interface{}) (string, error) {
jsonBytes, err := json.Marshal(val)
if err != nil {
return "", err
}
valueString := string(jsonBytes)
// remove unicode null char "\u0000", UNLESS escaped, i.e."\\u0000"
if strings.Contains(valueString, `\u0000`) {
log.Printf("[TRACE] null unicode character detected in JSON value - removing if not escaped")
re := regexp.MustCompile(`((?:^|[^\\])(?:\\\\)*)(?:\\u0000)+`)
valueString = re.ReplaceAllString(valueString, "$1")
}
return valueString, nil
}
func TimeToPgTime(t time.Time) int64 {
// Postgres stores dates as microseconds since Jan 1, 2000
// https://www.postgresql.org/docs/9.1/datatype-datetime.html
ts := t.UTC()
epoch := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
its := ts.Sub(epoch) / 1000
return int64(its)
}
func PgTimeToTimestamp(t int64) (*timestamp.Timestamp, error) {
// Postgres stores dates as microseconds since Jan 1, 2000
// https://www.postgresql.org/docs/9.1/datatype-datetime.html
// convert to go time
epoch := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
time := epoch.Add(time.Duration(t*1000) * time.Nanosecond)
// now convert to protoibuf timestamp
return ptypes.TimestampProto(time)
}