[clouchdb-cvs] CVS clouchdb/src

peddy peddy at common-lisp.net
Sat Feb 16 19:36:07 UTC 2008


Update of /project/clouchdb/cvsroot/clouchdb/src
In directory clnet:/tmp/cvs-serv13173/src

Modified Files:
	clouchdb.lisp examples.lisp 
Log Message:
- Updated docs to reflect 0.0.8 release
- Remove some junk code
- Updated example to demonstrate use of query-document


--- /project/clouchdb/cvsroot/clouchdb/src/clouchdb.lisp	2008/01/19 20:23:53	1.20
+++ /project/clouchdb/cvsroot/clouchdb/src/clouchdb.lisp	2008/02/16 19:36:07	1.21
@@ -202,22 +202,6 @@
                      (write-string "%20" s))
                    (t (format s "%~2,'0x" (char-code c)))))))
 
-;; (defun alist-to-url-encoded-string (alist &key (external-format +utf-8+))
-;;   "ALIST is supposed to be an alist of name/value pairs where both
-;; names and values are strings.  This function returns a string where
-;; this list is represented as for the content type
-;; `application/x-www-form-urlencoded', i.e. the values are URL-encoded
-;; using the external format EXTERNAL-FORMAT, the pairs are joined with a
-;; #\\& character, and each name is separated from its value with a #\\=
-;; character."
-;;   (with-output-to-string (out)
-;;     (loop for first = t then nil
-;;           for (name . value) in alist
-;;           unless first do (write-char #\& out)
-;;           do (format out "~A=~A"
-;;                       (url-encode name :external-format external-format)
-;;                       (url-encode value :external-format external-format)))))
-
 (defun make-uri (&rest rest)
   "Return a URI containing *protocol*://*host*:*port*/ and the
 concatenation of the remaining parameters."
@@ -297,14 +281,25 @@
           (t (rplacd (assoc name doc) value)))
     doc))
 
-(defun set-document-property (doc name value)
+;; (defun set-document-property (doc name value)
+;;   "Set a property of a document. If the named property does not exist,
+;; add it to the document, otherwise change the existing value.  Does not
+;; destructively modify input document, so be sure to use return value."
+;;   (let ((doc (copy-tree doc)))
+;;     (if (assoc name doc)
+;;         (setf (document-property name doc) value)
+;;         (cons `(,(as-keyword-symbol name) . ,value) doc))))
+
+(defun set-document-property (doc &rest args)
   "Set a property of a document. If the named property does not exist,
 add it to the document, otherwise change the existing value.  Does not
 destructively modify input document, so be sure to use return value."
   (let ((doc (copy-tree doc)))
-    (if (assoc name doc)
-        (setf (document-property name doc) value)
-        (cons `(,(as-keyword-symbol name) . ,value) doc))))
+    (loop for (name value) on args by #'cddr
+       do (if (assoc name doc)
+              (setf (document-property name doc) value)
+              (setf doc (cons `(,(as-keyword-symbol name) . ,value) doc))))
+    doc))
 
 (defun document-id (doc)
   "Shortcut for getting the ID from the specified document. First
@@ -338,7 +333,7 @@
 document or null."
   (let ((res))
     (labels ((q (query doc rec)
-               ;;(format t "~%test: r=~s, query=~s doc=~s~%" rec query doc)
+;;               (format t "~%test: r=~s, query=~s doc=~s~%" rec query doc)
                (cond ((null doc)
                       nil)
                      ((null query)
--- /project/clouchdb/cvsroot/clouchdb/src/examples.lisp	2007/12/28 16:25:51	1.6
+++ /project/clouchdb/cvsroot/clouchdb/src/examples.lisp	2008/02/16 19:36:07	1.7
@@ -55,7 +55,7 @@
     (create-db :if-exists :recreate)
     ;; Create a document with ID of "haussmann"
     (create-document '(;; Fields with scalar values:
-		       (:name . "Georges-Eugène Haussmann") 
+		       (:name . "Georges-Eugène Haussmann") 
 		       (:aka . "Baron Haussmann")
 		       (:born . "1809-03-27")
 		       (:died . "1891-01-11"))
@@ -128,5 +128,109 @@
 		(document-property :id doc) doc)))))
 
 	
-      
-	     
\ No newline at end of file
+(defun example3 ()
+  "Demonstrate the use of (query-document). Create a database,
+populate it with some documents representing countries,
+use (query-document) to operate on this data."
+  (with-connection (:db-name "example3")
+    (create-db :if-exists :recreate)
+    (create-document '((:name . "Czech Republic")
+		       (:tags . ("country" "european"))
+		       (:motto . "Truth prevails")
+		       (:demographics . ((:population . 10230000)
+                                         (:capital . "Prague")
+                                         (:eu-accession . "2004-05-01")
+                                         (:president . "Václav Klaus")
+                                         (:prime-minister . "Mirek Topolánek"))))
+		     :id "czechrepublic")
+    (create-document '((:name . "France")
+		       (:tags . ("country" "european"))
+		       (:motto . "Liberté, Égalité, Fraternité")
+		       (:demographics . ((:population . 64473140)
+                                         (:capital . "Paris")
+                                         (:eu-accession . "1957-03-25")
+                                         (:president . "Nicolas Sarkozy")
+                                         (:prime-minister . "François Fillon"))))
+		     :id "france")
+    (create-document '((:name . "Italy")
+		       (:tags . ("country" "european"))
+		       (:motto . "Il Canto degli Italiani")
+		       (:demographics . ((:population . 59337888)
+                                         (:capital . "Rome")
+                                         (:eu-accession . "1957-03-17")
+                                         (:president . "Giorgio Napolitano")
+                                         (:prime-minister . "Romano Prodi"))))
+		     :id "italy")
+    (create-document '((:name . "Turkey")
+		       (:tags . ("country" "eurasian"))
+		       (:motto . "Yurtta Sulh, Cihanda Sulh")
+		       (:demographics . ((:population . 70586256)
+                                         (:capital . "Ankara")
+                                         (:prime-minister . "Recep Tayyip ErdoÄŸan")
+                                         (:president . "Abdulla Gül"))))
+		     :id "turkey")
+    ;; (get-all-documents) returns information about all documents in
+    ;; the database:
+    ;;
+    ;; ((:|total_rows| . 3) 
+    ;;  (:|offset| . 0) 
+    ;;  (:|rows| 
+    ;;    ((:|id| . "turkey") 
+    ;;     (:|key| . "turkey") 
+    ;;     (:|value| (:|rev| . "1081123848"))) 
+    ;;    ((:|id| . "czechrepublic") 
+    ;;     (:|key| . "czechrepublic") 
+    ;;     (:|value| (:|rev| . "3192261183")))
+    ;;    ((:|id| . "france") 
+    ;;     (:|key| . "france") 
+    ;;     (:|value| (:|rev| . "3199678281"))) 
+    ;;    ((:|id| . "italy") 
+    ;;     (:|key| . "italy")
+    ;;     (:|value| (:|rev| . "2716436578")))))
+    ;;
+    ;; The first use of (query-document) below retuns a list of
+    ;; document IDs (:|id|) values extracted from the alist values in
+    ;; :|rows|:
+    ;;
+    ;; (query-document '(:|rows| :|id|) (get-all-documents))
+    ;;  ==> ("turkey" "italy" "france" "czechrepublic")
+    ;;
+    ;; This list is then iterated and the country document ID is used
+    ;; to fetch the actual country document using (get-document).
+    ;;
+    ;; The second use of (query-document) below operates on individual
+    ;; country documents and recursively (:**) searches document
+    ;; properties for the :eu-accession values:
+    ;;
+    ;; (query-document '(:** :eu-accession) (get-document "france"))
+    ;; ==> ("1957-03-25")
+    ;;
+    ;; Alternatively this query could be expressed: 
+    ;; '(:demographics :eu-accession).
+    ;;
+    ;; This will produce the following output:
+    ;;
+    ;; Country: turkey, EU accession: NIL
+    ;; Country: italy, EU accession: 1957-03-17
+    ;; Country: france, EU accession: 1957-03-25
+    ;; Country: czechrepublic, EU accession: 2004-05-01
+    ;;
+    (dolist (country (query-document '(:|rows| :|id|) (get-all-documents)))
+      (format t "Country: ~a, EU accession: ~a~%" country
+              (car (query-document '(:** :eu-accession) (get-document country)))))
+    ;;
+    ;; It's possible to use a function in the query argument list of
+    ;; (query-document). The following example will return a list of
+    ;; all country presidents:
+    ;;
+    ;; ("Abdulla Gül" "Giorgio Napolitano" "Nicolas Sarkozy" "Václav Klaus")
+    ;;
+    ;; In this example the clouchdb function (get-document) is applied
+    ;; to all ID values found matching the previous :|id| query
+    ;; paramater, extracted from the (get-all-documents) results.  The
+    ;; remainder of the query expression is applied to the function
+    ;; result, which in this case is the fetched country document.
+    ;;
+    ;; Note the leading backquote in the query argument list
+    ;;
+    (query-document `(:|rows| :|id| ,#'get-document :** :president) (get-all-documents))))
\ No newline at end of file




More information about the clouchdb-cvs mailing list