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 * A class for managing ArangoDB Databases
15 *
16 * This class provides functions to manage Databases through ArangoDB's Database API<br>
17 *
18 * @link https://docs.arangodb.com/HTTP/Database/index.html
19 *
20 * @package triagens\ArangoDb
21 * @since 1.4
22 */
23 class Database
24 {
25 /**
26 * Databases index
27 */
28 const ENTRY_DATABASE_NAME = 'name';
29
30 /**
31 * Users index
32 */
33 const ENTRY_DATABASE_USERS = 'users';
34
35 /**
36 * creates a database
37 *
38 * This creates a new database<br>
39 *
40 * @param Connection $connection - the connection to be used
41 * @param string $name - the database specification, for example 'myDatabase'
42 *
43 * @link https://docs.arangodb.com/HTTP/Database/index.html
44 *
45 * @return array $responseArray - The response array.
46 * @throws \triagens\ArangoDb\Exception
47 * @throws \triagens\ArangoDb\ClientException
48 */
49 public static function create(Connection $connection, $name)
50 {
51 $payload = [
52 self::ENTRY_DATABASE_NAME => $name,
53 self::ENTRY_DATABASE_USERS => [
54 [
55 'username' => $connection->getOption(ConnectionOptions::OPTION_AUTH_USER),
56 'passwd' => $connection->getOption(ConnectionOptions::OPTION_AUTH_PASSWD)
57 ]
58 ]
59 ];
60
61 $response = $connection->post(Urls::URL_DATABASE, $connection->json_encode_wrapper($payload));
62
63 return $response->getJson();
64 }
65
66
67 /**
68 * Deletes a database
69 *
70 * This will delete an existing database.
71 *
72 * @param Connection $connection - the connection to be used
73 * @param string $name - the database specification, for example 'myDatabase'
74 *
75 * @link https://docs.arangodb.com/HTTP/Database/index.html
76 *
77 * @return array $responseArray - The response array.
78 * @throws \triagens\ArangoDb\Exception
79 * @throws \triagens\ArangoDb\ClientException
80 */
81 public static function delete(Connection $connection, $name)
82 {
83 $url = UrlHelper::buildUrl(Urls::URL_DATABASE, [$name]);
84
85 $response = $connection->delete($url);
86
87 return $response->getJson();
88 }
89
90
91 /**
92 * List databases
93 *
94 * This will list the databases that exist on the server
95 *
96 * @param Connection $connection - the connection to be used
97 *
98 * @link https://docs.arangodb.com/HTTP/Database/index.html
99 *
100 * @return array $responseArray - The response array.
101 * @throws \triagens\ArangoDb\Exception
102 * @throws \triagens\ArangoDb\ClientException
103 */
104 public static function listDatabases(Connection $connection)
105 {
106 return self::databases($connection);
107 }
108
109 /**
110 * List databases
111 *
112 * This will list the databases that exist on the server
113 *
114 * @param Connection $connection - the connection to be used
115 *
116 * @link https://docs.arangodb.com/HTTP/Database/index.html
117 *
118 * @return array $responseArray - The response array.
119 * @throws \triagens\ArangoDb\Exception
120 * @throws \triagens\ArangoDb\ClientException
121 */
122 public static function databases(Connection $connection)
123 {
124 $response = $connection->get(Urls::URL_DATABASE);
125
126 return $response->getJson();
127 }
128
129 /**
130 * List user databases
131 *
132 * Retrieves the list of all databases the current user can access without
133 * specifying a different username or password.
134 *
135 * @param Connection $connection - the connection to be used
136 *
137 * @link https://docs.arangodb.com/HTTP/Database/index.html
138 *
139 * @return array $responseArray - The response array.
140 * @throws \triagens\ArangoDb\Exception
141 * @throws \triagens\ArangoDb\ClientException
142 */
143 public static function listUserDatabases(Connection $connection)
144 {
145
146 $url = UrlHelper::buildUrl(Urls::URL_DATABASE, ['user']);
147
148 $response = $connection->get($url);
149
150 return $response->getJson();
151 }
152
153
154 /**
155 * Retrieves information about the current database
156 *
157 * This will get information about the currently used database from the server
158 *
159 * @param Connection $connection - the connection to be used
160 *
161 * @link https://docs.arangodb.com/HTTP/Database/index.html
162 *
163 * @return array $responseArray - The response array.
164 * @throws \triagens\ArangoDb\Exception
165 * @throws \triagens\ArangoDb\ClientException
166 */
167 public static function getInfo(Connection $connection)
168 {
169 $url = UrlHelper::buildUrl(Urls::URL_DATABASE, ['current']);
170
171 $response = $connection->get($url);
172
173 return $response->getJson();
174 }
175 }
176