Hi David, > I spotted where the problem comes from: it is when a text value is empty. I > have > produced a small but complete example [attached]: can you reproduce it ?
Yes, thanks for the test case, I can now reproduce it and hopefully the attached patches work for you also. The first one fixes the ignorance of 'proc' of the 'sqlite-map' procedure. It took me a while to figure this one out ;) The second patch extends 'tests/basic.test' with some statements that make the problem discovered by you show up. The third patch fixes the problem for me. Andy, is this the 'right way' to test for null pointers? I could not find a cleaner solution in my limited search... The fourth patch fixes a problem I discovered while playing with sqlite-bind. Really this should generate more testcases. Andy, I have now cloned your repo at gitorious[1] for you to pull if that's easier. I am perfectly happy to rework patches if anything needs to be done for that. Thanks Detlev [1] https://gitorious.org/~dzu/guile-sqlite3/guile-sqlite3-dzu -- Bacchus, n. A convenient deity invented by the ancients as an excuse for getting drunk. -- DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: d...@denx.de
>From 1018e0617b08c0f79556ea1e6188963ce01bb952 Mon Sep 17 00:00:00 2001 From: Detlev Zundel <d...@denx.de> Date: Mon, 4 Apr 2011 18:11:58 +0200 Subject: [PATCH 1/4] Make sqlite-map really apply the procedure argument --- sqlite3.scm | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-) diff --git a/sqlite3.scm b/sqlite3.scm index 3369e02..2492d15 100644 --- a/sqlite3.scm +++ b/sqlite3.scm @@ -425,4 +425,5 @@ seed)))) (define (sqlite-map proc stmt) - (reverse! (sqlite-fold cons '() stmt))) + (map proc + (reverse! (sqlite-fold cons '() stmt)))) -- 1.7.4.1
>From f45fc5c709d241a147e061e92062993cce1b4841 Mon Sep 17 00:00:00 2001 From: Detlev Zundel <d...@denx.de> Date: Mon, 4 Apr 2011 18:12:39 +0200 Subject: [PATCH 2/4] Add some basic functionality testing --- tests/basic.test | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 42 insertions(+), 1 deletions(-) diff --git a/tests/basic.test b/tests/basic.test index 1e5a3e0..46b395c 100644 --- a/tests/basic.test +++ b/tests/basic.test @@ -1,6 +1,6 @@ ;;;; basic.test --- -*- mode: scheme; coding: utf-8; -*- ;;;; -;;;; Copyright (C) 2010 Andy Wingo <wi...@pobox.com> +;;;; Copyright (C) 2011 Detlev Zundel <d...@denx.de> ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -17,6 +17,47 @@ ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA (define-module (tests basic-test) + #:use-module (ice-9 format) #:use-module (sqlite3)) +(define (sqlite-exec db sql) + (let ((stmt (sqlite-prepare db sql))) + (sqlite-map display stmt))) + +;; Cleanup database so we can check creation +(define db-name "tests/simple.db") +(if (file-exists? db-name) + (begin + (format #t "Removing leftover database ~a~%" db-name) + (delete-file db-name))) + +(format #t "Creating test database ~a:" db-name) +(define db (sqlite-open db-name (logior SQLITE_OPEN_CREATE + SQLITE_OPEN_READWRITE))) +(format #t "~40tOk~%") + +(format #t "Creating table 'project':") +(sqlite-exec + db + "create table project ( + reference integer primary key, + name text, + website text + )") +(format #t "~40tOk~%") + +(format #t "Inserting a dataset:") +(sqlite-exec db "insert into project values (1, 'Guile', '')") +(format #t "~40tOk~%") + +(format #t "Reading dataset: ") +(sqlite-exec db "select * from project") +(format #t "~40tOk~%") + +(sqlite-close db) + +(format #t "Removing database ~a" db-name) +(delete-file db-name) +(format #t "~40tOk~%") + (exit 0) -- 1.7.4.1
>From 97aae36202ca7ba0abec5cf0da95a56ac706805f Mon Sep 17 00:00:00 2001 From: Detlev Zundel <d...@denx.de> Date: Mon, 4 Apr 2011 18:13:18 +0200 Subject: [PATCH 3/4] Fix interpretation of null pointers from the library --- sqlite3.scm | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sqlite3.scm b/sqlite3.scm index 2492d15..9e48ff5 100644 --- a/sqlite3.scm +++ b/sqlite3.scm @@ -373,11 +373,15 @@ (value-double (stmt-pointer stmt) i)) ((3) ; SQLITE3_TEXT (let ((p (value-blob (stmt-pointer stmt) i))) - (utf8->string - (pointer->bytevector p (value-bytes (stmt-pointer stmt) i))))) + (if (eq? p %null-pointer) + "" + (utf8->string + (pointer->bytevector p (value-bytes (stmt-pointer stmt) i)))))) ((4) ; SQLITE_BLOB (let ((p (value-blob (stmt-pointer stmt) i))) - (pointer->bytevector p (value-bytes (stmt-pointer stmt) i)))) + (if (eq? p %null-pointer) + (make-bytevector 0) + (pointer->bytevector p (value-bytes (stmt-pointer stmt) i))))) ((5) ; SQLITE_NULL #f))))) -- 1.7.4.1
>From c37f372990d9edddd859e9b999eb38151ce33cf8 Mon Sep 17 00:00:00 2001 From: Detlev Zundel <d...@denx.de> Date: Mon, 4 Apr 2011 18:09:06 +0200 Subject: [PATCH 4/4] Fix sqlite-bind. - sqlite-transient needs to be a pointer - fix typo for string case --- sqlite3.scm | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sqlite3.scm b/sqlite3.scm index 9e48ff5..97550b4 100644 --- a/sqlite3.scm +++ b/sqlite3.scm @@ -290,7 +290,8 @@ int (dynamic-func "sqlite3_bind_null" libsqlite3) (list '* int))) - (sqlite-transient (make-bytevector (sizeof '*) #xff))) + (sqlite-transient (bytevector->pointer + (make-bytevector (sizeof '*) #xff)))) (lambda (stmt key val) (assert-live-stmt! stmt) (let ((idx (key->index stmt key)) @@ -300,7 +301,7 @@ (bind-blob p idx (bytevector->pointer val) (bytevector-length val) sqlite-transient)) ((string? val) - (let ((bv ((string->utf8 val)))) + (let ((bv (string->utf8 val))) (bind-text p idx (bytevector->pointer bv) (bytevector-length bv) sqlite-transient))) ((and (integer? val) (exact? val)) -- 1.7.4.1