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! | You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
| | | | Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
| | | | 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 DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
| | | | XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
| | | | This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
| | | | Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |