2 /***************************************************************************
5 * begin : Thu May 31, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: sql_parse.php,v 1.8 2002/03/18 23:53:12 psotfx Exp $
11 ****************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 ***************************************************************************/
22 /***************************************************************************
24 * These functions are mainly for use in the db_utilities under the admin
25 * however in order to make these functions available elsewhere, specifically
26 * in the installation phase of phpBB I have seperated out a couple of
27 * functions into this file. JLH
29 \***************************************************************************/
32 // remove_comments will strip the sql comment lines out of an uploaded sql file
33 // specifically for mssql and postgres type files in the install....
35 function remove_comments(&$output)
37 $lines = explode("\n", $output);
40 // try to keep mem. use down
41 $linecount = count($lines);
44 for($i = 0; $i < $linecount; $i++)
46 if( preg_match("/^\/\*/", preg_quote($lines[$i])) )
53 $output .= $lines[$i] . "\n";
56 if( preg_match("/\*\/$/", preg_quote($lines[$i])) )
67 // remove_remarks will strip the sql comment lines out of an uploaded sql file
69 function remove_remarks($sql)
71 $lines = explode("\n", $sql);
73 // try to keep mem. use down
76 $linecount = count($lines);
79 for ($i = 0; $i < $linecount; $i++)
81 if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
83 if ($lines[$i][0] != "#")
85 $output .= $lines[$i] . "\n";
91 // Trading a bit of speed for lower mem. use here.
101 // split_sql_file will split an uploaded sql file into single sql statements.
102 // Note: expects trim() to have already been run on $sql.
104 function split_sql_file($sql, $delimiter)
106 // Split up our string into "possible" SQL statements.
107 $tokens = explode($delimiter, $sql);
113 // we don't actually care about the matches preg gives us.
116 // this is faster than calling count($oktens) every time thru the loop.
117 $token_count = count($tokens);
118 for ($i = 0; $i < $token_count; $i++)
120 // Don't wanna add an empty string as the last thing in the array.
121 if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0)))
123 // This is the total number of single quotes in the token.
124 $total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
125 // Counts single quotes that are preceded by an odd number of backslashes,
126 // which means they're escaped quotes.
127 $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
129 $unescaped_quotes = $total_quotes - $escaped_quotes;
131 // If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
132 if (($unescaped_quotes % 2) == 0)
134 // It's a complete sql statement.
135 $output[] = $tokens[$i];
141 // incomplete sql statement. keep adding tokens until we have a complete one.
142 // $temp will hold what we have so far.
143 $temp = $tokens[$i] . $delimiter;
147 // Do we have a complete statement yet?
148 $complete_stmt = false;
150 for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++)
152 // This is the total number of single quotes in the token.
153 $total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
154 // Counts single quotes that are preceded by an odd number of backslashes,
155 // which means they're escaped quotes.
156 $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
158 $unescaped_quotes = $total_quotes - $escaped_quotes;
160 if (($unescaped_quotes % 2) == 1)
162 // odd number of unescaped quotes. In combination with the previous incomplete
163 // statement(s), we now have a complete statement. (2 odds always make an even)
164 $output[] = $temp . $tokens[$j];
171 $complete_stmt = true;
172 // make sure the outer loop continues at the right point.
177 // even number of unescaped quotes. We still don't have a complete statement.
178 // (1 odd and 1 even always make an odd)
179 $temp .= $tokens[$j] . $delimiter;