1
- <?php
2
- /**
3
- * Created by PhpStorm.
4
- * User: polyanin
5
- * Date: 13.09.2018
6
- * Time: 17:07
7
- */
8
-
9
- namespace Aplab \Pst \Lib \MysqliManager ;
10
-
1
+ <?php namespace Aplab \Pst \Lib \MysqliManager ;
11
2
12
3
use Aplab \Pst \Lib \Tools ;
13
4
use mysqli ;
@@ -68,48 +59,37 @@ public function __construct(MysqliManager $manager, $name = MysqliManager::DEFAU
68
59
$ this ->select_db ($ config ->dbname );
69
60
}
70
61
71
- /**
72
- * @return mysqli_driver
73
- */
74
- public function getDriver ()
62
+ public function getDriver (): mysqli_driver
75
63
{
76
64
$ hash = spl_object_hash ($ this );
77
65
return $ this ->{$ hash }['driver ' ];
78
66
}
79
67
80
- /**
81
- * execute query
82
- *
83
- * @param string $sql
84
- * @param int $result_mode
85
- * @return Result
86
- */
87
- public function query ($ sql , $ result_mode = MYSQLI_STORE_RESULT )
68
+ public function query (string $ query , $ result_mode = MYSQLI_STORE_RESULT ): Result
88
69
{
89
70
if (MysqliManager::$ debug ) {
90
- $ trace = debug_backtrace (null , 2 );
71
+ $ trace = debug_backtrace (0 , 2 );
91
72
$ trace = array_pop ($ trace );
92
- $ service_info = 'sql: ' . preg_replace ('/ \\s{2,}/ ' , ' ' , $ sql );
73
+ $ service_info = 'sql: ' . preg_replace ('/ \\s{2,}/ ' , ' ' , $ query );
93
74
if ($ trace ) {
94
75
$ service_info = ' line: ' . $ trace ['line ' ] . ' ' . $ service_info ;
95
76
$ service_info = 'file: ' . $ trace ['file ' ] . $ service_info ;
96
77
}
97
78
var_dump ($ service_info );
98
79
}
99
80
try {
100
- $ this ->real_query ($ sql );
101
- /** @noinspection PhpMethodParametersCountMismatchInspection */
81
+ $ this ->real_query ($ query );
102
82
return new Result ($ this );
103
83
} catch (mysqli_sql_exception $ e ) {
104
- /** @noinspection PhpUndefinedFieldInspection */
105
- $ e ->sql = preg_replace ('/ \\s{2,}/ ' , ' ' , $ sql );
84
+ /** @noinspection PhpDynamicFieldDeclarationInspection */
85
+ $ e ->sql = preg_replace ('/ \\s{2,}/ ' , ' ' , $ query );
106
86
$ trace = $ e ->getTrace ();
107
87
$ function = __FUNCTION__ ;
108
88
$ class = get_class ($ this );
109
89
$ break = false ;
110
90
foreach ($ trace as $ data_item ) {
111
91
if ($ break ) {
112
- /** @noinspection PhpUndefinedFieldInspection */
92
+ /** @noinspection PhpDynamicFieldDeclarationInspection */
113
93
$ e ->called_from = $ data_item ;
114
94
break ;
115
95
}
@@ -123,27 +103,16 @@ public function query($sql, $result_mode = MYSQLI_STORE_RESULT)
123
103
}
124
104
}
125
105
126
- /**
127
- * mysql_real_escape_string wrapper shortcut
128
- *
129
- * @param string $string
130
- * @return string|false
131
- */
132
- public function e ($ string )
106
+ public function e (string $ string ): false |string
133
107
{
134
108
return $ this ->real_escape_string ($ string );
135
109
}
136
110
137
111
/**
138
112
* mysql_real_escape_string wrapper advanced and shortcut
139
- * It handles multi-dimensional arrays recursively.
140
- *
141
- * @param string|array $string
142
- * @param bool $quote
143
- * @param bool $double
144
- * @return false|string
113
+ * It handles multidimensional arrays recursively.
145
114
*/
146
- public function q ($ string , $ quote = true , $ double = true )
115
+ public function q (array | string $ string , bool $ quote = true , bool $ double = true ): array | string
147
116
{
148
117
if (is_array ($ string )) {
149
118
$ db = $ this ;
@@ -164,11 +133,8 @@ public function q($string, $quote = true, $double = true)
164
133
/**
165
134
* Put values into backquotes
166
135
* Processes multidimensional arrays recursively.
167
- *
168
- * @param string|array $value
169
- * @return array|string
170
136
*/
171
- public function bq ($ value )
137
+ public function bq (array | string $ value ): array | string
172
138
{
173
139
if (is_array ($ value )) {
174
140
$ db = $ this ;
@@ -182,11 +148,8 @@ public function bq($value)
182
148
183
149
/**
184
150
* Returns a list of tables in the current database
185
- *
186
- * @param boolean $reload
187
- * @return array
188
151
*/
189
- public function getListTables ($ reload = false )
152
+ public function getListTables (bool $ reload = false ): array
190
153
{
191
154
$ hash = spl_object_hash ($ this );
192
155
if ($ reload || (!isset ($ this ->data ['list_tables ' ]))) {
@@ -197,11 +160,8 @@ public function getListTables($reload = false)
197
160
198
161
/**
199
162
* Returns a list of table fields in the current database
200
- *
201
- * @param void
202
- * @return array
203
163
*/
204
- public function listFields ($ table )
164
+ public function listFields (string $ table ): array
205
165
{
206
166
$ hash = spl_object_hash ($ this );
207
167
if (!isset ($ this ->data ['list_fields ' ][$ table ])) {
@@ -214,23 +174,16 @@ public function listFields($table)
214
174
215
175
/**
216
176
* Returns true if table exists into current database, false otherwise
217
- *
218
- * @param string $table
219
- * @param boolean $reload
220
- * @return boolean
221
177
*/
222
- public function tableExists ($ table , $ reload = false )
178
+ public function tableExists (string $ table , bool $ reload = false ): bool
223
179
{
224
180
return in_array ($ table , $ this ->getListTables ($ reload ));
225
181
}
226
182
227
183
/**
228
184
* Returns true if table is empty, false otherwise
229
- *
230
- * @param string $table
231
- * @return boolean
232
185
*/
233
- public function isEmpty ($ table )
186
+ public function isEmpty (string $ table ): bool
234
187
{
235
188
/** @noinspection SqlNoDataSourceInspection */
236
189
/** @noinspection SqlResolve */
@@ -239,14 +192,11 @@ public function isEmpty($table)
239
192
}
240
193
241
194
/**
242
- * @param $table
243
- * @param bool|false $post_check
244
195
* @throws Exception
245
196
*/
246
- public function drop ($ table , $ post_check = false )
197
+ public function drop (string $ table , bool $ post_check = false )
247
198
{
248
199
/** @noinspection SqlNoDataSourceInspection */
249
- /** @noinspection SqlResolve */
250
200
$ this ->query ('DROP TABLE ` ' . $ this ->e ($ table ) . '` ' );
251
201
if ($ post_check && $ this ->tableExists ($ table , true )) {
252
202
$ msg = 'Unable to drop table ' ;
@@ -255,14 +205,11 @@ public function drop($table, $post_check = false)
255
205
}
256
206
257
207
/**
258
- * @param $table
259
- * @param bool|false $post_check
260
208
* @throws Exception
261
209
*/
262
- public function dropIfExists ($ table , $ post_check = false )
210
+ public function dropIfExists (string $ table , bool $ post_check = false )
263
211
{
264
212
/** @noinspection SqlNoDataSourceInspection */
265
- /** @noinspection SqlResolve */
266
213
$ this ->query ('DROP TABLE IF EXISTS ` ' . $ this ->e ($ table ) . '` ' );
267
214
if ($ post_check && $ this ->tableExists ($ table , true )) {
268
215
$ msg = 'Unable to drop table ' ;
@@ -272,16 +219,11 @@ public function dropIfExists($table, $post_check = false)
272
219
273
220
/**
274
221
* Delete the table if it is empty
275
- *
276
- * @param $table
277
- * @param bool|false $post_check
278
- * @throws Exception
279
222
*/
280
- public function dropIfEmpty ($ table , $ post_check = false )
223
+ public function dropIfEmpty (string $ table , bool $ post_check = false )
281
224
{
282
225
if ($ this ->isEmpty ($ table )) {
283
226
/** @noinspection SqlNoDataSourceInspection */
284
- /** @noinspection SqlResolve */
285
227
$ this ->query ('DROP TABLE IF EXISTS ` ' . $ this ->e ($ table ) . '` ' );
286
228
if ($ post_check && $ this ->tableExists ($ table , true )) {
287
229
$ msg = 'Unable to drop table ' ;
@@ -292,15 +234,11 @@ public function dropIfEmpty($table, $post_check = false)
292
234
293
235
/**
294
236
* Split a string that represents multiple sql queries, separated by a separator ";"
295
- *
296
- * @param string $query
297
- * @return array
298
237
*/
299
- public function splitMultiQuery ($ query )
238
+ public function splitMultiQuery (string $ query ): array
300
239
{
301
240
$ ret = array ();
302
241
$ token = Tools::my_strtok ($ query , "; " );
303
-
304
242
while ($ token ) {
305
243
$ prev = $ token ;
306
244
$ ret [] = $ prev ;
@@ -310,17 +248,13 @@ public function splitMultiQuery($query)
310
248
return $ ret ;
311
249
}
312
250
}
313
-
314
251
return $ ret ;
315
252
}
316
253
317
254
/**
318
255
* Returns current database name
319
- *
320
- * @param void
321
- * @return string
322
256
*/
323
- public function selectSchema ()
257
+ public function selectSchema (): string
324
258
{
325
259
return $ this ->query ('select schema() ' )->fetch_one ();
326
260
}
0 commit comments