forked from softlion/SQLite.Net-PCL2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteApi.cs
More file actions
414 lines (346 loc) · 13.6 KB
/
SQLiteApi.cs
File metadata and controls
414 lines (346 loc) · 13.6 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.Text.RegularExpressions;
using SQLitePCL;
namespace SQLite.Net2
{
public class SqliteApi : ISQLiteApi
{
public static SqliteApi Instance { get;} = new SqliteApi();
public Result Open(string filename, out IDbHandle db, int flags, string zvfs)
{
var r = raw.sqlite3_open_v2(filename, out var db3, flags, zvfs);
db = new DbHandle(db3);
return (Result)r;
}
public int BindRegexpFunction(IDbHandle db)
{
void RegexMatch(
sqlite3_context ctx,
object user_data,
sqlite3_value[] args)
{
var pattern = raw.sqlite3_value_text(args[0]).utf8_to_string();
var input = raw.sqlite3_value_text(args[1]).utf8_to_string();
var r = Regex.IsMatch(input, pattern);
raw.sqlite3_result_int(ctx, r ? 1 : 0);
}
var handle = (DbHandle)db;
return raw.sqlite3_create_function(handle.DbPtr, "REGEXP", 2, null, RegexMatch);
}
public ExtendedResult ExtendedErrCode(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
return (ExtendedResult)raw.sqlite3_extended_errcode(internalDbHandle.DbPtr);
}
public int LibVersionNumber()
{
return raw.sqlite3_libversion_number();
}
public int Threadsafe()
{
return raw.sqlite3_threadsafe();
}
public string SourceID()
{
return raw.sqlite3_sourceid().utf8_to_string();
}
public Result EnableLoadExtension(IDbHandle db, int onoff)
{
throw new NotImplementedException("");
//var internalDbHandle = (DbHandle) db;
//return (Result)p.sqlite3_enable_load_extension(internalDbHandle.DbPtr, onoff);
}
public Result Close(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
return (Result)raw.sqlite3_close(internalDbHandle.DbPtr);
}
public Result Initialize()
{
return (Result)raw.sqlite3_initialize();
}
public Result Shutdown()
{
return (Result)raw.sqlite3_shutdown();
}
public Result Config(ConfigOption option)
{
return (Result)raw.sqlite3_config((int)option);
}
public Result BusyTimeout(IDbHandle db, int milliseconds)
{
var internalDbHandle = (DbHandle)db;
return (Result)raw.sqlite3_busy_timeout(internalDbHandle.DbPtr, milliseconds);
}
public int Changes(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
return raw.sqlite3_changes(internalDbHandle.DbPtr);
}
public IDbStatement Prepare2(IDbHandle db, string query)
{
var internalDbHandle = (DbHandle)db;
sqlite3_stmt stmt;
Result r = (Result)raw.sqlite3_prepare_v2(internalDbHandle.DbPtr, query, out stmt);
if (r != Result.OK)
{
throw new SQLiteException(r, Errmsg16(internalDbHandle));
}
return new DbStatement(stmt);
}
public Result Step(IDbStatement stmt)
{
var internalStmt = (DbStatement)stmt;
return (Result)raw.sqlite3_step(internalStmt.StmtPtr);
}
public Result Reset(IDbStatement stmt)
{
var internalStmt = (DbStatement)stmt;
return (Result)raw.sqlite3_reset(internalStmt.StmtPtr);
}
public Result Finalize(IDbStatement stmt)
{
var internalStmt = (DbStatement)stmt;
return (Result)raw.sqlite3_finalize(internalStmt.StmtPtr);
}
public long LastInsertRowid(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
return raw.sqlite3_last_insert_rowid(internalDbHandle.DbPtr);
}
public string Errmsg16(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
return raw.sqlite3_errmsg(internalDbHandle.DbPtr).utf8_to_string();
}
public int BindParameterIndex(IDbStatement stmt, string name)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_parameter_index(internalStmt.StmtPtr, name);
}
public int BindNull(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_null(internalStmt.StmtPtr, index);
}
public int BindInt(IDbStatement stmt, int index, int val)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_int(internalStmt.StmtPtr, index, val);
}
public int BindInt64(IDbStatement stmt, int index, long val)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_int64(internalStmt.StmtPtr, index, val);
}
public int BindDouble(IDbStatement stmt, int index, double val)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_double(internalStmt.StmtPtr, index, val);
}
public int BindText16(IDbStatement stmt, int index, string val, int n, IntPtr free)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_text(internalStmt.StmtPtr, index, val);
}
public int BindBlob(IDbStatement stmt, int index, byte[] val, int n, IntPtr free)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_bind_blob(internalStmt.StmtPtr, index, val);
}
public int ColumnCount(IDbStatement stmt)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_count(internalStmt.StmtPtr);
}
public string ColumnName16(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_name(internalStmt.StmtPtr, index).utf8_to_string();
}
public ColType ColumnType(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return (ColType)raw.sqlite3_column_type(internalStmt.StmtPtr, index);
}
public int ColumnInt(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_int(internalStmt.StmtPtr, index);
}
public long ColumnInt64(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_int64(internalStmt.StmtPtr, index);
}
public double ColumnDouble(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_double(internalStmt.StmtPtr, index);
}
public string ColumnText16(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_text(internalStmt.StmtPtr, index).utf8_to_string();
}
public ReadOnlySpan<byte> ColumnBlob(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_blob(internalStmt.StmtPtr, index);
}
public int ColumnBytes(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_bytes(internalStmt.StmtPtr, index);
}
public ReadOnlySpan<byte> ColumnByteArray(IDbStatement stmt, int index)
{
var internalStmt = (DbStatement)stmt;
return raw.sqlite3_column_blob(internalStmt.StmtPtr, index);
}
/// <summary>
/// http://www.sqlite.org/c3ref/get_autocommit.html
/// </summary>
public int GetAutoCommit(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
return raw.sqlite3_get_autocommit(internalDbHandle.DbPtr);
}
#region Backup
public IDbBackupHandle BackupInit(IDbHandle destHandle, string destName, IDbHandle srcHandle, string srcName)
{
var internalDestDb = (DbHandle)destHandle;
var internalSrcDb = (DbHandle)srcHandle;
var p = raw.sqlite3_backup_init(internalDestDb.DbPtr,
destName,
internalSrcDb.DbPtr,
srcName);
if (p == null)
{
return null;
}
else
{
return new DbBackupHandle(p);
}
}
public Result BackupStep(IDbBackupHandle handle, int pageCount)
{
var internalBackup = (DbBackupHandle)handle;
return (Result)raw.sqlite3_backup_step(internalBackup.DbBackupPtr, pageCount);
}
public Result BackupFinish(IDbBackupHandle handle)
{
var internalBackup = (DbBackupHandle)handle;
return (Result)raw.sqlite3_backup_finish(internalBackup.DbBackupPtr);
}
public int BackupRemaining(IDbBackupHandle handle)
{
var internalBackup = (DbBackupHandle)handle;
return raw.sqlite3_backup_remaining(internalBackup.DbBackupPtr);
}
public int BackupPagecount(IDbBackupHandle handle)
{
var internalBackup = (DbBackupHandle)handle;
return raw.sqlite3_backup_pagecount(internalBackup.DbBackupPtr);
}
// public int Sleep(int millis)
// {
// throw new NotImplementedException("Sleep is not implemented");
// //return raw.sqlite3_sleep(millis);
// }
private struct DbBackupHandle : IDbBackupHandle
{
public sqlite3_backup DbBackupPtr { get; private set; }
public DbBackupHandle(sqlite3_backup dbBackupPtr) : this()
{
DbBackupPtr = dbBackupPtr;
}
public bool Equals(IDbBackupHandle other)
=> other is DbBackupHandle handle && DbBackupPtr == handle.DbBackupPtr;
}
#endregion
#region Serialize
/// <summary>
/// Serialize the database, passing the memory to the given function to be copied as appropriate.
/// <see cref="copy"/> will be called with a pointer to the serialized data and its size, and its
/// return value or any exceptions will be passed through. The serialized memory is only valid
/// during the callback, and will be freed immediately after regardless of success or failure.
/// </summary>
public T Serialize<T>(IDbHandle db, string schema, Func<IntPtr, long, T> copy)
{
var internalDbHandle = (DbHandle)db;
var bytesPtr = raw.sqlite3_serialize(internalDbHandle.DbPtr, schema, out var size, 0);
if (bytesPtr == IntPtr.Zero)
{
throw new SQLiteException(Result.NoMem, Errmsg16(internalDbHandle));
}
try
{
return copy(bytesPtr, size);
}
finally
{
raw.sqlite3_free(bytesPtr);
}
}
/// <summary>
/// Deserialize a database. A buffer of the requested <see cref="size"/> will be allocated and
/// passed with the size to the given function to be filled as appropriate. Any exceptions will
/// be passed through. The buffer is only valid during the callback, and will be freed or passed
/// to sqlite immediately after depending on success.
/// </summary>
public void Deserialize(IDbHandle db, string schema, long size, Action<IntPtr, long> copy)
{
var internalDbHandle = (DbHandle)db;
var bytesPtr = raw.sqlite3_malloc64(size);
if (bytesPtr == IntPtr.Zero)
{
throw new SQLiteException(Result.NoMem, Errmsg16(internalDbHandle));
}
try
{
copy(bytesPtr, size);
}
catch
{
raw.sqlite3_free(bytesPtr);
throw;
}
const int flags = raw.SQLITE_DESERIALIZE_RESIZEABLE | raw.SQLITE_DESERIALIZE_FREEONCLOSE;
var r = (Result)raw.sqlite3_deserialize(internalDbHandle.DbPtr, schema, bytesPtr, size, size, flags);
if (r != Result.OK)
{
// note: sqlite will free the buffer on error, thanks to SQLITE_DESERIALIZE_FREEONCLOSE
throw new SQLiteException(r, Errmsg16(internalDbHandle));
}
}
#endregion
private class DbHandle : IDbHandle
{
public sqlite3 DbPtr { get; private set; }
public DbHandle(sqlite3 handle)
{
DbPtr = handle;
}
public bool Equals(IDbHandle other)
{
return other is DbHandle && DbPtr == ((DbHandle)other).DbPtr;
}
}
private struct DbStatement : IDbStatement
{
public object Handle => StmtPtr;
public sqlite3_stmt StmtPtr { get; private set; }
public DbStatement(sqlite3_stmt stmtPtr)
: this()
{
StmtPtr = stmtPtr;
}
public bool Equals(IDbStatement other)
{
return other is DbStatement && StmtPtr == ((DbStatement)other).StmtPtr;
}
}
}
}