MrFish Posted January 29, 2013 Share Posted January 29, 2013 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. class SQLCommand { private $sql; private $executables; //String private $command; private $table; //String[] private $flags; private $parameterSets; function SQLCommand($str) { $this->sql = str; } function getExecutables() { return $this->executables; } } 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- function parseCommands() { $commandRegex = "|([^;]+)|"; preg_match_all($commandRegex, $this->sql, $matches); foreach($matches[1] as $match) { $this->commands[] = self::SQLCommandFactory($match); } } .... public static function SQLCommandFactory($commandSQL) { $knownCommands = array( "CREATE TABLE" => &SQLCreate; ); print_r($knownCommands); return NULL; } Is this possible? So I can eventually do $CommandCls &= $knownCommands[$commandStr];$Command = new *$CommandCls($commandStr); My pointers might be off. Haven't had much practice with them. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 29, 2013 Share Posted January 29, 2013 The syntax isn't right, but the idea is fine. You can create objects by string name, for example. Link to comment Share on other sites More sharing options...
MrFish Posted January 29, 2013 Author Share Posted January 29, 2013 I'm doing some tests now. How would I do this if possible? Trying to avoid eval. <?php class A { function __toString(){return "A";} } $Class = A; $Instance = new $Class($parameters = NULL); echo "No Error";?> Link to comment Share on other sites More sharing options...
justsomeguy Posted January 29, 2013 Share Posted January 29, 2013 I didn't test this, but I believe the class name should be a string: $Class = 'A'; Link to comment Share on other sites More sharing options...
MrFish Posted January 29, 2013 Author Share Posted January 29, 2013 Yes that worked. I wouldn't have guessed with $Class being a string and all. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now