1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\ConnectionInterface; |
6
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammars\Grammar; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Processors\Processor; |
10
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException; |
11
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
12
|
|
|
|
13
|
|
|
class Builder extends IlluminateQueryBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var QueryBuilder |
17
|
|
|
*/ |
18
|
|
|
public $aqb; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @override |
22
|
|
|
* Create a new query builder instance. |
23
|
|
|
* |
24
|
|
|
* @param ConnectionInterface $connection |
25
|
|
|
* @param Grammar $grammar |
26
|
|
|
* @param Processor $processor |
27
|
|
|
* @param QueryBuilder|null $aqb |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ConnectionInterface $connection, |
30
|
|
|
Grammar $grammar = null, |
31
|
|
|
Processor $processor = null, |
32
|
|
|
QueryBuilder $aqb = null) |
33
|
|
|
{ |
34
|
|
|
$this->connection = $connection; |
35
|
|
|
$this->grammar = $grammar ?: $connection->getQueryGrammar(); |
|
|
|
|
36
|
|
|
$this->processor = $processor ?: $connection->getPostProcessor(); |
37
|
|
|
if (!$aqb instanceof QueryBuilder) { |
38
|
|
|
$aqb = new QueryBuilder(); |
39
|
|
|
} |
40
|
|
|
$this->aqb = $aqb; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Run the query as a "select" statement against the connection. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
protected function runSelect() |
49
|
|
|
{ |
50
|
|
|
return $this->connection->select($this->grammar->compileSelect($this)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the SQL representation of the query. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function toSql() |
59
|
|
|
{ |
60
|
|
|
return $this->grammar->compileSelect($this)->query; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Insert a new record and get the value of the primary key. |
65
|
|
|
* |
66
|
|
|
* @param array $values |
67
|
|
|
* @param string|null $sequence |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function insertGetId(array $values, $sequence = null) |
71
|
|
|
{ |
72
|
|
|
$qb = $this->grammar->compileInsertGetId($this, $values, $sequence); |
73
|
|
|
|
74
|
|
|
$result = $this->getConnection()->execute($qb); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Update a record in the database. |
81
|
|
|
* |
82
|
|
|
* @param array $values |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function update(array $values) |
86
|
|
|
{ |
87
|
|
|
$aqb = $this->grammar->compileUpdate($this, $values); |
88
|
|
|
|
89
|
|
|
return $this->connection->update($aqb); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Delete a record from the database. |
94
|
|
|
* |
95
|
|
|
* @param mixed $_key |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function delete($_key = null) |
99
|
|
|
{ |
100
|
|
|
// If an ID is passed to the method, we will set the where clause to check the |
101
|
|
|
// ID to let developers to simply and quickly remove a single row from this |
102
|
|
|
// database without manually specifying the "where" clauses on the query. |
103
|
|
|
if (! is_null($_key)) { |
104
|
|
|
$this->where($this->from.'._key', '=', $_key); |
105
|
|
|
} |
106
|
|
|
$aqb = $this->grammar->compileDelete($this, $_key); |
|
|
|
|
107
|
|
|
var_dump($aqb->query); |
|
|
|
|
108
|
|
|
return $this->connection->delete($aqb); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.