SQLStatementExtractor.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: SQLStatementExtractor.php,v 1.3 2004/05/23 11:05:27 micha Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00030 class SQLStatementExtractor 00031 { 00032 var $delimiter = ';'; 00033 00040 function extractFile($filename) 00041 { 00042 $buffer = file_get_contents($filename); 00043 if ($buffer === false) { 00044 return new Exception(CREOLE_ERROR, "Unable to read file: " . $filename); 00045 } 00046 return SQLStatementExtractor::extractStatements(SQLStatementExtractor::getLines($buffer)); 00047 } 00048 00055 function & extract(&$buffer) 00056 { 00057 return SQLStatementExtractor::extractStatements(SQLStatementExtractor::getLines($buffer)); 00058 } 00059 00066 function & extractStatements(&$lines) 00067 { 00068 $self =& SQLStatementExtractor::getInstance(); 00069 $statements = array(); 00070 $sql = ""; 00071 00072 foreach($lines as $line) 00073 { 00074 $line = trim($line); 00075 00076 if (SQLStatementExtractor::startsWith("//", $line) || 00077 SQLStatementExtractor::startsWith("--", $line) || 00078 SQLStatementExtractor::startsWith("#", $line)) { 00079 continue; 00080 } 00081 00082 if (strlen($line) > 4 && strtoupper(substr($line,0, 4)) == "REM ") { 00083 continue; 00084 } 00085 00086 $sql .= " " . $line; 00087 $sql = trim($sql); 00088 00089 // SQL defines "--" as a comment to EOL 00090 // and in Oracle it may contain a hint 00091 // so we cannot just remove it, instead we must end it 00092 if (strpos($line, "--") !== false) { 00093 $sql .= "\n"; 00094 } 00095 00096 if (SQLStatementExtractor::endsWith($self->delimiter, $sql)) { 00097 $statements[] = SQLStatementExtractor::substring($sql, 0, strlen($sql) - strlen($self->delimiter)); 00098 $sql = ""; 00099 } 00100 } 00101 00102 return $statements; 00103 } 00104 00105 // 00106 // Some string helper functions 00107 // 00108 00110 function startsWith($check, $string) 00111 { 00112 if ($check === "" || $check === $string) { 00113 return true; 00114 } else { 00115 return (strpos($string, $check) === 0) ? true : false; 00116 } 00117 } 00118 00120 function endsWith($check, $string) 00121 { 00122 if ($check === "" || $check === $string) { 00123 return true; 00124 } else { 00125 return (strpos(strrev($string), strrev($check)) === 0) ? true : false; 00126 } 00127 } 00128 00133 function substring($string, $startpos, $endpos = -1) 00134 { 00135 $len = strlen($string); 00136 $endpos = (int) (($endpos === -1) ? $len-1 : $endpos); 00137 if ($startpos > $len-1 || $startpos < 0) { 00138 trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR); 00139 } 00140 if ($endpos > $len-1 || $endpos < $startpos) { 00141 trigger_error("substring(), Endindex out of bounds must be $startpos<n<".($len-1), E_USER_ERROR); 00142 } 00143 if ($startpos === $endpos) { 00144 return (string) $string{$startpos}; 00145 } else { 00146 $len = $endpos-$startpos; 00147 } 00148 return substr($string, $startpos, $len+1); 00149 } 00150 00157 function & getLines($buffer) 00158 { 00159 $lines = preg_split("/\r?\n|\r/", $buffer); 00160 return $lines; 00161 } 00162 00163 /* 00164 * @private 00165 */ 00166 function & getInstance() 00167 { 00168 static $instance; 00169 00170 if ($instance === null) 00171 { 00172 $instance = new SQLStatementExtractor(); 00173 } 00174 00175 return $instance; 00176 } 00177 00178 }

This file is part of the Creole[php4] library.


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS