From ecaf51e11a50032b3afa8690ef31dd1b2de0c5ce Mon Sep 17 00:00:00 2001 From: Shahruk Khan <Shahruksemail@gmail.com> Date: Tue, 30 Jul 2013 01:39:32 -0400 Subject: [PATCH 01/27] CakePHP 2.3 fixes --- Model/Datasource/CouchDBSource.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index 6e2a994..23aa426 100644 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -133,7 +133,7 @@ public function disconnect() { * * @return array Databases. */ - public function listSources() { + public function listSources($data = NULL) { $databases = $this->__decode($this->Socket->get($this->__uri('_all_dbs')), true); return $databases; } @@ -171,7 +171,7 @@ public function describe($model) { * @param array $values An array with key values of the fields. If null, $model->data will be used to generate the field names. * @return boolean Success. */ - public function create($model, $fields = null, $values = null) { + public function create(Model $model, $fields = null, $values = null) { $data = $model->data; if ($fields !== null && $values !== null) { $data = array_combine($fields, $values); @@ -202,7 +202,7 @@ public function create($model, $fields = null, $values = null) { * @param integer $recursive Level number of associations. * @return mixed False if an error occurred, otherwise an array of results. */ - public function read($model, $queryData = array(), $recursive = null) { + public function read(Model $model, $queryData = array(), $recursive = null) { if ($recursive === null && isset($queryData['recursive'])) { $recursive = $queryData['recursive']; } @@ -286,7 +286,7 @@ private function __readResult($model, $queryData, $result) { * @param mixed $conditions * @return boolean Success. */ - public function update($model, $fields = null, $values = null, $conditions = null) { + public function update(Model $model, $fields = null, $values = null, $conditions = null) { $data = $model->data[$model->alias]; if ($fields !== null && $values !== null) { $data = array_combine($fields, $values); @@ -346,7 +346,7 @@ private function __lastRevision(&$model, $id) { * @param mixed $conditions * @return boolean Success. */ - public function delete($model, $conditions = null) { + public function delete(Model $model, $conditions = null) { $id = $model->id; $rev = $model->rev; From 3f68aa92308759c2083175c6353d939066fd72ad Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 25 Sep 2013 16:34:42 -0300 Subject: [PATCH 02/27] Change just to keep the code pattern --- Model/Datasource/CouchDBSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index 23aa426..3614ab1 100644 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -133,7 +133,7 @@ public function disconnect() { * * @return array Databases. */ - public function listSources($data = NULL) { + public function listSources($data = null) { $databases = $this->__decode($this->Socket->get($this->__uri('_all_dbs')), true); return $databases; } From d5b9eed6facc4ecc8fa2c071231f977d52de7d82 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 15:05:01 -0300 Subject: [PATCH 03/27] Model example --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 3d1bed5..a4a11cd 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,25 @@ class DATABASE_CONFIG { The datasource works basically like CakePHP +### Creating a model (example) +```php +class Post extends AppModel { + + public $schema = array( + 'title' => array( + 'type' => 'string', + 'null' => true, + 'key' => 'primary', + 'length' => 32 + ) + ); + +} +``` + +You can set another CouchDB database name in your model using `public $useTable = 'adlayer';` + + ### Saving a document ```php From b971c1d677b40de62c99b98ef3033fd4730f0d15 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 15:24:52 -0300 Subject: [PATCH 04/27] Documentation improvement --- README.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a4a11cd..6b708ab 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # CouchDB datasource plugin for CakePHP +CouchDB datasource is a way to improve the communication from CakePHP application to CouchDB database. + DataSources are the link between models and the source of data that models represent. CouchDB is an open source document-oriented database written mostly in the Erlang programming language. ### Version -Written for CakePHP 2.1+ +Written for CakePHP 2.x ### Installation @@ -21,7 +23,6 @@ git clone https://github.com/maurymmarques/couchdb-datasource.git CouchDB Bootstrap the plugin in app/Config/bootstrap.php: ```php -<?php CakePlugin::load('CouchDB'); ``` @@ -30,7 +31,6 @@ CakePlugin::load('CouchDB'); Connection in app/Config/database.php: ```php -<?php class DATABASE_CONFIG { public $default = array( @@ -51,7 +51,8 @@ class DATABASE_CONFIG { The datasource works basically like CakePHP -### Creating a model (example) +### Creating a model + ```php class Post extends AppModel { @@ -67,13 +68,15 @@ class Post extends AppModel { } ``` -You can set another CouchDB database name in your model using `public $useTable = 'adlayer';` +You can set another CouchDB database name in your model using the attribute `Model::useTable` +```php +public $useTable = 'post'; +``` ### Saving a document ```php -<?php $data = array('title' => 'My new title'); $this->Post->save($data); @@ -87,7 +90,6 @@ $this->Post->rev; ### Search for a document ```php -<?php $conditions = array('Post.id' => $this->Post->id); $result = $this->Post->find('first', compact('conditions')); ``` @@ -95,7 +97,6 @@ $result = $this->Post->find('first', compact('conditions')); ### Change a document (changing the last revision) ```php -<?php $data = array('title' => 'My new title'); $this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420'; $this->Post->save($data); @@ -104,7 +105,6 @@ $this->Post->save($data); ### Change a document to a particular revision ```php -<?php $data = array('title' => 'My new title'); $this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420'; $this->Post->rev = '26-5cd5713759905feeee9b384edc4cfb61'; @@ -114,7 +114,6 @@ $this->Post->save($data); ### Deleting a document ```php -<?php $this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420'; $this->Post->delete($data); ``` @@ -124,7 +123,6 @@ $this->Post->delete($data); You can use the methods: curlGet, curlPost, curlPut, curlDelete ```php -<?php $post = array( 'source' => 'post', 'target' => 'post-replicate', From 7684529be97e48f0d9641556a23963d58373ce16 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 15:26:02 -0300 Subject: [PATCH 05/27] Documentation improvement --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b708ab..43af4d2 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ class Post extends AppModel { You can set another CouchDB database name in your model using the attribute `Model::useTable` ```php -public $useTable = 'post'; +public $useTable = 'posts'; ``` ### Saving a document From 8b88b0c2e1091f4456daa1ecc65ec2fc00a712ca Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 15:59:40 -0300 Subject: [PATCH 06/27] Documentation improvement --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 43af4d2..fb164ef 100644 --- a/README.md +++ b/README.md @@ -131,3 +131,7 @@ $post = array( $return = $this->Post->curlPost('_replicate', $post, true, false); ``` + +### Copyright + +Copyright (c) 2011 Maury M. Marques From ebbd284c34f4cd8d012aa6688c6c839a94e1fcd4 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 16:00:40 -0300 Subject: [PATCH 07/27] Documentation improvement --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fb164ef..3e1c73b 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,6 @@ $post = array( $return = $this->Post->curlPost('_replicate', $post, true, false); ``` -### Copyright +## Copyright Copyright (c) 2011 Maury M. Marques From ed3c4d1cf3c6ad7c2c6a52d2608c978f7532d941 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 16:04:59 -0300 Subject: [PATCH 08/27] Documentation improvement --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3e1c73b..9dd91ea 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ CouchDB is an open source document-oriented database written mostly in the Erlan Written for CakePHP 2.x -### Installation +## Installation You can clone the plugin into your project (or if you want you can use as a [submodule](http://help.github.com/submodules)): @@ -26,7 +26,7 @@ Bootstrap the plugin in app/Config/bootstrap.php: CakePlugin::load('CouchDB'); ``` -### Configuration +## Configuration Connection in app/Config/database.php: From 9bca1b0ce5117eb14318347d0d9b218693586f1a Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 16:07:11 -0300 Subject: [PATCH 09/27] Documentation improvement --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9dd91ea..0c2ddb5 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ CouchDB is an open source document-oriented database written mostly in the Erlan Written for CakePHP 2.x +### Copyright + +Copyright (c) 2011 Maury M. Marques ## Installation @@ -131,7 +134,3 @@ $post = array( $return = $this->Post->curlPost('_replicate', $post, true, false); ``` - -## Copyright - -Copyright (c) 2011 Maury M. Marques From e1b79b82c10210005b7e9c993855ef823cb364e9 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Wed, 9 Oct 2013 16:36:37 -0300 Subject: [PATCH 10/27] Documentation improvement --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c2ddb5..11a3a86 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CouchDB datasource plugin for CakePHP -CouchDB datasource is a way to improve the communication from CakePHP application to CouchDB database. +CouchDB datasource is a way to facilitate the communication from CakePHP application to CouchDB database. DataSources are the link between models and the source of data that models represent. From b4489be106723408f8cb4d44a1950f2657893617 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Tue, 15 Oct 2013 13:20:00 -0300 Subject: [PATCH 11/27] Removed unnecessary attributes and methods --- Model/Datasource/CouchDBSource.php | 54 ++++++++---------------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index 3614ab1..9ccf163 100644 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -1,47 +1,32 @@ <?php /** - * CouchDB Datasource + * CouchDB layer for DBO * - * PHP version 5 + * PHP 5 * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) - * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project - * @package datasources - * @subpackage datasources.models.datasources - * @since CakePHP Datasources v 0.3 - * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + * @package Plugin.Model.Datasource + * @since CakePHP(tm) v 0.9 + * @license http://www.opensource.org/licenses/mit-license.php MIT License */ + App::uses('HttpSocket', 'Network/Http'); -App::uses('DataSource', 'Model/Datasource'); /** * CouchDB Datasource * - * @package datasources - * @subpackage datasources.models.datasources + * @package Plugin.Model.Datasource */ class CouchDBSource extends DataSource { -/** - * Start quote - * - * @var string - */ - public $startQuote = null; - -/** - * End quote - * - * @var string - */ - public $endQuote = null; - /** * Constructor. * @@ -131,6 +116,7 @@ public function disconnect() { /** * List of databases. * + * @param mixed $data * @return array Databases. */ public function listSources($data = null) { @@ -138,19 +124,6 @@ public function listSources($data = null) { return $databases; } -/** - * Convenience method for DboSource::listSources(). - * Returns the names of databases in lowercase. - * - * @return array Lowercase databases. - */ - public function sources($reset = false) { - if ($reset === true) { - $this->_sources = null; - } - return array_map('strtolower', $this->listSources()); - } - /** * Returns a description of the model (metadata). * @@ -390,9 +363,10 @@ public function expression($expression) { * * @param mixed $model * @param boolean $quote + * @param boolean $schema * @return string Full name of table. */ - public function fullTableName($model = null, $quote = true) { + public function fullTableName($model, $quote = true, $schema = true) { $table = null; if (is_object($model)) { $table = $model->tablePrefix . $model->table; @@ -421,7 +395,7 @@ public function fullTableName($model = null, $quote = true) { * $this->Model->curlPost('document_name', array('field' => 'value'), true , false); * * @param string $method - * @param array $params ParĂ¢metros aceitos na ordem: uri, data, decode, assoc + * @param array $params Accepted parameters in order: uri, data, decode, assoc * @return object */ public function query($method, $params) { From 8d4b34e33e0362f3e0725d8ca22e7477888bac3f Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Tue, 15 Oct 2013 13:20:53 -0300 Subject: [PATCH 12/27] Test fixed --- Test/Case/Datasource/CouchDBSourceTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Test/Case/Datasource/CouchDBSourceTest.php b/Test/Case/Datasource/CouchDBSourceTest.php index 3424cad..31d002b 100644 --- a/Test/Case/Datasource/CouchDBSourceTest.php +++ b/Test/Case/Datasource/CouchDBSourceTest.php @@ -28,7 +28,6 @@ class Post extends AppModel { public $name = 'Post'; - public $useDbConfig = 'couchdb_test'; public $displayField = 'title'; public $recursive = -1; @@ -104,9 +103,10 @@ class CouchDBTestCase extends CakeTestCase { /** * Start Test * + * @param string $method * @return void */ - public function startTest() { + public function startTest($method) { config('database'); $config = new DATABASE_CONFIG(); @@ -117,6 +117,7 @@ public function startTest() { ConnectionManager::create('couchdb_test', $this->config); $this->Post = ClassRegistry::init('Post'); + $this->Post->useDbConfig = 'couchdb_test'; $this->removeAllDocuments(); } @@ -127,7 +128,7 @@ public function startTest() { */ public function testConnection() { $this->CouchDB = new CouchDBSource($this->config); - $this->CouchDB =& ConnectionManager::getDataSource($this->Post->useDbConfig); + $this->CouchDB = ConnectionManager::getDataSource($this->Post->useDbConfig); $reconnect = $this->CouchDB->reconnect($this->config); $this->assertIdentical($reconnect, true, __d('test_cases', 'Not reconnected')); @@ -608,9 +609,10 @@ private function removeAllDocuments() { /** * End Test * + * @param string $method * @return void */ - public function endTest() { + public function endTest($method) { $this->removeAllDocuments(); unset($this->Post); unset($this->CouchDB); From 627432f22144a442199bcd025a74b80bcbe01723 Mon Sep 17 00:00:00 2001 From: Fabien Mercier <fabien.mercier@mediapart.fr> Date: Thu, 21 Nov 2013 13:57:20 +0100 Subject: [PATCH 13/27] add parameter skip in read() method: usefull for pagination --- Model/Datasource/CouchDBSource.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) mode change 100644 => 100755 Model/Datasource/CouchDBSource.php diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php old mode 100644 new mode 100755 index 9ccf163..00480ba --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -188,9 +188,14 @@ public function read(Model $model, $queryData = array(), $recursive = null) { if (empty($queryData['conditions'])) { $params = $params . '_all_docs?include_docs=true'; + if (!empty($queryData['limit'])) { $params = $params . '&limit=' . $queryData['limit']; } + + if (!empty($queryData['offset'])) { + $params .= '&skip=' . $queryData['offset']; + } } else { if (isset($queryData['conditions'][$model->alias . '.' . $model->primaryKey])) { $params = $queryData['conditions'][$model->alias . '.' . $model->primaryKey]; @@ -487,4 +492,4 @@ private function __checkOk($object = null) { return isset($object->ok) && $object->ok === true; } } -?> \ No newline at end of file +?> From 151c06f351bd418b7ced1f502c7dc9a0f4f0f03c Mon Sep 17 00:00:00 2001 From: Fabien Mercier <fabien.mercier@mediapart.fr> Date: Thu, 2 Jan 2014 15:43:28 +0100 Subject: [PATCH 14/27] methods obsoletes replaced --- Test/Case/Datasource/CouchDBSourceTest.php | 140 ++++++++++----------- 1 file changed, 69 insertions(+), 71 deletions(-) mode change 100644 => 100755 Test/Case/Datasource/CouchDBSourceTest.php diff --git a/Test/Case/Datasource/CouchDBSourceTest.php b/Test/Case/Datasource/CouchDBSourceTest.php old mode 100644 new mode 100755 index 31d002b..918e245 --- a/Test/Case/Datasource/CouchDBSourceTest.php +++ b/Test/Case/Datasource/CouchDBSourceTest.php @@ -103,10 +103,9 @@ class CouchDBTestCase extends CakeTestCase { /** * Start Test * - * @param string $method * @return void */ - public function startTest($method) { + public function setUp() { config('database'); $config = new DATABASE_CONFIG(); @@ -131,10 +130,10 @@ public function testConnection() { $this->CouchDB = ConnectionManager::getDataSource($this->Post->useDbConfig); $reconnect = $this->CouchDB->reconnect($this->config); - $this->assertIdentical($reconnect, true, __d('test_cases', 'Not reconnected')); + $this->assertSame($reconnect, true, __d('test_cases', 'Not reconnected')); $disconnect = $this->CouchDB->disconnect(); - $this->assertIdentical($disconnect, true, __d('test_cases', 'Not disconnect')); + $this->assertSame($disconnect, true, __d('test_cases', 'Not disconnect')); } /** @@ -150,15 +149,15 @@ public function testFind() { $this->Post->save($data); $result = $this->Post->find('all'); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); $resultData = $result[0]['Post']; - $this->assertEqual(4, count($resultData)); + $this->assertEquals(4, count($resultData)); $this->assertTrue(!empty($resultData['id'])); - $this->assertEqual($this->Post->id, $resultData['id']); - $this->assertEqual($this->Post->rev, $resultData['rev']); - $this->assertEqual($data['title'], $resultData['title']); - $this->assertEqual($data['description'], $resultData['description']); + $this->assertEquals($this->Post->id, $resultData['id']); + $this->assertEquals($this->Post->rev, $resultData['rev']); + $this->assertEquals($data['title'], $resultData['title']); + $this->assertEquals($data['description'], $resultData['description']); } @@ -178,13 +177,13 @@ public function testFindConditions() { $this->Post->save($data); $result = $this->Post->find('all'); - $this->assertEqual(2, count($result)); + $this->assertEquals(2, count($result)); $result = $this->Post->find('all', array('conditions' => array('Post.id' => $this->Post->id))); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); $result = $this->Post->find('all', array('conditions' => array('id' => $this->Post->id))); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); } /** @@ -202,7 +201,7 @@ public function testFindRevs() { $this->Post->recursive = 0; $result = $this->Post->find('all', array('conditions' => array('id' => $this->Post->id))); - $this->assertEqual(2, count($result[0]['Post']['_revs_info'])); + $this->assertEquals(2, count($result[0]['Post']['_revs_info'])); } /** @@ -218,18 +217,18 @@ public function testSave() { $this->Post->create(); $saveResult = $this->Post->save($data); - $this->assertIdentical(is_array($saveResult), true); + $this->assertSame(is_array($saveResult), true); $result = $this->Post->find('all'); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); $resultData = $result[0]['Post']; - $this->assertEqual(4, count($resultData)); + $this->assertEquals(4, count($resultData)); $this->assertTrue(!empty($resultData['id'])); - $this->assertEqual($this->Post->id, $resultData['id']); - $this->assertEqual($this->Post->rev, $resultData['rev']); - $this->assertEqual($data['title'], $resultData['title']); - $this->assertEqual($data['description'], $resultData['description']); + $this->assertEquals($this->Post->id, $resultData['id']); + $this->assertEquals($this->Post->rev, $resultData['rev']); + $this->assertEquals($data['title'], $resultData['title']); + $this->assertEquals($data['description'], $resultData['description']); } /** @@ -246,19 +245,19 @@ public function testSaveWithId() { $this->Post->create(); $saveResult = $this->Post->save($data); - $this->assertIdentical(is_array($saveResult), true); + $this->assertSame(is_array($saveResult), true); $result = $this->Post->find('all'); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); $resultData = $result[0]['Post']; - $this->assertEqual(4, count($resultData)); + $this->assertEquals(4, count($resultData)); $this->assertTrue(!empty($resultData['id'])); - $this->assertEqual($resultData['id'], $data['id']); - $this->assertEqual($this->Post->id, $resultData['id']); - $this->assertEqual($this->Post->rev, $resultData['rev']); - $this->assertEqual($data['title'], $resultData['title']); - $this->assertEqual($data['description'], $resultData['description']); + $this->assertEquals($resultData['id'], $data['id']); + $this->assertEquals($this->Post->id, $resultData['id']); + $this->assertEquals($this->Post->rev, $resultData['rev']); + $this->assertEquals($data['title'], $resultData['title']); + $this->assertEquals($data['description'], $resultData['description']); } /** @@ -281,19 +280,19 @@ public function testSaveAll() { $saveResult = $this->Post->saveAll($data); $result = $this->Post->find('all'); - $this->assertEqual(2, count($result)); + $this->assertEquals(2, count($result)); $resultData = $result[0]['Post']; - $this->assertEqual(4, count($resultData)); + $this->assertEquals(4, count($resultData)); $this->assertTrue(!empty($resultData['id'])); - $this->assertEqual($data[0]['Post']['title'], $resultData['title']); - $this->assertEqual($data[0]['Post']['description'], $resultData['description']); + $this->assertEquals($data[0]['Post']['title'], $resultData['title']); + $this->assertEquals($data[0]['Post']['description'], $resultData['description']); $resultData = $result[1]['Post']; - $this->assertEqual(4, count($resultData)); + $this->assertEquals(4, count($resultData)); $this->assertTrue(!empty($resultData['id'])); - $this->assertEqual($data[1]['Post']['title'], $resultData['title']); - $this->assertEqual($data[1]['Post']['description'], $resultData['description']); + $this->assertEquals($data[1]['Post']['title'], $resultData['title']); + $this->assertEquals($data[1]['Post']['description'], $resultData['description']); } /** @@ -321,15 +320,15 @@ public function updateTest() { // Final test $result = $this->Post->find('all'); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); $resultData = $result[0]['Post']; - $this->assertEqual(4, count($resultData)); + $this->assertEquals(4, count($resultData)); $this->assertTrue(!empty($resultData['id'])); - $this->assertEqual($this->Post->id, $resultData['id']); - $this->assertEqual($this->Post->rev, $resultData['rev']); - $this->assertNotEqual($updateData['title'], $resultData['title']); - $this->assertNotEqual($updateData['description'], $resultData['description']); + $this->assertEquals($this->Post->id, $resultData['id']); + $this->assertEquals($this->Post->rev, $resultData['rev']); + $this->assertNotEquals($updateData['title'], $resultData['title']); + $this->assertNotEquals($updateData['description'], $resultData['description']); } @@ -349,13 +348,13 @@ private function updateTest1($uri, $post, $previousCount) { $this->Post->create(); $saveResult = $this->Post->save($data); - $this->assertIdentical(is_array($saveResult), true); - $this->assertIdentical(!empty($this->Post->id), true); + $this->assertSame(is_array($saveResult), true); + $this->assertSame(!empty($this->Post->id), true); $mapReduce = $this->Post->curlPost($uri, $post); $count1 = $mapReduce['rows'][0]['value']; - $this->assertIdentical($count1 - $previousCount, 1); + $this->assertSame($count1 - $previousCount, 1); return $count1; } @@ -370,7 +369,7 @@ private function updateTest1($uri, $post, $previousCount) { */ private function updateTest2($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); - $this->assertEqual(4, count($findResult['Post'])); + $this->assertEquals(4, count($findResult['Post'])); $updateData = array( 'title' => 'My post update', @@ -380,12 +379,12 @@ private function updateTest2($uri, $post, $previousCount) { $this->Post->id = $findResult['Post']['id']; $this->Post->rev = $findResult['Post']['rev']; $saveResult = $this->Post->save($updateData); - $this->assertIdentical(is_array($saveResult), true); + $this->assertSame(is_array($saveResult), true); $mapReduce = $this->Post->curlPost($uri, $post); $count2 = $mapReduce['rows'][0]['value']; - $this->assertIdentical($count2 - $previousCount, 0); + $this->assertSame($count2 - $previousCount, 0); return $count2; } @@ -400,7 +399,7 @@ private function updateTest2($uri, $post, $previousCount) { */ private function updateTest3($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); - $this->assertEqual(4, count($findResult['Post'])); + $this->assertEquals(4, count($findResult['Post'])); $updateData = array( 'id' => $findResult['Post']['id'], @@ -410,13 +409,13 @@ private function updateTest3($uri, $post, $previousCount) { $this->Post->rev = $findResult['Post']['rev']; $saveResult = $this->Post->save($updateData); - $this->assertIdentical(is_array($saveResult), true); - $this->assertIdentical($this->Post->id, $findResult['Post']['id']); + $this->assertSame(is_array($saveResult), true); + $this->assertSame($this->Post->id, $findResult['Post']['id']); $mapReduce = $this->Post->curlPost($uri, $post); $count3 = $mapReduce['rows'][0]['value']; - $this->assertIdentical($count3 - $previousCount, 0); + $this->assertSame($count3 - $previousCount, 0); return $count3; } @@ -431,7 +430,7 @@ private function updateTest3($uri, $post, $previousCount) { */ private function updateTest4($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); - $this->assertEqual(4, count($findResult['Post'])); + $this->assertEquals(4, count($findResult['Post'])); $updateData = array( 'id' => $findResult['Post']['id'], @@ -441,13 +440,13 @@ private function updateTest4($uri, $post, $previousCount) { ); $saveResult = $this->Post->save($updateData); - $this->assertIdentical(is_array($saveResult), true); - $this->assertIdentical($this->Post->id, $findResult['Post']['id']); + $this->assertSame(is_array($saveResult), true); + $this->assertSame($this->Post->id, $findResult['Post']['id']); $mapReduce = $this->Post->curlPost($uri, $post); $count4 = $mapReduce['rows'][0]['value']; - $this->assertIdentical($count4 - $previousCount, 0); + $this->assertSame($count4 - $previousCount, 0); return $count4; } @@ -462,7 +461,7 @@ private function updateTest4($uri, $post, $previousCount) { */ private function updateTest5($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); - $this->assertEqual(4, count($findResult['Post'])); + $this->assertEquals(4, count($findResult['Post'])); $updateData = array( 'id' => $findResult['Post']['id'], @@ -473,12 +472,12 @@ private function updateTest5($uri, $post, $previousCount) { $saveResult = $this->Post->save($updateData); $this->assertFalse($saveResult); - $this->assertIdentical($this->Post->id, $findResult['Post']['id']); + $this->assertSame($this->Post->id, $findResult['Post']['id']); $mapReduce = $this->Post->curlPost($uri, $post); $count5 = $mapReduce['rows'][0]['value']; - $this->assertIdentical($count5 - $previousCount, 0); + $this->assertSame($count5 - $previousCount, 0); return $updateData; } @@ -504,8 +503,8 @@ public function testUpdateWithoutRevision() { $updateResult = $this->Post->save($result); - $this->assertIdentical(is_array($updateResult), true); - $this->assertIdentical($this->Post->id, $saveResult['Post']['id']); + $this->assertSame(is_array($updateResult), true); + $this->assertSame($this->Post->id, $saveResult['Post']['id']); } /** @@ -523,14 +522,14 @@ public function testDelete() { $saveResult = $this->Post->save($data); $result = $this->Post->find('all'); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); $this->Post->id = $result[0]['Post']['id']; $this->Post->rev = $result[0]['Post']['rev']; $this->Post->delete(); $result = $this->Post->find('all'); - $this->assertEqual(0, count($result)); + $this->assertEquals(0, count($result)); } /** @@ -548,7 +547,7 @@ public function testDeleteWithoutRevision() { $saveResult = $this->Post->save($data); $result = $this->Post->find('all'); - $this->assertEqual(1, count($result)); + $this->assertEquals(1, count($result)); unset($result['Post']['rev']); unset($this->Post->rev); @@ -556,7 +555,7 @@ public function testDeleteWithoutRevision() { $this->Post->delete(); $result = $this->Post->find('all'); - $this->assertEqual(0, count($result)); + $this->assertEquals(0, count($result)); } /** @@ -567,7 +566,7 @@ public function testDeleteWithoutRevision() { public function testQuery() { // GET $result = $this->Post->curlGet('_all_dbs'); - $this->assertIdentical(is_array($result), true); + $this->assertSame(is_array($result), true); // POST $data = array( @@ -576,7 +575,7 @@ public function testQuery() { ); $result = $this->Post->curlPost('/posts', $data); - $this->assertIdentical($result['ok'], true); + $this->assertSame($result['ok'], true); // PUT $data = array( @@ -586,11 +585,11 @@ public function testQuery() { ); $result = $this->Post->curlPut('/posts/' . $result['id'], $data); - $this->assertIdentical($result['ok'], true); + $this->assertSame($result['ok'], true); // DELETE $result = $this->Post->curlDelete('/posts/' . $result['id'] . '/?rev=' . $result['rev']); - $this->assertIdentical($result['ok'], true); + $this->assertSame($result['ok'], true); } /** @@ -609,10 +608,9 @@ private function removeAllDocuments() { /** * End Test * - * @param string $method * @return void */ - public function endTest($method) { + public function tearDown() { $this->removeAllDocuments(); unset($this->Post); unset($this->CouchDB); From 65160e2a204e69f240e93bb65293679bf197a127 Mon Sep 17 00:00:00 2001 From: Fabien Mercier <fabien.mercier@mediapart.fr> Date: Thu, 2 Jan 2014 16:15:04 +0100 Subject: [PATCH 15/27] quality of code improved --- Test/Case/Datasource/CouchDBSourceTest.php | 74 ++++++++++++---------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/Test/Case/Datasource/CouchDBSourceTest.php b/Test/Case/Datasource/CouchDBSourceTest.php index 918e245..1f38dda 100755 --- a/Test/Case/Datasource/CouchDBSourceTest.php +++ b/Test/Case/Datasource/CouchDBSourceTest.php @@ -20,7 +20,7 @@ App::uses('AppModel', 'Model'); /** - * Post Model for the test + * Post Model for the test. * * @package app * @subpackage app.model.post @@ -70,7 +70,7 @@ class Post extends AppModel { } /** - * CouchDBTestCase + * CouchDBTestCase. * * @package datasources * @subpackage datasources.tests.cases.models.datasources @@ -78,30 +78,30 @@ class Post extends AppModel { class CouchDBTestCase extends CakeTestCase { /** - * CouchDB Datasource object + * CouchDB Datasource object. * * @var object */ public $CouchDB = null; /** - * Configuration + * Configuration. * * @var array */ protected $config = array( - 'datasource' => 'CouchDB.CouchDBSource', - 'persistent' => false, - 'host' => 'localhost', - 'port' => '5984', - 'login' => 'root', - 'password' => 'root', - 'database' => null, - 'prefix' => '' + 'datasource' => 'CouchDB.CouchDBSource', + 'persistent' => false, + 'host' => 'localhost', + 'port' => '5984', + 'login' => 'root', + 'password' => '', + 'database' => null, + 'prefix' => '', ); /** - * Start Test + * Start Test. * * @return void */ @@ -121,7 +121,7 @@ public function setUp() { } /** - * testConnection + * Test connection. * * @return void */ @@ -137,7 +137,7 @@ public function testConnection() { } /** - * testFind + * Test find. * * @return void */ @@ -160,9 +160,8 @@ public function testFind() { $this->assertEquals($data['description'], $resultData['description']); } - /** - * testFindConditions + * Test find conditions. * * @return void */ @@ -187,7 +186,7 @@ public function testFindConditions() { } /** - * testFindRevs + * Test find revs. * * @return void */ @@ -309,8 +308,13 @@ public function updateTest() { ); $mapReduce = $this->Post->query($uri, $post); - if(isset($mapReduce->rows[0]->value)) $count0 = $mapReduce->rows[0]->value; - else $count0 = 0; + + if (isset($mapReduce->rows[0]->value)) { + $count0 = $mapReduce->rows[0]->value; + } + else { + $count0 = 0; + } $count1 = $this->updateTest1($uri, $post, $count0); $count2 = $this->updateTest2($uri, $post, $count1); @@ -329,7 +333,6 @@ public function updateTest() { $this->assertEquals($this->Post->rev, $resultData['rev']); $this->assertNotEquals($updateData['title'], $resultData['title']); $this->assertNotEquals($updateData['description'], $resultData['description']); - } /** @@ -337,8 +340,8 @@ public function updateTest() { * * @param string $uri * @param array $post - * @param interger $previousCount - * @return void + * @param integer $previousCount + * @return integer */ private function updateTest1($uri, $post, $previousCount) { $data = array( @@ -364,8 +367,8 @@ private function updateTest1($uri, $post, $previousCount) { * * @param string $uri * @param array $post - * @param interger $previousCount - * @return void + * @param integer $previousCount + * @return integer */ private function updateTest2($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); @@ -394,8 +397,8 @@ private function updateTest2($uri, $post, $previousCount) { * * @param string $uri * @param array $post - * @param interger $previousCount - * @return void + * @param integer $previousCount + * @return integer */ private function updateTest3($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); @@ -425,8 +428,8 @@ private function updateTest3($uri, $post, $previousCount) { * * @param string $uri * @param array $post - * @param interger $previousCount - * @return void + * @param integer $previousCount + * @return integer */ private function updateTest4($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); @@ -456,8 +459,8 @@ private function updateTest4($uri, $post, $previousCount) { * * @param string $uri * @param array $post - * @param interger $previousCount - * @return void + * @param integer $previousCount + * @return integer */ private function updateTest5($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); @@ -479,11 +482,11 @@ private function updateTest5($uri, $post, $previousCount) { $this->assertSame($count5 - $previousCount, 0); - return $updateData; + return $count5; } /** - * Test update without revision + * Test update without revision. * * @return void */ @@ -533,7 +536,7 @@ public function testDelete() { } /** - * Test delete without revision + * Test delete without revision. * * @return void */ @@ -593,12 +596,13 @@ public function testQuery() { } /** - * Remove all documents from database + * Remove all documents from database. * * @return void */ private function removeAllDocuments() { $posts = $this->Post->find('list', array('fields' => array('Post.rev'))); + foreach($posts as $id => $post) { $this->Post->rev = $post; $this->Post->delete($id); From 273f44e54d847b0cc4702fcc786ae7ed4eb8535c Mon Sep 17 00:00:00 2001 From: Fabien Mercier <fabien.mercier@mediapart.fr> Date: Sun, 12 Jan 2014 19:40:31 +0100 Subject: [PATCH 16/27] startkey_docid and descending options added --- Model/Datasource/CouchDBSource.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index 00480ba..4f06689 100755 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -196,6 +196,14 @@ public function read(Model $model, $queryData = array(), $recursive = null) { if (!empty($queryData['offset'])) { $params .= '&skip=' . $queryData['offset']; } + + if (!empty($queryData['startkey_docid'])) { + $params .= '&startkey_docid=' . $queryData['startkey_docid']; + } + + if (!empty($queryData['descending'])) { + $params .= '&descending=' . $queryData['descending']; + } } else { if (isset($queryData['conditions'][$model->alias . '.' . $model->primaryKey])) { $params = $queryData['conditions'][$model->alias . '.' . $model->primaryKey]; From 2b9beda75ca7f4ef1327482b3a9cd6d19a78aa37 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Mon, 20 Jan 2014 17:47:10 -0200 Subject: [PATCH 17/27] Fixed creating document with pre-defined primary key --- Model/Datasource/CouchDBSource.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index 4f06689..c0d572d 100755 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -152,6 +152,7 @@ public function create(Model $model, $fields = null, $values = null) { if (isset($data[$model->primaryKey]) && !empty($data[$model->primaryKey])) { $params = $data[$model->primaryKey]; + unset($data[$model->primaryKey]); } else { $uuids = $this->__decode($this->Socket->get('/_uuids')); $params = $uuids->uuids[0]; From f08f89fb22650e0b5f8d3fe1f319fe0811805c81 Mon Sep 17 00:00:00 2001 From: Fabien Mercier <fabien.mercier@mediapart.fr> Date: Sun, 26 Jan 2014 14:44:10 +0100 Subject: [PATCH 18/27] Clean code with phpcs and CakePHP rules --- Model/Datasource/CouchDBSource.php | 92 +++++++++++++++------ Test/Case/Datasource/CouchDBSourceTest.php | 95 +++++++++++++--------- 2 files changed, 124 insertions(+), 63 deletions(-) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index 4f06689..3fc0860 100755 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -1,8 +1,6 @@ <?php /** - * CouchDB layer for DBO - * - * PHP 5 + * CouchDB layer for DBO. * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) @@ -13,7 +11,7 @@ * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project - * @package Plugin.Model.Datasource + * @package CouchDB.Model.Datasource * @since CakePHP(tm) v 0.9 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ @@ -21,9 +19,9 @@ App::uses('HttpSocket', 'Network/Http'); /** - * CouchDB Datasource + * CouchDB Datasource. * - * @package Plugin.Model.Datasource + * @package CouchDB.Model.Datasource */ class CouchDBSource extends DataSource { @@ -60,6 +58,7 @@ public function reconnect($config = null) { $this->disconnect(); $this->setConfig($config); $this->_sources = null; + return $this->connect(); } @@ -67,14 +66,17 @@ public function reconnect($config = null) { * Connects to the database. Options are specified in the $config instance variable. * * @return boolean Connected. + * @throws MissingConnectionException */ public function connect() { if ($this->connected !== true) { - if (Set::check($this->config, 'login')) + if (Set::check($this->config, 'login')) { $this->config = Set::insert($this->config, 'request.uri.user', Set::get($this->config, 'login')); + } - if (Set::check($this->config, 'password')) + if (Set::check($this->config, 'password')) { $this->config = Set::insert($this->config, 'request.uri.pass', Set::get($this->config, 'password')); + } try { $this->Socket = new HttpSocket($this->config); @@ -83,6 +85,7 @@ public function connect() { throw new MissingConnectionException(array('class' => $e->getMessage())); } } + return $this->connected; } @@ -97,6 +100,7 @@ public function close() { if (Configure::read('debug') > 1) { //$this->showLog(); } + $this->disconnect(); } @@ -109,7 +113,9 @@ public function disconnect() { if (isset($this->results) && is_resource($this->results)) { $this->results = null; } + $this->connected = false; + return !$this->connected; } @@ -121,6 +127,7 @@ public function disconnect() { */ public function listSources($data = null) { $databases = $this->__decode($this->Socket->get($this->__uri('_all_dbs')), true); + return $databases; } @@ -146,6 +153,7 @@ public function describe($model) { */ public function create(Model $model, $fields = null, $values = null) { $data = $model->data; + if ($fields !== null && $values !== null) { $data = array_combine($fields, $values); } @@ -162,8 +170,10 @@ public function create(Model $model, $fields = null, $values = null) { if ($this->__checkOk($result)) { $model->id = $result->id; $model->rev = $result->rev; + return true; } + return false; } @@ -218,6 +228,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) { $result = array(); $result[0][$model->alias] = $this->__decode($this->Socket->get($this->__uri($model, $params)), true); + return $this->__readResult($model, $queryData, $result); } @@ -242,10 +253,10 @@ private function __readResult($model, $queryData, $result) { unset($result[0][$model->alias]['_rev']); return $result; - } else if (isset($result[0][$model->alias]['rows'])) { + } elseif (isset($result[0][$model->alias]['rows'])) { $docs = array(); - foreach ($result[0][$model->alias]['rows'] as $k => $doc) { + foreach ($result[0][$model->alias]['rows'] as $k => $doc) { $docs[$k][$model->alias]['id'] = $doc['doc']['_id']; $docs[$k][$model->alias]['rev'] = $doc['doc']['_rev']; @@ -258,8 +269,10 @@ private function __readResult($model, $queryData, $result) { $docs[$k][$model->alias][$field] = $value; } } + return $docs; } + return false; } @@ -274,6 +287,7 @@ private function __readResult($model, $queryData, $result) { */ public function update(Model $model, $fields = null, $values = null, $conditions = null) { $data = $model->data[$model->alias]; + if ($fields !== null && $values !== null) { $data = array_combine($fields, $values); } @@ -282,11 +296,14 @@ public function update(Model $model, $fields = null, $values = null, $conditions if (!empty($model->id)) { $result = $this->__decode($this->Socket->put($this->__uri($model, $model->id), $this->__encode($data))); + if ($this->__checkOk($result)) { $model->rev = $result->rev; + return true; } } + return false; } @@ -306,7 +323,7 @@ private function __idRevData(&$model, &$data) { if (isset($data['rev']) && !empty($data['rev'])) { $data['_rev'] = $data['rev']; unset($data['rev']); - } else if ($model->rev) { + } elseif ($model->rev) { $data['_rev'] = $model->rev; } else { $data['_rev'] = $this->__lastRevision($model, $model->id); @@ -322,6 +339,7 @@ private function __idRevData(&$model, &$data) { */ private function __lastRevision(&$model, $id) { $result = $this->__decode($this->Socket->get($this->__uri($model, $id))); + return $result->_rev; } @@ -337,11 +355,16 @@ public function delete(Model $model, $conditions = null) { $rev = $model->rev; if (!empty($id)) { - if (empty($rev)) $rev = $this->__lastRevision($model, $id); - $id_rev = $id . '/?rev=' . $rev; - $result = $this->__decode($this->Socket->delete($this->__uri($model, $id_rev))); + if (empty($rev)) { + $rev = $this->__lastRevision($model, $id); + } + + $idRev = $id . '/?rev=' . $rev; + $result = $this->__decode($this->Socket->delete($this->__uri($model, $idRev))); + return $this->__checkOk($result); } + return false; } @@ -368,6 +391,7 @@ public function expression($expression) { $obj = new stdClass(); $obj->type = 'expression'; $obj->value = $expression; + return $obj; } @@ -381,6 +405,7 @@ public function expression($expression) { */ public function fullTableName($model, $quote = true, $schema = true) { $table = null; + if (is_object($model)) { $table = $model->tablePrefix . $model->table; } elseif (isset($this->config['prefix'])) { @@ -388,6 +413,7 @@ public function fullTableName($model, $quote = true, $schema = true) { } else { $table = strval($model); } + return $table; } @@ -400,7 +426,7 @@ public function fullTableName($model, $quote = true, $schema = true) { * * The method can be performed by a Model of the following ways: * - * $this->Model->curlGet('_all_dbs'); + * $this->Model->curlGet('_all_dbs'); * $this->Model->curlPut('document_name'); * $this->Model->curlPost('document_name', array('field' => 'value')); * $this->Model->curlDelete('document_name'); @@ -418,11 +444,13 @@ public function query($method, $params) { 'method' => strtoupper(str_replace('curl', '', $method)) ); - if (!empty($uri)) + if (!empty($uri)) { $request['uri'] = '/' . $uri; + } - if (!empty($data)) + if (!empty($data)) { $request['body'] = $this->__encode($data); + } $result = $this->Socket->request($request); @@ -440,17 +468,29 @@ public function query($method, $params) { * @return array */ private function __queryParams($params) { - if (isset($params[0])) $uri = $params[0]; - else $uri = ''; + if (isset($params[0])) { + $uri = $params[0]; + } else { + $uri = ''; + } - if (isset($params[1])) $data = $params[1]; - else $data = array(); + if (isset($params[1])) { + $data = $params[1]; + } else { + $data = array(); + } - if (isset($params[2])) $decode = $params[2]; - else $decode = true; + if (isset($params[2])) { + $decode = $params[2]; + } else { + $decode = true; + } - if (isset($params[3])) $assoc = $params[3]; - else $assoc = true; + if (isset($params[3])) { + $assoc = $params[3]; + } else { + $assoc = true; + } return array($uri, $data, $decode, $assoc); } @@ -466,6 +506,7 @@ private function __uri($model = null, $params = null) { if (!is_null($params)) { $params = '/' . $params; } + return '/' . $this->fullTableName($model) . $params; } @@ -500,4 +541,3 @@ private function __checkOk($object = null) { return isset($object->ok) && $object->ok === true; } } -?> diff --git a/Test/Case/Datasource/CouchDBSourceTest.php b/Test/Case/Datasource/CouchDBSourceTest.php index 1f38dda..097f74a 100755 --- a/Test/Case/Datasource/CouchDBSourceTest.php +++ b/Test/Case/Datasource/CouchDBSourceTest.php @@ -1,36 +1,54 @@ <?php /** - * CouchDB DataSource Test file - * PHP version 5 + * CouchDB DataSource Test file. * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) - * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) - * @link http://cakephp.org CakePHP(tm) Project - * @package datasources - * @subpackage datasources.models.datasources - * @since CakePHP Datasources v 0.3 - * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @package CouchDB.Test.Case.Datasource + * @since CakePHP Datasources v 0.3 + * @license http://www.opensource.org/licenses/mit-license.php MIT License */ App::uses('AppModel', 'Model'); /** * Post Model for the test. - * - * @package app - * @subpackage app.model.post */ class Post extends AppModel { +/** + * Name of the model. + * + * @var string + */ public $name = 'Post'; + +/** + * Custom display field name. + * + * @var string + */ public $displayField = 'title'; + +/** + * Number of associations to recurse through during find calls. + * + * @var integer + */ public $recursive = -1; +/** + * List of validation rules. + * + * @var array + */ public $validate = array( 'title' => array( 'notempty' => array( @@ -44,6 +62,11 @@ class Post extends AppModel { ), ); +/** + * Field-by-field table metadata. + * + * @var array + */ public $schema = array( 'id' => array( 'type' => 'string', @@ -72,8 +95,7 @@ class Post extends AppModel { /** * CouchDBTestCase. * - * @package datasources - * @subpackage datasources.tests.cases.models.datasources + * @package CouchDB.Test.Case.Datasource */ class CouchDBTestCase extends CakeTestCase { @@ -89,7 +111,7 @@ class CouchDBTestCase extends CakeTestCase { * * @var array */ - protected $config = array( + protected $_config = array( 'datasource' => 'CouchDB.CouchDBSource', 'persistent' => false, 'host' => 'localhost', @@ -110,14 +132,14 @@ public function setUp() { $config = new DATABASE_CONFIG(); if (isset($config->couchdb_test)) { - $this->config = $config->couchdb_test; + $this->_config = $config->couchdb_test; } - ConnectionManager::create('couchdb_test', $this->config); + ConnectionManager::create('couchdb_test', $this->_config); $this->Post = ClassRegistry::init('Post'); $this->Post->useDbConfig = 'couchdb_test'; - $this->removeAllDocuments(); + $this->__removeAllDocuments(); } /** @@ -126,10 +148,10 @@ public function setUp() { * @return void */ public function testConnection() { - $this->CouchDB = new CouchDBSource($this->config); + $this->CouchDB = new CouchDBSource($this->_config); $this->CouchDB = ConnectionManager::getDataSource($this->Post->useDbConfig); - $reconnect = $this->CouchDB->reconnect($this->config); + $reconnect = $this->CouchDB->reconnect($this->_config); $this->assertSame($reconnect, true, __d('test_cases', 'Not reconnected')); $disconnect = $this->CouchDB->disconnect(); @@ -311,16 +333,15 @@ public function updateTest() { if (isset($mapReduce->rows[0]->value)) { $count0 = $mapReduce->rows[0]->value; - } - else { + } else { $count0 = 0; } - $count1 = $this->updateTest1($uri, $post, $count0); - $count2 = $this->updateTest2($uri, $post, $count1); - $count3 = $this->updateTest3($uri, $post, $count2); - $count4 = $this->updateTest4($uri, $post, $count2); - $updateData = $this->updateTest5($uri, $post, $count4); + $count1 = $this->__updateTest1($uri, $post, $count0); + $count2 = $this->__updateTest2($uri, $post, $count1); + $count3 = $this->__updateTest3($uri, $post, $count2); + $count4 = $this->__updateTest4($uri, $post, $count2); + $updateData = $this->__updateTest5($uri, $post, $count4); // Final test $result = $this->Post->find('all'); @@ -343,7 +364,7 @@ public function updateTest() { * @param integer $previousCount * @return integer */ - private function updateTest1($uri, $post, $previousCount) { + private function __updateTest1($uri, $post, $previousCount) { $data = array( 'title' => 'My first post', 'description' => 'My first post' @@ -370,7 +391,7 @@ private function updateTest1($uri, $post, $previousCount) { * @param integer $previousCount * @return integer */ - private function updateTest2($uri, $post, $previousCount) { + private function __updateTest2($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); $this->assertEquals(4, count($findResult['Post'])); @@ -400,7 +421,7 @@ private function updateTest2($uri, $post, $previousCount) { * @param integer $previousCount * @return integer */ - private function updateTest3($uri, $post, $previousCount) { + private function __updateTest3($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); $this->assertEquals(4, count($findResult['Post'])); @@ -431,7 +452,7 @@ private function updateTest3($uri, $post, $previousCount) { * @param integer $previousCount * @return integer */ - private function updateTest4($uri, $post, $previousCount) { + private function __updateTest4($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); $this->assertEquals(4, count($findResult['Post'])); @@ -462,7 +483,7 @@ private function updateTest4($uri, $post, $previousCount) { * @param integer $previousCount * @return integer */ - private function updateTest5($uri, $post, $previousCount) { + private function __updateTest5($uri, $post, $previousCount) { $findResult = $this->Post->find('first'); $this->assertEquals(4, count($findResult['Post'])); @@ -577,7 +598,7 @@ public function testQuery() { 'description' => 'My first post' ); - $result = $this->Post->curlPost('/posts', $data); + $result = $this->Post->curlPost('/posts', $data); $this->assertSame($result['ok'], true); // PUT @@ -587,7 +608,7 @@ public function testQuery() { 'description' => 'My first update' ); - $result = $this->Post->curlPut('/posts/' . $result['id'], $data); + $result = $this->Post->curlPut('/posts/' . $result['id'], $data); $this->assertSame($result['ok'], true); // DELETE @@ -600,10 +621,10 @@ public function testQuery() { * * @return void */ - private function removeAllDocuments() { + private function __removeAllDocuments() { $posts = $this->Post->find('list', array('fields' => array('Post.rev'))); - foreach($posts as $id => $post) { + foreach ($posts as $id => $post) { $this->Post->rev = $post; $this->Post->delete($id); } @@ -615,7 +636,7 @@ private function removeAllDocuments() { * @return void */ public function tearDown() { - $this->removeAllDocuments(); + $this->__removeAllDocuments(); unset($this->Post); unset($this->CouchDB); ClassRegistry::flush(); From 2468c54bd0b9a63f06d9f79f55348d2fa9f23331 Mon Sep 17 00:00:00 2001 From: Fabien Mercier <fabien.mercier@mediapart.fr> Date: Sun, 2 Feb 2014 14:50:18 +0100 Subject: [PATCH 19/27] Just little optimizations --- Model/Datasource/CouchDBSource.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index aaef66a..5833c98 100755 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -126,9 +126,7 @@ public function disconnect() { * @return array Databases. */ public function listSources($data = null) { - $databases = $this->__decode($this->Socket->get($this->__uri('_all_dbs')), true); - - return $databases; + return $this->__decode($this->Socket->get($this->__uri('_all_dbs')), true); } /** @@ -250,8 +248,7 @@ private function __readResult($model, $queryData, $result) { $result[0][$model->alias]['id'] = $result[0][$model->alias]['_id']; $result[0][$model->alias]['rev'] = $result[0][$model->alias]['_rev']; - unset($result[0][$model->alias]['_id']); - unset($result[0][$model->alias]['_rev']); + unset($result[0][$model->alias]['_id'], $result[0][$model->alias]['_rev']); return $result; } elseif (isset($result[0][$model->alias]['rows'])) { @@ -261,10 +258,7 @@ private function __readResult($model, $queryData, $result) { $docs[$k][$model->alias]['id'] = $doc['doc']['_id']; $docs[$k][$model->alias]['rev'] = $doc['doc']['_rev']; - unset($doc['doc']['_id']); - unset($doc['doc']['_rev']); - unset($doc['doc']['id']); - unset($doc['doc']['rev']); + unset($doc['doc']['_id'], $doc['doc']['_rev'], $doc['doc']['id'], $doc['doc']['rev']); foreach ($doc['doc'] as $field => $value) { $docs[$k][$model->alias][$field] = $value; From 9034f4d7584b9967486d0f7675343d5650fd2281 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Fri, 16 May 2014 17:21:24 -0300 Subject: [PATCH 20/27] Implemented search for specific document revision --- Model/Datasource/CouchDBSource.php | 6 ++++++ README.md | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/Model/Datasource/CouchDBSource.php b/Model/Datasource/CouchDBSource.php index c0d572d..1e207f4 100755 --- a/Model/Datasource/CouchDBSource.php +++ b/Model/Datasource/CouchDBSource.php @@ -215,6 +215,12 @@ public function read(Model $model, $queryData = array(), $recursive = null) { if ($model->recursive > -1) { $params = $params . '?revs_info=true'; } + + if (isset($queryData['conditions'][$model->alias . '.rev'])) { + if (strstr($this->__uri($model, $params), '?rev')) $type = '&'; + else $type = '?'; + $params = $params . $type . 'rev=' . $queryData['conditions'][$model->alias . '.rev']; + } } $result = array(); diff --git a/README.md b/README.md index 11a3a86..e5db55c 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,13 @@ $conditions = array('Post.id' => $this->Post->id); $result = $this->Post->find('first', compact('conditions')); ``` +### Search for a document by specific revision + +```php +$conditions = array('Post.id' => $this->Post->id, 'Post.rev' => $this->Post->rev); +$result = $this->Post->find('first', compact('conditions')); +``` + ### Change a document (changing the last revision) ```php From e00788e2b1871abebbc931c57c7e4d3f460b9795 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Sun, 9 Nov 2014 06:05:53 -0200 Subject: [PATCH 21/27] Documentation improvement --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e5db55c..c90b56f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Copyright (c) 2011 Maury M. Marques ## Installation -You can clone the plugin into your project (or if you want you can use as a [submodule](http://help.github.com/submodules)): +You can clone the plugin into your project (or if you want you can use it as a [submodule](http://help.github.com/submodules)): ``` cd path/to/app/Plugin or /plugins From c7e8e42a5b0d787c01991fa63648c1fa2f3010bb Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Sun, 8 Feb 2015 04:07:42 -0200 Subject: [PATCH 22/27] Update composer.json --- composer.json | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index db84230..e089b28 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,25 @@ { "name": "maurymmarques/couchdb-datasource-cakephp", - "license": "MIT", + "description": "CouchDB datasource is a CakePHP plugin to facilitate the communication from CakePHP application to CouchDB database.", "type": "cakephp-plugin", - "version": "1.0.0", + "keywords": ["cakephp", "couchdb", "datasource"], + "homepage": "https://github.com/maurymmarques/couchdb-datasource-cakephp", + "license": "MIT", + "authors": [ + { + "name": "Maury M. Marques", + "role": "Author" + } + ], + "support": { + "issues": "https://github.com/maurymmarques/couchdb-datasource-cakephp/issues", + "source": "https://github.com/maurymmarques/couchdb-datasource-cakephp" + }, "require": { + "php": ">=5.3.0", "composer/installers": "*" }, - "extra": { - "installer-name": "Couchdb" + "extra": { + "installer-name": "CouchDB" } -} \ No newline at end of file +} From 809d334893331624e6d3d2d722abf483eca9860c Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Sun, 8 Feb 2015 04:22:51 -0200 Subject: [PATCH 23/27] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e089b28..9ff096c 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "maurymmarques/couchdb-datasource-cakephp", "description": "CouchDB datasource is a CakePHP plugin to facilitate the communication from CakePHP application to CouchDB database.", "type": "cakephp-plugin", - "keywords": ["cakephp", "couchdb", "datasource"], + "keywords": ["cakephp", "couchdb", "datasource", "plugin"], "homepage": "https://github.com/maurymmarques/couchdb-datasource-cakephp", "license": "MIT", "authors": [ From 4a65a0780ce5b2c93444039666bf57096a02bffd Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Sun, 8 Feb 2015 04:39:33 -0200 Subject: [PATCH 24/27] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9ff096c..b570667 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "maurymmarques/couchdb-datasource-cakephp", + "name": "maurymmarques/couchdb-datasource-plugin", "description": "CouchDB datasource is a CakePHP plugin to facilitate the communication from CakePHP application to CouchDB database.", "type": "cakephp-plugin", "keywords": ["cakephp", "couchdb", "datasource", "plugin"], From efe0f48361ddf29f64d6dbedb8e86ab90eeb80e9 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Sun, 8 Feb 2015 05:18:27 -0200 Subject: [PATCH 25/27] Update README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c90b56f..4b2f610 100644 --- a/README.md +++ b/README.md @@ -16,21 +16,54 @@ Copyright (c) 2011 Maury M. Marques ## Installation -You can clone the plugin into your project (or if you want you can use it as a [submodule](http://help.github.com/submodules)): +You can install this plugin using Composer, GIT Submodule, GIT Clone or Manually +_[Using [Composer](http://getcomposer.org/)]_ + +Add the plugin to your project's `composer.json` - something like this: + +```javascript +{ + "require": { + "maurymmarques/couchdb-datasource-plugin": "dev-master" + } +} +``` +Because this plugin has the type `cakephp-plugin` set in it's own `composer.json`, composer knows to install it inside your `/Plugin` directory, rather than in the usual vendors file. + +_[GIT Submodule]_ + +In your app directory (`app/Plugin`) type: + +```bash +git submodule add git://github.com/maurymmarques/couchdb-datasource.git Plugin/CouchDB +git submodule init +git submodule update ``` -cd path/to/app/Plugin or /plugins + +_[GIT Clone]_ + +In your plugin directory (`app/Plugin` or `plugins`) type: + +```bash git clone https://github.com/maurymmarques/couchdb-datasource.git CouchDB ``` +_[Manual]_ + +* Download the [CouchDB archive](https://github.com/maurymmarques/couchdb-datasource/archive/master.zip). +* Unzip that download. +* Rename the resulting folder to `CouchDB` +* Then copy this folder into `app/Plugin/` or `plugins` + +## Configuration + Bootstrap the plugin in app/Config/bootstrap.php: ```php CakePlugin::load('CouchDB'); ``` -## Configuration - Connection in app/Config/database.php: ```php From 6062f2d0696d488e27136a4437781a76c09b8f3e Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Sun, 8 Feb 2015 06:20:23 -0200 Subject: [PATCH 26/27] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 4b2f610..8b9f67a 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,11 @@ Add the plugin to your project's `composer.json` - something like this: { "require": { "maurymmarques/couchdb-datasource-plugin": "dev-master" + }, + "extra": { + "installer-paths": { + "app/Plugin/CouchDB": ["maurymmarques/couchdb-datasource-plugin"] + } } } ``` From 59da5b11ce9b5cb969f43637179743cd8186bc03 Mon Sep 17 00:00:00 2001 From: "Maury M. Marques" <maurymmarques@gmail.com> Date: Tue, 10 Feb 2015 03:01:58 -0200 Subject: [PATCH 27/27] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8b9f67a..1f047eb 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ Add the plugin to your project's `composer.json` - something like this: } } ``` +Then just run `composer install` + Because this plugin has the type `cakephp-plugin` set in it's own `composer.json`, composer knows to install it inside your `/Plugin` directory, rather than in the usual vendors file. _[GIT Submodule]_