Permalink
Browse files

couchDocument update() and show() proxy methods. useDatabase() method…

… to change the target database
  • Loading branch information...
1 parent 0eb1250 commit 6158af32efcd14f667082fdf76105a2700ef37c4 @dready92 committed May 20, 2010
Showing with 97 additions and 10 deletions.
  1. +19 −6 doc/couch_client-database.md
  2. +35 −0 doc/couch_document.md
  3. +1 −1 frameworks/ci/README.md
  4. +15 −3 lib/couchClient.php
  5. +27 −0 lib/couchDocument.php
@@ -88,21 +88,34 @@ Example :
Get Universally Unique IDentifiers
-================
+==================================
The method **getUuids($count)** sends back an array of universally unique identifiers (that is, big strings that can be used as document ids)
Example :
print_r($client->getUuids(5));
/*
- array ( 0 => "65a8f6d272b3e5e62ee9de8eacc083a5",
- 1 => "e43b04e44233d72b353c1cd8915b886d",
- 2 => "7498fb296f19ebc2554a4812f3d9ae12",
- 3 => "f3f855a15eb90e9fcdbda5e017b9f2cd",
- 4 => "9d9a8214762d06cdf0158d7f6697cac9" )
+ array ( 0 => "65a8f6d272b3e5e62ee9de8eacc083a5",
+ 1 => "e43b04e44233d72b353c1cd8915b886d",
+ 2 => "7498fb296f19ebc2554a4812f3d9ae12",
+ 3 => "f3f855a15eb90e9fcdbda5e017b9f2cd",
+ 4 => "9d9a8214762d06cdf0158d7f6697cac9" )
*/
+Change the current database on a server
+=======================================
+
+The method **useDatabase($dbname)** changes the working database on the CouchDB server.
+
+Example :
+
+ $client = new couchClient("http://localhost:5984", "db1");
+ $all_docs_db1 = $client->getAllDocs(); //retrieve all docs of database db1
+ $client->useDatabase("db2"); //switch to "db2" database
+ $all_docs_db2 = $client->getAllDocs(); //retrieve all docs of database db2
+
+
Database changes interface
==========================
View
@@ -263,3 +263,38 @@ Example :
$doc->replicateTo("http://another.server.com:5984/mydb/");
The replicateFrom can have another argument, a boolean one. If true, the database will be created on the destination server if it doesn't exist.
+
+Formating Documents with show functions
+=======================================
+
+The **show($id,$name,$additionnal_parameters)** method parses the current document through a CouchDB show function.
+
+Example : the database contains the following design document :
+
+ {
+ "_id": "_design/clean",
+ "shows": {
+ "html": "function (doc, req) {
+ send('<p>ID: '+doc._id+', rev: '+doc._rev+'</p>');
+ }"
+ }
+ }
+
+and another document that got the id "some_doc". We load the "some_doc" document as a couchDocument object:
+
+ $doc = couchDocument::getInstance($client,"some_doc");
+
+We can then request couchDB to parse this document through a show function :
+
+ $html = $doc->show("clean","html");
+ // html should contain "<p>ID: some_doc, rev: 3-2342342346</p>"
+
+The show method is a proxy method to the **getShow()** method of **couchClient**.
+
+Updating a document using update handlers
+=========================================
+
+The **update($id,$name,$additionnal_params)** method allows to use the CouchDB [update handlers](http://wiki.apache.org/couchdb/Document_Update_Handlers) feature to update an existing document.
+The couchDocument object shouldd have an id for this to work ! Please see **couchClient** *updateDoc* method for more infos.
+
+
@@ -29,7 +29,7 @@ How to use it
- copy the PHP on Couch classes *couch.php*, *couchClient.php*, *couchDocument.php* and *couchReplicator.php* inside the **libraries** folder of your CodeIgniter application.
- copy the *libraries/couchdb.php* file inside the **libraries** folder of your CodeIgniter application.
- copy the *config/couchdb.php* file inside the **config** folder of your application
-- edit the config/couchdb.php file to set the two configuration options *couch_dsn* and *couch_database* :
+- edit the *config/couchdb.php* file to set the two configuration options *couch_dsn* and *couch_database* :
$config['couch_dsn'] = "http://localhost:5984/";
View
@@ -101,10 +101,9 @@ class couchClient extends couch {
* @param string $dbname CouchDB database name
*/
public function __construct($dsn, $dbname) {
- if ( !strlen($dbname) ) throw new InvalidArgumentException("Database name can't be empty");
- if ( !$this->isValidDatabaseName($dbname) ) throw new InvalidArgumentException('Database name contains invalid characters. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed.');
+ $this->useDatabase($dbname);
parent::__construct($dsn);
- $this->dbname = $dbname;
+
}
/**
@@ -159,6 +158,19 @@ function __call($name, $args) {
}
/**
+ * set the name of the couchDB database to work on
+ *
+ * @param string $dbname name of the database
+ * @return couchClient $this
+ */
+ public function useDatabase( $dbname ) {
+ if ( !strlen($dbname) ) throw new InvalidArgumentException("Database name can't be empty");
+ if ( !$this->isValidDatabaseName($dbname) ) throw new InvalidArgumentException('Database name contains invalid characters. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed.');
+ $this->dbname = $dbname;
+ return $this;
+ }
+
+ /**
* Tests a CouchDB database name and tell if it's a valid one
*
*
View
@@ -393,4 +393,31 @@ public function getAttachmentUri ($attachment_name ) {
return $this->getUri().'/'.$attachment_name;
}
+ /**
+ * just a proxy method to couchClient->getShow()
+ *
+ * @param string $id name of the design document containing the show function
+ * @param string $name name of the show function
+ * @param array $additionnal_params other parameters to send to couchServer
+ * @return mixed CouchDB show response
+ */
+ public function show ( $id,$name,$additionnal_params = array() ) {
+ return $this->__couch_data->client->getShow($id,$name,$this->_id,$additionnal_params);
+ }
+
+ /**
+ * just a proxy method to couchClient->updateDoc
+ *
+ * @param string $id name of the design document containing the update function
+ * @param string $name name of the update function
+ * @param array $additionnal_params other parameters to send to couch
+ * @return mixed couchDB update response
+ */
+ public function update( $id,$name,$additionnal_params = array() ) {
+ if ( !$this->_id ) return false;
+ $back = $this->__couch_data->client->updateDoc($id,$name, $additionnal_params, $this->_id);
+ // we should reload document to be sure that we have an up-to-date version
+ $this->load($this->_id);
+ return $back;
+ }
}

0 comments on commit 6158af3

Please sign in to comment.