/sqlite-ffi.lisp

http://github.com/dmitryvk/cl-sqlite · Lisp · 200 lines · 162 code · 38 blank · 0 comment · 0 complexity · 69b91177fea7ecffa1a9d3af6fceaeec MD5 · raw file

  1. (defpackage :sqlite-ffi
  2. (:use :cl :cffi)
  3. (:export :error-code
  4. :p-sqlite3
  5. :sqlite3-open
  6. :sqlite3-close
  7. :sqlite3-errmsg
  8. :sqlite3-busy-timeout
  9. :p-sqlite3-stmt
  10. :sqlite3-prepare
  11. :sqlite3-finalize
  12. :sqlite3-step
  13. :sqlite3-reset
  14. :sqlite3-clear-bindings
  15. :sqlite3-column-count
  16. :sqlite3-column-type
  17. :sqlite3-column-text
  18. :sqlite3-column-int64
  19. :sqlite3-column-double
  20. :sqlite3-column-bytes
  21. :sqlite3-column-blob
  22. :sqlite3-column-name
  23. :sqlite3-bind-parameter-count
  24. :sqlite3-bind-parameter-name
  25. :sqlite3-bind-parameter-index
  26. :sqlite3-bind-double
  27. :sqlite3-bind-int64
  28. :sqlite3-bind-null
  29. :sqlite3-bind-text
  30. :sqlite3-bind-blob
  31. :destructor-transient
  32. :destructor-static
  33. :sqlite3-last-insert-rowid))
  34. (in-package :sqlite-ffi)
  35. (define-foreign-library sqlite3-lib
  36. (:darwin (:default "libsqlite3"))
  37. (:unix (:or "libsqlite3.so.0" "libsqlite3.so"))
  38. (t (:or (:default "libsqlite3") (:default "sqlite3"))))
  39. (use-foreign-library sqlite3-lib)
  40. (defcenum error-code
  41. (:OK 0)
  42. (:ERROR 1)
  43. (:INTERNAL 2)
  44. (:PERM 3)
  45. (:ABORT 4)
  46. (:BUSY 5)
  47. (:LOCKED 6)
  48. (:NOMEM 7)
  49. (:READONLY 8)
  50. (:INTERRUPT 9)
  51. (:IOERR 10)
  52. (:CORRUPT 11)
  53. (:NOTFOUND 12)
  54. (:FULL 13)
  55. (:CANTOPEN 14)
  56. (:PROTOCOL 15)
  57. (:EMPTY 16)
  58. (:SCHEMA 17)
  59. (:TOOBIG 18)
  60. (:CONSTRAINT 19)
  61. (:MISMATCH 20)
  62. (:MISUSE 21)
  63. (:NOLFS 22)
  64. (:AUTH 23)
  65. (:FORMAT 24)
  66. (:RANGE 25)
  67. (:NOTADB 26)
  68. (:ROW 100)
  69. (:DONE 101))
  70. (defcstruct sqlite3)
  71. (defctype p-sqlite3 (:pointer sqlite3))
  72. (defcfun sqlite3-open error-code
  73. (filename :string)
  74. (db (:pointer p-sqlite3)))
  75. (defcfun sqlite3-close error-code
  76. (db p-sqlite3))
  77. (defcfun sqlite3-errmsg :string
  78. (db p-sqlite3))
  79. (defcfun sqlite3-busy-timeout :int
  80. (db p-sqlite3)
  81. (ms :int))
  82. (defcstruct sqlite3-stmt)
  83. (defctype p-sqlite3-stmt (:pointer sqlite3-stmt))
  84. (defcfun (sqlite3-prepare "sqlite3_prepare_v2") error-code
  85. (db p-sqlite3)
  86. (sql :string)
  87. (sql-length-bytes :int)
  88. (stmt (:pointer p-sqlite3-stmt))
  89. (tail (:pointer (:pointer :char))))
  90. (defcfun sqlite3-finalize error-code
  91. (statement p-sqlite3-stmt))
  92. (defcfun sqlite3-step error-code
  93. (statement p-sqlite3-stmt))
  94. (defcfun sqlite3-reset error-code
  95. (statement p-sqlite3-stmt))
  96. (defcfun sqlite3-clear-bindings error-code
  97. (statement p-sqlite3-stmt))
  98. (defcfun sqlite3-column-count :int
  99. (statement p-sqlite3-stmt))
  100. (defcenum type-code
  101. (:integer 1)
  102. (:float 2)
  103. (:text 3)
  104. (:blob 4)
  105. (:null 5))
  106. (defcfun sqlite3-column-type type-code
  107. (statement p-sqlite3-stmt)
  108. (column-number :int))
  109. (defcfun sqlite3-column-text :string
  110. (statement p-sqlite3-stmt)
  111. (column-number :int))
  112. (defcfun sqlite3-column-int64 :int64
  113. (statement p-sqlite3-stmt)
  114. (column-number :int))
  115. (defcfun sqlite3-column-double :double
  116. (statement p-sqlite3-stmt)
  117. (column-number :int))
  118. (defcfun sqlite3-column-bytes :int
  119. (statement p-sqlite3-stmt)
  120. (column-number :int))
  121. (defcfun sqlite3-column-blob :pointer
  122. (statement p-sqlite3-stmt)
  123. (column-number :int))
  124. (defcfun sqlite3-column-name :string
  125. (statement p-sqlite3-stmt)
  126. (column-number :int))
  127. (defcfun sqlite3-bind-parameter-count :int
  128. (statement p-sqlite3-stmt))
  129. (defcfun sqlite3-bind-parameter-name :string
  130. (statement p-sqlite3-stmt)
  131. (column-number :int))
  132. (defcfun sqlite3-bind-parameter-index :int
  133. (statement p-sqlite3-stmt)
  134. (name :string))
  135. (defcfun sqlite3-bind-double error-code
  136. (statement p-sqlite3-stmt)
  137. (parameter-index :int)
  138. (value :double))
  139. (defcfun sqlite3-bind-int64 error-code
  140. (statement p-sqlite3-stmt)
  141. (parameter-index :int)
  142. (value :int64))
  143. (defcfun sqlite3-bind-null error-code
  144. (statement p-sqlite3-stmt)
  145. (parameter-index :int))
  146. (defcfun sqlite3-bind-text error-code
  147. (statement p-sqlite3-stmt)
  148. (parameter-index :int)
  149. (value :string)
  150. (octets-count :int)
  151. (destructor :pointer))
  152. (defcfun sqlite3-bind-blob error-code
  153. (statement p-sqlite3-stmt)
  154. (parameter-index :int)
  155. (value :pointer)
  156. (bytes-count :int)
  157. (destructor :pointer))
  158. (defconstant destructor-transient-address (mod -1 (expt 2 (* 8 (cffi:foreign-type-size :pointer)))))
  159. (defun destructor-transient () (cffi:make-pointer destructor-transient-address))
  160. (defun destructor-static () (cffi:make-pointer 0))
  161. (defcfun sqlite3-last-insert-rowid :int64
  162. (db p-sqlite3))