1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the moss-storage package |
5
|
|
|
* |
6
|
|
|
* (c) Michal Wachowski <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moss\Storage\Query; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\Connection; |
15
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
16
|
|
|
use Moss\Storage\Model\ModelInterface; |
17
|
|
|
use Moss\Storage\Query\Accessor\AccessorInterface; |
18
|
|
|
use Moss\Storage\Query\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use Moss\Storage\Query\Relation\RelationFactoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract Query |
23
|
|
|
* Implements basic query methods |
24
|
|
|
* |
25
|
|
|
* @package Moss\Storage |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractQuery extends AbstractRelational |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Connection |
31
|
|
|
*/ |
32
|
|
|
protected $connection; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ModelInterface |
36
|
|
|
*/ |
37
|
|
|
protected $model; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var QueryBuilder |
41
|
|
|
*/ |
42
|
|
|
protected $builder; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AccessorInterface |
46
|
|
|
*/ |
47
|
|
|
protected $accessor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var EventDispatcherInterface |
51
|
|
|
*/ |
52
|
|
|
protected $dispatcher; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor |
56
|
|
|
* |
57
|
|
|
* @param Connection $connection |
58
|
|
|
* @param ModelInterface $model |
59
|
|
|
* @param RelationFactoryInterface $factory |
60
|
|
|
* @param AccessorInterface $accessor |
61
|
|
|
* @param EventDispatcherInterface $dispatcher |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function __construct(Connection $connection, ModelInterface $model, RelationFactoryInterface $factory, AccessorInterface $accessor, EventDispatcherInterface $dispatcher) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->connection = $connection; |
66
|
|
|
$this->model = $model; |
67
|
|
|
$this->factory = $factory; |
68
|
|
|
$this->accessor = $accessor; |
69
|
|
|
$this->dispatcher = $dispatcher; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns var type |
74
|
|
|
* |
75
|
|
|
* @param mixed $var |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
abstract protected function getType($var); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns connection |
83
|
|
|
* |
84
|
|
|
* @return Connection |
85
|
|
|
*/ |
86
|
|
|
public function connection() |
87
|
|
|
{ |
88
|
|
|
return $this->connection; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns model |
93
|
|
|
* |
94
|
|
|
* @return ModelInterface |
95
|
|
|
*/ |
96
|
|
|
public function model() |
97
|
|
|
{ |
98
|
|
|
return $this->model; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns query builder instance |
103
|
|
|
* |
104
|
|
|
* @return QueryBuilder |
105
|
|
|
*/ |
106
|
|
|
public function builder() |
107
|
|
|
{ |
108
|
|
|
return $this->builder; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns current query string |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getSQL() |
117
|
|
|
{ |
118
|
|
|
return (string) $this->builder->getSQL(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Removes bound values by their prefix |
123
|
|
|
* If prefix is null - clears all bound values |
124
|
|
|
* |
125
|
|
|
* @param null|string $prefix |
126
|
|
|
*/ |
127
|
|
|
protected function resetBinds($prefix = null) |
128
|
|
|
{ |
129
|
|
|
if ($prefix === null) { |
130
|
|
|
$this->builder->setParameters([]); |
131
|
|
|
|
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$params = (array) $this->builder->getParameters(); |
136
|
|
|
$types = (array) $this->builder->getParameterTypes(); |
137
|
|
|
|
138
|
|
|
foreach (array_keys($params) as $key) { |
139
|
|
|
if (strpos($key, $prefix) === 1) { |
140
|
|
|
unset($params[$key], $types[$key]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->builder->setParameters($params, $types); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns array with bound values and their placeholders as keys |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function binds() |
153
|
|
|
{ |
154
|
|
|
return $this->builder->getParameters(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.