basic.test 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;;; basic.test --- -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2011 Detlev Zundel <dzu@denx.de>
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (tests basic-test)
  19. #:use-module (ice-9 format)
  20. #:use-module (sqlite3))
  21. (define (sqlite-exec db sql)
  22. (let ((stmt (sqlite-prepare db sql)))
  23. (sqlite-map display stmt)))
  24. (define (sqlite-exec* db sql key value)
  25. (let ((stmt (sqlite-prepare db sql)))
  26. (sqlite-bind stmt key value)
  27. (sqlite-map display stmt)))
  28. ;; Cleanup database so we can check creation
  29. (define db-name "tests/simple.db")
  30. (if (file-exists? db-name)
  31. (begin
  32. (format #t "Removing leftover database ~a~%" db-name)
  33. (delete-file db-name)))
  34. (format #t "Creating test database ~a:" db-name)
  35. (define db (sqlite-open db-name (logior SQLITE_OPEN_CREATE
  36. SQLITE_OPEN_READWRITE)))
  37. (format #t "~40tOk~%")
  38. (format #t "Creating table 'project':")
  39. (sqlite-exec
  40. db
  41. "create table project (
  42. reference integer primary key,
  43. name text,
  44. website text
  45. )")
  46. (format #t "~40tOk~%")
  47. (format #t "Inserting a dataset:")
  48. (sqlite-exec db "insert into project values (1, 'Guile', '')")
  49. (format #t "~40tOk~%")
  50. (let* ((s "SELECT * FROM project")
  51. (stmt (sqlite-prepare db s #:cache? #t)))
  52. (unless (eq? stmt (sqlite-prepare db s #:cache? #t))
  53. (error "caching is not caching much!")))
  54. (format #t "Reading dataset: ")
  55. (sqlite-exec db "select * from project")
  56. (format #t "~40tOk~%")
  57. (format #t "Reading dataset with named parameter: ")
  58. (sqlite-exec* db "select * from project where 'bla' = :foo" ":foo" "bla")
  59. (format #t "~40tOk~%")
  60. (format #t "Reading dataset with named parameter, v2: ")
  61. (sqlite-exec* db "select * from project where 'bla' = :foo" 'foo "bla")
  62. (format #t "~40tOk~%")
  63. (format #t "Inserting a dataset using sqlite-bind: ")
  64. (sqlite-exec db "CREATE TABLE foos (dbid INTEGER PRIMARY KEY, name TEXT)")
  65. (let ((stmt (sqlite-prepare db "INSERT INTO foos(name) VALUES(?)")))
  66. (sqlite-bind stmt 1 "myfoo")
  67. (sqlite-step stmt)
  68. (sqlite-finalize stmt))
  69. (sqlite-exec db "DROP TABLE IF EXISTS foos")
  70. (format #t "~40tOk~%")
  71. (sqlite-close db)
  72. (format #t "Removing database ~a" db-name)
  73. (delete-file db-name)
  74. (format #t "~40tOk~%")
  75. (exit 0)