. */ /** * Static class for extracting SQL statements from a string or file. * * @author Hans Lellelid * @version $Revision: 1.5 $ * @package creole.util.sql */ class Tools_SQLStatementExtractor { protected static $delimiter = ';'; /** * Get SQL statements from file. * * @param string $filename Path to file to read. * @return array SQL statements */ public static function extractFile($filename) { $buffer = file_get_contents($filename); if ($buffer !== false) { return self::extractStatements(self::getLines($buffer)); } throw new Exception("Unable to read file: " . $filename); } /** * Extract statements from string. * * @param string $txt * @return array */ public static function extract($buffer) { return self::extractStatements(self::getLines($buffer)); } /** * Extract SQL statements from array of lines. * * @param array $lines Lines of the read-in file. * @return string */ protected static function extractStatements($lines) { $statements = array(); $sql = ""; foreach($lines as $line) { $line = trim($line); if (self::startsWith("//", $line) || self::startsWith("--", $line) || self::startsWith("#", $line)) { continue; } if (strlen($line) > 4 && strtoupper(substr($line,0, 4)) == "REM ") { continue; } $sql .= " " . $line; $sql = trim($sql); // SQL defines "--" as a comment to EOL // and in Oracle it may contain a hint // so we cannot just remove it, instead we must end it if (strpos($line, "--") !== false) { $sql .= "\n"; } if (self::endsWith(self::$delimiter, $sql)) { $statements[] = self::substring($sql, 0, strlen($sql)-1 - strlen(self::$delimiter)); $sql = ""; } } return $statements; } // // Some string helper methods // /** * Tests if a string starts with a given string. * @param string $check The substring to check. * @param string $string The string to check in (haystack). * @return boolean True if $string starts with $check, or they are equal, or $check is empty. */ protected static function startsWith($check, $string) { if ($check === "" || $check === $string) { return true; } else { return (strpos($string, $check) === 0) ? true : false; } } /** * Tests if a string ends with a given string. * @param string $check The substring to check. * @param string $string The string to check in (haystack). * @return boolean True if $string ends with $check, or they are equal, or $check is empty. */ protected static function endsWith(/*.string.*/$check, $string) { if ($check === "" || $check === $string) { return true; } else { return (strpos(strrev($string), strrev($check)) === 0) ? true : false; } } /** * a natural way of getting a subtring, php's circular string buffer and strange * return values suck if you want to program strict as of C or friends */ protected static function substring($string, $startpos, $endpos = -1) { $len = strlen($string); $endpos = (int) (($endpos === -1) ? $len-1 : $endpos); if ($startpos > $len-1 || $startpos < 0) { trigger_error("substring(), Startindex out of bounds must be 0 $len-1 || $endpos < $startpos) { trigger_error("substring(), Endindex out of bounds must be $startpos