-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathCouchDBSource.php
More file actions
517 lines (458 loc) · 13.5 KB
/
CouchDBSource.php
File metadata and controls
517 lines (458 loc) · 13.5 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?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
* @package datasources
* @subpackage datasources.models.datasources
* @since CakePHP Datasources v 0.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('HttpSocket', 'Network/Http');
App::uses('DataSource', 'Model/Datasource');
/**
* CouchDB Datasource
*
* @package datasources
* @subpackage datasources.models.datasources
*/
class CouchDBSource extends DataSource {
/**
* Start quote
*
* @var string
*/
public $startQuote = null;
/**
* End quote
*
* @var string
*/
public $endQuote = null;
/**
* 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();
} else {
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'];
try {
$this->Socket = new HttpSocket($this->config);
$this->Socket->get();
$this->connected = true;
} catch (SocketException $e) {
throw new MissingConnectionException(array('class' => $e->getMessage()));
}
}
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() {
$databases = $this->__decode($this->Socket->get($this->__uri('_all_dbs')), true);
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).
*
* @param Model $model
* @return array Schema.
*/
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->Model->curlPut('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, $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, $queryData = array(), $recursive = null) {
if ($recursive === null && isset($queryData['recursive'])) {
$recursive = $queryData['recursive'];
}
if (!is_null($recursive)) {
$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.
*/
private function __readResult($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;
} else 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, $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
*/
private function __idRevData(&$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']);
} else if ($model->rev) {
$data['_rev'] = $model->rev;
} else {
$data['_rev'] = $this->__lastRevision($model, $model->id);
}
}
/**
* The method searches for the latest revision of a document
*
* @param object $model
* @param int $id
* @return string Last revision of the document
*/
private function __lastRevision(&$model, $id) {
$result = $this->__decode($this->Socket->get($this->__uri($model, $id)));
return $result->_rev;
}
/**
* Generates and executes a DELETE statement.
*
* @param Model $model
* @param mixed $conditions
* @return boolean Success.
*/
public function delete($model, $conditions = null) {
$id = $model->id;
$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)));
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, $func, $params = array()) {
return true;
}
/**
* Returns an object to represent a database expression in a query. Expression objects
* are not sanitized or escaped.
*
* @param string $expression An arbitrary expression to be inserted into a query.
* @return stdClass An object representing a database expression to be used in a query
*/
public function expression($expression) {
$obj = new stdClass();
$obj->type = 'expression';
$obj->value = $expression;
return $obj;
}
/**
* 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.
* The first position of the $params array is used to mount the uri.
* The second place in the $params array is used to assemble data from a POST or PUT.
* The third parameter is used to decode the return json.
* The fourth parameter is used to build an associative array.
*
* The method can be performed by a Model of the following ways:
*
* $this->Model->curlGet('_all_dbs');
* $this->Model->curlPut('document_name');
* $this->Model->curlPost('document_name', array('field' => 'value'));
* $this->Model->curlDelete('document_name');
* $this->Model->curlPost('document_name', array('field' => 'value'), false);
* $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
* @return object
*/
public function query($method, $params) {
list($uri, $data, $decode, $assoc) = $this->__queryParams($params);
$request = array(
'method' => strtoupper(str_replace('curl', '', $method))
);
if (!empty($uri))
$request['uri'] = '/' . $uri;
if (!empty($data))
$request['body'] = $this->__encode($data);
$result = $this->Socket->request($request);
if ($decode === true) {
$result = $this->__decode($result, $assoc);
}
return $result;
}
/**
* Construct the parameter of the query method.
*
* @param array $params
* @return array
*/
private function __queryParams($params) {
if (isset($params[0])) $uri = $params[0];
else $uri = '';
if (isset($params[1])) $data = $params[1];
else $data = array();
if (isset($params[2])) $decode = $params[2];
else $decode = true;
if (isset($params[3])) $assoc = $params[3];
else $assoc = true;
return array($uri, $data, $decode, $assoc);
}
/**
* Get a URI.
*
* @param mixed $model
* @param string $params
* @return string URI.
*/
private function __uri($model = null, $params = null) {
if (!is_null($params)) {
$params = '/' . $params;
}
return '/' . $this->fullTableName($model) . $params;
}
/**
* JSON encode.
*
* @param string json $data
* @return string JSON.
*/
private 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.
*/
private function __decode($data, $assoc = false) {
return json_decode($data, $assoc);
}
/**
* Checks if the result returned ok = true.
*
* @param object $object
* @return boolean
*/
private function __checkOk($object = null) {
return isset($object->ok) && $object->ok === true;
}
}
?>