-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.go
214 lines (174 loc) · 7.4 KB
/
binding.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
package sqlite3
import "github.com/ebitengine/purego"
type CSQLiteCtx struct {
ctx uintptr
}
type CSQLiteStmt struct {
stmt uintptr
}
type CSQLite interface {
SQLiteInit(path string) *CSQLiteCtx
SQLiteExec(ctx *CSQLiteCtx, sql string) string
SQLiteClose(ctx *CSQLiteCtx) int
SQLiteErrMsg(ctx *CSQLiteCtx) string
SQLiteErrCode(ctx *CSQLiteCtx) int
Close() error
SQLitePrepare(ctx *CSQLiteCtx, zSql string) *CSQLiteStmt
SQLiteFinalize(stmt *CSQLiteStmt) int
SQLiteReset(stmt *CSQLiteStmt) int
SQLiteStep(stmt *CSQLiteStmt) int
SQLiteBindBlob(stmt *CSQLiteStmt, index int, zData []byte) int
SQLiteBindBlob64(stmt *CSQLiteStmt, index int, zData []byte) int
SQLiteBindDouble(stmt *CSQLiteStmt, index int, zData float64) int
SQLiteBindInt(stmt *CSQLiteStmt, index int, zData int) int
SQLiteBindInt64(stmt *CSQLiteStmt, index int, zData int64) int
SQLiteBindNull(stmt *CSQLiteStmt, index int) int
SQLiteBindText(stmt *CSQLiteStmt, index int, zData string) int
SQLiteColumnName(stmt *CSQLiteStmt, index int) string
SQLiteColumnText(stmt *CSQLiteStmt, index int) string
}
type CSQLiteImpl struct {
libSQLite uintptr
sqliteInit func(path string) uintptr
sqliteExec func(db uintptr, sql string) string
sqliteClose func(db uintptr) int
sqliteErrMsg func(db uintptr) string
sqliteErrCode func(db uintptr) int
sqlitePrepareV2 func(db uintptr, zSql string, nByte int) uintptr
sqliteFinalize func(stmt uintptr) int
sqliteReset func(stmt uintptr) int
sqliteStep func(stmt uintptr) int
sqliteBindBlob func(stmt uintptr, index int, zData *byte, nData int, callback uintptr) int
sqliteBindBlob64 func(stmt uintptr, index int, zData *byte, nData uint64, callback uintptr) int
sqliteBindDouble func(stmt uintptr, index int, zData float64) int
sqliteBindInt func(stmt uintptr, index int, zData int) int
sqliteBindInt64 func(stmt uintptr, index int, zData int64) int
sqliteBindNull func(stmt uintptr, index int) int
sqliteBindText func(stmt uintptr, index int, zData string, nData int) int
sqlite3ColumnName func(stmt uintptr, index int) string
sqlite3ColumnText func(stmt uintptr, index int) string
}
func NewCSQLite(libraryPath string) (CSQLite, error) {
library, err := openLibrary(libraryPath)
if err != nil {
return nil, err
}
var (
sqliteInit func(path string) uintptr
sqliteExec func(db uintptr, sql string) string
sqliteClose func(db uintptr) int
sqliteErrMsg func(db uintptr) string
sqliteErrCode func(db uintptr) int
sqlitePrepareV2 func(db uintptr, zSql string, nByte int) uintptr
sqliteFinalize func(stmt uintptr) int
sqliteReset func(stmt uintptr) int
sqliteStep func(stmt uintptr) int
sqliteBindBlob func(stmt uintptr, index int, zData *byte, nData int, callback uintptr) int
sqliteBindBlob64 func(stmt uintptr, index int, zData *byte, nData uint64, callback uintptr) int
sqliteBindDouble func(stmt uintptr, index int, zData float64) int
sqliteBindInt func(stmt uintptr, index int, zData int) int
sqliteBindInt64 func(stmt uintptr, index int, zData int64) int
sqliteBindNull func(stmt uintptr, index int) int
sqliteBindText func(stmt uintptr, index int, zData string, nData int) int
sqlite3ColumnName func(stmt uintptr, index int) string
sqlite3ColumnText func(stmt uintptr, index int) string
)
purego.RegisterLibFunc(&sqliteInit, library, "sqlite3_abi_init")
purego.RegisterLibFunc(&sqliteExec, library, "sqlite3_abi_exec")
purego.RegisterLibFunc(&sqliteClose, library, "sqlite3_close")
purego.RegisterLibFunc(&sqliteErrMsg, library, "sqlite3_errmsg")
purego.RegisterLibFunc(&sqliteErrCode, library, "sqlite3_errcode")
purego.RegisterLibFunc(&sqlitePrepareV2, library, "sqlite3_prepare_v2")
purego.RegisterLibFunc(&sqliteFinalize, library, "sqlite3_finalize")
purego.RegisterLibFunc(&sqliteReset, library, "sqlite3_reset")
purego.RegisterLibFunc(&sqliteStep, library, "sqlite3_step")
purego.RegisterLibFunc(&sqliteBindBlob, library, "sqlite3_bind_blob")
purego.RegisterLibFunc(&sqliteBindBlob64, library, "sqlite3_bind_blob64")
purego.RegisterLibFunc(&sqliteBindDouble, library, "sqlite3_bind_double")
purego.RegisterLibFunc(&sqliteBindInt, library, "sqlite3_bind_int")
purego.RegisterLibFunc(&sqliteBindInt64, library, "sqlite3_bind_int64")
purego.RegisterLibFunc(&sqliteBindNull, library, "sqlite3_bind_null")
purego.RegisterLibFunc(&sqliteBindText, library, "sqlite3_bind_text")
purego.RegisterLibFunc(&sqlite3ColumnName, library, "sqlite3_column_name")
purego.RegisterLibFunc(&sqlite3ColumnText, library, "sqlite3_column_text")
return &CSQLiteImpl{
libSQLite: library,
sqliteInit: sqliteInit,
sqliteExec: sqliteExec,
sqliteClose: sqliteClose,
sqliteErrMsg: sqliteErrMsg,
sqliteErrCode: sqliteErrCode,
sqlitePrepareV2: sqlitePrepareV2,
sqliteFinalize: sqliteFinalize,
sqliteBindBlob: sqliteBindBlob,
sqliteBindBlob64: sqliteBindBlob64,
sqliteBindDouble: sqliteBindDouble,
sqliteBindInt: sqliteBindInt,
sqliteBindInt64: sqliteBindInt64,
sqliteBindNull: sqliteBindNull,
sqliteBindText: sqliteBindText,
sqlite3ColumnName: sqlite3ColumnName,
sqlite3ColumnText: sqlite3ColumnText,
}, nil
}
func (c *CSQLiteImpl) SQLiteInit(path string) *CSQLiteCtx {
ctx := c.sqliteInit(path)
return &CSQLiteCtx{
ctx,
}
}
func (c *CSQLiteImpl) SQLiteExec(ctx *CSQLiteCtx, sql string) string {
return c.sqliteExec(ctx.ctx, sql)
}
func (c *CSQLiteImpl) SQLiteClose(ctx *CSQLiteCtx) int {
return c.sqliteClose(ctx.ctx)
}
func (c *CSQLiteImpl) SQLiteErrMsg(ctx *CSQLiteCtx) string {
return c.sqliteErrMsg(ctx.ctx)
}
func (c *CSQLiteImpl) SQLiteErrCode(ctx *CSQLiteCtx) int {
return c.sqliteErrCode(ctx.ctx)
}
func (c *CSQLiteImpl) Close() error {
return closeLibrary(c.libSQLite)
}
func (c *CSQLiteImpl) SQLitePrepare(ctx *CSQLiteCtx, zSql string) *CSQLiteStmt {
stmt := c.sqlitePrepareV2(ctx.ctx, zSql, len(zSql))
return &CSQLiteStmt{stmt}
}
func (c *CSQLiteImpl) SQLiteFinalize(stmt *CSQLiteStmt) int {
return c.sqliteFinalize(stmt.stmt)
}
func (c *CSQLiteImpl) SQLiteReset(stmt *CSQLiteStmt) int {
return c.sqliteReset(stmt.stmt)
}
func (c *CSQLiteImpl) SQLiteStep(stmt *CSQLiteStmt) int {
return c.sqliteStep(stmt.stmt)
}
func (c *CSQLiteImpl) SQLiteBindBlob(stmt *CSQLiteStmt, index int, zData []byte) int {
return c.sqliteBindBlob(stmt.stmt, index, &zData[0], len(zData), 0)
}
func (c *CSQLiteImpl) SQLiteBindBlob64(stmt *CSQLiteStmt, index int, zData []byte) int {
return c.sqliteBindBlob64(stmt.stmt, index, &zData[0], uint64(len(zData)), 0)
}
func (c *CSQLiteImpl) SQLiteBindDouble(stmt *CSQLiteStmt, index int, zData float64) int {
return c.sqliteBindDouble(stmt.stmt, index, zData)
}
func (c *CSQLiteImpl) SQLiteBindInt(stmt *CSQLiteStmt, index int, zData int) int {
return c.sqliteBindInt(stmt.stmt, index, zData)
}
func (c *CSQLiteImpl) SQLiteBindInt64(stmt *CSQLiteStmt, index int, zData int64) int {
return c.sqliteBindInt64(stmt.stmt, index, zData)
}
func (c *CSQLiteImpl) SQLiteBindNull(stmt *CSQLiteStmt, index int) int {
return c.sqliteBindNull(stmt.stmt, index)
}
func (c *CSQLiteImpl) SQLiteBindText(stmt *CSQLiteStmt, index int, zData string) int {
return c.sqliteBindText(stmt.stmt, index, zData, len(zData))
}
func (c *CSQLiteImpl) SQLiteColumnName(stmt *CSQLiteStmt, index int) string {
return c.sqlite3ColumnName(stmt.stmt, index)
}
func (c *CSQLiteImpl) SQLiteColumnText(stmt *CSQLiteStmt, index int) string {
return c.sqlite3ColumnText(stmt.stmt, index)
}