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