1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\ConnectionInterface; |
6
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Connection; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar; |
12
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Processor; |
13
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException; |
14
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
15
|
|
|
|
16
|
|
|
class Builder extends IlluminateQueryBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Grammar |
20
|
|
|
*/ |
21
|
|
|
public $grammar; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Connection |
25
|
|
|
*/ |
26
|
|
|
public $connection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var QueryBuilder |
30
|
|
|
*/ |
31
|
|
|
public $aqb; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Alias' are AQL variables |
35
|
|
|
* Sticking with the SQL based naming as this is the Laravel driver. |
36
|
|
|
* @var QueryBuilder |
37
|
|
|
*/ |
38
|
|
|
protected $aliasRegistry = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @override |
42
|
|
|
* Create a new query builder instance. |
43
|
|
|
* |
44
|
|
|
* @param ConnectionInterface $connection |
45
|
|
|
* @param Grammar $grammar |
46
|
|
|
* @param Processor $processor |
47
|
|
|
* @param QueryBuilder|null $aqb |
48
|
|
|
*/ |
49
|
|
|
public function __construct(ConnectionInterface $connection, |
50
|
|
|
Grammar $grammar = null, |
51
|
|
|
Processor $processor = null, |
52
|
|
|
QueryBuilder $aqb = null) |
53
|
|
|
{ |
54
|
|
|
$this->connection = $connection; |
|
|
|
|
55
|
|
|
$this->grammar = $grammar ?: $connection->getQueryGrammar(); |
56
|
|
|
$this->processor = $processor ?: $connection->getPostProcessor(); |
57
|
|
|
if (!$aqb instanceof QueryBuilder) { |
58
|
|
|
$aqb = new QueryBuilder(); |
59
|
|
|
} |
60
|
|
|
$this->aqb = $aqb; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Run the query as a "select" statement against the connection. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function runSelect() |
69
|
|
|
{ |
70
|
|
|
$response = $this->connection->select($this->grammar->compileSelect($this)->aqb); |
|
|
|
|
71
|
|
|
$this->aqb = new QueryBuilder(); |
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the SQL representation of the query. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function toSql() |
81
|
|
|
{ |
82
|
|
|
return $this->grammar->compileSelect($this)->aqb->query; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Insert a new record into the database. |
87
|
|
|
* @param array $values |
88
|
|
|
* @return bool |
89
|
|
|
* @throws BindException |
90
|
|
|
*/ |
91
|
|
|
public function insert(array $values) : bool |
92
|
|
|
{ |
93
|
|
|
$response = $this->getConnection()->insert($this->grammar->compileInsert($this, $values)->aqb); |
|
|
|
|
94
|
|
|
$this->aqb = new QueryBuilder(); |
95
|
|
|
return $response; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Insert a new record and get the value of the primary key. |
100
|
|
|
* |
101
|
|
|
* @param array $values |
102
|
|
|
* @param string|null $sequence |
103
|
|
|
* @return int |
104
|
|
|
* @throws BindException |
105
|
|
|
*/ |
106
|
|
|
public function insertGetId(array $values, $sequence = null) |
107
|
|
|
{ |
108
|
|
|
$response = $this->getConnection()->execute($this->grammar->compileInsertGetId($this, $values, $sequence)->aqb); |
|
|
|
|
109
|
|
|
$this->aqb = new QueryBuilder(); |
110
|
|
|
return $response; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Execute the query as a "select" statement. |
115
|
|
|
* |
116
|
|
|
* @param array|string $columns |
117
|
|
|
* @return Collection |
118
|
|
|
*/ |
119
|
|
|
public function get($columns = ['*']) |
120
|
|
|
{ |
121
|
|
|
$results = collect($this->onceWithColumns(Arr::wrap($columns), function () { |
122
|
|
|
return $this->runSelect(); |
123
|
|
|
})); |
124
|
|
|
$this->aqb = new QueryBuilder(); |
125
|
|
|
return $results; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Update a record in the database. |
130
|
|
|
* |
131
|
|
|
* @param array $values |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function update(array $values) |
135
|
|
|
{ |
136
|
|
|
$response = $this->connection->update($this->grammar->compileUpdate($this, $values)->aqb); |
|
|
|
|
137
|
|
|
$this->aqb = new QueryBuilder(); |
138
|
|
|
return $response; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Delete a record from the database. |
143
|
|
|
* |
144
|
|
|
* @param mixed $_key |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
|
|
public function delete($_key = null) |
148
|
|
|
{ |
149
|
|
|
$response = $this->connection->delete($this->grammar->compileDelete($this, $_key)->aqb); |
|
|
|
|
150
|
|
|
$this->aqb = new QueryBuilder(); |
151
|
|
|
return $response; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function registerAlias(string $table, string $alias) : void |
155
|
|
|
{ |
156
|
|
|
$this->aliasRegistry[$table] = $alias; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getAlias(string $table) : string |
160
|
|
|
{ |
161
|
|
|
return $this->aliasRegistry[$table]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.