2 /* Add your header comment here */
3 /* Do not use <sqlite3.h>! */
4 #include <sqlite3ext.h>
8 /* Insert your extension code here */
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".
17 static int vercmp(void *not_used, int lena, const void *a,
18 int lenb, const void *b) {
22 if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
24 if (lena == 0 && lenb > 0) return 1;
25 if (lenb == 0 && lena > 0) return -1;
27 bufa = sqlite3_malloc(lena+1);
28 bufb = sqlite3_malloc(lenb+1);
30 strncpy(bufa, a, lena);
31 strncpy(bufb, b, lenb);
35 rv = zpm_vercmp(bufa, bufb);
41 static void vercmpf(sqlite3_context *ctx, int nargs, sqlite3_value **vals) {
45 } else if (nargs == 1) {
49 (const char *)sqlite3_value_text(vals[0]),
50 (const char *)sqlite3_value_text(vals[1])
53 sqlite3_result_int(ctx, rv);
56 static int zpm_sqlite_vercmp_init(sqlite3 *db, char **pzErrMsg,
57 const sqlite3_api_routines *pApi
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
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.
74 if (rc == SQLITE_OK) {
75 fprintf(stderr, "vercmp collation created\n");
81 void zpm_setup_extensions(void) {
82 sqlite3_auto_extension( (void (*)(void))zpm_sqlite_vercmp_init);