SQLStatementExtractor.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: SQLStatementExtractor.php,v 1.3 2004/03/20 04:16:51 hlellelid 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 00029 class SQLStatementExtractor { 00030 00031 protected static $delimiter = ';'; 00032 00039 public static function extractFile($filename) { 00040 $buffer = file_get_contents($filename); 00041 if ($buffer === false) { 00042 throw new Exception("Unable to read file: " . $filename); 00043 } 00044 return self::extractStatements(self::getLines($buffer)); 00045 } 00046 00053 public static function extract($buffer) { 00054 return self::extractStatements(self::getLines($buffer)); 00055 } 00056 00063 protected static function extractStatements($lines) { 00064 00065 $statements = array(); 00066 $sql = ""; 00067 00068 foreach($lines as $line) { 00069 00070 $line = trim($line); 00071 00072 if (self::startsWith("//", $line) || 00073 self::startsWith("--", $line) || 00074 self::startsWith("#", $line)) { 00075 continue; 00076 } 00077 00078 if (strlen($line) > 4 && strtoupper(substr($line,0, 4)) == "REM ") { 00079 continue; 00080 } 00081 00082 $sql .= " " . $line; 00083 $sql = trim($sql); 00084 00085 // SQL defines "--" as a comment to EOL 00086 // and in Oracle it may contain a hint 00087 // so we cannot just remove it, instead we must end it 00088 if (strpos($line, "--") !== false) { 00089 $sql .= "\n"; 00090 } 00091 00092 if (self::endsWith(self::$delimiter, $sql)) { 00093 $statements[] = self::substring($sql, 0, strlen($sql) - strlen(self::$delimiter)); 00094 $sql = ""; 00095 } 00096 } 00097 return $statements; 00098 } 00099 00100 // 00101 // Some string helper functions 00102 // 00103 00105 function startsWith($check, $string) { 00106 if ($check === "" || $check === $string) { 00107 return true; 00108 } else { 00109 return (strpos($string, $check) === 0) ? true : false; 00110 } 00111 } 00112 00114 function endsWith($check, $string) { 00115 if ($check === "" || $check === $string) { 00116 return true; 00117 } else { 00118 return (strpos(strrev($string), strrev($check)) === 0) ? true : false; 00119 } 00120 } 00121 00126 function substring($string, $startpos, $endpos = -1) { 00127 $len = strlen($string); 00128 $endpos = (int) (($endpos === -1) ? $len-1 : $endpos); 00129 if ($startpos > $len-1 || $startpos < 0) { 00130 trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR); 00131 } 00132 if ($endpos > $len-1 || $endpos < $startpos) { 00133 trigger_error("substring(), Endindex out of bounds must be $startpos<n<".($len-1), E_USER_ERROR); 00134 } 00135 if ($startpos === $endpos) { 00136 return (string) $string{$startpos}; 00137 } else { 00138 $len = $endpos-$startpos; 00139 } 00140 return substr($string, $startpos, $len+1); 00141 } 00142 00149 protected static function getLines($buffer) { 00150 $lines = preg_split("/\r?\n|\r/", $buffer); 00151 return $lines; 00152 } 00153 00154 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS