forked from freepk/sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc-contains.go
43 lines (38 loc) · 1.12 KB
/
func-contains.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
package sqlite3
/*
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "sqlite3.h"
typedef unsigned char byte;
static void contains(sqlite3_context *context, int argc, sqlite3_value **argv) {
int value = sqlite3_value_int(argv[1]);
const int value_size = sizeof(value);
byte bytes[value_size];
for (int i = 0; i < value_size; ++i) {
bytes[i] = value >> (i * 8);
}
int blob_size = sqlite3_value_bytes(argv[0]);
byte *blob = (byte *)sqlite3_value_blob(argv[0]);
for (int i = 0; i < blob_size / value_size; ++i) {
int offset = i * value_size;
if (memcmp(bytes, &blob[offset], value_size) == 0) {
sqlite3_result_int(context, i + 1);
return;
}
}
sqlite3_result_int(context, 0);
}
int sqlite3_register_blob_contains_func(sqlite3 *db) {
return sqlite3_create_function_v2(db, "contains", 2, SQLITE_UTF8, NULL, contains, NULL, NULL, NULL);
}
*/
import "C"
import "errors"
func (d *DB) RegisterContainsFunc() error {
r := C.sqlite3_register_blob_contains_func(d.p)
if r != C.SQLITE_OK {
return errors.New("cannot register function")
}
return nil
}