Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added a new method updateDocFullAPI to implement extended features of…

… update handlers. Closes #23
  • Loading branch information...
commit c6a3916e564719a83346f4b2734605d3ffbe770e 1 parent cf058ed
Michael BAILLY authored
View
22 doc/couch_client-document.md
@@ -220,6 +220,10 @@ Updating a document
===================
Using CouchDB [Update handlers](http://wiki.apache.org/couchdb/Document_Update_Handlers), you can easily update any document part without having to send back the whole document.
+
+Basic API
+---------
+
The method **updateDoc( $ddoc_id, $handler_name, $params, $doc_id = null )** will try to update document according to the code defined in the update handler *$handler_name* of th design document *_design/$ddoc_id*.
Example : incrementing a document counter
@@ -241,6 +245,24 @@ To bump the counter of the document "some_doc" , use :
$client->updateDoc("myapp","bump-counter",array(),"some_doc");
+Full API
+--------
+
+The method **updateDocFullAPI($ddoc_id, $handler_name, $options)** will try to update document according to the code defined in the update handler *$handler_name* of th design document *_design/$ddoc_id*.
+
+$options is an array of optionnal query modifiers :
+"params" : array|object of variable to pass in the URL ( /?foo=bar )
+"data" : string|array|object data to set in the body of the request. If data is an array or an object it will be urlencoded using PHP http_build_query function and the request Content-Type header will be set to "application/x-www-form-urlencoded".
+"Content-Type": string the Content-Type HTTP header to send to the couch server
+
+
+Example :
+---------
+
+ $client->updateDocFullAPI("myapp","bump-counter",array( "data" => array("Something"=>"is set") ) );
+
+
+
Deleting a document
===================
View
61 lib/couchClient.php
@@ -145,9 +145,10 @@ public function __construct($dsn, $dbname, $options = array() ) {
* @param $array $allowed_status_code the list of HTTP response status codes that prove a successful request
* @param array $parameters additionnal parameters to send with the request
* @param string|object|array $data the request body. If it's an array or an object, $data is json_encode()d
+ * @param string $content_type set the content-type of the request
*/
- protected function _queryAndTest ( $method, $url,$allowed_status_codes, $parameters = array(),$data = NULL ) {
- $raw = $this->query($method,$url,$parameters,$data);
+ protected function _queryAndTest ( $method, $url,$allowed_status_codes, $parameters = array(),$data = NULL, $content_type = NULL ) {
+ $raw = $this->query($method,$url,$parameters,$data,$content_type);
$response = $this->parseRawResponse($raw, $this->results_as_array);
$this->results_as_array = false;
if ( in_array($response['status_code'], $allowed_status_codes) ) {
@@ -520,6 +521,7 @@ public function deleteDocs ( $docs, $all_or_nothing = false ) {
/**
* update a couchDB document through an Update Handler
+ * wrapper to $this->updateDocFullAPI
*
* @link http://wiki.apache.org/couchdb/Document_Update_Handlers
* @param string $ddoc_id name of the design doc containing the update handler definition (without _design)
@@ -530,10 +532,57 @@ public function deleteDocs ( $docs, $all_or_nothing = false ) {
public function updateDoc ( $ddoc_id, $handler_name, $params, $doc_id = null ) {
if ( !is_array($params) && !is_object($params) ) throw new InvalidArgumentException ("params parameter should be an array or an object");
if ( is_object($params) ) $params = (array)$params;
- $method = 'PUT';
- $url = '/'.urlencode($this->dbname).'/_design/'.urlencode($ddoc_id).'/_update/'.$handler_name.'/';
- if ( $doc_id ) $url .= urlencode($doc_id);
- return $this->_queryAndTest ($method, $url, array(200,201),$params);
+
+ $options = array();
+ if ( $doc_id ) $options["doc_id"] = $doc_id;
+ if ( $params ) $options["params"] = $params;
+
+ return $this->updateDocFullAPI($ddoc_id, $handler_name, $options);
+ }
+
+
+ /**
+ * update a couchDB document through an Update Handler
+ *
+ * @link http://wiki.apache.org/couchdb/Document_Update_Handlers
+ * @param string $ddoc_id name of the design doc containing the update handler definition (without _design)
+ * @param string $handler_name name of the update handler
+ * @param array $options list of optionnal data to send to the couch update handler.
+ * - "params" : array|object of variables being sent in the URL ( /?foo=bar )
+ * - "data" : string|array|object data being sent in the body of the request.
+ * If data is an array or an object it's parsed through PHP http_build_query function
+ * and the content-type of the request is set to "application/x-www-form-urlencoded"
+ * - "Content-Type" : the http header "Content-Type" to send to the couch server
+ * @param array|object $params parameters to send to the update handler
+ * @param string $doc_id id of the document to update (can be null)
+ */
+ public function updateDocFullAPI ( $ddoc_id, $handler_name, $options = array() ) {
+ $params = array();
+ $data = null;
+ $contentType = null;
+ $method = 'PUT';
+ $url = '/'.urlencode($this->dbname).'/_design/'.urlencode($ddoc_id).'/_update/'.$handler_name.'/';
+ if ( array_key_exists("doc_id",$options) && is_string($options["doc_id"]) ) {
+ $url .= urlencode($options["doc_id"]);
+ }
+ if ( array_key_exists("params",$options) && (is_array($options["params"]) || is_object($options["params"])) ) {
+ $params = $options["params"];
+ }
+ if ( array_key_exists("Content-Type",$options) && is_string($options["Content-Type"]) ) {
+ $contentType = $options["Content-Type"];
+ }
+
+ if ( array_key_exists("data",$options) ) {
+ if ( is_string($options["data"]) ) {
+ $data = $options["data"];
+ if ( !$contentType ) $contentType = "application/x-www-form-urlencoded";
+ } elseif ( is_array($options["data"]) || is_object($options["data"]) ) {
+ $data = http_build_query($options["data"]);
+ $contentType = "application/x-www-form-urlencoded";
+ }
+ }
+
+ return $this->_queryAndTest ($method, $url, array(200,201),$params,$data,$contentType);
}
/**
View
106 tests/couchClientUpdateDocTest.php
@@ -0,0 +1,106 @@
+<?php
+
+// error_reporting(E_STRICT);
+error_reporting(E_ALL);
+
+require_once 'PHPUnit/Framework.php';
+
+require_once "lib/couch.php";
+require_once "lib/couchClient.php";
+require_once "lib/couchDocument.php";
+require_once "lib/couchReplicator.php";
+
+
+class couchClientTest extends PHPUnit_Framework_TestCase
+{
+
+ private $couch_server = "http://localhost:5984/";
+
+ private $updateFn = <<<EOT
+function(doc,req) {
+ var resp = {query:null,form:null};
+ if ( "query" in req ) {
+ resp.query = req.query;
+ }
+ if ( "form" in req ) {
+ resp.form = req.form;
+ }
+ return [doc,{
+ headers: {"Content-Type": "application/json"},
+ body: JSON.stringify(resp)
+ }];
+}
+EOT
+;
+
+
+ public function setUp()
+ {
+ $this->client = new couchClient($this->couch_server,"couchclienttest");
+ try {
+ $this->client->deleteDatabase();
+ } catch ( Exception $e) {}
+ $this->client->createDatabase();
+
+ $ddoc = new stdClass();
+ $ddoc->_id = "_design/test";
+ $ddoc->updates = array("test" => $this->updateFn);
+ $this->client->storeDoc($ddoc);
+ $doc = new stdClass();
+ $doc->_id = "foo";
+ $this->client->storeDoc($doc);
+ }
+
+ public function tearDown()
+ {
+ $this->client = null;
+ }
+
+ public function testUpdate () {
+ $update = $this->client->updateDoc("test","test",array());
+ $this->assertType("object", $update);
+ $this->assertObjectHasAttribute("query",$update);
+ $this->assertType("object", $update->query);
+ $this->assertEquals(0, count((array)$update->query));
+ $this->assertObjectHasAttribute("form",$update);
+ $this->assertType("object", $update->form);
+ $this->assertEquals(0, count((array)$update->form));
+
+ }
+
+ public function testUpdateQuery () {
+ $update = $this->client->updateDoc("test","test",array("var1"=>"val1/?\"","var2"=>"val2"));
+ $this->assertType("object", $update);
+ $this->assertObjectHasAttribute("query",$update);
+ $this->assertType("object", $update->query);
+ $this->assertEquals(2, count((array)$update->query));
+ $this->assertObjectHasAttribute("var1",$update->query);
+ $this->assertType("string", $update->query->var1);
+ $this->assertEquals("val1/?\"", $update->query->var1);
+
+
+
+ $this->assertObjectHasAttribute("form",$update);
+ $this->assertType("object", $update->form);
+ $this->assertEquals(0, count((array)$update->form));
+ }
+
+ public function testUpdateForm () {
+ $update = $this->client->updateDocFullAPI("test","test",array(
+ "data"=> array("var1"=>"val1/?\"","var2"=>"val2")
+ ));
+ $this->assertType("object", $update);
+ $this->assertObjectHasAttribute("query",$update);
+ $this->assertType("object", $update->query);
+ $this->assertEquals(0, count((array)$update->query));
+ $this->assertObjectHasAttribute("form",$update);
+ $this->assertType("object", $update->form);
+ $this->assertEquals(2, count((array)$update->form));
+ $this->assertObjectHasAttribute("var1",$update->form);
+ $this->assertType("string", $update->form->var1);
+ $this->assertEquals("val1/?\"", $update->form->var1);
+
+ }
+
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.