-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDatabase.php
217 lines (190 loc) · 6.45 KB
/
Database.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
<?php
/**
* ArangoDB PHP client: single database
*
* @package ArangoDBClient
* @author Frank Mayer
* @copyright Copyright 2013, triagens GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
/**
* A class for managing ArangoDB Databases
*
* This class provides functions to manage Databases through ArangoDB's Database API<br>
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @package ArangoDBClient
* @since 1.4
*/
class Database
{
/**
* Databases index
*/
const ENTRY_DATABASE_NAME = 'name';
/**
* Users index
*/
const ENTRY_DATABASE_USERS = 'users';
/**
* Options index
*/
const ENTRY_OPTIONS = 'options';
/**
* creates a database
*
* This creates a new database<br>
*
* @param Connection $connection - the connection to be used
* @param string $name - database name, for example 'myDatabase' - must be NFC-normalized!
* @param array $options - extra options for new collections in this database.
* <p>Options are :<br>
* <li>'replicationFactor'</li>
* <li>'writeConcern'</li>
* <li>'sharding'</li>
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
* @throws \ArangoDBClient\ClientException
*/
public static function create(Connection $connection, $name, array $options = [])
{
$payload = [
self::ENTRY_DATABASE_NAME => $name,
self::ENTRY_DATABASE_USERS => [
[
'username' => $connection->getOption(ConnectionOptions::OPTION_AUTH_USER),
'passwd' => $connection->getOption(ConnectionOptions::OPTION_AUTH_PASSWD)
]
],
];
if (count($options) > 0) {
$payload[self::ENTRY_OPTIONS] = $options;
}
$response = $connection->post(Urls::URL_DATABASE, $connection->json_encode_wrapper($payload));
return $response->getJson();
}
/**
* Deletes a database
*
* This will delete an existing database.
*
* @param Connection $connection - the connection to be used
* @param string $name - the database specification, for example 'myDatabase'
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
* @throws \ArangoDBClient\ClientException
*/
public static function delete(Connection $connection, $name)
{
$url = UrlHelper::buildUrl(Urls::URL_DATABASE, [$name]);
$response = $connection->delete($url);
return $response->getJson();
}
/**
* List databases
*
* This will list the databases that exist on the server
*
* @param Connection $connection - the connection to be used
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
* @throws \ArangoDBClient\ClientException
*/
public static function listDatabases(Connection $connection)
{
return self::databases($connection);
}
/**
* List databases
*
* This will list the databases that exist on the server
*
* @param Connection $connection - the connection to be used
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
* @throws \ArangoDBClient\ClientException
*/
public static function databases(Connection $connection)
{
$response = $connection->get(Urls::URL_DATABASE);
return $response->getJson();
}
/**
* List user databases
*
* Retrieves the list of all databases the current user can access without
* specifying a different username or password.
*
* @param Connection $connection - the connection to be used
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
* @throws \ArangoDBClient\ClientException
*/
public static function listUserDatabases(Connection $connection)
{
$url = UrlHelper::buildUrl(Urls::URL_DATABASE, ['user']);
$response = $connection->get($url);
return $response->getJson();
}
/**
* Retrieves information about the current database
*
* This will get information about the currently used database from the server
*
* @param Connection $connection - the connection to be used
*
* @link https://www.arangodb.com/docs/stable/http/database.html
*
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
* @throws \ArangoDBClient\ClientException
*/
public static function getInfo(Connection $connection)
{
$url = UrlHelper::buildUrl(Urls::URL_DATABASE, ['current']);
$response = $connection->get($url);
return $response->getJson();
}
/**
* normalizes a database name
*
* UTF-8 NFC Normalization is required for database names in case
* the extended naming scheme for databases is used. This has to
* be enabled on the server side and is present since server version
* 3.9.
* If the name needs normalization but no normalizer is installed,
* this function can fail and abort the program with a PHP fatal error.
*
* @param string $name - database name to normalize.
*
* @return string $name - The normalized name
*/
public static function normalizeName($name)
{
// first check if the database name follows the traditional
// naming scheme. if so, there is no need to normalize it.
if (!preg_match("/^[a-zA-Z0-9_\-]+$/", $name)) {
// extended database naming scheme. now NFC-normalize
// the database name, as this is required by the server
$name = \Normalizer::normalize($name, \Normalizer::FORM_C);
}
return $name;
}
}
class_alias(Database::class, '\triagens\ArangoDb\Database');