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! | The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times. FREE! Go There Now!
| | | | You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
| | | | Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
| | | | Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
| | | | In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
| | | | Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. 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!
| | | | Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
| | | | Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |