/
CouchdbSource.php
415 lines (371 loc) · 10.8 KB
/
CouchdbSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* CouchDB Datasource
*
* PHP version 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* 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
* @since CakePHP Datasources v 0.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('HttpSocket', 'Network/Http');
/**
* CouchDB Datasource
*
*/
class CouchdbSource extends DataSource {
/**
* Constructor
*
* @param array $config Connection setup for CouchDB.
* @param integer $autoConnect Autoconnect
* @return boolean
*/
public function __construct($config = null, $autoConnect = true) {
if (!isset($config['request'])) {
$config['request']['uri'] = $config;
$config['request']['header']['Content-Type'] = 'application/json';
}
parent::__construct($config);
$this->fullDebug = Configure::read('debug') > 1;
if ($autoConnect) {
return $this->connect();
}
return true;
}
/**
* Reconnects to the database with optional new settings
*
* @param array $config New settings
* @return boolean Success
*/
public function reconnect($config = null) {
$this->disconnect();
$this->setConfig($config);
$this->_sources = null;
return $this->connect();
}
/**
* Connects to the database. Options are specified in the $config instance variable
*
* @return boolean Connected
*/
public function connect() {
if ($this->connected !== true) {
if (isset($this->config['login'])) {
$this->config['request']['uri']['user'] = $this->config['login'];
}
if (isset($this->config['password'])) {
$this->config['request']['uri']['pass'] = $this->config['password'];
}
$this->Socket = new HttpSocket($this->config);
if (strpos($this->Socket->get(), 'couchdb') !== false) {
$this->connected = true;
} else {
trigger_error(__('CouchDB Error: connection failed'), E_USER_WARNING);
return $this->cakeError('missingConnection', array(array('code' => 500, 'className' => 'CouchdbSource')));
}
}
return $this->connected;
}
/**
* Disconnects from the database, kills the connection and advises that the
* connection is closed, and if DEBUG is turned on (equal to 2) displays the
* log of stored data.
*
* @return boolean Disconnected
*/
public function close() {
if (Configure::read('debug') > 1) {
//$this->showLog();
}
$this->disconnect();
}
/**
* Disconnect from the database
*
* @return boolean Disconnected
*/
public function disconnect() {
if (isset($this->results) && is_resource($this->results)) {
$this->results = null;
}
$this->connected = false;
return !$this->connected;
}
/**
* List of databases
*
* @return array Databases
*/
public function listSources() {
return $this->_decode($this->Socket->get($this->_uri('_all_dbs')));
}
/**
* 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)
*
* @param Model $model
* @return array
*/
public function describe($model) {
return $model->schema;
}
/**
* Creates a new document in the database.
* If the primaryKey is declared, creates the document with the specified ID.
* To create a new database: $this->_decode($this->Socket->put($this->_uri('databaseName')));
*
* @param Model $model
* @param array $fields An array of field names to insert. If null, $model->data will be used to generate the field names.
* @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 $model, $fields = null, $values = null) {
$data = $model->data;
if ($fields !== null && $values !== null) {
$data = array_combine($fields, $values);
}
if (isset($data[$model->primaryKey]) && !empty($data[$model->primaryKey])) {
$params = $data[$model->primaryKey];
} else {
$uuids = $this->_decode($this->Socket->get('/_uuids'));
$params = $uuids->uuids[0];
}
$result = $this->_decode($this->Socket->put($this->_uri($model, $params), $this->_encode($data)));
if ($this->_checkOk($result)) {
$model->id = $result->id;
$model->rev = $result->rev;
return true;
}
return false;
}
/**
* Reads data from a document.
*
* @param Model $model
* @param array $queryData An array of information containing $queryData keys, similar to Model::find()
* @param integer $recursive Level number of associations.
* @return mixed False if an error occurred, otherwise an array of results.
*/
public function read(Model $model, $queryData = array(), $recursive = null) {
if ($recursive === null && isset($queryData['recursive'])) {
$recursive = $queryData['recursive'];
}
if ($recursive !== null) {
$model->recursive = $recursive;
}
$params = null;
if (empty($queryData['conditions'])) {
$params = $params . '_all_docs?include_docs=true';
if (!empty($queryData['limit'])) {
$params = $params . '&limit=' . $queryData['limit'];
}
} else {
if (isset($queryData['conditions'][$model->alias . '.' . $model->primaryKey])) {
$params = $queryData['conditions'][$model->alias . '.' . $model->primaryKey];
} else {
$params = $queryData['conditions'][$model->primaryKey];
}
if ($model->recursive > -1) {
$params = $params . '?revs_info=true';
}
}
$result = array();
$result[0][$model->alias] = $this->_decode($this->Socket->get($this->_uri($model, $params)), true);
return $this->_readResult($model, $queryData, $result);
}
/**
* Applies the rules to the document read.
*
* @param Model $model
* @param array $queryData An array of information containing $queryData keys, similar to Model::find()
* @param array $result Data read from the document.
* @return mixed False if an error occurred, otherwise an array of results.
*/
protected function _readResult(Model $model, $queryData, $result) {
if (isset($result[0][$model->alias]['_id'])) {
if (isset($queryData['fields']) && $queryData['fields'] === true) {
$result[0][0]['count'] = 1;
}
$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']);
return $result;
}
if (isset($result[0][$model->alias]['rows'])) {
$docs = array();
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'];
unset($doc['doc']['_id']);
unset($doc['doc']['_rev']);
unset($doc['doc']['id']);
unset($doc['doc']['rev']);
foreach ($doc['doc'] as $field => $value) {
$docs[$k][$model->alias][$field] = $value;
}
}
return $docs;
}
return false;
}
/**
* Generates and executes an UPDATE statement for a given model, fields and values.
*
* @param Model $model
* @param array $fields
* @param array $values
* @param mixed $conditions
* @return boolean Success
*/
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);
}
$this->_idRevData($model, $data);
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;
}
/**
* The method sets the "id" and "rev"to avoid problems in update of a document written shortly after a create a other document.
*
* @param object $model
* @param array $data
* @return void
*/
protected function _idRevData(Model $model, &$data) {
if (isset($data[$model->primaryKey]) && !empty($data[$model->primaryKey])) {
$model->id = $data[$model->primaryKey];
unset($data[$model->primaryKey]);
}
if (isset($data['rev']) && !empty($data['rev'])) {
$data['_rev'] = $data['rev'];
unset($data['rev']);
} elseif ($model->rev) {
$data['_rev'] = $model->rev;
}
}
/**
* Generates and executes a DELETE statement
*
* @param Model $model
* @param mixed $conditions
* @return boolean Success
*/
public function delete(Model $model, $conditions = null) {
$id = $model->id;
$rev = $model->rev;
if (!empty($id) && !empty($rev)) {
$idRev = $id . '/?rev=' . $rev;
$result = $this->_decode($this->Socket->delete($this->_uri($model, $idRev)));
return $this->_checkOk($result);
}
return false;
}
/**
* Returns an instruction to count data. (SQL, i.e. COUNT() or MAX())
*
* @param model $model
* @param string $func Lowercase name of SQL function, i.e. 'count' or 'max'
* @param array $params Function parameters (any values must be quoted manually)
* @return string An SQL calculation function
*/
public function calculate(Model $model, $func, $params = array()) {
return true;
}
/**
* Gets full table name including prefix
*
* @param mixed $model
* @param boolean $quote
* @return string Full name of table
*/
public function fullTableName($model = null, $quote = true) {
$table = null;
if (is_object($model)) {
$table = $model->tablePrefix . $model->table;
} elseif (isset($this->config['prefix'])) {
$table = $this->config['prefix'] . strval($model);
} else {
$table = strval($model);
}
return $table;
}
/**
* Perform any function in CouchDB
*
* @param string $uri
* @param array $post
* @return object
*/
public function query($uri, $post) {
return $this->_decode($this->Socket->post($uri, $this->_encode($post)));
}
/**
* Get a URI
*
* @param mixed $model
* @param string $params
* @return string URI
*/
protected function _uri(Model $model = null, $params = null) {
if ($params !== null) {
$params = '/' . $params;
}
return '/' . $this->fullTableName($model) . $params;
}
/**
* JSON encode
*
* @param string json $data
* @return string JSON
*/
protected function _encode($data) {
return json_encode($data);
}
/**
* JSON decode
* @param string json $data
* @param boolean $assoc If true, returns array. If false, returns object.
* @return mixed Object or Array.
*/
protected function _decode($data, $assoc = false) {
return json_decode($data, $assoc);
}
/**
* Checks if the result returned ok = true
*
* @param object $object
* @return boolean
*/
protected function _checkOk($object = null) {
return isset($object->ok) && $object->ok === true;
}
}