1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class SqlPreprocessor extends Object
22: {
23:
24: private $connection;
25:
26:
27: private $driver;
28:
29:
30: private $params;
31:
32:
33: private $remaining;
34:
35:
36: private $counter;
37:
38:
39: private $arrayMode;
40:
41:
42:
43: public function __construct(Connection $connection)
44: {
45: $this->connection = $connection;
46: $this->driver = $connection->getSupplementalDriver();
47: }
48:
49:
50:
51: 52: 53: 54: 55:
56: public function process($sql, $params)
57: {
58: $this->params = $params;
59: $this->counter = 0;
60: $this->remaining = array();
61:
62: $cmd = strtoupper(substr(ltrim($sql), 0, 6));
63: $this->arrayMode = $cmd === 'INSERT' || $cmd === 'REPLAC' ? 'values' : 'assoc';
64:
65: 66: 67: 68:
69: $sql = Strings::replace($sql, '~\'.*?\'|".*?"|\?~s', array($this, 'callback'));
70:
71: while ($this->counter < count($params)) {
72: $sql .= ' ' . $this->formatValue($params[$this->counter++]);
73: }
74:
75: return array($sql, $this->remaining);
76: }
77:
78:
79:
80:
81: public function callback($m)
82: {
83: $m = $m[0];
84: if ($m[0] === "'" || $m[0] === '"') {
85: return $m;
86:
87: } else {
88: return $this->formatValue($this->params[$this->counter++]);
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 TableRow) {
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: