-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres.go
85 lines (72 loc) · 1.75 KB
/
postgres.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
package main
/*
#cgo LDFLAGS: -L/usr/local/lib -llua
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
*/
import "C"
import (
"database/sql"
"encoding/json"
"strconv"
"time"
"unsafe"
)
func map_postgres_row(L *C.lua_State, col string, value interface{}, columnType *sql.ColumnType) {
column := C.CString(col)
defer C.free(unsafe.Pointer(column))
C.lua_pushstring(L, column) // set column name
// map column value
if v, ok := value.(int64); ok == true {
C.lua_pushinteger(L, C.longlong(v))
}
if v, ok := value.(float64); ok == true {
C.lua_pushnumber(L, C.double(v))
}
if v, ok := value.(string); ok == true {
vp := C.CString(v)
defer C.free(unsafe.Pointer(vp))
C.lua_pushstring(L, vp)
}
if v, ok := value.(time.Time); ok == true {
vp := C.CString(v.Format(time.RFC3339))
defer C.free(unsafe.Pointer(vp))
C.lua_pushstring(L, vp)
}
if v, ok := value.(bool); ok == true {
if v == true {
C.lua_pushboolean(L, 1)
} else {
C.lua_pushboolean(L, 0)
}
}
if v, ok := value.([]byte); ok == true {
switch {
case columnType.DatabaseTypeName() == "JSONB":
fallthrough
case columnType.DatabaseTypeName() == "JSON":
data := make(map[string]interface{})
err := json.Unmarshal(v, &data)
error_handling(L, err)
go_to_lua(L, data)
case columnType.DatabaseTypeName() == "NUMERIC":
fallthrough
case columnType.DatabaseTypeName() == "DECIMAL":
val, err := strconv.ParseFloat(string(v), 8)
error_handling(L, err)
C.lua_pushnumber(L, C.double(val))
default:
C.lua_createtable(L, 0, 0)
for idx, b := range v {
C.lua_pushinteger(L, C.longlong(int64(idx+1)))
C.lua_pushinteger(L, C.longlong(b))
C.lua_settable(L, -3)
}
}
}
if value == nil {
C.lua_pushnil(L)
}
}