-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouchDb.class.php
executable file
·310 lines (272 loc) · 7.85 KB
/
CouchDb.class.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
<?php
class CouchDb implements CouchQueryable , ArrayAccess
{
protected static
$connection = null, // CouchConnection where this DB resides
$name = null; // the database name
/**
* Constructor
*
* @return void
*/
public function __construct(CouchConnection $connection, $name)
{
$this->connection = $connection;
$this->name = $name;
}
public function getConnection()
{
return $this->connection;
}
public function getName()
{
return $this->name;
}
/**
* send sends a couchrequest to the connection and returns the couchresponse
*
* @return CouchResponse The reponse
* @author The Young Shepherd
**/
public function send(CouchRequest $request)
{
$request = clone $request;
$request->setQuery($this->name.'/'.$request->getQuery());
return new CouchResponse($this, $request);
}
/**
* Compact a database
*
* @param null|string $db
* @return Phly_Couch_Result
* @throws Phly_Couch_Exception when fails or no database specified
*/
public function compact()
{
$request = new CouchRequest('POST','_compact');
$response = $this->send($request);
$status = $response->getStatus();
if (202 !== $status) {
throw new Phly_Couch_Exception(sprintf('Failed compacting database "%s": received response code "%s"', $db, (string) $response->getStatus()));
}
return $response;
}
/**
* Get database info
*
* @return Phly_Couch_Result
* @throws Phly_Couch_Exception when fails
*/
public function getInfo()
{
$request = new CouchRequest('GET','');
$response = $this->send($request);
if (!$response->isSuccessful()) {
throw new CouchException(sprintf('Failed querying database "%s"; received response code "%s"', $this->name, (string) $response->getStatus()));
}
return $response;
}
/**
* Drop database
*
* @return Phly_Couch_Result
* @throws Phly_Couch_Exception when fails
*/
public function drop()
{
return $this->connection->drop($this);
}
// CRUD
public function create($doc, $id = null)
{
if (!$doc instanceof CouchDocument)
{
if (!is_array($doc))
{
$doc = Zend_JSON::decode($doc);
}
$data = new CouchData($doc);
$doc = CouchDocument::create($this, $data);
}
if (!is_null($id))
{
$doc->id = $id;
}
if (isset($doc->id))
{
$request = new CouchRequest('PUT', urlencode($doc->id), $doc);
}
else
{
$request = new CouchRequest('POST', "", $doc);
}
$response = $this->send($request);
$responseObject = $response->getObject();
$doc->id = $responseObject['id'];
$doc->rev = $responseObject['rev'];
return $doc;
}
public function update(CouchDocument $doc)
{
if (!isset($doc->id) || !isset($doc->rev))
{
throw new CouchException('Cannot update document without an id and prior rev');
}
$request = new CouchRequest('PUT', urlencode($doc->id), $doc);
$response = $this->send($request);
$responseObject = $response->getObject();
$doc->rev = $responseObject['rev'];
return $doc;
}
public function delete($id)
{
if ($id instanceof CouchDocument)
{
$doc = $id;
$id = $doc->id;
}
$request = new CouchRequest('PUT', urlencode($id));
$this->send($request);
return true;
}
public function get($id, array $params = array())
{
$request = new CouchRequestDocument(urlencode($id), $params);
$response = $this->send($request);
return $response->getObject();
}
// Document API methods
/**
* Retrieve all documents for a give database
*
* @param null|array $options Query options
* @return Phly_Couch_DocumentSet
* @throws Phly_Couch_Exception on failure or bad db
*/
public function getAll(array $options = null)
{
$request = new CouchRequestSet('GET', '_all_docs', $options);
$response = $this->send($request);
return $response->getObject();
}
/**
* Remove a document
*
* @param string $id
* @param array $options
* @return Phly_Couch_Result
* @throws Phly_Couch_Exception on failed call
*/
public function docRemove($id, array $options = null)
{
$db = null;
if (is_array($options) && array_key_exists('db', $options)) {
$db = $options['db'];
unset($options['db']);
}
$db = $this->_verifyDb($db);
$response = $this->_prepareAndSend($db . '/' . $id, 'DELETE', $options);
if (!$response->isSuccessful()) {
require_once 'Phly/Couch/Exception.php';
throw new Phly_Couch_Exception(sprintf('Failed deleting document with id "%s" from database "%s"; received response code "%s"', $id, $db, (string) $response->getStatus()));
}
require_once 'Phly/Couch/Result.php';
return new Phly_Couch_Result($response);
}
/**
* Bulk save many documents at once
*
* @param array|Phly_Couch_DocumentSet $documents
* @param array $options
* @return Phly_Couch_Result
* @throws Phly_Couch_Exception on failed save
*/
public function docBulkSave($documents, array $options = null)
{
$db = null;
if (is_array($options) && array_key_exists('db', $options)) {
$db = $options['db'];
unset($options['db']);
}
$db = $this->_verifyDb($db);
if (is_array($documents)) {
require_once 'Phly/Couch/DocumentSet.php';
$documents = new Phly_Couch_DocumentSet($documents);
} elseif (!$documents instanceof Phly_Couch_DocumentSet) {
require_once 'Phly/Couch/Exception.php';
throw new Phly_Couch_Exception('Invalid document set provided to bulk save operation');
}
$this->getHttpClient()->setRawData($documents->toJson());
$response = $this->_prepareAndSend($db . '/_bulk_docs', 'POST', $options);
if (!$response->isSuccessful()) {
require_once 'Phly/Couch/Exception.php';
throw new Phly_Couch_Exception(sprintf('Failed deleting document with id "%s" from database "%s"; received response code "%s"', $id, $db, (string) $response->getStatus()));
}
require_once 'Phly/Couch/Result.php';
return new Phly_Couch_Result($response);
}
/**
* Retrieve a view
*
* @param string $name
* @param null|array $options
* @return Phly_Couch_DocumentSet
* @throws Phly_Couch_Exception on failure or bad db
*/
public function view($name, array $options = null)
{
$db = null;
if (is_array($options) && array_key_exists('db', $options)) {
$db = $options['db'];
unset($options['db']);
}
$db = $this->_verifyDb($db);
$response = $this->_prepareAndSend($db . '/_view/'.$name, 'GET', $options);
if (!$response->isSuccessful()) {
require_once 'Phly/Couch/Exception.php';
throw new Phly_Couch_Exception(sprintf('Failed querying database "%s"; received response code "%s"', $db, (string) $response->getStatus()));
}
require_once 'Phly/Couch/DocumentSet.php';
return new Phly_Couch_DocumentSet($response->getBody());
}
public function offsetGet($id)
{
try {
$doc = $this->get($id);
} catch (CouchException $e) {
if ($e->getCode() != 404)
{
throw $e;
}
// if not found, return a new document with provided id
$doc = new CouchDocument($this, (string)$id);
}
return $doc;
}
public function offsetSet($id, $doc)
{
if (!$doc instanceof CouchDocument)
{
$doc = CouchDocument::create($this, $doc);
}
$doc->id = $id;
$doc->save();
}
public function offsetExists($id)
{
try {
$this->get($id);
return true;
} catch (Exception $e) {
return false;
}
}
public function offsetUnset($id)
{
try {
$doc = $this->get($id);
$doc->delete();
} catch (Exception $e) {
}
}
}