Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: sql.php,v 1.10.2.1 2006/03/23 16:58:09 nijel Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /* SQL import plugin for phpMyAdmin */
6  
7 if (isset($plugin_list)) {
8 $plugin_list['sql'] = array(
9 'text' => 'strSQL',
10 'extension' => 'sql',
11 'options_text' => 'strSQLImportOptions',
12 );
13 } else {
14 /* We do not define function when plugin is just queried for information above */
15 $buffer = '';
16 // Defaults for parser
17 $sql = '';
18 $start_pos = 0;
19 $i = 0;
20 while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
21 $data = PMA_importGetNextChunk();
22 if ($data === FALSE) {
23 // subtract data we didn't handle yet and stop processing
24 $offset -= strlen($buffer);
25 break;
26 } elseif ($data === TRUE) {
27 // Handle rest of buffer
28 } else {
29 // Append new data to buffer
30 $buffer .= $data;
31 // Do not parse string when we're not at the end and don't have ; inside
32 if ((strpos($buffer, ';') === FALSE) && !$finished) {
33 continue;
34 }
35 }
36 // Current length of our buffer
37 $len = strlen($buffer);
38 // Grab some SQL queries out of it
39 while ($i < $len) {
40 // Find first interesting character, several strpos seem to be faster than simple loop in php:
41 //while (($i < $len) && (strpos('\'";#-/', $buffer[$i]) === FALSE)) $i++;
42 //if ($i == $len) break;
43 $oi = $i;
44 $p1 = strpos($buffer, '\'', $i);
45 if ($p1 === FALSE) {
46 $p1 = 2147483647;
47 }
48 $p2 = strpos($buffer, '"', $i);
49 if ($p2 === FALSE) {
50 $p2 = 2147483647;
51 }
52 $p3 = strpos($buffer, ';', $i);
53 if ($p3 === FALSE) {
54 $p3 = 2147483647;
55 }
56 $p4 = strpos($buffer, '#', $i);
57 if ($p4 === FALSE) {
58 $p4 = 2147483647;
59 }
60 $p5 = strpos($buffer, '--', $i);
61 if ($p5 === FALSE || $p5 >= ($len - 2) || $buffer[$p5 + 2] > ' ') {
62 $p5 = 2147483647;
63 }
64 $p6 = strpos($buffer, '/*', $i);
65 if ($p6 === FALSE) {
66 $p6 = 2147483647;
67 }
68 $p7 = strpos($buffer, '`', $i);
69 if ($p7 === FALSE) {
70 $p7 = 2147483647;
71 }
72 $i = min ($p1, $p2, $p3, $p4, $p5, $p6, $p7);
73 if ($i == 2147483647) {
74 $i = $oi;
75 if (!$finished) {
76 break;
77 }
78 // at the end there might be some whitespace...
79 if (trim($buffer) == '') {
80 $buffer = '';
81 $len = 0;
82 break;
83 }
84 // We hit end of query, go there!
85 $i = strlen($buffer) - 1;
86 }
87  
88 // Grab current character
89 $ch = $buffer[$i];
90  
91 // Quotes
92 if (!(strpos('\'"`', $ch) === FALSE)) {
93 $quote = $ch;
94 $endq = FALSE;
95 while (!$endq) {
96 // Find next quote
97 $pos = strpos($buffer, $quote, $i + 1);
98 // No quote? Too short string
99 if ($pos === FALSE) {
100 // We hit end of string => unclosed quote, but we handle it as end of query
101 if ($finished) {
102 $endq = TRUE;
103 $i = $len - 1;
104 }
105 break;
106 }
107 // Was not the quote escaped?
108 $j = $pos - 1;
109 while ($buffer[$j] == '\\') $j--;
110 // Even count means it was not escaped
111 $endq = (((($pos - 1) - $j) % 2) == 0);
112 // Skip the string
113 $i = $pos;
114 }
115 if (!$endq) {
116 break;
117 }
118 $i++;
119 // Aren't we at the end?
120 if ($finished && $i == $len) {
121 $i--;
122 } else {
123 continue;
124 }
125 }
126  
127 // Not enough data to decide
128 if ((($i == ($len - 1) && ($ch == '-' || $ch == '/'))
129 || ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-') || ($ch == '/' && $buffer[$i + 1] == '*')))
130 ) && !$finished) {
131 break;
132 }
133  
134 // Comments
135 if ($ch == '#'
136 || ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-' && (($i < ($len - 2) && $buffer[$i + 2] <= ' ') || ($i == ($len - 1) && $finished)))
137 || ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*')
138 ) {
139 // Copy current string to SQL
140 if ($start_pos != $i) {
141 $sql .= substr($buffer, $start_pos, $i - $start_pos);
142 }
143 // Skip the rest
144 $i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i);
145 // didn't we hit end of string?
146 if ($i === FALSE) {
147 if ($finished) {
148 $i = $len - 1;
149 } else {
150 break;
151 }
152 }
153 // Skip *
154 if ($ch == '/') {
155 $i++;
156 }
157 // Skip last char
158 $i++;
159 // Next query part will start here
160 $start_pos = $i;
161 // Aren't we at the end?
162 if ($i == $len) {
163 $i--;
164 } else {
165 continue;
166 }
167 }
168  
169 // End of SQL
170 if ($ch == ';' || ($finished && ($i == $len - 1))) {
171 $tmp_sql = $sql;
172 if ($start_pos < $len) {
173 $tmp_sql .= substr($buffer, $start_pos, $i - $start_pos + 1);
174 }
175 // Do not try to execute empty SQL
176 if (!preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
177 $sql = $tmp_sql;
178 PMA_importRunQuery($sql, substr($buffer, 0, $i + 1));
179 $buffer = substr($buffer, $i + 1);
180 // Reset parser:
181 $len = strlen($buffer);
182 $sql = '';
183 $i = 0;
184 $start_pos = 0;
185 // Any chance we will get a complete query?
186 if ((strpos($buffer, ';') === FALSE) && !$finished) {
187 break;
188 }
189 } else {
190 $i++;
191 $start_pos = $i;
192 }
193 }
194 } // End of parser loop
195 } // End of import loop
196 // Commit any possible data in buffers
197 PMA_importRunQuery('', substr($buffer, 0, $len));
198 PMA_importRunQuery();
199 }
200 ?>