summaryrefslogtreecommitdiffhomepage
path: root/dlang/src/ocda/sqlite.d
diff options
context:
space:
mode:
Diffstat (limited to 'dlang/src/ocda/sqlite.d')
-rw-r--r--dlang/src/ocda/sqlite.d21
1 files changed, 21 insertions, 0 deletions
diff --git a/dlang/src/ocda/sqlite.d b/dlang/src/ocda/sqlite.d
index 1552fc4..6f3753c 100644
--- a/dlang/src/ocda/sqlite.d
+++ b/dlang/src/ocda/sqlite.d
@@ -28,6 +28,8 @@ extern (C) {
int sqlite3_column_int(sqlite3_stmt* stmt, int col);
int sqlite3_column_type(sqlite3_stmt* stmt, int col);
const(char)* sqlite3_errmsg(sqlite3* db);
+ const(void)* sqlite3_column_blob(sqlite3_stmt* stmt, int col);
+ int sqlite3_column_bytes(sqlite3_stmt* stmt, int col);
}
enum SQLITE_OK = 0;
@@ -94,6 +96,25 @@ struct Db {
return (r.length > 0 && r[0].length > 0) ? r[0][0] : "";
}
+ /+ ↓ the first column of the first row as bytes, for a blob.
+ Separate from one(), because a blob is not text: an image read as text
+ stops at the first zero byte, and a png's ninth byte is zero.
+ +/
+ ubyte[] blob(string sql) {
+ sqlite3_stmt* stmt;
+ if (sqlite3_prepare_v2(_handle, sql.toStringz, -1, &stmt, null) != SQLITE_OK) {
+ throw new SqliteError("prepare: " ~ sqlite3_errmsg(_handle).fromStringz.idup
+ ~ " [" ~ sql ~ "]");
+ }
+ scope(exit) { sqlite3_finalize(stmt); }
+ if (sqlite3_step(stmt) != SQLITE_ROW) { return []; }
+ int n = sqlite3_column_bytes(stmt, 0);
+ if (n <= 0) { return []; }
+ auto p = cast(const(ubyte)*) sqlite3_column_blob(stmt, 0);
+ if (p is null) { return []; }
+ return p[0 .. n].dup;
+ }
+
/+ ↓ does a table or view of this name exist +/
bool has(string name) {
return one("SELECT count(*) FROM sqlite_master WHERE name = '" ~ name ~ "'") != "0";