[clouchdb-cvs] CVS clouchdb/src

peddy peddy at common-lisp.net
Fri Dec 28 12:43:15 UTC 2007


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

Modified Files:
	clouchdb.lisp 
Log Message:
Added :if-exists options to various functions


--- /project/clouchdb/cvsroot/clouchdb/src/clouchdb.lisp	2007/12/21 19:58:32	1.13
+++ /project/clouchdb/cvsroot/clouchdb/src/clouchdb.lisp	2007/12/28 12:43:15	1.14
@@ -449,7 +449,7 @@
 				;; ?descending=false causes error ATM
 				`(("descending" . "true"))))))
 
-(defun get-document (id &key revision revisions revision-info)
+(defun get-document (id &key revision revisions revision-info (if-missing nil if-missing-p))
   "Get a document by ID. Returns nil if the document does not exist.
 The revision property specifies an optional revision number, if
 unspecified, the most recent revision is returned. The revisions and
@@ -472,7 +472,12 @@
 					 :method :get 
 					 :parameters parameters))))
       (if (document-property :error res)
-	  (error 'document-missing :id id)
+          (progn
+            (cond ((eq if-missing :ignore)
+                   nil)
+                  ((and if-missing-p (not (eq if-missing :error)))
+                   if-missing)
+                  (t (error 'document-missing :id id))))
 	  res))))
       
 (defun put-document (doc &key id)
@@ -545,7 +550,7 @@
 		     (string-join (mapcar #'document-to-json docs)) 
 		     " ] "))))
 
-(defun delete-document (&key document id revision)
+(defun delete-document (&key document id revision if-missing)
   "Delete a document. By default delete the current revision of the
 document. If specified, the document parameter must include the
 CouchDb special variables :_id and :_rev. If the id is speicified but
@@ -557,11 +562,16 @@
 			 :method :delete)))
     (cond ((not (null document))
 	   (delete-document :id (document-property :_id document)
-			    :revision (document-property :_rev document)))
+			    :revision (document-property :_rev document)
+                            :if-missing if-missing))
 	  ((not id)
 	   (error 'id-missing))
 	  ((not revision)
-	   (del id (document-property :_rev (get-document id))))
+           (let ((doc (get-document id :if-missing (if (eq if-missing :ignore) 
+                                                       :ignore
+                                                       :error))))
+             (when doc
+               (del id (document-property :_rev doc)))))
 	  (t (del id revision)))))
 ;;
 ;; Views API
@@ -601,10 +611,11 @@
 		  (cat "{\"language\" : \"text/javascript\"," 
 		       "\"views\" : {" (mk-view-js view-defs) "}}")))))
 
-(defun delete-view (id &key revision)
+(defun delete-view (id &key revision if-missing)
   "Delete identified view document"
   (ensure-db ()
-    (delete-document :id (cat "_design/" (url-encode id)) :revision revision)))
+    (delete-document :id (cat "_design/" (url-encode id)) 
+                     :revision revision :if-missing if-missing)))
 
 (defun invoke-view (id view &rest options &key key start-key start-key-docid
                     end-key count update descending skip)




More information about the clouchdb-cvs mailing list