Subject: | User defined functions cannot return blob values (patch) |
User defined functions cannot currently return values that contain NUL's. Effectively, this means that they cannot return blobs.
The problem is that dbdimp.c:sqlite_db_set_result() presumes that anything that is not a number is text, and sets the return value with sqlite3_result_text(). This causes bad results for NUL containing values.
I've attached a patch that sets the value as a blob if it contains NUL's. While this works for my purposes, I don't know enough about the internals to know if this is a good general solution. A better approach might be to allow the function to explicitly set a return type.
Patch attached.
--- dbdimp.c.orig 2005-11-10 19:27:55.000000000 -0700
+++ dbdimp.c 2005-11-10 20:03:14.000000000 -0700
@@ -707,7 +713,13 @@
sqlite3_result_double( context, SvNV(result));
} else {
s = SvPV(result, len);
- sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
+ if (memchr(s, 0, len)) {
+ /* if the result contains NUL(s) treat it as a blob */
+ sqlite3_result_blob(context, s, len, SQLITE_TRANSIENT );
+ }
+ else {
+ sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
+ }
}
}