I'm trying to write an sql parsing tool for specific purposes. I'm going to run all the sql statements through mysql but any create statement needs to update the table if there are new fields. So it doubles as an alter statement.
The problem I'm having is a simple one but I'm wonder how this could be possible in PHP. Each statement I parse out will be called a Command and extend the SQLCommand super class.
I'm going to determine what command it's using by looping through each word of a command, adding it to a string where I try to match it to an associative array.
$this->sql = str;
class SQLCreate extends SQLCommand
CREATE TABLE IF NOT EXISTS `builders` (
`bld_id` int(11) NOT NULL AUTO_INCREMENT,
`bld_name` varchar(30) NOT NULL,
`bld_brandLogo_med` mediumint(9) NOT NULL,
`bld_brandLogo_sm` mediumint(9) NOT NULL,
`bld_reportingName` varchar(75) NOT NULL,
`bld_defaultLeadsEmail` varchar(100) NOT NULL,
`bld_copyLeadsEmail` varchar(100) NOT NULL,
`bld_builderWebsite` varchar(255) NOT NULL,
`bld_LeadsPerMessage` enum('All','1') NOT NULL,
PRIMARY KEY (`bld_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
This would look for "CREATE" then "CREATE TABLE" (where it will make a match). I could create a long if-else or switch to choose what Command to pass back but here is what I'd like to do-
$commandRegex = "|([^;]+)|";
preg_match_all($commandRegex, $this->sql, $matches);
foreach($matches as $match)
$this->commands = self::SQLCommandFactory($match);
public static function SQLCommandFactory($commandSQL)
$knownCommands = array(
"CREATE TABLE" => &SQLCreate;
Is this possible? So I can eventually do
$CommandCls &= $knownCommands[$commandStr];
$Command = new *$CommandCls($commandStr);
My pointers might be off.