branch: elpa/emacsql commit 578a71d2d8fca0da023197e1dbd014ca8f1cdaa0 Author: Christopher Wellons <well...@nullprogram.com> Commit: Christopher Wellons <well...@nullprogram.com>
Re-order some definitions to group them. --- emacsql.el | 72 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/emacsql.el b/emacsql.el index 2196d282ed..abec8f6771 100644 --- a/emacsql.el +++ b/emacsql.el @@ -61,10 +61,34 @@ (defvar emacsql-sqlite3-executable "sqlite3" "Path to the sqlite3 executable.") +(defun emacsql-sqlite3-unavailable-p () + "Return a reason if the sqlite3 executable is not available. + +:no-executable -- cannot find the executable +:cannot-execute -- cannot run the executable +:old-version -- sqlite3 version is too old" + (let ((sqlite3 emacsql-sqlite3-executable)) + (if (null (executable-find sqlite3)) + :no-executable + (condition-case _ + (with-temp-buffer + (call-process sqlite3 nil (current-buffer) nil "--version") + (let ((version (car (split-string (buffer-string))))) + (if (version< version "3.7.15") + :old-version + nil))) + (error :cannot-execute))))) + +;;; Connection handling: + (cl-defstruct (emacsql (:constructor emacsql--create)) "A connection to a SQLite database." process file log) +(defun emacsql-buffer (conn) + "Get proccess buffer for CONN." + (process-buffer (emacsql-process conn))) + (defvar emacsql-connections () "Collection of all known emacsql connections. This collection exists for cleanup purposes.") @@ -90,24 +114,6 @@ This collection exists for cleanup purposes.") do (accept-process-output))) (emacsql--clear conn)) -(defun emacsql-sqlite3-unavailable-p () - "Return a reason if the sqlite3 executable is not available. - -:no-executable -- cannot find the executable -:cannot-execute -- cannot run the executable -:old-version -- sqlite3 version is too old" - (let ((sqlite3 emacsql-sqlite3-executable)) - (if (null (executable-find sqlite3)) - :no-executable - (condition-case _ - (with-temp-buffer - (call-process sqlite3 nil (current-buffer) nil "--version") - (let ((version (car (split-string (buffer-string))))) - (if (version< version "3.7.15") - :old-version - nil))) - (error :cannot-execute))))) - (cl-defun emacsql-connect (file &key log) "Open a connected to database stored in FILE. If FILE is nil use an in-memory database. @@ -163,10 +169,6 @@ A statement can be a list, containing a statement with its arguments." else collect (append (list 'emacsql 'emacsql--conn) statement)))) -(defun emacsql-buffer (conn) - "Get proccess buffer for CONN." - (process-buffer (emacsql-process conn))) - (defun emacsql-reap () "Clean up after lost connections." (cl-loop for (conn-copy . ref) in emacsql-connections @@ -189,6 +191,8 @@ A statement can be a list, containing a statement with its arguments." (cancel-timer emacsql-reap-timer) (setf emacsql-reap-timer nil))) +;;; Sending and receiving: + (defun emacsql--log (conn &rest messages) "Log MESSAGES into CONN's log." (let ((log (emacsql-log conn))) @@ -231,6 +235,18 @@ A statement can be a list, containing a statement with its arguments." collect row into rows and do (setf row ()) finally (cl-return rows))))) +(defun emacsql--check-error (conn) + "Return non-nil or throw an appropriate error." + (with-current-buffer (emacsql-buffer conn) + (emacsql-wait conn) + (setf (point) (point-min)) + (prog1 t + (when (looking-at "Error:") + (error (buffer-substring (line-beginning-position) + (line-end-position))))))) + +;;; Escaping: + (defun emacsql-quote (string) "Quote STRING for use in a SQL expression." (format "'%s'" (replace-regexp-in-string "'" "''" string))) @@ -249,16 +265,6 @@ A statement can be a list, containing a statement with its arguments." (replace-regexp-in-string ":" "." string) string))) -(defun emacsql--check-error (conn) - "Return non-nil or throw an appropriate error." - (with-current-buffer (emacsql-buffer conn) - (emacsql-wait conn) - (setf (point) (point-min)) - (prog1 t - (when (looking-at "Error:") - (error (buffer-substring (line-beginning-position) - (line-end-position))))))) - (defun emacsql-wait (conn &optional timeout) "Block Emacs until CONN has finished sending output." (let ((end (when timeout (+ (float-time) timeout)))) @@ -279,7 +285,7 @@ A statement can be a list, containing a statement with its arguments." (list (mapconcat #'emacsql-escape-vector vector ", ")) (vector (concat "(" (mapconcat #'emacsql-escape-value vector ", ") ")")))) -;; SQL Expansion: +;; Structured SQL compilation: (defvar emacsql-expanders () "Alist of all expansion functions.")