Simple database: Difference between revisions

Add emacs lisp
m (→‎{{header|Wren}}: Wren-trait -> Wren-iterate)
(Add emacs lisp)
Line 1,417:
item6,2014-06-04T16:01:55,cat4
item7,2014-06-04T16:02:01,cat4</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun dbe-next-id ()
(unless (boundp '**dbe-current-id**) (setq **dbe-current-id** 1))
(let ((id **dbe-current-id**))
(cl-incf **dbe-current-id**)
id ) )
 
(defun dbe-rows (fn-handle-row &rest params)
(let ((has-more 't) current-line linum1 linum2
(fn-continue (plist-get params :continue)))
(save-window-excursion
(switch-to-buffer "**content**")
(beginning-of-buffer)
(if fn-continue (setq has-more (funcall fn-continue)))
(while has-more
(setq current-line
(buffer-substring (line-beginning-position)
(line-end-position)))
(unless (string= current-line "")
(funcall fn-handle-row (read current-line)))
(if fn-continue (setq has-more (funcall fn-continue)))
(setq linum1 (line-number-at-pos (point)))
(forward-line)
(setq linum2 (line-number-at-pos (point)))
(if (= linum1 linum2) (setq has-more nil))
)
)
)
)
(defun dbe-insert (row)
(interactive "xPlease enter the row data in plist format: ")
(save-window-excursion
(switch-to-buffer "**content**")
(end-of-buffer)
(insert (format "%s\n"
(append (list 'id (dbe-next-id)) (ensure-list row))))
)
)
 
(defun dbe-find-by-id (row-id)
(interactive "nPlease enter row id: ")
(let (row-found)
(dbe-rows (lambda (row)
;;(message " checking row: %s" row)
(when (equal (plist-get row 'id) row-id)
(setq row-found row)) )
:continue (lambda () (null row-found)))
(message "row found: %s" row-found)
)
)
</syntaxhighlight>
 
{{out}}
 
<pre>
M-x dbe-insert
Please enter the row data in plist format: (name "book1" author "author1")
 
M-x dbe-insert
Please enter the row data in plist format: (name "book2" author "author2")
 
M-x dbe-find-by-id
Please enter row id: 2
row found: (id 2 name book2 author author2)
</pre>
 
=={{header|Erlang}}==
59

edits