Skip to content

Commit 5df92f9

Browse files
committed
Added path to make life easier while debugging awesome queries and support for multi query
1 parent b7d845f commit 5df92f9

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
2-
"name": "websitebeaver/simple-mysqli",
2+
"name": "wpoets/simple-mysqli",
33
"description": "Wrapper class for MySQLi prepared statements",
44
"keywords": ["MySQLi", "MySQL", "prepared", "database"],
55
"license": "MIT",
66
"authors": [
77
{
88
"name": "Daniel Marcus",
99
"email": "daniel@websitebeaver.com"
10+
},
11+
{
12+
"name": "WPoets Team",
13+
"email": "daniel@websitebeaver.com"
1014
}
1115
],
1216
"require": {

simple-mysqli.php

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SimpleMySQLi {
1515
'assoc', 'obj', 'num', 'col'
1616
];
1717
private const ALLOWED_FETCH_TYPES_FETCH_ALL = [
18-
'keyPair', 'keyPairArr', 'group', 'groupCol', 'groupObj'
18+
'keyPair', 'keyPairArr', 'group', 'groupCol', 'groupObj', 'scalar', 'count', 'metaKeys'
1919
];
2020

2121
/**
@@ -61,7 +61,20 @@ public function query(string $sql, $values = [], string $types = ''): self {
6161
if(!is_array($values)) $values = [$values]; //Convert scalar to array
6262

6363
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);
6578
if(!$values) {
6679
$this->stmtResult = $this->mysqli->query($sql); //Use non-prepared query if no values to bind for efficiency
6780
} else {
@@ -70,9 +83,51 @@ public function query(string $sql, $values = [], string $types = ''): self {
7083
$stmt->execute();
7184
$this->stmtResult = $stmt->get_result();
7285
}
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]);
73124

125+
74126
return $this;
127+
75128
}
129+
130+
76131

77132
/**
78133
* 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
205260
return $row;
206261
}
207262

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+
208291
/**
209292
* Fetch all results in array
210293
*
@@ -261,7 +344,27 @@ public function fetchAll(string $fetchType = '', string $className = 'stdClass',
261344
while($row = $stmtResult->fetch_row()) {
262345
$arr[] = $row[0];
263346
}
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') {
265368
if($stmtResult->field_count !== 2) {
266369
throw new SimpleMySQLiException("The fetch type: '$fetchType' must have exactly two columns in query");
267370
}
@@ -270,7 +373,17 @@ public function fetchAll(string $fetchType = '', string $className = 'stdClass',
270373
if($fetchType === 'keyPair') $arr[$row[0]] = $row[1];
271374
else if($fetchType === 'groupCol') $arr[$row[0]][] = $row[1];
272375
}
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') {
274387
$firstColName = $stmtResult->fetch_field_direct(0)->name;
275388

276389
if(!$className) $className = 'stdClass';

0 commit comments

Comments
 (0)