Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions sqlite3.carp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
(Integer [Long])
(Floating [Double])
(Text [String])
(Blob [String]))
(Blob [(Array Byte)]))

(private SQLiteColumn)
(hidden SQLiteColumn)
Expand All @@ -86,7 +86,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
(register int (Fn [Long] SQLiteColumn) "SQLiteColumn_int")
(register float (Fn [Double] SQLiteColumn) "SQLiteColumn_float")
(register text (Fn [String] SQLiteColumn) "SQLiteColumn_text")
(register blob (Fn [String] SQLiteColumn) "SQLiteColumn_blob"))
(register blob (Fn [(Array Byte)] SQLiteColumn) "SQLiteColumn_blob"))

(defn = [a b]
(match-ref a
Expand Down Expand Up @@ -114,6 +114,9 @@ primitive Carp types can be casted to appropriate SQLite types by using the
(register from-integer (Fn [SQLiteColumn] Long) "SQLiteColumn_from_int")
(register from-floating (Fn [SQLiteColumn] Double) "SQLiteColumn_from_float")
(register from-text (Fn [SQLiteColumn] String) "SQLiteColumn_from_str")
(register from-blob
(Fn [SQLiteColumn] (Array Byte))
"SQLiteColumn_from_blob")

(defn to-carp [c]
(case (tag &c)
Expand All @@ -124,7 +127,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
sql_text
(Type.Text (from-text c))
sql_blob
(Type.Blob (from-text c))
(Type.Blob (from-blob c))
(Type.Null))))

(private SQLiteRow)
Expand Down
20 changes: 15 additions & 5 deletions sqlite3_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct {

typedef struct {
int tag;
int blob_len;
union {
int64_t i;
double f;
Expand Down Expand Up @@ -58,10 +59,19 @@ SQLiteColumn SQLiteColumn_text(char* s) {
return res;
}

SQLiteColumn SQLiteColumn_blob(char* s) {
SQLiteColumn SQLiteColumn_blob(Array a) {
SQLiteColumn res;
res.tag = SQLITE_BLOB;
res.s = s;
res.blob_len = a.len;
res.s = a.data;
return res;
}

Array SQLiteColumn_from_blob(SQLiteColumn col) {
Array res;
res.len = col.blob_len;
res.capacity = col.blob_len;
res.data = col.s;
return res;
}

Expand Down Expand Up @@ -183,9 +193,9 @@ const char* SQLite3_exec_internal(sqlite3_stmt* s, SQLiteRows* rows) {
}
case SQLITE_BLOB: {
len = sqlite3_column_bytes(s, i);
c->s = CARP_MALLOC(len+1);
c->blob_len = len;
c->s = CARP_MALLOC(len);
memcpy(c->s, sqlite3_column_blob(s, i), len);
c->s[len] = '\0';
break;
}
case SQLITE_NULL:
Expand Down Expand Up @@ -239,7 +249,7 @@ const char* SQLite3_bind(sqlite3_stmt* s, Array* p) {
res = sqlite3_bind_text(s, i+1, val.s, strlen(val.s), SQLITE_STATIC);
break;
case SQLITE_BLOB:
res = sqlite3_bind_blob(s, i+1, val.s, strlen(val.s), SQLITE_STATIC);
res = sqlite3_bind_blob(s, i+1, val.s, val.blob_len, SQLITE_STATIC);
break;
}
if (res != SQLITE_OK) {
Expand Down
16 changes: 15 additions & 1 deletion test/sqlite3.carp
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,18 @@
(let-do [r (SQLite3.query &db "SELECT * FROM t;" &[])]
(SQLite3.close db)
r))
"with-transaction rolls back on error"))
"with-transaction rolls back on error")

(assert-equal test
&(Result.Success [[(SQLite3.Type.Blob [0b 1b 0b 2b 3b])]])
&(let [db (open-memory)]
(let-do [r (do
(ignore (SQLite3.query &db "CREATE TABLE t (data BLOB);" &[]))
(ignore
(SQLite3.query &db
"INSERT INTO t VALUES (?1);"
&[(SQLite3.Type.Blob [0b 1b 0b 2b 3b])]))
(SQLite3.query &db "SELECT * FROM t;" &[]))]
(SQLite3.close db)
r))
"blob with embedded nulls round-trips correctly"))
Loading