A mysqldump parser which returns table structure in an associative array. The array contains the table name, field name(s), primary key(s), default value and data type.
By : deecodameeko
<?
/****************************************************************************************************************** *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 @ dan@deecodameeko.com *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; ***************************************/ ?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles More By Codewalkers developerWorks - FREE Tools! | Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
| | | | Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
| | | | Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
| | | | Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
| | | | Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
| | | | This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
| | | | As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
| | | | IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
| | | | With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |