2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
17 * Function from phpMyAdmin (http://phpwizard.net/projects/phpMyAdmin/)
19 * Removes comment and splits large sql files into individual queries
21 * Last revision: September 23, 2001 - gandon
23 * @param array the splitted sql commands
24 * @param string the sql commands
25 * @return boolean always true
28 function splitSqlFile(&$ret, $sql)
31 $sql_len = strlen($sql);
36 for ($i = 0; $i < $sql_len; ++$i) {
39 // We are in a string, check for not escaped end of
40 // strings except for backquotes that can't be escaped
43 $i = strpos($sql, $string_start, $i);
44 // No end of string found -> add the current
45 // substring to the returned array
50 // Backquotes or no backslashes before
51 // quotes: it's indeed the end of the
52 // string -> exit the loop
53 else if ($string_start == '`' || $sql[$i-1] != '\\') {
58 // one or more Backslashes before the presumed
61 // first checks for escaped backslashes
63 $escaped_backslash = false;
64 while ($i-$j > 0 && $sql[$i-$j] == '\\') {
65 $escaped_backslash = !$escaped_backslash;
68 // ... if escaped backslashes: it's really the
69 // end of the string -> exit the loop
70 if ($escaped_backslash) {
79 } // end if...elseif...else
81 } // end if (in string)
82 // We are not in a string, first check for delimiter...
83 else if ($char == ';') {
84 // if delimiter found, add the parsed part to the returned array
85 $ret[] = substr($sql, 0, $i);
86 $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
87 $sql_len = strlen($sql);
91 // The submited statement(s) end(s) here
94 } // end else if (is delimiter)
95 // ... then check for start of a string,...
96 else if (($char == '"') || ($char == '\'') || ($char == '`')) {
98 $string_start = $char;
99 } // end else if (is start of string)
101 // for start of a comment (and remove this comment if found)...
102 else if ($char == '#' || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
103 // starting position of the comment depends on the comment type
104 $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
105 // if no "\n" exits in the remaining string, checks for "\r"
107 $end_of_comment = (strpos(' ' . $sql, "\012", $i+2))
108 ? strpos(' ' . $sql, "\012", $i+2)
109 : strpos(' ' . $sql, "\015", $i+2);
110 if (!$end_of_comment) {
111 // no eol found after '#', add the parsed part to the returned
113 $ret[] = trim(substr($sql, 0, $i-1));
116 $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
117 $sql_len = strlen($sql);
120 } // end else if (is comment)
123 // add any rest to the returned array
124 if (!empty($sql) && trim($sql) != '') {
131 * add a prefix.'_' to all tablenames in a query
133 * @param string $query valid MySQL query string
134 * @param string $prefix prefix to add to all table names
135 * @return mixed FALSE on failure
137 function prefixQuery($query, $prefix)
139 $pattern = "/^(INSERT INTO|REPLACE INTO|CREATE TABLE|ALTER TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU";
140 $pattern2 = "/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU";
141 if (preg_match($pattern, $query, $matches) || preg_match($pattern2, $query, $matches)) {
142 $replace = "\\1 ".$prefix."\\4\\5";
143 $matches[0] = preg_replace($pattern, $replace, $query);
145 if ($matches[1] == 'INSERT INTO') {
146 $parts = explode(' ', $matches[0]);
147 $size_of_parts = count($parts);
148 if ($parts[$size_of_parts-2] == 'FROM') {
149 $parts[$size_of_parts-1] = $prefix . str_replace('`', '', $parts[$size_of_parts-1]);
150 $matches[0] = implode(' ', $parts);
152 } else if ($matches[1] == 'ALTER TABLE') {
153 $parts = explode(' ', $matches[0]);
154 if ($parts[3] == 'RENAME') {
155 $parts[4] = $prefix . str_replace('`', '', $parts[4]);
156 $matches[0] = implode(' ', $parts);