X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=sqlite%2Fextensions.c;h=0cc8daa2da952e09788ebdfda619c41d5a534c48;hb=be765ec4d3dc14dd27826326e29da8a62d5603f7;hp=7502734a6b0344fc0a330068cda5329928c93496;hpb=3cd4e5c6f411d181d27c6a169fc214245e913286;p=zpackage diff --git a/sqlite/extensions.c b/sqlite/extensions.c index 7502734..0cc8daa 100644 --- a/sqlite/extensions.c +++ b/sqlite/extensions.c @@ -1,6 +1,7 @@ +#include /* Add your header comment here */ /* Do not use ! */ -#include +#include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #include "zpm.h" @@ -15,11 +16,41 @@ SQLITE_EXTENSION_INIT1 static int vercmp(void *not_used, int lena, const void *a, int lenb, const void *b) { - /* not sure what the ints are, possibly string lengths */ + int rv; + char *bufa, *bufb; + if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n", not_used); if (lena == 0 && lenb > 0) return 1; - return zpm_vercmp(a, b); + if (lenb == 0 && lena > 0) return -1; + + bufa = sqlite3_malloc(lena+1); + bufb = sqlite3_malloc(lenb+1); + + strncpy(bufa, a, lena); + strncpy(bufb, b, lenb); + bufa[lena] = 0; + bufb[lenb] = 0; + + rv = zpm_vercmp(bufa, bufb); + sqlite3_free(bufa); + sqlite3_free(bufb); + return rv; +} + +static void vercmpf(sqlite3_context *ctx, int nargs, sqlite3_value **vals) { + int rv; + if (nargs == 0) { + rv = 0; + } else if (nargs == 1) { + rv = -1; + } else { + rv = zpm_vercmp( + (const char *)sqlite3_value_text(vals[0]), + (const char *)sqlite3_value_text(vals[1]) + ); + } + sqlite3_result_int(ctx, rv); } static int zpm_sqlite_vercmp_init(sqlite3 *db, char **pzErrMsg, @@ -28,6 +59,10 @@ static int zpm_sqlite_vercmp_init(sqlite3 *db, char **pzErrMsg, int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); rc = sqlite3_create_collation(db, "vercmp", SQLITE_UTF8, NULL, vercmp); + rc = sqlite3_create_function_v2(db, "vercmp", 2, + SQLITE_UTF8|SQLITE_DETERMINISTIC, + NULL, vercmpf, NULL, NULL, NULL + ); /* Insert here calls to ** sqlite3_create_function_v2(), ** sqlite3_create_collation_v2(), @@ -35,6 +70,11 @@ static int zpm_sqlite_vercmp_init(sqlite3 *db, char **pzErrMsg, ** sqlite3_vfs_register() ** to register the new features that your extension adds. */ +#if 0 + if (rc == SQLITE_OK) { + fprintf(stderr, "vercmp collation created\n"); + } +#endif return rc; }