/
QueryBuilder.php
179 lines (152 loc) · 4.32 KB
/
QueryBuilder.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
<?php
/**
* This file is part of the spiritix/lada-cache package.
*
* @copyright Copyright (c) Matthias Isler <mi@matthias-isler.ch>
* @license MIT
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Spiritix\LadaCache\Reflector;
use Spiritix\LadaCache\Database\QueryBuilder as EloquentQueryBuilder;
/**
* Query builder reflector provides information about an Eloquent query builder object.
*
* @package Spiritix\LadaCache\Reflector
* @author Matthias Isler <mi@matthias-isler.ch>
*/
class QueryBuilder implements HashableReflectorInterface
{
/**
* Since the query builder doesn't know about the related model, we have no way to figure out the name of the
* primary key column. If someone is not using this value as primary key column it won't break anything, it just
* wont consider the row ID's when creating the cache tags.
*
* @todo Get the primary key column from the model.
*/
const PRIMARY_KEY_COLUMN = 'id';
/**
* Query builder instance.
*
* @var QueryBuilder
*/
protected $queryBuilder;
/**
* Initialize reflector.
*
* @param EloquentQueryBuilder $queryBuilder
*/
public function __construct(EloquentQueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
/**
* {@inheritdoc}
*
* @return string
*/
public function getDatabase()
{
return $this->queryBuilder
->getConnection()
->getDatabaseName(); // Fuck this shit
}
/**
* {@inheritdoc}
*
* @return array
*/
public function getTables()
{
// Get main table
$tables = [$this->queryBuilder->from];
// Add possible join tables
$joins = $this->queryBuilder->joins ?: [];
foreach ($joins as $join) {
if (!in_array($join->table, $tables)) {
$tables[] = $join->table;
}
}
return $tables;
}
/**
* {@inheritdoc}
*
* @return array
*/
public function getRows()
{
$rows = [];
$wheres = $this->queryBuilder->wheres ?: [];
foreach ($wheres as $where) {
// Skip unsupported clauses
if (!isset($where['column'])) {
continue;
}
list($table, $column) = $this->splitTableAndColumn($where['column']);
// Make sure that the where clause applies for the main table
if ($table !== null && $table != $this->queryBuilder->from) {
continue;
}
// Make sure that the where clause applies for the primary key column
if ($column != self::PRIMARY_KEY_COLUMN) {
continue;
}
if ($where['type'] == 'Basic') {
if ($where['operator'] == '=' && is_int($where['value'])) {
$rows[] = $where['value'];
}
}
if ($where['type'] == 'In') {
$rows += $where['values'];
}
}
return $rows;
}
/**
* {@inheritdoc}
*
* @return string
*/
public function getSql()
{
return $this->queryBuilder->toSql();
}
/**
* {@inheritdoc}
*
* @return array
*/
public function getParameters()
{
return $this->queryBuilder->getBindings();
}
/**
* Splits an SQL column identifier into table and column.
*
* @param string $sqlString SQL column identifier
*
* @return array [table|null, column]
*/
protected function splitTableAndColumn($sqlString)
{
// Most column identifiers don't contain a database or table name
// In this case just return what we've got
if (strpos($sqlString, '.') === false) {
return [null, $sqlString];
}
$parts = explode('.', $sqlString);
// If we have three parts, the identifier also contains the database name
if (count($parts) === 3) {
$table = $parts[1];
}
// Otherwise it contains table and column
else {
$table = $parts[0];
}
// Column is always the last part
$column = end($parts);
return [$table, $column];
}
}