]> pd.if.org Git - zpackage/blob - sqlite/extensions.c
remove stray debug fprintf
[zpackage] / sqlite / extensions.c
1 #include <string.h>
2 /* Add your header comment here */
3 /* Do not use <sqlite3.h>! */
4 #include "sqlite3ext.h"
5 SQLITE_EXTENSION_INIT1
6
7 #include "zpm.h"
8 /* Insert your extension code here */
9
10 /* TODO: Change the entry point name so that "extension" is replaced by
11 ** text derived from the shared library filename as follows:  Copy every
12 ** ASCII alphabetic character from the filename after the last "/" through
13 ** the next following ".", converting each character to lowercase, and
14 ** discarding the first three characters if they are "lib".
15 */
16
17 static int vercmp(void *not_used, int lena, const void *a,
18                 int lenb, const void *b) {
19         int rv;
20         char *bufa, *bufb;
21
22         if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
23                         not_used);
24         if (lena == 0 && lenb > 0) return 1;
25         if (lenb == 0 && lena > 0) return -1;
26
27         bufa = sqlite3_malloc(lena+1);
28         bufb = sqlite3_malloc(lenb+1);
29
30         strncpy(bufa, a, lena);
31         strncpy(bufb, b, lenb);
32         bufa[lena] = 0;
33         bufb[lenb] = 0;
34
35         rv = zpm_vercmp(bufa, bufb);
36         sqlite3_free(bufa);
37         sqlite3_free(bufb);
38         return rv;
39 }
40
41 static void vercmpf(sqlite3_context *ctx, int nargs, sqlite3_value **vals) {
42         int rv;
43         if (nargs == 0) {
44                 rv = 0;
45         } else if (nargs == 1) {
46                 rv = -1;
47         } else {
48                 rv = zpm_vercmp(
49                                 (const char *)sqlite3_value_text(vals[0]),
50                                 (const char *)sqlite3_value_text(vals[1])
51                                 );
52         }
53         sqlite3_result_int(ctx, rv);
54 }
55
56 static int zpm_sqlite_vercmp_init(sqlite3 *db, char **pzErrMsg, 
57                 const sqlite3_api_routines *pApi
58                 ) {
59         int rc = SQLITE_OK;
60         SQLITE_EXTENSION_INIT2(pApi);
61         rc = sqlite3_create_collation(db, "vercmp", SQLITE_UTF8, NULL, vercmp);
62         rc = sqlite3_create_function_v2(db, "vercmp", 2,
63                         SQLITE_UTF8|SQLITE_DETERMINISTIC,
64                         NULL, vercmpf, NULL, NULL, NULL
65                         );
66         /* Insert here calls to
67          **     sqlite3_create_function_v2(),
68          **     sqlite3_create_collation_v2(),
69          **     sqlite3_create_module_v2(), and/or
70          **     sqlite3_vfs_register()
71          ** to register the new features that your extension adds.
72          */
73 #if 0
74         if (rc == SQLITE_OK) {
75                 fprintf(stderr, "vercmp collation created\n");
76         }
77 #endif
78         return rc;
79 }
80
81 void zpm_setup_extensions(void) {
82         sqlite3_auto_extension( (void (*)(void))zpm_sqlite_vercmp_init);
83         return;
84 }