Location: PHPKode > projects > Entier Studio > entierstudio/framework/mysql.explorer.php
<?php
/**
 * Entier Studio
 *
 * LICENSE
 *
 *	Copyright 2006 Entier Studio team.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 *
 * @package    entier.framework
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 * @version	   $Id: mysql.explorer.php 81 2008-01-17 23:08:21Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefMySQLExplorer")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefMySQLExplorer", "1");
    //-------------------------------------------------------------------------
    //	Include
    @require_once (FRAMEWORK_DIR . "datasource.explorer.php");
    @require_once (FRAMEWORK_DIR . "mysql.datasource.php");
    //-------------------------------------------------------------------------
    //	Class
    class MySQLExplorer extends DataSourceExplorer {
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * Data source constructor
         *
         * @param	string	host
         * @param	string	port
         * @param	string	user
         * @param	string	pass
         * @param	string	base
         *
         */
        function MySQLExplorer($hostName = "", $hostPort = 0, $userName = "", $passWord = "", $dataBase = "") {
            $this->m_Source = new MySQLDataSource($hostName, $hostPort, $userName, $passWord, $dataBase);
        }
        //---------------------------------------------------------------------
        //	Methods
        //	DataSourceExplorer interface
        
        /**
         * build_field_flags
         *
         * @param	string	table
         * @param	string	field
         * @return	array
         */
        function build_field_flags($table, $field) {
            //
            $dataRow = NULL;
            $querySz = "SHOW COLUMNS FROM $table LIKE  \"$field\"";
            //
            $queryId = $this->m_Source->executeQuery($querySz);
            if ($queryId != false) {
                //
                $dataRow = $this->m_Source->fetchDataRow($queryId);
                $queryId = $this->m_Source->freeStatement($queryId);
                //
                $dataRow = @array_slice($dataRow, 1);
            }
            //
            return ($dataRow);
        }
        /**
         * Parse a file source catalog
         *
         * @param	string	Target element name ( format parent/chield )
         * @param	string	Target element type
         * @return	integer parsed set size
         */
        function parseObject($name = "", $type = "") {
            //
            if (empty($name) || empty($type)) return (0);
            //
            if ($this->openSource() == false) return (false);
            //
            $this->resetObject();
            //
            $database = $this->m_Source->Database();
            //
            switch ($type) {
                case "field":
                    //	 parse field informations
                    $path = explode("/", $name);
                    if (count($path) < 3) return (false);
                    //
                    if ($database == "") $database = $path[0];
                    $table = $path[1];
                    $field = $path[2];
                    //
                    $fields = @mysql_list_fields($database, $table, $this->m_Source->Connection());
                    for ($i = 0;$i < @mysql_num_fields($fields);$i++) {
                        //
                        if ($field == @mysql_field_name($fields, $i)) {
                            //
                            $datype = @mysql_field_type($fields, $i);
                            $length = @mysql_field_len($fields, $i);
                            $flags = $this->build_field_flags($table, $field);
                            //
                            $this->m_Element = new ModelElement($field, "field", FIELD_NODE, "$database/$table/$field", $datype, $length, @join(" ", $flags));
                            //
                            $parts = @explode(" ", $flags);
                            if (is_array($parts)) {
                                //
                                while (list($line, $flag) = @each($parts)) {
                                    //
                                    $this->m_Element->addElement("$flag", new ModelElement($flag, "property", PROPERTY_NODE));
                                }
                            }
                        }
                    }
                    //
                    break;
                case "table":
                    //	 parse table fields
                    $path = explode("/", $name);
                    if (count($path) < 2) return (false);
                    //
                    if ($database == "") $database = $path[0];
                    $table = $path[1];
                    //
                    $this->m_Element = new ModelElement($table, "table", TABLE_NODE, "$database/$table", "table");
                    //
                    $fields = @mysql_list_fields($database, $table, $this->m_Source->Connection());
                    for ($i = 0;$i < @mysql_num_fields($fields);$i++) {
                        //
                        $field = @mysql_field_name($fields, $i);
                        $datype = @mysql_field_type($fields, $i);
                        $length = @mysql_field_len($fields, $i);
                        $flags = $this->build_field_flags($table, $field);
                        //
                        $this->m_Element->addElement($field, new ModelElement($field, "field", FIELD_NODE, "$database/$table/$field", $datype, $length, @join(" ", $flags)));
                    }
                    //
                    break;
                case "database":
                    //	 parse database tables
                    if ($database == "") $database = $name;
                    //
                    $this->m_Element = new ModelElement($database, "database", DATABASE_NODE, $database, "database");
                    //
                    $tables = @mysql_list_tables($database, $this->m_Source->Connection());
                    for ($i = 0;$i < @mysql_num_rows($tables);$i++) {
                        //
                        $table = @mysql_tablename($tables, $i);
                        //
                        $this->m_Element->addElement($table, new ModelElement($table, "table", TABLE_NODE, "$database/$table", "table"));
                    }
                    //
                    break;
                case "server":
                    //	 parse server databases
                    $databases = @mysql_list_dbs($this->m_Source->Connection());
                    //
                    $this->m_Element = new ModelElement("MySQL", "server", SERVER_NODE, $this->m_Source->URLocator() , "server");
                    //
                    for ($i = 0;$i < @mysql_num_rows($databases);$i++) {
                        //
                        $database = @mysql_db_name($databases, $i);
                        //
                        $this->m_Element->addElement($database, new ModelElement($database, "database", DATABASE_NODE, $database, "database"));
                    }
                    //
                    break;
                default:
                    //
                    return (0);
                    //
                    break;
                }
                //
                $this->m_Element->resetElements();
                //
                return ($this->sizeOfElements());
            }
        };
        //	Class
        //-------------------------------------------------------------------------
        
    }
    //	namespace
    //-----------------------------------------------------------------------------
    
?>
Return current item: Entier Studio