-
Notifications
You must be signed in to change notification settings - Fork 10
/
pdo-debug.php
62 lines (58 loc) · 1.81 KB
/
pdo-debug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* Class PdoDebugger
*
* Emulates the PDO SQL statement in an extremely simple kind of way
*/
class PdoDebugger
{
/**
* Returns the emulated SQL string
*
* @param $raw_sql
* @param $parameters
* @return mixed
*/
static public function show($raw_sql, $parameters)
{
$keys = array();
$values = array();
/*
* Get longest keys first, sot the regex replacement doesn't
* cut markers (ex : replace ":username" with "'joe'name"
* if we have a param name :user )
*/
$isNamedMarkers = false;
if (count($parameters) && is_string(key($parameters))) {
uksort($parameters, function($k1, $k2) {
return strlen($k2) - strlen($k1);
});
$isNamedMarkers = true;
}
foreach ($parameters as $key => $value) {
// check if named parameters (':param') or anonymous parameters ('?') are used
if (is_string($key)) {
$keys[] = '/:'.ltrim($key, ':').'/';
} else {
$keys[] = '/[?]/';
}
// bring parameter into human-readable format
if (is_string($value)) {
$values[] = "'" . addslashes($value) . "'";
} elseif(is_int($value)) {
$values[] = strval($value);
} elseif (is_float($value)) {
$values[] = strval($value);
} elseif (is_array($value)) {
$values[] = implode(',', $value);
} elseif (is_null($value)) {
$values[] = 'NULL';
}
}
if ($isNamedMarkers) {
return preg_replace($keys, $values, $raw_sql);
} else {
return preg_replace($keys, $values, $raw_sql, 1, $count);
}
}
}