1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent; |
6
|
|
|
|
7
|
|
|
use ArangoClient\ArangoClient; |
8
|
|
|
use Illuminate\Database\Connection as IlluminateConnection; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Concerns\DetectsDeadlocks; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Concerns\DetectsLostConnections; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Concerns\HandlesArangoDb; |
12
|
|
|
use LaravelFreelancerNL\Aranguent\Concerns\ManagesTransactions; |
13
|
|
|
use LaravelFreelancerNL\Aranguent\Concerns\RunsQueries; |
14
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar as QueryGrammar; |
15
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Processor; |
16
|
|
|
use LaravelFreelancerNL\Aranguent\Schema\Builder as SchemaBuilder; |
17
|
|
|
use LaravelFreelancerNL\Aranguent\Schema\Grammar; |
18
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder; |
19
|
|
|
use LogicException; |
20
|
|
|
use Spatie\DataTransferObject\Exceptions\UnknownProperties; |
21
|
|
|
|
22
|
|
|
class Connection extends IlluminateConnection |
23
|
|
|
{ |
24
|
|
|
use HandlesArangoDb; |
25
|
|
|
use DetectsDeadlocks; |
26
|
|
|
use DetectsLostConnections; |
27
|
|
|
use ManagesTransactions; |
28
|
|
|
use RunsQueries; |
|
|
|
|
29
|
|
|
|
30
|
|
|
protected ?ArangoClient $arangoClient = null; |
31
|
|
|
|
32
|
|
|
protected $database; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The ArangoDB driver name. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected string $driverName = 'arangodb'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Connection constructor. |
43
|
|
|
* |
44
|
215 |
|
* @param array $config |
45
|
|
|
* @throws UnknownProperties |
46
|
215 |
|
*/ |
47
|
|
|
public function __construct($config = []) |
48
|
215 |
|
{ |
49
|
215 |
|
$this->config = $config; |
50
|
|
|
|
51
|
|
|
$this->database = (isset($this->config['database'])) ? $this->config['database'] : null; |
52
|
215 |
|
$this->tablePrefix = $this->config['tablePrefix'] ?? null; |
53
|
|
|
|
54
|
|
|
// activate and set the database client connection |
55
|
|
|
$this->arangoClient = new ArangoClient($this->config); |
56
|
|
|
|
57
|
215 |
|
// We need to initialize a query grammar and the query post processors |
58
|
|
|
// which are both very important parts of the database abstractions |
59
|
215 |
|
// so, we initialize these to their default values while starting. |
60
|
215 |
|
$this->useDefaultQueryGrammar(); |
61
|
|
|
|
62
|
|
|
$this->useDefaultPostProcessor(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a schema builder instance for the connection. |
67
|
163 |
|
* |
68
|
|
|
* @return SchemaBuilder |
69
|
163 |
|
*/ |
70
|
|
|
public function getSchemaBuilder(): SchemaBuilder |
71
|
|
|
{ |
72
|
|
|
if (is_null($this->schemaGrammar)) { |
73
|
163 |
|
$this->useDefaultSchemaGrammar(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new SchemaBuilder($this); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the default query grammar instance. |
81
|
215 |
|
* |
82
|
|
|
* @return QueryGrammar |
83
|
215 |
|
*/ |
84
|
|
|
protected function getDefaultQueryGrammar(): QueryGrammar |
85
|
|
|
{ |
86
|
|
|
return new QueryGrammar(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the default post processor instance. |
91
|
215 |
|
* |
92
|
|
|
* @return Processor |
93
|
215 |
|
*/ |
94
|
|
|
protected function getDefaultPostProcessor(): Processor |
95
|
|
|
{ |
96
|
|
|
return new Processor(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the schema grammar used by the connection. |
101
|
|
|
* |
102
|
|
|
* @return Grammar |
103
|
|
|
*/ |
104
|
|
|
public function getSchemaGrammar() |
105
|
|
|
{ |
106
|
|
|
return $this->schemaGrammar; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the collection prefix for the connection. |
111
|
7 |
|
* |
112
|
|
|
* @return string |
113
|
7 |
|
*/ |
114
|
7 |
|
public function getTablePrefix(): string |
115
|
|
|
{ |
116
|
|
|
return $this->tablePrefix; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Disconnect from the underlying ArangoDB connection. |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
3 |
|
*/ |
124
|
|
|
public function disconnect() |
125
|
3 |
|
{ |
126
|
3 |
|
$this->arangoClient = null; |
127
|
|
|
} |
128
|
3 |
|
|
129
|
|
|
/** |
130
|
3 |
|
* Reconnect to the database. |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
* |
134
|
|
|
* @throws \LogicException |
135
|
|
|
*/ |
136
|
|
|
public function reconnect() |
137
|
|
|
{ |
138
|
|
|
if (is_callable($this->reconnector)) { |
139
|
|
|
// $this->arangoClient = null; |
140
|
|
|
|
141
|
174 |
|
$result = call_user_func($this->reconnector, $this); |
142
|
|
|
|
143
|
174 |
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
174 |
|
throw new LogicException('Lost connection and no reconnector available.'); |
147
|
|
|
} |
148
|
175 |
|
|
149
|
|
|
/** |
150
|
175 |
|
* Reconnect to the database if an ArangoDB connection is missing. |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
protected function reconnectIfMissingConnection() |
155
|
|
|
{ |
156
|
1 |
|
if (is_null($this->arangoClient)) { |
157
|
|
|
$this->reconnect(); |
158
|
1 |
|
} |
159
|
1 |
|
} |
160
|
1 |
|
|
161
|
|
|
public function getArangoClient(): ArangoClient|null |
162
|
2 |
|
{ |
163
|
|
|
return $this->arangoClient; |
164
|
2 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
4 |
|
* @param string|null $database |
168
|
|
|
*/ |
169
|
4 |
|
public function setDatabaseName($database): void |
170
|
|
|
{ |
171
|
|
|
$this->database = $database; |
172
|
|
|
$this->arangoClient->setDatabase($database); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getDatabaseName(): string |
176
|
|
|
{ |
177
|
|
|
return $this->database; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public static function aqb(): ArangoQueryBuilder |
181
|
|
|
{ |
182
|
|
|
return new ArangoQueryBuilder(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|