#native_company# #native_desc#
#native_cta#

deeMySQLParser

By Dan Cochran -Toronto
on December 16, 2005

Version: 1.0

Type: Function

Category: Databases

License: GNU General Public License

Description: a mysqldump parser..returns table structure of a dump in a nice associative array.

<?

/******************************************************************************************************************
*Date: December 13, 2005
*Author: Dan Cochran
*
*Description: This will parse a mysql schema. All table definitions found will be returned . 
*             What you get... Primary KEY(s), table_name, field type, field name and default value.   
*              
*email me with question or comments/bugs @ [email protected]
*see it in action: http://deecodameeko.com/code/dbClassBuilder/
*Disclaimer: 
*Copyright (C) 2005  Dan Cochran
*
*This program is free software; you can redistribute it and/or
*modify it under the terms of the GNU General Public License
*as published by the Free Software Foundation; either version 2
*of the License, or (at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program; if not, write to the Free Software
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
******************************************************************************************************************/

function parse($filename){

$content = '';
$tables = array();

if($fp= fopen($filename,"r")){
  
  While(!feof($fp)){
  	
  	$content = fgets($fp, 80);
  	$content = trim($content);
  	if(!ereg("#", $content) && !empty($content)){
  		
  		if(!ereg("TYPE=", $content)){
  			//the line with the "{" is first line of create statement
  			
  			if(ereg("CREATE TABLE", $content)){
  				//strip out the table name
  				$content = ereg_replace("CREATE TABLE", "", $content);
  				$content = ereg_replace("(", "", $content);
  				
  				//echo $content."   1<br>";
  				$content = trim($content);
  				
  				if(!empty($content)){
  				  $tables[$count]['name']= $content;
  				}
  				
  			}
  			elseif(ereg("PRIMARY KEY",$content)){
  			
  				$tmp = trim(ereg_replace('PRIMARY KEY','',$content));  
  		        $tmp = ereg_replace('(','',$tmp);
		        $tmp = ereg_replace(')','',$tmp);
		      
		        $tables[$count]['pk'][] = trim($tmp);
		        $count++;
  			}
  			else{//fields
  			
   				$field_definition = preg_split ("/[s,]+/", $content);
   				$field_name = $field_definition[0];
   				$type = $field_definition[1];
   				
				if(count($field_definition) > 5){ 
   				  $default = $field_definition[5];   					
   				}
   				else{
   				  $default = $field_definition[3];	
   				}
   				
				if($field_name != 'KEY'){   			   				
   			   	  $default = ereg_replace("'","", $default);			
   			   				
  				  $tables[$count]['field_name'][] = $field_name;
  				  $tables[$count]['default'][] = $default;
  				  $tables[$count]['type'][] = $type;
  				  
				}else{
				  	$tables[$count]['pk'][] = $field_name;
				}
  			}
  		}
  		else{
  			continue;
  		}
  		
  	}
  	
  	
  }	
}
else {
	echo "oops";
}

fclose($fp);

return($tables);

}//end function parse


/***************************************
Sample use:

argument: $file
desc: $file is the name of the variable containing the contents of the mysql schema

include("deeMySQLParser.php");

$tables = parse($file);

$content = '';

for($i=0; $i<count($tables); $i++){
		
    $content .= "Table Name: ."$tables[$i]['name']." Field Name: ".$tables[$i]['field_name']."<br><br>";

}

echo $content;	
***************************************/
?>