This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
l7g-p7e-untap.go
170 lines (125 loc) · 3.54 KB
/
l7g-p7e-untap.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 "os"
import "fmt"
import "log"
import "io/ioutil"
import "database/sql"
import _ "github.com/mattn/go-sqlite3"
import "github.com/abeconnelly/sloppyjson"
import "reflect"
import "time"
type LPUD struct {
DB *sql.DB
HTMLDir string
JSDir string
Port int
}
func (lpud *LPUD) Init(sql_fn string) error {
var err error
lpud.DB, err = sql.Open("sqlite3", "file:" + sql_fn + "?parseTime=true")
if err !=nil { return err }
return nil
}
func (lpud *LPUD) SQLExec(req string) ([][]string, error ) {
local_debug := false
rows,err := lpud.DB.Query(req)
if err!=nil { return nil, err }
cols,e := rows.Columns() ; _ = cols
if e!=nil { return nil, e }
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i:=0; i<len(cols); i++ {
columnPointers[i] = &columns[i]
}
res_str_array := [][]string{}
ti := time.Time{}
bt := []byte{}
res_str_array = append(res_str_array, cols)
for rows.Next() {
if err := rows.Scan(columnPointers...); err != nil {
return nil, err
}
strstr := []string{}
for _,raw := range columns {
if reflect.TypeOf(raw) == reflect.TypeOf(ti) {
t := raw.(time.Time)
s := t.String()
strstr = append(strstr, s)
} else if reflect.TypeOf(raw) == reflect.TypeOf(bt) {
var s = fmt.Sprintf("%s", raw.([]byte))
strstr = append(strstr, s)
} else {
var s = fmt.Sprintf("%v", raw)
strstr = append(strstr, s)
}
}
res_str_array = append(res_str_array, strstr)
}
//DEBUG
if local_debug { fmt.Printf(">>>>\n%v\n", res_str_array) }
return res_str_array, nil
}
func (lpud *LPUD) SQLExecS(req string) ([][]string, error ) {
local_debug := true
rows,err := lpud.DB.Query(req)
if err!=nil { return nil, err }
cols,e := rows.Columns() ; _ = cols
if e!=nil { return nil, e }
rawResult := make([][]byte, len(cols))
res_str_array := [][]string{}
// add column names to first row
//
res_str_array = append(res_str_array, cols)
dest := make([]interface{}, len(cols))
for i,_ := range rawResult {
dest[i] = &rawResult[i]
}
for rows.Next() {
err := rows.Scan(dest...)
if err!=nil { return nil,err }
result := make([]string, len(cols))
for i,raw := range rawResult {
raw_type := reflect.TypeOf(raw)
if raw==nil {
result[i] = "\n"
} else if el := raw_type.Elem() ; (el.PkgPath() == "time" || el.Name() == "Time" ) {
result[i] = fmt.Sprintf("%v", raw)
//ti := raw.(time.Time)
//result[i] = ti.String()
} else {
result[i] = string(raw)
//DEBUG
if local_debug {
fmt.Printf("raw>>>>\n%v\n", string(raw))
}
}
}
res_str_array = append(res_str_array, result)
}
//DEBUG
if local_debug {
fmt.Printf(">>>>\n%v\n", res_str_array)
}
return res_str_array, nil
}
func main() {
local_debug := true
lpud := LPUD{}
cfg_fn := "./l7g-p7e-config.json"
if len(os.Args)>1 { cfg_fn = os.Args[1] }
//config_s,e := ioutil.ReadFile("./l7g-p7e-config.json")
config_s,e := ioutil.ReadFile(cfg_fn)
if e!=nil { log.Fatal(e) }
config_json,e := sloppyjson.Loads(string(config_s))
if e!=nil { log.Fatal(e) }
err := lpud.Init(config_json.O["database"].S)
if err!=nil { log.Fatal(err) }
lpud.Port = int(config_json.O["port"].P)
lpud.HTMLDir = config_json.O["html-dir"].S
lpud.JSDir = config_json.O["js-dir"].S
if local_debug {
fmt.Printf(">> starting\n")
}
err = lpud.StartSrv()
if err!=nil { panic(err) }
}