1

我在 Common Lisp 中迈出了第一步,感谢 clouchdb http://common-lisp.net/project/clouchdb/#examples

我设法从 couchdb 获取了一些简单的数据

      (invoke-view "hulk" "hulk" )
 ((:|total_rows| . 2) (:|offset| . 0) (:|rows| ((:|id| . "gjc") (:|key| . "hulk") (:|value|                             
 (:|_id| . "gjc2321o3io13") (:|_rev| . "3-b6433781c65460f2c9b1f1a153953171")
  (:NAME . "Dr Bruce Banner") (:|kind| . "users") (:|username| . "hulk") (:|title| . "gamma r adia
 tions: what to do ?"))) ((:|id| . "irnmn239223") (:|key| . "ironman") (:|value| (:|_id| . "irnmn2     39223") 

  (:|_  rev| . "5-2b6cf739d24b1208fe8eca70e37ffdc9") (:|name| . "tony stark") (:|title| . 
(:|name| . "tony stark") (:|title| . "why iphone 5 sucks - but i own one \"") (:|kind| . "users") (:|username| . "ironman") (:|text| .  "welcome to post number one ......")))))

7 >

我正在使用 SEXML 显示 HTML 记录,因此我的 HTML 显示模板如下所示

 ;;static here 
 (<:h2 "((LISP RETRO BLOG))")
 (<:h3 "(( ***** RETRO BUT STILL COOL *****))")
 (<:p "( (MADE IN LISP ))")
 (<:p "READY.")
 (<:img :src "/img/prompt.gif" :alt "cursor"))
 ;;this is a variable
 (<:p "universal time: " mytime) 

现在我是否可以通过上述结果创建一个简单的循环(例如,用户名是 Ironman 和 hulk)以显示类似

  (<:p "Welcome!" username) 

很抱歉为最终可能是一个非常简单的循环发布了这么多代码。我确实阅读并尝试了示例(http://psg.com/~dlamkins/sl/chapter05.html)和其他资源,但我可能缺少一些非常基本的东西,希望您能提供帮助。请注意,couchdb 文档可能具有不同的字段,因此它与遍历具有模式的某些记录并不完全相同。这可能是相关的,例如,如果一个文档是一篇博客文章,它可能包含也可能不包含标签,所以我可能想显示/创建一个包含文档中所有可用数据的页面(可能不包括 _id)。

如果有不清楚的地方,请发表评论,我很乐意编辑问题。

提前致谢 !

4

2 回答 2

2

如果您将其重新格式化以使其更具可读性,您将更好地理解输出数据。像这样:

((:|total_rows| . 2) (:|offset| . 0)
 (:|rows|
   ((:|id| . "gjc") (:|key| . "hulk")
    (:|value|
      (:|_id| . "gjc2321o3io13")
      (:|_rev| . "3-b6433781c65460f2c9b1f1a153953171")
      (:NAME . "Dr Bruce Banner")
      (:|kind| . "users")
      (:|username| . "hulk")
      (:|title| . "gamma radiations: what to do ?")))
   ((:|id| . "irnmn239223") (:|key| . "ironman")
    (:|value|
      (:|_id| . "irnmn2     39223") 
      (:|_  rev| . "5-2b6cf739d24b1208fe8eca70e37ffdc9")
      (:|name| . "tony stark")
      (:|title| .

      ;; here you repeat name and title, so the previous and next lines are erroneous

      (:|name| . "tony stark")
      (:|title| . "why iphone 5 sucks - but i own one \"")
      (:|kind| . "users")
      (:|username| . "ironman")
      (:|text| .  "welcome to post number one ......")))))

所以,你通过 clouchdb 从 CouhcDB 得到的是一个特殊结构的列表, 在 Lisp 用语中称为alist 。有一组用于处理 alist 的函数,其中最重要的是ASSOC.

结果告诉您,您有 2 行,每行都将数据保存为另一个 alist。要迭代它们,您可以使用以下函数:

(defun maprows (fn data)
  (mapcar fn (cdr (assoc :|rows| data))))

现在你必须传入一个参数MAPROWS的函数FN。例如,如果您只想以合理的方式打印值,则可以传递以下函数。

(defun print-row (record)
  (dolist (pair (cdr (assoc :|value| record)))
    (format t "~A: ~A~%" (car pair) (cdr pair)))
  (terpri))

让我们看看它是如何工作的:

CL-USER> (maprows 'print-row
                  '((:|total_rows| . 2) (:|offset| . 0)
                    (:|rows|
                     ((:|id| . "gjc") (:|key| . "hulk")
                      (:|value|
                        (:|_id| . "gjc2321o3io13")
                        (:|_rev| . "3-b6433781c65460f2c9b1f1a153953171")
                        (:NAME . "Dr Bruce Banner")
                        (:|kind| . "users")
                        (:|username| . "hulk")
                        (:|title| . "gamma radiations: what to do ?")))
                     ((:|id| . "irnmn239223") (:|key| . "ironman")
                      (:|value|
                        (:|_id| . "irnmn2     39223") 
                        (:|_rev| . "5-2b6cf739d24b1208fe8eca70e37ffdc9")
                        (:|name| . "tony stark")
                        (:|title| . "why iphone 5 sucks - but i own one \"")
                        (:|kind| . "users")
                        (:|username| . "ironman")
                        (:|text| .  "welcome to post number one ......"))))))
_id: gjc2321o3io13
_rev: 3-b6433781c65460f2c9b1f1a153953171
NAME: Dr Bruce Banner
kind: users
username: hulk
title: gamma radiations: what to do ?

_id: irnmn2     39223
_rev: 5-2b6cf739d24b1208fe8eca70e37ffdc9
name: tony stark
title: why iphone 5 sucks - but i own one "
kind: users
username: ironman
text: welcome to post number one ......

(NIL NIL)

如您所见,MAPROWS它也收集应用程序的结果,FN底层也是如此MAPCAR

于 2012-10-02T05:04:25.067 回答
2

这里有一点不同的方法,虽然,我并不完全满足于此。也许有人会纠正我/提出更好的方法,但这种整体策略被称为对象映射。即,当您从数据库中的表中检索数据时,您将构建一个对象,该对象在您编写应用程序逻辑的语言中更加舒适。

现在,具有讽刺意味的是,CouchDB 应该超越这个阶段——因为它将对象编码为 JSON 格式,它对于 JavaScript 来说是“原生的”,它是常用的。但是,您在 CL 中收到的格式不适合使用/它不是对象的良好原生表示。现在,由于您不能在客户端浏览器应用程序中使用 CL,我真的看不出您的组合有什么意义......我的意思是,您在服务器端没有赢得任何东西(很可能,您会失去性能- 明智,但由于这是您的个人博客,因此这不太可能成为问题)。

所以,下面是一些天真的对象映射的尝试:

(defclass db-object () ())

(defun slot-list-from-query (query)
  (mapcar
   #'(lambda (pair)
       (let ((name (string-upcase (symbol-name (car pair)))))
         (list (intern name)
               :accessor
               (intern (concatenate 'string name "-OF"))
               :initarg (intern name "KEYWORD")))) (eval query)))

(defmacro table-to-class (name describe-table-query)
  `(let ((db-class (defclass ,name (db-object)
                     ,(slot-list-from-query describe-table-query))))
     (defmethod initialize-instance :after ((object ,name) &key raw-data)
                (mapcar
                 #'(lambda (pair)
                     (setf (slot-value object
                                       (find-symbol
                                        (string-upcase
                                         (symbol-name (car pair)))))
                           (cdr pair))) raw-data))
     db-class))

(defparameter *raw-data*
  '((:|total_rows| . 2) (:|offset| . 0)
    (:|rows|
     ((:|id| . "gjc") (:|key| . "hulk")
      (:|value|
        (:|_id| . "gjc2321o3io13")
        (:|_rev| . "3-b6433781c65460f2c9b1f1a153953171")
        (:|name| . "Dr Bruce Banner")
        (:|kind| . "users")
        (:|username| . "hulk")
        (:|title| . "gamma radiations: what to do ?")))
     ((:|id| . "irnmn239223") (:|key| . "ironman")
      (:|value|
        (:|_id| . "irnmn2     39223") 
        (:|_rev| . "5-2b6cf739d24b1208fe8eca70e37ffdc9")
        (:|name| . "tony stark")
        (:|title| . "why iphone 5 sucks - but i own one \"")
        (:|kind| . "users")
        (:|username| . "ironman")
        (:|text| .  "welcome to post number one ......"))))))

(table-to-class example-mapping (car (cdaddr *raw-data*)))
(id-of (make-instance 'example-mapping :id "foo")) ; foo
(id-of (make-instance 'example-mapping :raw-data (car (cdaddr *raw-data*)))) ; gjc

我冒昧地稍微“修复”了您的数据,以使其看起来一致。此外,通常您会在运行代码之前对表进行描述,因此在运行时声明类的整个麻烦将减少为您在部署之前会执行的一些机械操作,并且不需要eval在此宏。我这样发布它是为了保留您拥有的信息并让它看起来好像在部署代码后收到了一样。

如果您想使用它,您可以删除eval并执行以下操作:

(table-to-class example-mapping ((name-1 . value-1) (name-2 . value-2) ...))
于 2012-10-02T09:31:10.497 回答