Skip to content

Commit c31efc7

Browse files
committed
Implemented Multi-Database and Database API support. Closes #146
1 parent dedc529 commit c31efc7

File tree

5 files changed

+366
-2
lines changed

5 files changed

+366
-2
lines changed

lib/triagens/ArangoDb/Connection.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Connection
2929
*
3030
* @var string
3131
*/
32-
public static $_apiVersion = '1.2.0';
32+
public static $_apiVersion = '1.4.0';
3333

3434
/**
3535
* Connection options
@@ -80,6 +80,13 @@ class Connection
8080
*/
8181
private $_batchRequest = false;
8282

83+
/**
84+
* $_database string
85+
*
86+
* @var string
87+
*/
88+
private $_database = '';
89+
8390
/**
8491
* Set up the connection object, validate the options provided
8592
*
@@ -93,6 +100,7 @@ public function __construct(array $options)
93100
{
94101
$this->_options = new ConnectionOptions($options);
95102
$this->_useKeepAlive = ($this->_options[ConnectionOptions::OPTION_CONNECTION] === 'Keep-Alive');
103+
$this->setDatabase($this->_options[ConnectionOptions::OPTION_DATABASE]);
96104
}
97105

98106
/**
@@ -302,6 +310,13 @@ public function parseResponse(HttpResponse $response)
302310
private function executeRequest($method, $url, $data)
303311
{
304312
HttpHelper::validateMethod($method);
313+
$database = $this->getDatabase();
314+
if ($database === '') {
315+
$url = '/_db/' . '_system' . $url;
316+
} else {
317+
$url = '/_db/' . $database . $url;
318+
}
319+
305320

306321
// create request data
307322
if ($this->_batchRequest === false) {
@@ -608,4 +623,30 @@ public function json_encode_wrapper($data, $options = null)
608623

609624
return $response;
610625
}
626+
627+
628+
/**
629+
* Set the database to use with this connection
630+
*
631+
* Sets the database to use with this connection, for example: 'my_database'<br>
632+
* Further calls to the database will be addressed to the given database.
633+
*
634+
* @param string $database the database to use
635+
*/
636+
public function setDatabase($database)
637+
{
638+
$this->_database = $database;
639+
}
640+
641+
/**
642+
* Get the database that is currently used with this connection
643+
*
644+
* Get the database to use with this connection, for example: 'my_database'
645+
*
646+
* @return string
647+
*/
648+
public function getDatabase()
649+
{
650+
return $this->_database;
651+
}
611652
}

lib/triagens/ArangoDb/ConnectionOptions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ class ConnectionOptions implements
168168
*/
169169
const OPTION_BATCHPART = 'BatchPart';
170170

171+
/**
172+
* Database flag
173+
*/
174+
const OPTION_DATABASE = 'database';
175+
171176
/**
172177
* UTF-8 CHeck Flag
173178
*/
@@ -305,6 +310,7 @@ private static function getDefaults()
305310
self::OPTION_RECONNECT => false,
306311
self::OPTION_BATCH => false,
307312
self::OPTION_BATCHPART => false,
313+
self::OPTION_DATABASE => '_system',
308314
self::OPTION_CHECK_UTF8_CONFORM => DefaultValues::DEFAULT_CHECK_UTF8_CONFORM,
309315
);
310316
}

lib/triagens/ArangoDb/Database.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: single database
5+
*
6+
* @package triagens\ArangoDb
7+
* @author Frank Mayer
8+
* @copyright Copyright 2013, triagens GmbH, Cologne, Germany
9+
*/
10+
11+
namespace triagens\ArangoDb;
12+
13+
/**
14+
* Value object representing a database
15+
*
16+
* <br>
17+
*
18+
* @package triagens\ArangoDb
19+
* @since 1.4
20+
*/
21+
class Database
22+
{
23+
/**
24+
* Databases index
25+
*/
26+
const ENTRY_DATABASE_NAME = 'name';
27+
28+
/**
29+
* creates a database
30+
*
31+
* This creates a new database<br>
32+
*
33+
* @param Connection $connection - the connection to be used
34+
* @param string $name - the database specification, for example 'myDatabase'
35+
*
36+
* @link http://www.arangodb.org/manuals/1.4/HttpDatabase.html
37+
*
38+
* @return array $responseArray - The response array.
39+
*/
40+
public static function create(Connection $connection, $name)
41+
{
42+
$payload = array(self::ENTRY_DATABASE_NAME => $name);
43+
44+
$response = $connection->post(Urls::URL_DATABASE, $connection->json_encode_wrapper($payload));
45+
46+
$responseArray = $response->getJson();
47+
48+
return $responseArray;
49+
}
50+
51+
52+
/**
53+
* Deletes a database
54+
*
55+
* This will delete an existing database.
56+
*
57+
* @param Connection $connection - the connection to be used
58+
* @param string $name - the database specification, for example 'myDatabase'
59+
*
60+
* @link http://www.arangodb.org/manuals/1.4/HttpDatabase.html
61+
*
62+
* @return array $responseArray - The response array.
63+
*/
64+
public static function delete(Connection $connection, $name)
65+
{
66+
$url = UrlHelper::buildUrl(Urls::URL_DATABASE, array($name));
67+
68+
$response = $connection->delete($url);
69+
70+
$responseArray = $response->getJson();
71+
72+
return $responseArray;
73+
}
74+
75+
76+
/**
77+
* List databases
78+
*
79+
* This will list the databases that exist on the server
80+
*
81+
* @param Connection $connection - the connection to be used
82+
*
83+
* @link http://www.arangodb.org/manuals/1.4/HttpDatabase.html
84+
*
85+
* @return array $responseArray - The response array.
86+
*/
87+
public static function listDatabases(Connection $connection)
88+
{
89+
$response = $connection->get(Urls::URL_DATABASE);
90+
91+
$responseArray = $response->getJson();
92+
93+
return $responseArray;
94+
}
95+
96+
97+
/**
98+
* Retrieves information about the current database
99+
*
100+
* This will get information about the currently used database from the server
101+
*
102+
* @param Connection $connection - the connection to be used
103+
*
104+
* @link http://www.arangodb.org/manuals/1.4/HttpDatabase.html
105+
*
106+
* @return array $responseArray - The response array.
107+
*/
108+
public static function getInfo(Connection $connection)
109+
{
110+
$url = UrlHelper::buildUrl(Urls::URL_DATABASE, array('current'));
111+
112+
$response = $connection->get($url);
113+
114+
$responseArray = $response->getJson();
115+
116+
return $responseArray;
117+
}
118+
}

lib/triagens/ArangoDb/Urls.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ abstract class Urls
194194
const URL_TRAVERSAL = '/_api/traversal';
195195

196196
/**
197-
* base URL part for user management
197+
* base URL part for endpoint management
198198
*/
199199
const URL_ENDPOINT = '/_api/endpoint';
200+
201+
/**
202+
* base URL part for database management
203+
*/
204+
const URL_DATABASE = '/_api/database';
200205
}

0 commit comments

Comments
 (0)