1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
namespace samson\activerecord; |
3
|
|
|
|
4
|
|
|
use samsonframework\orm\ArgumentInterface; |
5
|
|
|
use samsonframework\orm\Condition; |
6
|
|
|
use samsonframework\orm\ConditionInterface; |
7
|
|
|
use samsonframework\orm\QueryHandler; |
8
|
|
|
use samsonframework\orm\QueryInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class will be removed in next major release. |
12
|
|
|
* @author Vitaly Iegorov <[email protected]> |
13
|
|
|
* @deprecated Should be removed ASAP in favor of generated classes or DI |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class dbQuery extends QueryHandler |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** Virtual field for base table */ |
19
|
|
|
public $own_virtual_fields = array(); |
20
|
|
|
/** Virtual fields */ |
21
|
|
|
public $virtual_fields = array(); |
22
|
|
|
public $empty = false; |
23
|
|
|
protected $class_name; |
24
|
|
|
/** |
25
|
|
|
* @var QueryInterface |
26
|
|
|
* @\samsonframework\containerannotation\Injectable |
27
|
|
|
*/ |
28
|
|
|
protected $query; |
29
|
|
|
/** @var bool True to show requests */ |
30
|
|
|
protected $debug = false; |
31
|
|
|
|
32
|
|
|
/** Constructor */ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->database = $GLOBALS['__core']->getContainer()->getDatabase(); |
|
|
|
|
36
|
|
|
$this->sqlBuilder = $GLOBALS['__core']->getContainer()->getSqlBuilder(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $metadata |
41
|
|
|
* |
42
|
|
|
* @deprecated Use entity() |
43
|
|
|
* @return QueryInterface|string |
44
|
|
|
*/ |
45
|
|
|
public function className(string $metadata = null) |
46
|
|
|
{ |
47
|
|
|
if (func_num_args() === 0) { |
48
|
|
|
return $this->metadata->className; |
|
|
|
|
49
|
|
|
} else { |
50
|
|
|
return $this->entity($metadata); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** @deprecated Use QueryInterface implementation */ |
56
|
|
|
public function own_limit($st, $en = 0) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->query->limit($st, $en); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @deprecated Use QueryInterface implementation */ |
64
|
|
|
public function own_group_by($params) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->query->groupBy($this->class_name, $params); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @deprecated Use QueryInterface implementation */ |
72
|
|
|
public function own_order_by($field, $direction = 'ASC') |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->query->orderBy($this->class_name, $field, $direction); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @deprecated Use QueryInterface implementation */ |
80
|
|
|
public function flush() |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @see idbQuery::random() */ |
86
|
|
|
public function random(& $return_value = null) |
87
|
|
|
{ |
88
|
|
|
// Add random ordering |
89
|
|
|
$this->order_by('', 'RAND()'); |
|
|
|
|
90
|
|
|
|
91
|
|
|
// Correctly perform db request for multiple data |
92
|
|
|
return func_num_args() ? $this->exec($return_value) : $this->exec(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $columnName |
97
|
|
|
* @param string $sorting |
98
|
|
|
* |
99
|
|
|
* @deprecated Use groupBy() |
100
|
|
|
* @return QueryInterface|static |
101
|
|
|
*/ |
102
|
|
|
public function order_by($columnName, $sorting = 'ASC') |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return $this->orderBy($this->metadata->tableName, $columnName, $sorting); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Execute current query and receive collection of RecordInterface objects from database. |
109
|
|
|
* @deprecated Use self::find() |
110
|
|
|
* @return RecordInterface[] Database entities collection |
111
|
|
|
*/ |
112
|
|
|
public function exec() : array |
113
|
|
|
{ |
114
|
|
|
return $this->find(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @deprecated Use QueryInterface implementation */ |
118
|
|
|
public function or_($relation = 'OR') |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
// Получим либо переданную группу условий, либо создадим новую, потом добавим её в массив групп условий запроса |
121
|
|
|
$cond_group = new Condition($relation); |
122
|
|
|
|
123
|
|
|
// Установим текущую группу условий с которой работает запрос |
124
|
|
|
$this->cConditionGroup = &$cond_group; |
|
|
|
|
125
|
|
|
|
126
|
|
|
// Добавим нову группу условий в коллекцию групп |
127
|
|
|
$this->condition->arguments[] = $cond_group; |
|
|
|
|
128
|
|
|
|
129
|
|
|
// Вернем себя для цепирования |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @deprecated Use QueryInterface implementation */ |
134
|
|
|
public function debug($value = true) |
135
|
|
|
{ |
136
|
|
|
db()->debug($this->debug = $value); |
|
|
|
|
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** @deprecated Use QueryInterface implementation */ |
142
|
|
|
public function fieldsNew($fieldName, & $return = null) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
return call_user_func_array(array($this, 'fields'), func_get_args()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** @deprecated Use QueryInterface implementation */ |
148
|
|
|
public function group_by($field) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$this->query->groupBy($this->metadata->tablename, $field); |
|
|
|
|
151
|
|
|
|
152
|
|
|
// Вернем себя для цепирования |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @deprecated Use QueryInterface implementation */ |
157
|
|
|
public function add_field($field, $alias = null, $own = true) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
// Если передан псевдоним для поля, то подставим его |
160
|
|
|
if (isset($alias)) { |
161
|
|
|
$field = $field . ' as ' . $alias; |
162
|
|
|
} else { |
163
|
|
|
$alias = $field; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Добавим виртуальное поле |
167
|
|
|
if ($own) { |
168
|
|
|
$this->own_virtual_fields[$alias] = $field; |
169
|
|
|
} else { |
170
|
|
|
$this->virtual_fields[$alias] = $field; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Вернем себя для цепирования |
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** @deprecated Use QueryInterface implementation */ |
178
|
|
|
public function innerCount($field = '*') |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
return $this->query->count(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** @deprecated Use QueryInterface implementation */ |
184
|
|
|
public function id($value) |
185
|
|
|
{ |
186
|
|
|
$this->query->primary($value); |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add condition to current query. |
193
|
|
|
* This method supports receives three possible types for $fieldName, |
194
|
|
|
* this is deprecated logic and this should be changed to use separate methods |
195
|
|
|
* for each argument type. |
196
|
|
|
* |
197
|
|
|
* @param string|ConditionInterface|ArgumentInterface $fieldName Entity field name |
198
|
|
|
* @param string $fieldValue Value |
199
|
|
|
* @param string $relation Relation between field name and its value |
200
|
|
|
* |
201
|
|
|
* @deprecated Use QueryInterface implementation |
202
|
|
|
* @return self Chaining |
203
|
|
|
*/ |
204
|
|
|
public function cond($fieldName, $fieldValue = null, $relation = '=') |
205
|
|
|
{ |
206
|
|
|
// If empty array is passed |
207
|
|
|
if (is_string($fieldName)) { |
208
|
|
|
return $this->where($fieldName, $fieldValue, $relation); |
|
|
|
|
209
|
|
|
} elseif (is_array($fieldValue) && !sizeof($fieldValue)) { |
210
|
|
|
$this->empty = true; |
211
|
|
|
return $this; |
212
|
|
|
} elseif ($fieldName instanceof ConditionInterface) { |
213
|
|
|
$this->whereCondition($fieldName); |
|
|
|
|
214
|
|
|
} elseif ($fieldName instanceof ArgumentInterface) { |
215
|
|
|
$this->whereCondition((new Condition())->addArgument($fieldName)); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.