Location: PHPKode > projects > Entier Studio > entierstudio/framework/mssql.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: mssql.explorer.php 81 2008-01-17 23:08:21Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefMsSQLExplorer")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefMsSQLExplorer", "1");
    //-------------------------------------------------------------------------
    //	Include
    @require_once (FRAMEWORK_DIR . "datasource.explorer.php");
    @require_once (FRAMEWORK_DIR . "mssql.datasource.php");
    //-------------------------------------------------------------------------
    //	Class
    class MsSQLExplorer extends DataSourceExplorer {
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * Data source constructor
         *
         * @param	string	host
         * @param	string	port
         * @param	string	user
         * @param	string	pass
         * @param	string	base
         *
         */
        function MsSQLExplorer($hostName = "", $hostPort = 0, $userName = "", $passWord = "", $dataBase = "") {
            $this->m_Source = new MsSQLDataSource($hostName, $hostPort, $userName, $passWord, $dataBase);
        }
        //---------------------------------------------------------------------
        //	Methods
        //	DataSourceExplorer interface
        
        /**
         * Parse a data 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->m_Source->isConnectionOpen() == false) && ($this->m_Source->openConnection() == 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 = @mssql_query("select c.name,
													t.name,
													c.length 
											 from 
													syscolumns c 
											 join 
													systypes t 
											 on 
													t.xusertype=c.xusertype 
											 join 
													sysobjects o 
											 on 
													o.id=c.id 
											 where 
													o.name='$table'", $this->m_Source->Connection());
                    //
                    for ($i = 0;$i < @mssql_num_rows($fields);$i++) {
                        //
                        if ($field == @mssql_result($fields, $i, 0)) {
                            //
                            $field = @mssql_result($fields, $i, 0);
                            $datype = @mssql_result($fields, $i, 1);
                            $length = @mssql_result($fields, $i, 2);
                            $flags = "";
                            //
                            $this->m_Element = new ModelElement($field, "field", FIELD_NODE, "$database/$table/$field", $datype, $length, $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 "query":
                    //	 parse procedure parameters
                    //
                    $path = explode("/", $name);
                    if (count($path) < 2) return (false);
                    //
                    if ($database == "") $database = $path[0];
                    $query = $path[1];
                    $this->m_Element = new ModelElement($query, "query", QUERY_NODE, "$database/$query", "query");
                    //
                    $statement = @mssql_init("sp_sproc_columns", $this->m_Source->Connection());
                    @mssql_bind($statement, "@procedure_name", $query, SQLVARCHAR);
                    $fields = @mssql_execute($statement);
                    for ($i = 0;$i < @mssql_num_rows($fields);$i++) {
                        //
                        $field = @mssql_result($fields, $i, 3);
                        $way = @mssql_result($fields, $i, 4);
                        $datype = @mssql_result($fields, $i, 6);
                        $length = @mssql_result($fields, $i, 8);
                        //
                        $this->m_Element->addElement("$field", new ModelElement($field, "parameter", PARAMETER_NODE, "$database/$query/$field", $datype, $length));
                    }
                    //
                    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 = @mssql_query("select c.name,
													t.name,
													c.length 
											 from 
													syscolumns c 
											 join 
													systypes t 
											 on 
													t.xusertype=c.xusertype 
											 join 
													sysobjects o 
											 on 
													o.id=c.id 
											 where 
													o.name='$table'", $this->m_Source->Connection());
                    for ($i = 0;$i < @mssql_num_rows($fields);$i++) {
                        //
                        $field = @mssql_result($fields, $i, 0);
                        $datype = @mssql_result($fields, $i, 1);
                        $length = @mssql_result($fields, $i, 2);
                        $flags = "";
                        //
                        $this->m_Element->addElement($field, new ModelElement($field, "field", FIELD_NODE, "$database/$table/$field", $datype, $length, $flags));
                    }
                    //
                    break;
                case "database":
                    //	 parse database tables
                    if ($database == "") $database = $name;
                    //
                    $this->m_Element = new ModelElement($database, // nodeName
                    "database", // typeName
                    DATABASE_NODE, // nodeType
                    $database, // dataPath
                    "database"
                    // dataType
                    );
                    //
                    $tables = @mssql_query("select 
												name, 
												type 
											from 
												sysobjects 
											where 
												( type = 'U' OR type = 'P' ) 
											ORDER BY 
												type DESC, name", $this->m_Source->Connection());
                    for ($i = 0;$i < @mssql_num_rows($tables);$i++) {
                        //
                        $table = @mssql_result($tables, $i, 0);
                        $xtype = @mssql_result($tables, $i, 1);
                        //
                        switch (@trim($xtype)) {
                            case 'U':
                                //
                                $this->m_Element->addElement($table, new ModelElement($table, "table", TABLE_NODE, "$database/$table", "table"));
                                //
                                
                            break;
                            case "P":
                                //
                                $this->m_Element->addElement($table, new ModelElement($table, "query", QUERY_NODE, "$database/$table", "query"));
                                //
                                
                            break;
                        }
                    }
                    //
                    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 < @mssql_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