@@ -15,7 +15,7 @@ class SimpleMySQLi {
15
15
'assoc ' , 'obj ' , 'num ' , 'col '
16
16
];
17
17
private const ALLOWED_FETCH_TYPES_FETCH_ALL = [
18
- 'keyPair ' , 'keyPairArr ' , 'group ' , 'groupCol ' , 'groupObj '
18
+ 'keyPair ' , 'keyPairArr ' , 'group ' , 'groupCol ' , 'groupObj ' , ' scalar ' , ' count ' , ' metaKeys '
19
19
];
20
20
21
21
/**
@@ -61,7 +61,20 @@ public function query(string $sql, $values = [], string $types = ''): self {
61
61
if (!is_array ($ values )) $ values = [$ values ]; //Convert scalar to array
62
62
63
63
if (!$ types ) $ types = str_repeat ('s ' , count ($ values )); //String type for all variables if not specified
64
-
64
+
65
+ //query trace
66
+ $ str ='/* ' . PHP_EOL ;
67
+ $ str .='query:mysqli_query ' . PHP_EOL ;
68
+ $ str .='user: ' . \aw2_library::get ('app.user.email ' ) . PHP_EOL ;
69
+ $ str .='module: ' . \aw2_library::get ('module.slug ' ) . PHP_EOL ;
70
+ $ str .='post_type: ' . \aw2_library::get ('module.collection.post_type ' ) . PHP_EOL ;
71
+ $ str .='template: ' . \aw2_library::get ('template.name ' ) . PHP_EOL ;
72
+ $ str .='*/ ' . PHP_EOL ;
73
+
74
+ //prepare query
75
+ $ sql = $ str .$ sql ;
76
+
77
+ $ start =microtime (true );
65
78
if (!$ values ) {
66
79
$ this ->stmtResult = $ this ->mysqli ->query ($ sql ); //Use non-prepared query if no values to bind for efficiency
67
80
} else {
@@ -70,9 +83,51 @@ public function query(string $sql, $values = [], string $types = ''): self {
70
83
$ stmt ->execute ();
71
84
$ this ->stmtResult = $ stmt ->get_result ();
72
85
}
86
+
87
+ if (\aw2_library::get ('debug_config.mysqli ' )==='yes ' )\aw2 \debug \query (['start ' =>$ start ,'main ' =>$ sql ]);
88
+
89
+ return $ this ;
90
+ }
91
+
92
+
93
+ public function multi_query (string $ sql ): self {
94
+
95
+ //query trace
96
+ $ str ='/* ' . PHP_EOL ;
97
+ $ str .='query:multi_query ' . PHP_EOL ;
98
+
99
+ $ str .='user: ' . \aw2_library::get ('app.user.email ' ) . PHP_EOL ;
100
+ $ str .='module: ' . \aw2_library::get ('module.slug ' ) . PHP_EOL ;
101
+ $ str .='post_type: ' . \aw2_library::get ('module.collection.post_type ' ) . PHP_EOL ;
102
+ $ str .='template: ' . \aw2_library::get ('template.name ' ) . PHP_EOL ;
103
+ $ str .='*/ ' . PHP_EOL ;
104
+
105
+ //prepare query
106
+ $ sql = $ str .$ sql ;
107
+
108
+ $ start =microtime (true );
109
+ $ reply = $ this ->mysqli ->multi_query ($ sql ); //Use non-prepared query if no values to bind for efficiency
110
+
111
+
112
+ do {
113
+
114
+ /* store first result set */
115
+ $ temp =$ this ->mysqli ->store_result ();
116
+ //\util::var_dump($temp);
117
+ if (is_object ($ temp )){
118
+ $ this ->stmtResult =$ temp ;
119
+
120
+ }
121
+ } while ($ this ->mysqli ->more_results () && $ this ->mysqli ->next_result ());
122
+
123
+ if (\aw2_library::get ('debug_config.mysqli ' )==='yes ' )\aw2 \debug \query (['start ' =>$ start ,'main ' =>$ sql ]);
73
124
125
+
74
126
return $ this ;
127
+
75
128
}
129
+
130
+
76
131
77
132
/**
78
133
* Used in order to be more efficient if same SQL is used with different values. Is really a re-execute function
@@ -205,6 +260,34 @@ public function fetch(string $fetchType = '', string $className = 'stdClass', ar
205
260
return $ row ;
206
261
}
207
262
263
+
264
+ public function fetchTranspose (bool $ transpose ): array {
265
+ $ stmtResult = $ this ->stmtResult ;
266
+ $ dataset = ["raw " =>array (),"rows " =>array ()];
267
+
268
+ if (!$ transpose ){
269
+ $ dataset ['raw ' ]=$ stmtResult ->fetch_all (MYSQLI_ASSOC );
270
+ $ dataset ['rows ' ]=$ dataset ['raw ' ];
271
+ return $ dataset ;
272
+ }
273
+
274
+
275
+ while ($ row = $ stmtResult ->fetch_assoc ()) {
276
+ $ dataset ['raw ' ][]=$ row ;
277
+ switch ($ row ['type ' ]){
278
+ case 'data_id ' :
279
+ $ dataset ['rows ' ][$ row ['data_id ' ]]=array ();
280
+ break ;
281
+ case 'meta ' :
282
+ $ dataset ['rows ' ][$ row ['data_id ' ]][$ row ['meta_key ' ]]=$ row ['meta_value ' ];
283
+ break ;
284
+ }
285
+ }
286
+
287
+ return $ dataset ;
288
+ }
289
+
290
+
208
291
/**
209
292
* Fetch all results in array
210
293
*
@@ -261,7 +344,27 @@ public function fetchAll(string $fetchType = '', string $className = 'stdClass',
261
344
while ($ row = $ stmtResult ->fetch_row ()) {
262
345
$ arr [] = $ row [0 ];
263
346
}
264
- } else if ($ fetchType === 'keyPair ' || $ fetchType === 'groupCol ' ) {
347
+ }
348
+
349
+ else if ($ fetchType === 'scalar ' ) {
350
+ if ($ stmtResult ->num_rows > 0 ) {
351
+ $ row = $ stmtResult ->fetch_row ();
352
+ $ arr [] = $ row [0 ];
353
+ }
354
+ }
355
+
356
+ else if ($ fetchType === 'count ' ) {
357
+ if ($ stmtResult ->num_rows !== 1 ) {
358
+ throw new SimpleMySQLiException ("Fetch Count must have 1 and exactly 1 row " );
359
+ }
360
+ if ($ stmtResult ->field_count !== 1 ) {
361
+ throw new SimpleMySQLiException ("Fetch Count must have 1 and exactly 1 column " );
362
+ }
363
+ $ row = $ stmtResult ->fetch_row ();
364
+ $ arr [] = $ row [0 ];
365
+ }
366
+
367
+ else if ($ fetchType === 'keyPair ' || $ fetchType === 'groupCol ' ) {
265
368
if ($ stmtResult ->field_count !== 2 ) {
266
369
throw new SimpleMySQLiException ("The fetch type: ' $ fetchType' must have exactly two columns in query " );
267
370
}
@@ -270,7 +373,17 @@ public function fetchAll(string $fetchType = '', string $className = 'stdClass',
270
373
if ($ fetchType === 'keyPair ' ) $ arr [$ row [0 ]] = $ row [1 ];
271
374
else if ($ fetchType === 'groupCol ' ) $ arr [$ row [0 ]][] = $ row [1 ];
272
375
}
273
- } else if ($ fetchType === 'keyPairArr ' || $ fetchType === 'group ' || $ fetchType === 'groupObj ' ) {
376
+ }
377
+ else if ($ fetchType === 'metaKeys ' ) {
378
+ if ($ stmtResult ->field_count !== 2 ) {
379
+ throw new SimpleMySQLiException ("The fetch type: ' $ fetchType' must have exactly two columns in query " );
380
+ }
381
+
382
+ while ($ row = $ stmtResult ->fetch_assoc ()) {
383
+ $ arr [$ row ['meta_key ' ]] = $ row ['meta_value ' ];
384
+ }
385
+ }
386
+ else if ($ fetchType === 'keyPairArr ' || $ fetchType === 'group ' || $ fetchType === 'groupObj ' ) {
274
387
$ firstColName = $ stmtResult ->fetch_field_direct (0 )->name ;
275
388
276
389
if (!$ className ) $ className = 'stdClass ' ;
0 commit comments