SQLStatementExtractor Class Reference

List of all members.

Static Public Member Functions

static extractFile ($filename)
 Get SQL statements from file.
static extract ($buffer)
 Extract statements from string.

Static Protected Member Functions

static extractStatements ($lines)
 Extract SQL statements from array of lines.
static startsWith ($check, $string)
 Tests if a string starts with a given string.
static endsWith ($check, $string)
 Tests if a string ends with a given string.
static substring ($string, $startpos, $endpos=-1)
 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
static getLines ($buffer)
 Convert string buffer into array of lines.

Static Protected Attributes

static $delimiter = ';'


Detailed Description

Definition at line 29 of file SQLStatementExtractor.php.


Member Function Documentation

static SQLStatementExtractor::endsWith ( check,
string 
) [static, protected]

Tests if a string ends with a given string.

Parameters:
string $check The substring to check.
string $string The string to check in (haystack).
Returns:
boolean True if $string ends with $check, or they are equal, or $check is empty.

Definition at line 124 of file SQLStatementExtractor.php.

00124                                                         {
00125         if ($check === "" || $check === $string) {
00126             return true;
00127         } else {
00128             return (strpos(strrev($string), strrev($check)) === 0) ? true : false;
00129         }
00130     } 

static SQLStatementExtractor::extract ( buffer  )  [static]

Extract statements from string.

Parameters:
string $txt
Returns:
array

Definition at line 53 of file SQLStatementExtractor.php.

References extractStatements().

00053                                             {
00054         return self::extractStatements(self::getLines($buffer));
00055     }

static SQLStatementExtractor::extractFile ( filename  )  [static]

Get SQL statements from file.

Parameters:
string $filename Path to file to read.
Returns:
array SQL statements

Definition at line 39 of file SQLStatementExtractor.php.

References extractStatements().

00039                                                   {
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     }

static SQLStatementExtractor::extractStatements ( lines  )  [static, protected]

Extract SQL statements from array of lines.

Parameters:
array $lines Lines of the read-in file.
Returns:
string

Definition at line 63 of file SQLStatementExtractor.php.

References substring().

Referenced by extract(), and extractFile().

00063                                                         {
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)-1 - strlen(self::$delimiter));
00094                     $sql = "";
00095                 }
00096             }
00097         return $statements;           
00098     }

static SQLStatementExtractor::getLines ( buffer  )  [static, protected]

Convert string buffer into array of lines.

Parameters:
string $filename
Returns:
array string[] lines of file.

Definition at line 159 of file SQLStatementExtractor.php.

00159                                                 {       
00160        $lines = preg_split("/\r?\n|\r/", $buffer);
00161        return $lines;
00162     }

static SQLStatementExtractor::startsWith ( check,
string 
) [static, protected]

Tests if a string starts with a given string.

Parameters:
string $check The substring to check.
string $string The string to check in (haystack).
Returns:
boolean True if $string starts with $check, or they are equal, or $check is empty.

Definition at line 110 of file SQLStatementExtractor.php.

00110                                                           {
00111         if ($check === "" || $check === $string) {
00112             return true;
00113         } else {
00114             return (strpos($string, $check) === 0) ? true : false;
00115         }
00116     }

static SQLStatementExtractor::substring ( string,
startpos,
endpos = -1 
) [static, protected]

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

Definition at line 136 of file SQLStatementExtractor.php.

Referenced by extractStatements().

00136                                                                           {
00137         $len    = strlen($string);
00138         $endpos = (int) (($endpos === -1) ? $len-1 : $endpos);
00139         if ($startpos > $len-1 || $startpos < 0) {
00140             trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
00141         }
00142         if ($endpos > $len-1 || $endpos < $startpos) {
00143             trigger_error("substring(), Endindex out of bounds must be $startpos<n<".($len-1), E_USER_ERROR);
00144         }
00145         if ($startpos === $endpos) {
00146             return (string) $string{$startpos};
00147         } else {
00148             $len = $endpos-$startpos;
00149         }
00150         return substr($string, $startpos, $len+1);
00151     }


Member Data Documentation

SQLStatementExtractor::$delimiter = ';' [static, protected]

Definition at line 31 of file SQLStatementExtractor.php.


The documentation for this class was generated from the following file:

Generated on Wed May 6 23:10:50 2009 for fareofficelib by  doxygen 1.5.8