1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class SqlPreprocessor extends Nette\Object
24: {
25:
26: private $connection;
27:
28:
29: private $driver;
30:
31:
32: private $params;
33:
34:
35: private $remaining;
36:
37:
38: private $counter;
39:
40:
41: private $arrayMode;
42:
43:
44:
45: public function __construct(Connection $connection)
46: {
47: $this->connection = $connection;
48: $this->driver = $connection->getSupplementalDriver();
49: }
50:
51:
52:
53: 54: 55: 56: 57:
58: public function process($sql, $params)
59: {
60: $this->params = $params;
61: $this->counter = 0;
62: $this->remaining = array();
63:
64: $cmd = strtoupper(substr(ltrim($sql), 0, 6)); 65: $this->arrayMode = $cmd === 'INSERT' || $cmd === 'REPLAC' ? 'values' : 'assoc';
66:
67: 68: 69: 70: 71:
72: $sql = Nette\String::replace($sql, '~\'.*?\'|".*?"|:[a-zA-Z0-9_]+:|\?~s', array($this, 'callback'));
73:
74: while ($this->counter < count($params)) {
75: $sql .= ' ' . $this->formatValue($params[$this->counter++]);
76: }
77:
78: return array($sql, $this->remaining);
79: }
80:
81:
82:
83:
84: public function callback($m)
85: {
86: $m = $m[0];
87: if ($m[0] === "'" || $m[0] === '"') { 88: return $m;
89:
90: } elseif ($m[0] === '?') { 91: return $this->formatValue($this->params[$this->counter++]);
92:
93: } elseif ($m[0] === ':') { 94: $s = substr($m, 1, -1);
95: return isset($this->connection->substitutions[$s]) ? $this->connection->substitutions[$s] : $m;
96: }
97: }
98:
99:
100:
101: private function formatValue($value)
102: {
103: if (is_string($value)) {
104: if (strlen($value) > 20) {
105: $this->remaining[] = $value;
106: return '?';
107:
108: } else {
109: return $this->connection->quote($value);
110: }
111:
112: } elseif (is_int($value)) {
113: return (string) $value;
114:
115: } elseif (is_float($value)) {
116: return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
117:
118: } elseif (is_bool($value)) {
119: return $value ? 1 : 0;
120:
121: } elseif ($value === NULL) {
122: return 'NULL';
123:
124: } elseif (is_array($value) || $value instanceof \Traversable) {
125: $vx = $kx = array();
126:
127: if (isset($value[0])) { 128: foreach ($value as $v) {
129: $vx[] = $this->formatValue($v);
130: }
131: return implode(', ', $vx);
132:
133: } elseif ($this->arrayMode === 'values') { 134: $this->arrayMode = 'multi';
135: foreach ($value as $k => $v) {
136: $kx[] = $this->driver->delimite($k);
137: $vx[] = $this->formatValue($v);
138: }
139: return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
140:
141: } elseif ($this->arrayMode === 'assoc') { 142: foreach ($value as $k => $v) {
143: $vx[] = $this->driver->delimite($k) . '=' . $this->formatValue($v);
144: }
145: return implode(', ', $vx);
146:
147: } elseif ($this->arrayMode === 'multi') { 148: foreach ($value as $k => $v) {
149: $vx[] = $this->formatValue($v);
150: }
151: return ', (' . implode(', ', $vx) . ')';
152: }
153:
154: } elseif ($value instanceof \DateTime) {
155: return $this->driver->formatDateTime($value);
156:
157: } elseif ($value instanceof SqlLiteral) {
158: return $value->value;
159:
160: } else {
161: $this->remaining[] = $value;
162: return '?';
163: }
164: }
165:
166: }
167: