-
Notifications
You must be signed in to change notification settings - Fork 8
/
sqlite3.el
71 lines (57 loc) · 2.36 KB
/
sqlite3.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;;; sqlite3.el --- sqlite3 binding of Emacs Lisp
;; Copyright (C) 2017 by Syohei YOSHIDA
;; Author: Syohei YOSHIDA <[email protected]>
;; URL: https://github.com/syohex/emacs-sqlite3
;; Package-Requires: ((emacs "25"))
;; Version: 0.01
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'cl-lib)
(require 'sqlite3-core)
;;;###autoload
(defun sqlite3-new (&optional dbpath)
"Create `sqlite3' instance. If `dbpath' is omitted, then database is stored
into memory."
(sqlite3-core-new dbpath))
(defun sqlite3-execute-batch (sqlite query &optional bounds)
"Execute SQL `query' for `db' database."
(cl-assert (not (null sqlite)))
(cl-assert (stringp query))
(if (null bounds)
(sqlite3-core-execute-batch sqlite query nil)
(unless (vectorp bounds)
(cl-assert (listp bounds))
(setq bounds (vconcat bounds)))
(sqlite3-core-execute-batch sqlite query bounds)))
(defun sqlite3-execute (sqlite query &rest args)
"Execute SQL `query' which has `SELECT' command.
Rest parameters are `bounds' and `callback'. You can its argument as,
either '(bounds) or '(callback) or '(bounds callback). `callback' function
is called with database row. `callback' takes two arguments, first argument
is row element of list, second argument is field names of list."
(cl-assert (not (null sqlite)))
(cl-assert (stringp query))
(cl-assert (<= (length args) 2))
(let* ((rargs (reverse args))
(callback (car rargs))
bounds)
(if (functionp callback)
(setq rargs (cdr rargs))
(setq callback nil))
(when rargs
(setq bounds (car rargs)))
(when (and bounds (not (vectorp bounds)))
(setq bounds (vconcat bounds)))
(sqlite3-core-execute sqlite query bounds callback)))
(provide 'sqlite3)
;;; sqlite3.el ends here