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: $this->arrayMode = 'assoc';
64:
65: $sql = Nette\Utils\Strings::replace($sql, '~\'.*?\'|".*?"|\?|\b(?:INSERT|REPLACE|UPDATE)\b~si', array($this, 'callback'));
66:
67: while ($this->counter < count($params)) {
68: $sql .= ' ' . $this->formatValue($params[$this->counter++]);
69: }
70:
71: return array($sql, $this->remaining);
72: }
73:
74:
75:
76:
77: public function callback($m)
78: {
79: $m = $m[0];
80: if ($m[0] === "'" || $m[0] === '"') {
81: return $m;
82:
83: } elseif ($m === '?') {
84: return $this->formatValue($this->params[$this->counter++]);
85:
86: } else {
87: $this->arrayMode = strtoupper($m) === 'UPDATE' ? 'assoc' : 'values';
88: return $m;
89: }
90: }
91:
92:
93:
94: private function formatValue($value)
95: {
96: if (is_string($value)) {
97: if (strlen($value) > 20) {
98: $this->remaining[] = $value;
99: return '?';
100:
101: } else {
102: return $this->connection->quote($value);
103: }
104:
105: } elseif (is_int($value)) {
106: return (string) $value;
107:
108: } elseif (is_float($value)) {
109: return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
110:
111: } elseif (is_bool($value)) {
112: $this->remaining[] = $value;
113: return '?';
114:
115: } elseif ($value === NULL) {
116: return 'NULL';
117:
118: } elseif ($value instanceof Table\ActiveRow) {
119: return $value->getPrimary();
120:
121: } elseif (is_array($value) || $value instanceof \Traversable) {
122: $vx = $kx = array();
123:
124: if (isset($value[0])) {
125: foreach ($value as $v) {
126: $vx[] = $this->formatValue($v);
127: }
128: return implode(', ', $vx);
129:
130: } elseif ($this->arrayMode === 'values') {
131: $this->arrayMode = 'multi';
132: foreach ($value as $k => $v) {
133: $kx[] = $this->driver->delimite($k);
134: $vx[] = $this->formatValue($v);
135: }
136: return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
137:
138: } elseif ($this->arrayMode === 'assoc') {
139: foreach ($value as $k => $v) {
140: $vx[] = $this->driver->delimite($k) . '=' . $this->formatValue($v);
141: }
142: return implode(', ', $vx);
143:
144: } elseif ($this->arrayMode === 'multi') {
145: foreach ($value as $k => $v) {
146: $vx[] = $this->formatValue($v);
147: }
148: return '(' . implode(', ', $vx) . ')';
149: }
150:
151: } elseif ($value instanceof \DateTime) {
152: return $this->driver->formatDateTime($value);
153:
154: } elseif ($value instanceof SqlLiteral) {
155: return $value->__toString();
156:
157: } else {
158: $this->remaining[] = $value;
159: return '?';
160: }
161: }
162:
163: }
164: