#!/usr/bin/php
If the input file is -, stdin is used with the current path.
Output is always sent to stdout and can be piped to the mysql command-line client.
An include command starts with "-- @include(" and ends with a ")".
Anything else on that line is treated as a comment.
HLPTXT
);
class clsSqlScriptProcessor
{private $mstrDirectory;
public function __construct($strDirectory)
{if(! in_array(substr($strDirectory, -1), array('/', '\\'))):
$strDirectory .= '/'; // Make sure there is a trailing slash to the directory.
endif;
$this->mstrDirectory = $strDirectory;}
private function EolPosition($mixCrPosition, $mixLfPosition)
{$mixResult = $mixLfPosition;
if((! is_integer($mixLfPosition)) and is_integer($mixCrPosition)):
$mixResult = $mixCrPosition;
elseif(is_integer($mixLfPosition) and is_integer($mixCrPosition) and ($mixCrPosition<$mixLfPosition)):
$mixResult = $mixCrPosition;
endif;
return $mixResult; }
private function ProcessLine($strLine)
{if(substr($strLine, 0, 12)=='-- @include('):
$strLine = substr($strLine, 12);
$intCloseBracketPos = strpos($strLine, ')');
if($intCloseBracketPos>0):
$strReferencedFile = realpath($this->mstrDirectory . substr($strLine, 0, $intCloseBracketPos));
echo '-- Including ' . $strReferencedFile . ':' . PHP_EOL;
$objProcessor = new clsSqlScriptProcessor(dirname($strReferencedFile));
$objProcessor->ProcessScript(file_get_contents($strReferencedFile));
echo PHP_EOL;
endif;
else:
echo $strLine;
endif; }
public function ProcessScript($strScript)
{$intPreviousPosition = 0;
$blnEndReached = FALSE;
while(! $blnEndReached):
$intCursorPosition = $this->EolPosition(strpos($strScript, "\r", $intPreviousPosition), strpos($strScript, "\n", $intPreviousPosition));
if(is_bool($intCursorPosition)):
$blnEndReached = TRUE;
$intCursorPosition = strlen($strScript) - 1;
else:
while(in_array(substr($strScript, $intCursorPosition + 1, 1), array("\n", "\r"))):
$intCursorPosition++;
endwhile;
endif;
$this->ProcessLine(substr($strScript, $intPreviousPosition, $intCursorPosition - $intPreviousPosition + 1));
$intPreviousPosition = $intCursorPosition + 1;
endwhile; }
} // class clsSqlScriptProcessor
if(count($_SERVER['argv'])<2):
$strFile = '-help'; // No arguments? display help
else:
$strFile = $_SERVER['argv'][1];
if($strFile=='-'):
$strDirectory = getcwd();
$strContents = file_get_contents('php://stdin');
$strFile = '';
elseif($strFile!='-help'):
$strDirectory = dirname($strFile);
$strContents = file_get_contents($strFile);
endif;
endif;
if($strFile=='-help'):
echo str_replace('', basename(__FILE__), HELP_TEXT);
else:
$objProcessor = new clsSqlScriptProcessor($strDirectory);
$objProcessor->ProcessScript($strContents);
endif;
?>