Location: PHPKode > projects > Entier Studio > entierstudio/framework/mysql.datasource.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 
 * @version	   $Id: mysql.datasource.php 114 2008-03-07 22:18:46Z yannromefort $
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefMysqlDataSource")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefMysqlDataSource", "1");
    //-------------------------------------------------------------------------
    //	Include
    @require_once (FRAMEWORK_DIR . "datasource.php");
    //-------------------------------------------------------------------------
    //	Class
    class MySQLDataSource extends DataSource {
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         *
         * @param	string
         * @param	integer
         * @param	string
         * @param	string
         * @param	string
         */
        function MySQLDataSource($hostName = "", $hostPort = 0, $userName = "", $passWord = "", $dataBase = "") {
            $this->m_protocol = "mysql";
            $this->m_hostName = $hostName;
            $this->m_hostPort = $hostPort;
            $this->m_userName = $userName;
            $this->m_passWord = $passWord;
            $this->m_dataBase = $dataBase;
        }
        //---------------------------------------------------------------------
        //	Properties
        
        /**
         *
         * overrided DataSource property
         *
         * @return	object
         *
         * @see
         */
        function getDataHandler() {
            //
            return ($this);
        }
        /**
         *
         * overrided DataSource property
         *
         * @return	string
         */
        function errorString() {
            //
            if ($this->isConnectionOpen() == false) return ("mysql connection not open");
            //
            return (@mysql_error($this->m_resource));
        }
        /**
         *
         * overrided DataSource property
         *
         * @return	integer
         */
        function errorNumber() {
            //
            if ($this->isConnectionOpen() == false) return (CONNECTION_NOT_OPEN);
            //
            return (@mysql_errno($this->m_resource));
        }
        //---------------------------------------------------------------------
        //	Methods
        //
        // DataSource interface
        //
        
        /**
         * overrided DataSource method
         *
         * @param	string	Database name
         * @return	boolean
         */
        function openConnection() {
            //
            $this->m_resource = @mysql_pconnect($this->m_hostName, $this->m_userName, $this->m_passWord);
            if ($this->m_resource != false) {
                //
                if (@mysql_select_db($this->m_dataBase, $this->m_resource) == true) return (true);
            }
            //
            $this->clearConnection();
            //
            return (false);
        }
        /**
         * overrided DataSource method
         *
         * @return	boolean
         */
        function closeConnection() {
            //
            if ($this->isConnectionOpen() == false) return (false);
            //
            if (@mysql_close($this->m_resource)) {
                //
                $this->m_resource = false;
                //
                return (true);
            }
            //
            return (false);
        }
        //
        // SQLDataSource interface
        //
        
        /**
         *
         * @param	resource	Query Handler
         * @return	boolean
         */
        function freeStatement($resultId = 0) {
            //
            if ($this->isConnectionOpen() == false)
                return (false);
            //
            if (!is_resource($resultId))
                return (false);
            //
            return (@mysql_free_result($resultId));
        }
        /**
         *
         * @param	object	QueryDef
         * @param	array	LimitDef
         * @return	resource or boolean
         */
        function executeQuery($sqlQuery = "", $sqlLimit = NULL) {
            //
            if (is_array($sqlLimit)) {
                if (isset($sqlLimit[_ROWFIRST_]) && isset($sqlLimit[_ROWLIMIT_])) {
                    $sqlQuery.= (" LIMIT " . $sqlLimit[_ROWFIRST_] . ", " . $sqlLimit[_ROWLIMIT_]);
                } elseif (isset($sqlLimit[ROWNUMBER])) $sqlQuery.= (" LIMIT " . $sqlLimit[_ROWLIMIT_]);
            }
            //
            if (($this->isConnectionOpen() == false) && ($this->openConnection() == false)) return (false);
            //
            return (@mysql_query($sqlQuery, $this->m_resource));
        }
        //
        //	DataRecord interface
        //
        
        /**
         * Count affected rows
         * @param	integer	Handler
         * @return	integer
         */
        function rowsAffected($resultId = 0) {
            //
            if ($this->isConnectionOpen() == false) return (0);
            //
            return (@mysql_affected_rows($this->m_resource));
        }
        /**
         *
         * @param	integer	Handler
         */
        function lastInsertedID($resultId = 0) {
            //
            if ($this->isConnectionOpen() == false) return (0);
            //
            return (@mysql_insert_id($this->m_resource));
        }
        //
        //	DataResult interface
        //
        
        /**
         * Count selected rows
         * @param	integer	Handler
         * @return	integer
         */
        function rowsSelected($resultId = 0) {
            //
            if ($this->isConnectionOpen() == false) return (0);
            //
            if (!is_resource($resultId)) return (NULL);
            //
            return (@mysql_numrows($resultId));
        }
        /**
         *
         * @param	integer	Handler
         * @return	array
         */
        function fetchDataRow($resultId = 0) {
            //
            if ($this->isConnectionOpen() == false) return (NULL);
            //
            if (!is_resource($resultId)) return (NULL);
            //
            return (@mysql_fetch_array($resultId, MYSQL_ASSOC));
        }
        /**
         *
         * @param	integer	Handler
         * @return	array	Dataset
         */
        function fetchDataSet($resultId = 0) {
            //
            if ($this->isConnectionOpen() == false) return (NULL);
            //
            if (!is_resource($resultId)) return (NULL);
            //
            //
            @require_once (FRAMEWORK_DIR . "datalist.php");
            //
            $dataList = new DataList();
            while ($row = $this->fetchDataRow($resultId)) {
                $dataList->set_row_values($row);
            }
            //
            return ($dataList);
        }
        /**
         *
         * @param	integer	Handler
         * @param	integer
         * @return	boolean
         */
        function moveToCursor($resultId = 0, $cursorId = 0) {
            //
            if ($this->isConnectionOpen() == false) return (false);
            //
            if (!is_resource($resultId)) return (false);
            //
            return (@mysql_data_seek($resultId, $cursorId));
        }
    };
    //	Class
    //-------------------------------------------------------------------------
    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio