LISP 简单的数据库 3.8 使用宏优化Where语句

之前的查询条件判断语句如下,它们有太多的相似之处,使用宏来生成真正需要被执行的语句


        (if title (equal (getf cd :title) title) t)
	(if artist (equal (getf cd :artist) artist) t)
	(if rating (equal (getf cd :rating) rating) t)
	(if ripped-p (equal (getf cd :ripped) ripped) t)

增加代码的强度及通用性

将之前where函数注释掉

;使用全局变量记录数据
(defvar *db* nil)

;数据记录格式
(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped)
)

;添加记录
(defun add-record (cd)
 ( push cd *db*)
)

;查看数据库内容
(defun dump-db()
  (dolist (cd *db*)
    (format t "~{~a: ~10t~a~%~}~%" cd))
)

;提示用户输入CD信息
(defun prompt-read (prompt)
  (format *query-io* "~a: " prompt)
  (force-output *query-io*)
  (read-line *query-io*)
)

;依次提示用户输入信息
(defun prompt-for-cd()
  (make-cd
   (prompt-read "Title")
   (prompt-read "Artist")
   ;(prompt-read "Rating")
   ;(prompt-read "Ripped [y/n]")

   ;快餐式验证数据有效性
   (or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
   (y-or-n-p "Ripped [y/n]: ")

  )
)

;批量添加数据,使用回车退出
(defun add-cds()
  (loop (add-record (prompt-for-cd))
     (if (not (y-or-n-p "Another? [y/n]:"))
	 (return)
      )
   )
)

;保存数据
(defun save-db (filename)
  (with-open-file(out filename :direction :output :if-exists :supersede)
    (with-standard-io-syntax(print *db* out))
   )
)

;载入数据
(defun load-db(filename)
  (with-open-file(in filename)
    (with-standard-io-syntax(setf *db* (read in)))
   )
)

;通过艺术家查找记录
(defun select-by-artist(artist)
  (remove-if-not #'(lambda (cd) (equal (getf cd :artist) artist)) *db*)
)

;优化查询 
(defun select(selector-fn)
  (remove-if-not selector-fn *db*)
)

;where条件判断
;(defun where (&key title artist rating (ripped nil ripped-p))
;  #'( lambda (cd)
;      (and
;        (if title (equal (getf cd :title) title) t)
;	(if artist (equal (getf cd :artist) artist) t)
;	(if rating (equal (getf cd :rating) rating) t)
;	(if ripped-p (equal (getf cd :ripped) ripped) t)
;       )
;     )
;)

;更新记录
(defun update(selector-fn &key title artist rating (ripped nil ripped-p))
  (setf *db*
	(mapcar #'(lambda (row) (when (funcall selector-fn row)
				  (if title (setf (getf row :title) title))
				  (if artist (setf (getf row :artist) artist))
				  (if rating (setf (getf row :rating) rating))
				  (if ripped-p (setf (getf row :ripped) ripped))
				  ) row) *db*)
   )
)

;使用宏重构where语句
(defun make-comparison-expr (field value)
  `(equal (getf cd ,field) ,value)
)

(defun make-comparisons-list (fields)
  (loop while fields
     collecting (make-comparison-expr (pop fields) (pop fields)))
)

(defmacro where (&rest clauses)
  `#'(lambda (cd) (and ,@(make-comparisons-list clauses)))
)


使用macroexpand-1函数,调用宏代码并返回其展开式
CL-USER> (macroexpand-1 '(where :title "Title1" :artist "Artist1"))
#'(LAMBDA (CD) (AND (EQUAL (GETF CD :TITLE) "Title1") (EQUAL (GETF CD :ARTIST) "Artist1")))
T

执行函数:

CL-USER> (select (where :title "Title1" :artist "Artist1"))
((:TITLE "Title1" :ARTIST "Artist1" :RATING 6 :RIPPED T))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值