forked from angyvolin/mongodb-php-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
207 lines (177 loc) · 5.4 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
<?php
namespace Tequila\MongoDB;
use MongoDB\Driver\Exception\RuntimeException as MongoDBRuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use Tequila\MongoDB\Exception\InvalidArgumentException;
use Tequila\MongoDB\Traits\CommandExecutorTrait;
use Tequila\MongoDB\Traits\ExecuteCommandTrait;
class Database
{
use CommandExecutorTrait;
use ExecuteCommandTrait;
/**
* @var string
*/
private $databaseName;
/**
* @var Manager
*/
private $manager;
/**
* @var ReadConcern
*/
private $readConcern;
/**
* @var ReadPreference
*/
private $readPreference;
/**
* @var WriteConcern
*/
private $writeConcern;
/**
* @param Manager $manager
* @param string $databaseName
* @param array $options
*/
public function __construct(Manager $manager, $databaseName, array $options = [])
{
if (!$databaseName) {
throw new InvalidArgumentException('$databaseName cannot be empty.');
}
$options += [
'readConcern' => $manager->getReadConcern(),
'readPreference' => $manager->getReadPreference(),
'writeConcern' => $manager->getWriteConcern(),
];
$validTypes = [
'readConcern' => ReadConcern::class,
'readPreference' => ReadPreference::class,
'writeConcern' => WriteConcern::class,
];
foreach ($validTypes as $optionName => $validType) {
if (!$options[$optionName] instanceof $validType) {
throw new InvalidArgumentException(
sprintf(
'Option "%s" is expected to be an instance of %s, %s given.',
$optionName,
$validType,
\Tequila\MongoDB\getType($options[$optionName])
)
);
}
}
$this->readConcern = $options['readConcern'];
$this->readPreference = $options['readPreference'];
$this->writeConcern = $options['writeConcern'];
$this->manager = $manager;
$this->databaseName = $databaseName;
}
/**
* @param string $collectionName
* @param array $options
*
* @return array
*/
public function createCollection($collectionName, array $options = [])
{
$cursor = $this->executeCommand(['create' => $collectionName], $options);
return $cursor->current();
}
/**
* @param array $options
*
* @return array
*/
public function drop(array $options = [])
{
$cursor = $this->executeCommand(['dropDatabase' => 1], $options);
return $cursor->current();
}
/**
* @param string $collectionName
* @param array $options
*
* @return array
*/
public function dropCollection($collectionName, array $options = [])
{
try {
$cursor = $this->executeCommand(['drop' => $collectionName], $options);
} catch (MongoDBRuntimeException $e) {
if ('ns not found' === $e->getMessage()) {
return ['ok' => 0, 'errmsg' => $e->getMessage()];
}
throw $e;
}
return $cursor->current();
}
/**
* @return string
*/
public function getDatabaseName()
{
return $this->databaseName;
}
/**
* @param array $options
*
* @return Cursor
*/
public function listCollections(array $options = [])
{
return $this->executeCommand(['listCollections' => 1], $options);
}
/**
* @param array|object $command
* @param array $options
*
* @return CommandCursor
*/
public function runCommand($command, array $options = [])
{
if (!is_array($command) && !is_object($command)) {
throw new InvalidArgumentException(
sprintf(
'$command must be an array or an object, %s given.',
\Tequila\MongoDB\getType($command)
)
);
}
$readPreference = isset($options['readPreference'])
? $options['readPreference']
: $this->readPreference;
if (!$readPreference instanceof ReadPreference) {
throw new InvalidArgumentException(
sprintf(
'Option "readPreference" is expected to be an instance of %s, %s given.',
ReadPreference::class,
\Tequila\MongoDB\getType($readPreference)
)
);
}
if (!$command instanceof \MongoDB\Driver\Command) {
$command = new \MongoDB\Driver\Command($command);
}
$server = $this->manager->selectServer($readPreference);
$cursor = $server->executeCommand($this->databaseName, $command);
return new CommandCursor($cursor);
}
/**
* @param string $collectionName
* @param array $options
*
* @return Collection
*/
public function selectCollection($collectionName, array $options = [])
{
$options += [
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
}
}