1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Basic code for manipulating game databases
6 (defvar chess-database-handler nil)
8 (make-variable-buffer-local 'chess-database-handler)
10 (chess-message-catalog 'english
11 '((no-such-database . "There is no such chess database module '%s'")))
13 (defun chess-database-open (module file)
14 "Returns the opened database object, or nil."
15 (let* ((name (symbol-name module))
16 (handler (intern-soft (concat name "-handler")))
19 (chess-error 'no-such-database name))
20 (when (setq buffer (funcall handler 'open file))
21 (with-current-buffer buffer
22 (setq chess-database-handler handler)
23 (add-hook 'kill-buffer-hook 'chess-database-close nil t)
24 (add-hook 'after-revert-hook 'chess-database-rescan nil t)
27 (defsubst chess-database-command (database event &rest args)
28 (with-current-buffer database
29 (apply chess-database-handler event args)))
31 (defun chess-database-close (&optional database)
32 (let ((buf (or database (current-buffer))))
33 (when (buffer-live-p buf)
34 (with-current-buffer buf
35 (remove-hook 'kill-buffer-hook 'chess-database-close t))
36 (chess-database-command buf 'save)
37 (chess-database-command buf 'close)
40 (defun chess-database-save (database)
41 (chess-database-command database 'save))
43 (defun chess-database-rescan (&optional database)
44 (chess-database-command database 'rescan))
46 (defun chess-database-count (database)
47 (chess-database-command database 'count))
49 (defun chess-database-read (database index-or-moniker)
50 (if (integerp index-or-moniker)
51 (chess-database-command database 'read index-or-moniker)
52 (if (string-match "\\`\\([^:]+\\):\\([^#]+\\)#\\([0-9]+\\)\\'"
54 (let* ((type (match-string 1 index-or-moniker))
55 (path (match-string 2 index-or-moniker))
57 (match-string 3 index-or-moniker)))
58 (db (chess-database-open
59 (intern (concat "chess-" type)) path)))
61 (chess-database-read db index))))))
63 (defun chess-database-write (database game)
64 (chess-database-command database 'write game))
66 (defun chess-database-replace (database game &optional index)
67 (chess-database-command database 'replace game index))
69 (defun chess-database-query (database &rest terms)
70 (chess-database-command database 'query terms))
72 (provide 'chess-database)
74 ;;; chess-database.el ends here