2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * SQL import plugin for phpMyAdmin
6 * @package phpMyAdmin-Import
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['sql'] = array(
19 'options_text' => __('Options'),
21 $compats = PMA_DBI_getCompatibilities();
22 if (count($compats) > 0) {
24 foreach($compats as $val) {
27 $plugin_list['sql']['options'] = array(
28 array('type' => 'begin_group', 'name' => 'general_opts'),
31 'name' => 'compatibility',
32 'text' => __('SQL compatibility mode:'),
35 'manual_MySQL_Database_Administration',
41 'name' => 'no_auto_value_on_zero',
42 'text' => __('Do not use <code>AUTO_INCREMENT</code> for zero values'),
44 'manual_MySQL_Database_Administration',
46 'sqlmode_no_auto_value_on_zero'
50 array('type' => 'end_group'),
54 /* We do not define function when plugin is just queried for information above */
59 // Defaults for parser
64 $big_value = 2147483647;
65 $delimiter_keyword = 'DELIMITER '; // include the space because it's mandatory
66 $length_of_delimiter_keyword = strlen($delimiter_keyword);
68 if (isset($_POST['sql_delimiter'])) {
69 $sql_delimiter = $_POST['sql_delimiter'];
74 // Handle compatibility options
76 if (isset($_REQUEST['sql_compatibility']) && 'NONE' != $_REQUEST['sql_compatibility']) {
77 $sql_modes[] = $_REQUEST['sql_compatibility'];
79 if (isset($_REQUEST['sql_no_auto_value_on_zero'])) {
80 $sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO';
82 if (count($sql_modes) > 0) {
83 PMA_DBI_try_query('SET SQL_MODE="' . implode(',', $sql_modes) . '"');
88 * will be set in PMA_importGetNextChunk()
90 * @global boolean $GLOBALS['finished']
92 $GLOBALS['finished'] = false;
94 while (!($GLOBALS['finished'] && $i >= $len) && !$error && !$timeout_passed) {
95 $data = PMA_importGetNextChunk();
96 if ($data === FALSE) {
97 // subtract data we didn't handle yet and stop processing
98 $offset -= strlen($buffer);
100 } elseif ($data === TRUE) {
101 // Handle rest of buffer
103 // Append new data to buffer
107 // Do not parse string when we're not at the end and don't have ; inside
108 if ((strpos($buffer, $sql_delimiter, $i) === FALSE) && !$GLOBALS['finished']) {
112 // Current length of our buffer
113 $len = strlen($buffer);
115 // Grab some SQL queries out of it
117 $found_delimiter = false;
118 // Find first interesting character
120 // this is about 7 times faster that looking for each sequence i
121 // one by one with strpos()
122 if (preg_match('/(\'|"|#|-- |\/\*|`|(?i)(?<![A-Z0-9_])' . $delimiter_keyword . ')/', $buffer, $matches, PREG_OFFSET_CAPTURE
, $i)) {
123 // in $matches, index 0 contains the match for the complete
124 // expression but we don't use it
125 $first_position = $matches[1][1];
127 $first_position = $big_value;
130 * @todo we should not look for a delimiter that might be
131 * inside quotes (or even double-quotes)
133 // the cost of doing this one with preg_match() would be too high
134 $first_sql_delimiter = strpos($buffer, $sql_delimiter, $i);
135 if ($first_sql_delimiter === FALSE) {
136 $first_sql_delimiter = $big_value;
138 $found_delimiter = true;
141 // set $i to the position of the first quote, comment.start or delimiter found
142 $i = min($first_position, $first_sql_delimiter);
144 if ($i == $big_value) {
145 // none of the above was found in the string
148 if (!$GLOBALS['finished']) {
151 // at the end there might be some whitespace...
152 if (trim($buffer) == '') {
157 // We hit end of query, go there!
158 $i = strlen($buffer) - 1;
161 // Grab current character
165 if (strpos('\'"`', $ch) !== FALSE) {
170 $pos = strpos($buffer, $quote, $i +
1);
172 * Behave same as MySQL and accept end of query as end of backtick.
173 * I know this is sick, but MySQL behaves like this:
175 * SELECT * FROM `table
179 * SELECT * FROM `table`
181 if ($pos === FALSE && $quote == '`' && $found_delimiter) {
182 $pos = $first_sql_delimiter - 1;
183 // No quote? Too short string
184 } elseif ($pos === FALSE) {
185 // We hit end of string => unclosed quote, but we handle it as end of query
186 if ($GLOBALS['finished']) {
190 $found_delimiter = false;
193 // Was not the quote escaped?
195 while ($buffer[$j] == '\\') $j--;
196 // Even count means it was not escaped
197 $endq = (((($pos - 1) - $j) %
2) == 0);
201 if ($first_sql_delimiter < $pos) {
202 $found_delimiter = false;
209 // Aren't we at the end?
210 if ($GLOBALS['finished'] && $i == $len) {
217 // Not enough data to decide
218 if ((($i == ($len - 1) && ($ch == '-' ||
$ch == '/'))
219 ||
($i == ($len - 2) && (($ch == '-' && $buffer[$i +
1] == '-')
220 ||
($ch == '/' && $buffer[$i +
1] == '*')))) && !$GLOBALS['finished']) {
226 ||
($i < ($len - 1) && $ch == '-' && $buffer[$i +
1] == '-'
227 && (($i < ($len - 2) && $buffer[$i +
2] <= ' ')
228 ||
($i == ($len - 1) && $GLOBALS['finished'])))
229 ||
($i < ($len - 1) && $ch == '/' && $buffer[$i +
1] == '*')
231 // Copy current string to SQL
232 if ($start_pos != $i) {
233 $sql .= substr($buffer, $start_pos, $i - $start_pos);
236 $start_of_comment = $i;
237 // do not use PHP_EOL here instead of "\n", because the export
238 // file might have been produced on a different system
239 $i = strpos($buffer, $ch == '/' ?
'*/' : "\n", $i);
240 // didn't we hit end of string?
242 if ($GLOBALS['finished']) {
254 // We need to send the comment part in case we are defining
255 // a procedure or function and comments in it are valuable
256 $sql .= substr($buffer, $start_of_comment, $i - $start_of_comment);
257 // Next query part will start here
259 // Aren't we at the end?
266 // Change delimiter, if redefined, and skip it (don't send to server!)
267 if (strtoupper(substr($buffer, $i, $length_of_delimiter_keyword)) == $delimiter_keyword
268 && ($i +
$length_of_delimiter_keyword < $len)) {
269 // look for EOL on the character immediately after 'DELIMITER '
270 // (see previous comment about PHP_EOL)
271 $new_line_pos = strpos($buffer, "\n", $i +
$length_of_delimiter_keyword);
272 // it might happen that there is no EOL
273 if (FALSE === $new_line_pos) {
274 $new_line_pos = $len;
276 $sql_delimiter = substr($buffer, $i +
$length_of_delimiter_keyword, $new_line_pos - $i - $length_of_delimiter_keyword);
277 $i = $new_line_pos +
1;
278 // Next query part will start here
284 if ($found_delimiter ||
($GLOBALS['finished'] && ($i == $len - 1))) {
286 if ($start_pos < $len) {
287 $length_to_grab = $i - $start_pos;
289 if (! $found_delimiter) {
292 $tmp_sql .= substr($buffer, $start_pos, $length_to_grab);
293 unset($length_to_grab);
295 // Do not try to execute empty SQL
296 if (! preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
298 PMA_importRunQuery($sql, substr($buffer, 0, $i +
strlen($sql_delimiter)));
299 $buffer = substr($buffer, $i +
strlen($sql_delimiter));
301 $len = strlen($buffer);
305 // Any chance we will get a complete query?
306 //if ((strpos($buffer, ';') === FALSE) && !$GLOBALS['finished']) {
307 if ((strpos($buffer, $sql_delimiter) === FALSE) && !$GLOBALS['finished']) {
315 } // End of parser loop
316 } // End of import loop
317 // Commit any possible data in buffers
318 PMA_importRunQuery('', substr($buffer, 0, $len));
319 PMA_importRunQuery();