Skip to content

Deal with text containing NUL bytes #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 8 additions & 1 deletion sqlite-ffi.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@
(statement p-sqlite3-stmt)
(column-number :int))

(defcfun sqlite3-column-text :string
(defcfun ("sqlite3_column_text" sqlite3-column-text-pointer) :pointer
(statement p-sqlite3-stmt)
(column-number :int))

(defun sqlite3-column-text (statement column-number)
"Wraper around sqlite3-column-text-pointer to deal with strings
containing embedded NULs."
(cffi:foreign-string-to-lisp
(sqlite3-column-text-pointer statement column-number)
:count (sqlite3-column-bytes statement column-number)))

(defcfun sqlite3-column-int64 :int64
(statement p-sqlite3-stmt)
(column-number :int))
Expand Down
8 changes: 8 additions & 0 deletions sqlite-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@
(is (equalp (fetch-all) '()))))
(finalize-statement statement)))))

(test test-text-containing-null-bytes
(let ((string-with-nulls (concatenate 'string "foo" '(#\null) "bar" '(#\null) "baz")))
(with-open-database (db ":memory:")
(execute-non-query db "create table test (str text)")
(execute-non-query db "insert into test (str) values (?)" string-with-nulls)
(is (string= (execute-single db "select str from test")
string-with-nulls)))))

#+thread-support
(defparameter *db-file* "/tmp/test.sqlite")

Expand Down
2 changes: 1 addition & 1 deletion sqlite.asd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:components ((:file "sqlite-ffi")
(:file "cache")
(:file "sqlite" :depends-on ("sqlite-ffi" "cache")))
:depends-on (:iterate :cffi)
:depends-on (:iterate :cffi :babel)
:in-order-to ((test-op (load-op sqlite-tests))))

(defmethod perform ((o asdf:test-op) (c (eql (find-system :sqlite))))
Expand Down
4 changes: 3 additions & 1 deletion sqlite.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ Supported types:
(integer (sqlite-ffi:sqlite3-bind-int64 (handle statement) index value))
(double-float (sqlite-ffi:sqlite3-bind-double (handle statement) index value))
(real (sqlite-ffi:sqlite3-bind-double (handle statement) index (coerce value 'double-float)))
(string (sqlite-ffi:sqlite3-bind-text (handle statement) index value -1 (sqlite-ffi:destructor-transient)))
(string (sqlite-ffi:sqlite3-bind-text (handle statement) index value
(babel:string-size-in-octets value)
(sqlite-ffi:destructor-transient)))
((vector (unsigned-byte 8)) (cffi:with-pointer-to-vector-data (ptr value)
(sqlite-ffi:sqlite3-bind-blob (handle statement) index ptr (length value) (sqlite-ffi:destructor-transient))))
(vector (cffi:with-foreign-object (array :unsigned-char (length value))
Expand Down