1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript; |
3
|
|
|
|
4
|
|
|
use ORM; |
5
|
|
|
use PDO; |
6
|
|
|
use PDOException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* children class to handle the database |
10
|
|
|
* |
11
|
|
|
* @since 2.2.0 |
12
|
|
|
* |
13
|
|
|
* @package Redaxscript |
14
|
|
|
* @category Db |
15
|
|
|
* @author Henry Ruhs |
16
|
|
|
* |
17
|
|
|
* @method $this _addJoinSource(string $operator, string $table, string|array $constraint, string $tableAlias) |
18
|
|
|
* @method $this _addOrderBy(string $column, string $value) |
19
|
|
|
* @method $this _addWhere(string $clause, string|array $value) |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Db extends ORM |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* instance of the config class |
26
|
|
|
* |
27
|
|
|
* @var Config |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
protected static $_config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* constructor of the class |
34
|
|
|
* |
35
|
|
|
* @since 2.6.0 |
36
|
|
|
* |
37
|
|
|
* @param Config $config instance of the config class |
38
|
|
|
*/ |
39
|
|
|
|
40
|
4 |
|
public static function construct(Config $config) |
41
|
|
|
{ |
42
|
4 |
|
self::$_config = $config; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* init the class |
47
|
|
|
* |
48
|
|
|
* @since 3.1.0 |
49
|
|
|
*/ |
50
|
|
|
|
51
|
4 |
|
public static function init() |
52
|
|
|
{ |
53
|
4 |
|
$dbType = self::$_config->get('dbType'); |
54
|
4 |
|
$dbHost = self::$_config->get('dbHost'); |
55
|
4 |
|
$dbName = self::$_config->get('dbName'); |
56
|
4 |
|
$dbUser = self::$_config->get('dbUser'); |
57
|
4 |
|
$dbPassword = self::$_config->get('dbPassword'); |
58
|
4 |
|
$dbSocket = strstr($dbHost, '.sock'); |
59
|
|
|
|
60
|
|
|
/* handle various types */ |
61
|
|
|
|
62
|
4 |
|
if ($dbType === 'mssql' || $dbType === 'mysql' || $dbType === 'pgsql') |
63
|
|
|
{ |
64
|
3 |
|
if ($dbType === 'mssql') |
65
|
|
|
{ |
66
|
1 |
|
self::configure('connection_string', 'sqlsrv:server=' . $dbHost . ';database=' . $dbName); |
67
|
|
|
} |
68
|
3 |
|
if ($dbType === 'mysql') |
69
|
|
|
{ |
70
|
1 |
|
self::configure('connection_string', 'mysql:' . ($dbSocket ? 'unix_socket' : 'host') . '=' . $dbHost . ';dbname=' . $dbName . ';charset=utf8'); |
71
|
|
|
} |
72
|
3 |
|
if ($dbType === 'pgsql') |
73
|
|
|
{ |
74
|
1 |
|
self::configure('connection_string', 'pgsql:' . ($dbSocket ? 'unix_socket' : 'host') . '=' . $dbHost . ';dbname=' . $dbName . ';options=--client_encoding=utf8'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* username and password */ |
78
|
|
|
|
79
|
3 |
|
self::configure('username', $dbUser); |
80
|
3 |
|
if ($dbPassword) |
81
|
|
|
{ |
82
|
3 |
|
self::configure('password', $dbPassword); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/* else handle sqlite */ |
87
|
|
|
|
88
|
4 |
|
if ($dbType === 'sqlite') |
89
|
|
|
{ |
90
|
1 |
|
self::configure('sqlite:' . $dbHost); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* general */ |
94
|
|
|
|
95
|
4 |
|
self::configure( |
96
|
|
|
[ |
97
|
4 |
|
'caching' => true, |
98
|
|
|
'caching_auto_clear' => true, |
99
|
|
|
'return_result_sets' => true |
100
|
|
|
]); |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* get the database status |
105
|
|
|
* |
106
|
|
|
* @since 3.1.0 |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
|
111
|
1 |
|
public static function getStatus() : int |
112
|
|
|
{ |
113
|
1 |
|
$output = 0; |
114
|
|
|
|
115
|
|
|
/* has connection */ |
116
|
|
|
|
117
|
|
|
try |
118
|
|
|
{ |
119
|
1 |
|
$dbType = self::$_config->get('dbType') === 'mssql' ? 'sqlsrv' : self::$_config->get('dbType'); |
120
|
1 |
|
$dbDriver = self::getDb()->getAttribute(PDO::ATTR_DRIVER_NAME); |
121
|
1 |
|
if ($dbType === $dbDriver) |
122
|
|
|
{ |
123
|
1 |
|
$output = self::countTablePrefix() > 7 ? 2 : 1; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
catch (PDOException $exception) |
127
|
|
|
{ |
128
|
|
|
$output = 0; |
129
|
|
|
} |
130
|
1 |
|
return $output; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* set the auto increment |
135
|
|
|
* |
136
|
|
|
* @since 4.0.0 |
137
|
|
|
* |
138
|
|
|
* @param string $table name of the table |
139
|
|
|
* @param int $increment value of the auto increment |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
|
144
|
|
|
public static function setAutoIncrement(string $table = null, int $increment = 0) : bool |
145
|
|
|
{ |
146
|
|
|
$dbType = self::$_config->get('dbType'); |
147
|
|
|
$dbPrefix = self::$_config->get('dbPrefix'); |
148
|
|
|
|
149
|
|
|
/* mysql */ |
150
|
|
|
|
151
|
|
|
if ($dbType === 'mysql') |
152
|
|
|
{ |
153
|
|
|
return self::rawExecute('ALTER TABLE ' . $dbPrefix . $table . ' AUTO_INCREMENT = ' . $increment); |
154
|
|
|
} |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* count table with prefix |
160
|
|
|
* |
161
|
|
|
* @since 3.1.0 |
162
|
|
|
* |
163
|
|
|
* @return int |
164
|
|
|
*/ |
165
|
|
|
|
166
|
2 |
|
public static function countTablePrefix() : int |
167
|
|
|
{ |
168
|
2 |
|
$output = 0; |
169
|
2 |
|
$dbType = self::$_config->get('dbType'); |
170
|
2 |
|
$dbName = self::$_config->get('dbName'); |
171
|
2 |
|
$dbPrefix = self::$_config->get('dbPrefix'); |
172
|
|
|
|
173
|
|
|
/* mssql and mysql */ |
174
|
|
|
|
175
|
2 |
|
if ($dbType === 'mssql' || $dbType === 'mysql') |
176
|
|
|
{ |
177
|
|
|
$output = self::forTable('information_schema.tables') |
178
|
|
|
->where($dbType === 'mssql' ? 'table_catalog' : 'table_schema', $dbName) |
179
|
|
|
->whereLike('table_name', $dbPrefix . '%') |
180
|
|
|
->count(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/* pgsql */ |
184
|
|
|
|
185
|
2 |
|
if ($dbType === 'pgsql') |
186
|
|
|
{ |
187
|
|
|
$output = self::forTable('pg_catalog.pg_tables') |
188
|
|
|
->whereLike('tablename', $dbPrefix . '%') |
189
|
|
|
->whereNotLike('tablename', 'pg_%') |
190
|
|
|
->whereNotLike('tablename', 'sql_%') |
191
|
|
|
->count(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/* sqlite */ |
195
|
|
|
|
196
|
2 |
|
if ($dbType === 'sqlite') |
197
|
|
|
{ |
198
|
2 |
|
$output = self::forTable('sqlite_master') |
199
|
2 |
|
->whereLike('tbl_name', $dbPrefix . '%') |
200
|
2 |
|
->whereNotLike('tbl_name', 'sql_%') |
201
|
2 |
|
->count(); |
202
|
|
|
} |
203
|
2 |
|
return $output; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* for table with prefix |
208
|
|
|
* |
209
|
|
|
* @since 2.2.0 |
210
|
|
|
* |
211
|
|
|
* @param string $table name of the table |
212
|
|
|
* @param string $connection which connection to use |
213
|
|
|
* |
214
|
|
|
* @return self |
215
|
|
|
*/ |
216
|
|
|
|
217
|
14 |
|
public static function forTablePrefix(string $table = null, string $connection = self::DEFAULT_CONNECTION) : self |
218
|
|
|
{ |
219
|
14 |
|
return new self(self::$_config->get('dbPrefix') . $table, [], $connection); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* left join with prefix |
224
|
|
|
* |
225
|
|
|
* @since 2.2.0 |
226
|
|
|
* |
227
|
|
|
* @param string $table name of the table |
228
|
|
|
* @param string|array $constraint constraint as needed |
229
|
|
|
* @param string $tableAlias alias of the table |
230
|
|
|
* |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
|
|
|
234
|
1 |
|
public function leftJoinPrefix(string $table = null, string $constraint = null, string $tableAlias = null) : self |
235
|
|
|
{ |
236
|
1 |
|
return $this->_addJoinSource('LEFT', self::$_config->get('dbPrefix') . $table, $constraint, $tableAlias); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* where like with many |
241
|
|
|
* |
242
|
|
|
* @since 3.0.0 |
243
|
|
|
* |
244
|
|
|
* @param array $columnArray array of column names |
245
|
|
|
* @param array $likeArray array of the like |
246
|
|
|
* |
247
|
|
|
* @return self |
248
|
|
|
*/ |
249
|
|
|
|
250
|
1 |
|
public function whereLikeMany(array $columnArray = [], array $likeArray = []) : self |
251
|
|
|
{ |
252
|
1 |
|
return $this->_addWhere('(' . implode($columnArray, ' LIKE ? OR ') . ' LIKE ?)', $likeArray); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* where language is |
257
|
|
|
* |
258
|
|
|
* @since 3.0.0 |
259
|
|
|
* |
260
|
|
|
* @param string $language value of the language |
261
|
|
|
* |
262
|
|
|
* @return self |
263
|
|
|
*/ |
264
|
|
|
|
265
|
3 |
|
public function whereLanguageIs(string $language = null) : self |
266
|
|
|
{ |
267
|
3 |
|
return $this->_addWhere('(language = ? OR language IS NULL)', $language); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* order by global setting |
272
|
|
|
* |
273
|
|
|
* @since 3.3.0 |
274
|
|
|
* |
275
|
|
|
* @param string $column name of the column |
276
|
|
|
* |
277
|
|
|
* @return self |
278
|
|
|
*/ |
279
|
|
|
|
280
|
1 |
|
public function orderBySetting(string $column = null) : self |
281
|
|
|
{ |
282
|
1 |
|
$order = self::forTablePrefix('settings')->where('name', 'order')->findOne()->value; |
283
|
1 |
|
return $this->_addOrderBy($column, $order); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* limit by global setting |
288
|
|
|
* |
289
|
|
|
* @since 3.3.0 |
290
|
|
|
* |
291
|
|
|
* @param int $step step of the offset |
292
|
|
|
* |
293
|
|
|
* @return self |
294
|
|
|
*/ |
295
|
|
|
|
296
|
1 |
|
public function limitBySetting(int $step = null) : self |
297
|
|
|
{ |
298
|
1 |
|
$limit = self::forTablePrefix('settings')->where('name', 'limit')->findOne()->value; |
299
|
1 |
|
$this->_limit = $step > 0 ? $step * $limit . ',' . $limit : $limit; |
300
|
1 |
|
return $this; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|