[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/sqlite 4f83b27: Add an with-sqlite-transaction macro
From: |
Lars Ingebrigtsen |
Subject: |
scratch/sqlite 4f83b27: Add an with-sqlite-transaction macro |
Date: |
Tue, 7 Dec 2021 00:22:12 -0500 (EST) |
branch: scratch/sqlite
commit 4f83b278b3dcaa88108778452954bd3b417decaa
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
Add an with-sqlite-transaction macro
---
lisp/emacs-lisp/sticky.el | 67 ++++++++++++++++++++++-------------------------
lisp/sqlite.el | 42 +++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 36 deletions(-)
diff --git a/lisp/emacs-lisp/sticky.el b/lisp/emacs-lisp/sticky.el
index fcfc7d2..4c94836 100644
--- a/lisp/emacs-lisp/sticky.el
+++ b/lisp/emacs-lisp/sticky.el
@@ -25,6 +25,7 @@
(require 'cl-lib)
(require 'eieio)
+(require 'sqlite)
(defmacro define-sticky-variable (name initial-value &optional doc
&rest args)
@@ -68,20 +69,17 @@ DOC should be a doc string, and ARGS are keywords as
applicable to
(unless sticky--db
(setq sticky--db
(sqlite-open (expand-file-name "sticky.sqlite" "~/.emacs.d/"))))
- (unwind-protect
- (progn
- (sqlite-transaction sticky--db)
- (unless (sqlite-select
- sticky--db
- "select name from sqlite_master where type='table' and
name='sticky'")
- ;; Create the table.
- (sqlite-execute
- sticky--db
- "create table sticky (package text not null, key text not null,
sequence number not null, value text not null)")
- (sqlite-execute
- sticky--db
- "create unique index sticky_idx on sticky (package, key)")))
- (sqlite-commit sticky--db)))
+ (with-sqlite-transaction sticky--db
+ (unless (sqlite-select
+ sticky--db
+ "select name from sqlite_master where type='table' and
name='sticky'")
+ ;; Create the table.
+ (sqlite-execute
+ sticky--db
+ "create table sticky (package text not null, key text not null,
sequence number not null, value text not null)")
+ (sqlite-execute
+ sticky--db
+ "create unique index sticky_idx on sticky (package, key)"))))
(defun sticky-value (object)
"Return the value of the sticky OBJECT."
@@ -138,33 +136,30 @@ DOC should be a doc string, and ARGS are keywords as
applicable to
;; We have no backend, so just store the value.
(setf (sticky--cached-value object) value)
;; We have a backend.
- (unwind-protect
- (progn
- (sqlite-transaction sticky--db)
- (let* ((id (list (symbol-name (sticky--package object))
- (symbol-name (sticky--key object))))
- (old-sequence
- (sqlite-select
- sticky--db
- "select sequence from sticky where package = ? and key = ?"
id)))
- (if old-sequence
- (progn
- (setf (sticky--cached-sequence object) (1+ old-sequence))
- (sqlite-execute
- sticky--db
- "update sticky set value = ?, sequence = ? where package =
? and key = ?"
- (cons (prin1-to-string value)
- (cons (sticky--cached-sequence object)
- id))))
- (cl-incf (sticky--cached-sequence object))
+ (with-sqlite-transaction sticky--db
+ (let* ((id (list (symbol-name (sticky--package object))
+ (symbol-name (sticky--key object))))
+ (old-sequence
+ (sqlite-select
+ sticky--db
+ "select sequence from sticky where package = ? and key = ?"
id)))
+ (if old-sequence
+ (progn
+ (setf (sticky--cached-sequence object) (1+ old-sequence))
(sqlite-execute
sticky--db
- "insert into sticky (package, key, value, sequence) values (?,
?, ?, ?)"
+ "update sticky set value = ?, sequence = ? where package = ?
and key = ?"
(cons (prin1-to-string value)
(cons (sticky--cached-sequence object)
id))))
- (setf (sticky--cached-value object) value)))
- (sqlite-commit sticky--db))))
+ (cl-incf (sticky--cached-sequence object))
+ (sqlite-execute
+ sticky--db
+ "insert into sticky (package, key, value, sequence) values (?, ?,
?, ?)"
+ (cons (prin1-to-string value)
+ (cons (sticky--cached-sequence object)
+ id))))
+ (setf (sticky--cached-value object) value)))))
(gv-define-simple-setter sticky-value sticky--set-value)
diff --git a/lisp/sqlite.el b/lisp/sqlite.el
new file mode 100644
index 0000000..c95c8ef
--- /dev/null
+++ b/lisp/sqlite.el
@@ -0,0 +1,42 @@
+;;; sqlite.el --- Tests for empty.el -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2021 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs 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.
+
+;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+(defmacro with-sqlite-transaction (db &rest body)
+ "Execute BODY while holding a transaction for DB."
+ (declare (indent 1))
+ (let ((db-var (gensym)))
+ `(let ((,db-var ,db))
+ (if (sqlite-available-p)
+ (unwind-protect
+ (progn
+ (sqlite-transaction ,db-var)
+ ,@body)
+ (sqlite-commit ,db-var))
+ (progn
+ ,@body)))))
+
+(provide 'sqlite)
+
+;;; sqlite.el ends here
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- scratch/sqlite 4f83b27: Add an with-sqlite-transaction macro,
Lars Ingebrigtsen <=