forked from martti/gomdbtools
/
mdbtools.go
287 lines (231 loc) · 6.35 KB
/
mdbtools.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
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
package mdbtools
// #cgo pkg-config: libmdb
// #include <mdbtools.h>
// MdbCatalogEntry* _go_ptr_catalog(GPtrArray *array, guint index) {
// return (MdbCatalogEntry *)g_ptr_array_index(array, index);
// }
// MdbColumn* _go_ptr_col(GPtrArray *array, guint index) {
// return (MdbColumn *)g_ptr_array_index(array, index);
// }
// MdbIndex* _go_ptr_idx(GPtrArray *array, guint index) {
// return (MdbIndex *)g_ptr_array_index(array, index);
// }
import "C"
import (
"errors"
"fmt"
"runtime"
"strings"
"sync"
"unsafe"
)
type MDB struct {
dbfile string
mdb *C.MdbHandle
mu sync.Mutex
}
type MDBTableDef struct {
table *C.MdbTableDef
num_cols int
bound_values [][]byte
bound_lengths []int
column_names []string
}
type MDBColumn struct {
name string
Type int
size int
prec int
scale int
isFixed bool
}
type MDBIndex struct {
num int
name string
Type int
rows int
keys int
flags int
keyColNum []int
keyColOrder []int
}
type MapTableRow map[string]string
type MapTable []MapTableRow
type MapDB map[string] MapTable
func NewMDB(filename string) (*MDB, error) {
dbfile := C.CString(filename)
defer C.free(unsafe.Pointer(dbfile))
var mdbh *C.MdbHandle = C.mdb_open(dbfile, C.MDB_NOFLAGS)
if mdbh == nil {
return nil, fmt.Errorf("mdb: error opening %s", filename)
}
mdb := &MDB{
dbfile: filename,
mdb: mdbh,
}
runtime.SetFinalizer(mdb, (*MDB).Close)
return mdb, nil
}
func (db *MDB) Close() error {
var err error
C.mdb_close(db.mdb)
return err
}
func (db *MDB) Version() int {
return int(db.mdb.f.jet_version)
}
func (db *MDB) Tables() ([]string, error) {
var entry *C.MdbCatalogEntry
var tables []string
// tables := make([]string, int(db.mdb.num_catalog))
// append(tables, table)
rv := C.mdb_read_catalog(db.mdb, C.MDB_TABLE)
if rv == nil {
return nil, errors.New("mdb: error reading table catalog")
}
for i := 0; i < int(db.mdb.num_catalog); i++ {
entry = C._go_ptr_catalog(db.mdb.catalog, C.guint(i))
name := C.GoString(&entry.object_name[0])
if strings.HasPrefix(name, "MSys") {
continue
}
tables = append(tables, name)
}
return tables, nil
}
func (db *MDB) TableOpen(tableName string) (*MDBTableDef, error) {
var entry *C.MdbCatalogEntry
var col *C.MdbColumn
tableDef := &MDBTableDef{}
rv := C.mdb_read_catalog(db.mdb, C.MDB_TABLE)
if rv == nil {
return nil, errors.New("mdb: error reading table catalog")
}
for i := 0; i < int(db.mdb.num_catalog); i++ {
entry = C._go_ptr_catalog(db.mdb.catalog, C.guint(i))
name := C.GoString(&entry.object_name[0])
if name == tableName {
tableDef.table = C.mdb_read_table(entry)
if tableDef.table == nil {
return nil, errors.New("mdb: error reading table")
}
tableDef.num_cols = int(tableDef.table.num_cols)
C.mdb_read_columns(tableDef.table)
C.mdb_read_indices(tableDef.table)
C.mdb_rewind_table(tableDef.table)
tableDef.column_names = make([]string, tableDef.num_cols)
tableDef.bound_values = make([][]byte, tableDef.num_cols)
tableDef.bound_lengths = make([]int, tableDef.num_cols)
for j := 0; j < tableDef.num_cols; j++ {
col = C._go_ptr_col(tableDef.table.columns, C.guint(j))
colName := C.GoString(&col.name[0])
tableDef.column_names[j] = colName
tableDef.bound_values[j] = make([]byte, C.MDB_BIND_SIZE)
C.mdb_bind_column(tableDef.table, C.int(j+1), unsafe.Pointer(&tableDef.bound_values[j][0]), (*C.int)(unsafe.Pointer(&tableDef.bound_lengths[j])))
}
}
}
return tableDef, nil
}
func (db *MDB) FetchRow(tableDef *MDBTableDef) ([]string, error) {
row := make([]string, tableDef.num_cols)
rv := C.mdb_fetch_row(tableDef.table)
if rv == 0 {
return nil, nil
}
for i := 0; i < tableDef.num_cols; i++ {
row[i] = string(tableDef.bound_values[i][0:tableDef.bound_lengths[i]])
}
return row, nil
}
func (db *MDB) ToMap() * MapDB{
var table_names, _ = db.Tables()
var map_db = make(MapDB,0)
if table_names != nil {
for _, tbn := range table_names {
var tb, _ = db.TableOpen(tbn)
if tb != nil {
var map_table = make(MapTable,0)
for ri := 0; true; ri++ {
var row, _ = db.FetchAssoc(tb)
if (row == nil) {
break
} else {
map_table = append(map_table, row)
}
}
map_db[tbn] = map_table
db.TableClose(tb)
}
}
}
return &map_db
}
func (db *MDB) FetchAssoc(tableDef *MDBTableDef) (map[string]string, error) {
row := make(map[string]string)
rv := C.mdb_fetch_row(tableDef.table)
if rv == 0 {
return nil, nil
}
for i := 0; i < tableDef.num_cols; i++ {
row[tableDef.column_names[i]] = string(tableDef.bound_values[i][0:tableDef.bound_lengths[i]])
}
return row, nil
}
// mdb_fetch_row rewinds table on last row
func (db *MDB) Rewind(tableDef *MDBTableDef) int {
return int(C.mdb_rewind_table(tableDef.table))
}
func (db *MDB) TableClose(tableDef *MDBTableDef) {
// nothing to do here
C.free(unsafe.Pointer(tableDef.table))
}
func (db *MDB) NumFields(tableDef *MDBTableDef) (int, error) {
return int(tableDef.table.num_cols), nil
}
func (db *MDB) NumRows(tableDef *MDBTableDef) (int, error) {
return int(tableDef.table.num_rows), nil
}
func (db *MDB) TableFields(tableDef *MDBTableDef) (map[string]MDBColumn, error) {
var col *C.MdbColumn
columns := make(map[string]MDBColumn)
for j := 0; j < tableDef.num_cols; j++ {
col = C._go_ptr_col(tableDef.table.columns, C.guint(j))
columns[C.GoString(&col.name[0])] = MDBColumn{
C.GoString(&col.name[0]),
int(col.col_type),
int(col.col_size),
int(col.col_prec),
int(col.col_scale),
bool(int(col.is_fixed) == 1),
}
}
return columns, nil
// return int(tableDef.table.num_rows), nil
}
func (db *MDB) TableIndexes(tableDef *MDBTableDef) ([]MDBIndex, error) {
var idx *C.MdbIndex
indexes := make([]MDBIndex, tableDef.table.num_idxs)
for i := 0; i < int(tableDef.table.num_idxs); i++ {
idx = C._go_ptr_idx(tableDef.table.indices, C.guint(i))
mdbindex := MDBIndex{
int(idx.index_num),
C.GoString(&idx.name[0]),
int(idx.index_type),
int(idx.num_rows),
int(idx.num_keys),
int(idx.flags),
make([]int, int(idx.num_keys)),
make([]int, int(idx.num_keys)),
}
for j := 0; j < mdbindex.keys; j++ {
mdbindex.keyColNum[j] = int(idx.key_col_num[j])
mdbindex.keyColOrder[j] = int(idx.key_col_order[j])
}
indexes[i] = mdbindex
}
return indexes, nil
}
func (db *MDB) TypeName(typeCode int) string {
return ""
}