1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelSqlLogger\Objects\Concerns; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
|
7
|
|
|
trait ReplacesBindings |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Replace bindings. |
11
|
|
|
* |
12
|
|
|
* @param string $sql |
13
|
|
|
* @param array $bindings |
14
|
|
|
* |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
protected function replaceBindings($sql, array $bindings) |
18
|
|
|
{ |
19
|
|
|
$generalRegex = $this->getRegex(); |
20
|
|
|
|
21
|
|
|
foreach ($this->formatBindings($bindings) as $key => $binding) { |
22
|
|
|
$regex = is_numeric($key) ? $generalRegex : $this->getNamedParameterRegex($key); |
23
|
|
|
$sql = preg_replace($regex, $this->value($binding), $sql, 1); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $sql; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get final value that will be displayed in query. |
31
|
|
|
* |
32
|
|
|
* @param mixed $value |
33
|
|
|
* |
34
|
|
|
* @return int|string |
35
|
|
|
*/ |
36
|
|
|
protected function value($value) |
37
|
|
|
{ |
38
|
|
|
if ($value === null) { |
39
|
|
|
return 'null'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return is_numeric($value) ? $value : "'" . $value . "'"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get regex to be used for named parameter with given name. |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
protected function getNamedParameterRegex($name) |
53
|
|
|
{ |
54
|
|
|
if (mb_substr($name, 0, 1) == ':') { |
55
|
|
|
$name = mb_substr($name, 1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->wrapRegex($this->notInsideQuotes('\:' . preg_quote($name), false)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Format bindings values. |
63
|
|
|
* |
64
|
|
|
* @param array $bindings |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function formatBindings($bindings) |
69
|
|
|
{ |
70
|
|
|
foreach ($bindings as $key => $binding) { |
71
|
|
|
if ($binding instanceof DateTime) { |
72
|
|
|
$bindings[$key] = $binding->format('Y-m-d H:i:s'); |
73
|
|
|
} elseif (is_string($binding)) { |
74
|
|
|
$bindings[$key] = str_replace("'", "\\'", $binding); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $bindings; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get regex to be used to replace bindings. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getRegex() |
87
|
|
|
{ |
88
|
|
|
return $this->wrapRegex( |
89
|
|
|
$this->notInsideQuotes('?') |
90
|
|
|
. '|' . |
91
|
|
|
$this->notInsideQuotes('\:\w+', false) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Wrap regex. |
97
|
|
|
* |
98
|
|
|
* @param string $regex |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function wrapRegex($regex) |
103
|
|
|
{ |
104
|
|
|
return '#' . $regex . '#ms'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Create partial regex to find given text not inside quotes. |
109
|
|
|
* |
110
|
|
|
* @param string $string |
111
|
|
|
* @param bool $quote |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function notInsideQuotes($string, $quote = true) |
116
|
|
|
{ |
117
|
|
|
if ($quote) { |
118
|
|
|
$string = preg_quote($string); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return |
122
|
|
|
// double quotes - ignore "" and everything inside quotes for example " abc \"err " |
123
|
|
|
'(?:""|"(?:[^"]|\\")*?[^\\\]")(*SKIP)(*F)|' . $string . |
124
|
|
|
'|' . |
125
|
|
|
// single quotes - ignore '' and everything inside quotes for example ' abc \'err ' |
126
|
|
|
'(?:\\\'\\\'|\'(?:[^\']|\\\')*?[^\\\]\')(*SKIP)(*F)|' . $string; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|