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