forked from tpotlog/unqlitego
-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.go
192 lines (150 loc) · 3.66 KB
/
database.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
package unqlitego
// #include <unqlite.h>
// #include <wrappers.h>
// #include <stdlib.h>
import "C"
import (
"runtime"
"unsafe"
)
// Database represents a UnQLite Database.
type Database struct {
conn *C.unqlite
}
// NewDatabase creates and initalizes a new UnQLite database connection.
func NewDatabase(filename string) (db *Database, err error) {
db = &Database{}
name := C.CString(filename)
defer C.free(unsafe.Pointer(name))
// Initialize Native Library
if !Info().IsInitialized() {
Info().Init()
}
res := C.unqlite_open(&db.conn, name, C.UNQLITE_OPEN_CREATE)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
if db.conn != nil {
runtime.SetFinalizer(db, (*Database).Close)
}
return
}
// Compile a JX9 Script into a Virtual Machine.
func (db *Database) Compile(jx9 string, vm *VM) (string, error) {
res := C.unqlite_compile(db.conn, C.CString(jx9), C.int(len(jx9)), &vm.vm)
if res != C.UNQLITE_OK {
if res == C.UNQLITE_COMPILE_ERR {
err := UnQLiteError(res)
// Error Log
elog := new(C.char)
// Error Message
emsg := C.extract_unqlite_log_error(db.conn, elog)
// Global Error Message
gmsg := C.GoString(emsg)
return gmsg, err
}
}
return "", nil
}
// Close will close the database connection.
func (db *Database) Close() (err error) {
if db.conn != nil {
res := C.unqlite_close(db.conn)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
db.conn = nil
}
return
}
// Store will store a new Key/Value pair in the database.
func (db *Database) Store(key, value []byte) (err error) {
var k, v unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
if len(value) > 0 {
v = unsafe.Pointer(&value[0])
}
res := C.unqlite_kv_store(db.conn,
k, C.int(len(key)),
v, C.unqlite_int64(len(value)))
if res == C.UNQLITE_OK {
return nil
}
return UnQLiteError(res)
}
// Append will write a new record into the database.
// If the record does not exists it will be created, else the new
// data is appended to the end of the old data.
func (db *Database) Append(key, value []byte) (err error) {
var k, v unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
if len(value) > 0 {
v = unsafe.Pointer(&value[0])
}
res := C.unqlite_kv_append(db.conn,
k, C.int(len(key)),
v, C.unqlite_int64(len(value)))
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Fetch a record from the database.
func (db *Database) Fetch(key []byte) (value []byte, err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
var n C.unqlite_int64
res := C.unqlite_kv_fetch(db.conn, k, C.int(len(key)), nil, &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
return
}
value = make([]byte, int(n))
res = C.unqlite_kv_fetch(db.conn, k, C.int(len(key)), unsafe.Pointer(&value[0]), &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Delete a record from the database.
func (db *Database) Delete(key []byte) (err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_delete(db.conn, k, C.int(len(key)))
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Begin will start a transaction.
func (db *Database) Begin() (err error) {
res := C.unqlite_begin(db.conn)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Commit will commit a transaction to the database.
func (db *Database) Commit() (err error) {
res := C.unqlite_commit(db.conn)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Rollback a transaction.
func (db *Database) Rollback() (err error) {
res := C.unqlite_rollback(db.conn)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}