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
|
|
|
*/ |
27
|
|
|
protected $query; |
28
|
|
|
/** @var bool True to show requests */ |
29
|
|
|
protected $debug = false; |
30
|
|
|
|
31
|
|
|
/** Constructor */ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->database = $GLOBALS['__core']->getContainer()->getDatabase(); |
|
|
|
|
35
|
|
|
$this->sqlBuilder = $GLOBALS['__core']->getContainer()->getSqlBuilder(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $metadata |
40
|
|
|
* |
41
|
|
|
* @deprecated Use entity() |
42
|
|
|
* @return QueryInterface|string |
43
|
|
|
*/ |
44
|
|
|
public function className(string $metadata = null) |
45
|
|
|
{ |
46
|
|
|
if (func_num_args() === 0) { |
47
|
|
|
return $this->metadata->className; |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
return $this->entity($metadata); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** @deprecated Use QueryInterface implementation */ |
55
|
|
|
public function own_limit($st, $en = 0) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->query->limit($st, $en); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @deprecated Use QueryInterface implementation */ |
63
|
|
|
public function own_group_by($params) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->query->groupBy($this->class_name, $params); |
|
|
|
|
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @deprecated Use QueryInterface implementation */ |
71
|
|
|
public function own_order_by($field, $direction = 'ASC') |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->query->orderBy($this->class_name, $field, $direction); |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @see idbQuery::random() */ |
79
|
|
|
public function random(& $return_value = null) |
80
|
|
|
{ |
81
|
|
|
// Add random ordering |
82
|
|
|
$this->order_by('', 'RAND()'); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// Correctly perform db request for multiple data |
85
|
|
|
return func_num_args() ? $this->exec($return_value) : $this->exec(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $columnName |
90
|
|
|
* @param string $sorting |
91
|
|
|
* |
92
|
|
|
* @deprecated Use groupBy() |
93
|
|
|
* @return QueryInterface|static |
94
|
|
|
*/ |
95
|
|
|
public function order_by($columnName, $sorting = 'ASC') |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return $this->orderBy($this->metadata->tableName, $columnName, $sorting); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Execute current query and receive collection of RecordInterface objects from database. |
102
|
|
|
* @deprecated Use self::find() |
103
|
|
|
* @return RecordInterface[] Database entities collection |
104
|
|
|
*/ |
105
|
|
|
public function exec() : array |
106
|
|
|
{ |
107
|
|
|
return $this->find(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @deprecated Use QueryInterface implementation */ |
111
|
|
|
public function or_($relation = 'OR') |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
// Получим либо переданную группу условий, либо создадим новую, потом добавим её в массив групп условий запроса |
114
|
|
|
$cond_group = new Condition($relation); |
115
|
|
|
|
116
|
|
|
// Установим текущую группу условий с которой работает запрос |
117
|
|
|
$this->cConditionGroup = &$cond_group; |
|
|
|
|
118
|
|
|
|
119
|
|
|
// Добавим нову группу условий в коллекцию групп |
120
|
|
|
$this->condition->arguments[] = $cond_group; |
|
|
|
|
121
|
|
|
|
122
|
|
|
// Вернем себя для цепирования |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @deprecated Use QueryInterface implementation */ |
127
|
|
|
public function debug($value = true) |
128
|
|
|
{ |
129
|
|
|
db()->debug($this->debug = $value); |
|
|
|
|
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** @deprecated Use QueryInterface implementation */ |
135
|
|
|
public function fieldsNew($fieldName, & $return = null) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
return call_user_func_array(array($this, 'fields'), func_get_args()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** @deprecated Use QueryInterface implementation */ |
141
|
|
|
public function group_by($field) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$this->query->groupBy($this->metadata->tablename, $field); |
|
|
|
|
144
|
|
|
|
145
|
|
|
// Вернем себя для цепирования |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** @deprecated Use QueryInterface implementation */ |
150
|
|
|
public function add_field($field, $alias = null, $own = true) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
// Если передан псевдоним для поля, то подставим его |
153
|
|
|
if (isset($alias)) { |
154
|
|
|
$field = $field . ' as ' . $alias; |
155
|
|
|
} else { |
156
|
|
|
$alias = $field; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Добавим виртуальное поле |
160
|
|
|
if ($own) { |
161
|
|
|
$this->own_virtual_fields[$alias] = $field; |
162
|
|
|
} else { |
163
|
|
|
$this->virtual_fields[$alias] = $field; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Вернем себя для цепирования |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** @deprecated Use QueryInterface implementation */ |
171
|
|
|
public function innerCount($field = '*') |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
return $this->query->count(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** @deprecated Use QueryInterface implementation */ |
177
|
|
|
public function id($value) |
178
|
|
|
{ |
179
|
|
|
$this->query->primary($value); |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Add condition to current query. |
186
|
|
|
* This method supports receives three possible types for $fieldName, |
187
|
|
|
* this is deprecated logic and this should be changed to use separate methods |
188
|
|
|
* for each argument type. |
189
|
|
|
* |
190
|
|
|
* @param string|ConditionInterface|ArgumentInterface $fieldName Entity field name |
191
|
|
|
* @param string $fieldValue Value |
192
|
|
|
* @param string $relation Relation between field name and its value |
193
|
|
|
* |
194
|
|
|
* @deprecated Use QueryInterface implementation |
195
|
|
|
* @return self Chaining |
196
|
|
|
*/ |
197
|
|
|
public function cond($fieldName, $fieldValue = null, $relation = '=') |
198
|
|
|
{ |
199
|
|
|
// If empty array is passed |
200
|
|
|
if (is_string($fieldName)) { |
201
|
|
|
return $this->where($fieldName, $fieldValue, $relation); |
|
|
|
|
202
|
|
|
} elseif (is_array($fieldValue) && !sizeof($fieldValue)) { |
203
|
|
|
$this->empty = true; |
204
|
|
|
return $this; |
205
|
|
|
} elseif ($fieldName instanceof ConditionInterface) { |
206
|
|
|
$this->whereCondition($fieldName); |
|
|
|
|
207
|
|
|
} elseif ($fieldName instanceof ArgumentInterface) { |
208
|
|
|
$this->whereCondition((new Condition())->addArgument($fieldName)); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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
.