Database Code

  Home arrow Database Code arrow deeMySQLParser
DATABASE CODE

deeMySQLParser
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-12-14

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    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

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap