Repository navigation
Expand file tree
/
Copy pathdb.go
More file actions
289 lines (254 loc) · 7.3 KB
/
Copy pathdb.go
File metadata and controls
289 lines (254 loc) · 7.3 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package dev
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
)
type DB struct {
Sql *sql.DB
}
func StartDb(path, structure string) DB {
s := strings.Split(path, `db.db`)[0]
// if set.Dev {
// os.RemoveAll(s)
// }
if _, err := os.Stat(s); os.IsNotExist(err) {
os.MkdirAll(s, 0700)
}
// Best method for concurrent write
db, err := sql.Open(`sqlite3`, path+`?cache=shared&_journal_mode=WAL&pooling=true&_busy_timeout=5000`)
// this must have!but it not work when there is more then one connection
/*
db := dbs.Start(`data/db.db`, DBStructure())
dba := dbs.Start(`dataa/db.db`, DBStructure())
dba just return timeout
*/
db.SetMaxOpenConns(1)
if err != nil {
panic(err)
}
// Create new tables
_, err = db.Exec(structure)
if err != nil {
panic(err)
}
//
fmt.Println(`Database connected`)
return DB{Sql: db}
}
func (db DB) FetchRows(q string, args ...any) []byte {
return FetchRows(db.Sql, q, args...)
}
func (db DB) SendRows(w io.Writer, q string, args ...any) {
io.Writer.Write(w, FetchRows(db.Sql, q, args...))
}
func (db DB) FetchRow(q string, args ...any) []byte {
return FetchRow(db.Sql, q, args...)
}
func (db DB) SendRow(w io.Writer, q string, args ...any) {
io.Writer.Write(w, FetchRow(db.Sql, q, args...))
}
func (db DB) FetchRowMap(q string, args ...any) map[string]interface{} {
return aaa(db.Sql, q, args...)
}
func (db DB) Exec(q string, args ...any) (string, error) {
res, e := db.Sql.Exec(q, args...)
if e != nil {
fmt.Print(`Sql error: ` + e.Error() + q)
return "", e
}
jsn, e := json.Marshal(args)
if e != nil {
fmt.Println(e)
}
_, e = db.Sql.Exec(`insert into log(q,f,t)values(?,?,?)`, q, string(jsn), time.Now().Unix())
if e != nil {
fmt.Println(e)
}
var id int64
id, e = res.LastInsertId()
return strconv.FormatInt(id, 10), nil
}
func FetchRows(dbs *sql.DB, q string, args ...any) []byte {
rows, _ := dbs.Query(q, args...)
cols, _ := rows.Columns()
var data []map[string]interface{}
for rows.Next() {
// Create a slice of interface{}'s to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
return []byte(``)
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
m := make(map[string]interface{})
for i, colName := range cols {
val := columnPointers[i].(*interface{})
m[colName] = *val
}
data = append(data, m)
}
bolB, _ := json.Marshal(data)
if len(data) == 0 {
bolB = []byte(`[]`)
}
fmt.Print()
return bolB
}
func FetchRow(dbs *sql.DB, q string, args ...any) []byte {
rows, _ := dbs.Query(q, args...) // Note: Ignoring errors for brevity
cols, _ := rows.Columns()
for rows.Next() {
// Create a slice of interface{}'s to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
return nil
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
m := make(map[string]interface{})
for i, colName := range cols {
val := columnPointers[i].(*interface{})
m[colName] = *val
}
bolB, _ := json.Marshal(m)
return bolB
}
return nil
}
func FetchRowMap(dbs *sql.DB, q string, args ...any) map[string]interface{} {
rows, _ := dbs.Query(q, args...) // Note: Ignoring errors for brevity
cols, _ := rows.Columns()
for rows.Next() {
// Create a slice of interface{}'s to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
return nil
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
m := make(map[string]interface{})
for i, colName := range cols {
val := columnPointers[i].(*interface{})
m[colName] = *val
}
json.Marshal(m)
}
return nil
}
func aaa(db *sql.DB, q string, args ...any) map[string]interface{} {
rows, err := db.Query(q, args...)
if err != nil {
// Handle error
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
// Handle error
}
values := make([]interface{}, len(columns))
valuePtrs := make([]interface{}, len(columns))
for i := range values {
valuePtrs[i] = &values[i]
}
var m map[string]interface{}
for rows.Next() {
err := rows.Scan(valuePtrs...)
if err != nil {
// Handle error
}
m = make(map[string]interface{})
for i, col := range columns {
val := values[i]
// Handle null values if needed
m[col] = val
}
// Use the map
// fmt.Println(m)
}
// no results = m is nil
// fmt.Println(m == nil)
return m
// return nil
}
/*
func FetchRows(q string) []byte {
rows, _ := dbs.Query(q)
cols, _ := rows.Columns()
var data []map[string]interface{}
for rows.Next() {
// Create a slice of interface{}'s to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
return []byte(``)
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
m := make(map[string]interface{})
for i, colName := range cols {
val := columnPointers[i].(*interface{})
m[colName] = *val
}
data = append(data, m)
}
bolB, _ := json.Marshal(data)
return bolB
}
func FetchRow(q string) []byte {
rows, _ := dbs.Query(q) // Note: Ignoring errors for brevity
cols, _ := rows.Columns()
for rows.Next() {
// Create a slice of interface{}'s to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
return nil
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
m := make(map[string]interface{})
for i, colName := range cols {
val := columnPointers[i].(*interface{})
m[colName] = *val
}
bolB, _ := json.Marshal(m)
return bolB
}
return nil
}
*/