Location: PHPKode > projects > Entier Studio > entierstudio/framework/dataset.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: dataset.php 81 2008-01-17 23:08:21Z yannromefort $
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefDataSet")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefDataSet", "1");
    //
    //	Limit definition enumeration //
    define("ROWOFFSET", 0);
    define("ROWNUMBER", 1);
    define("ALLNUMBER", 2);
    //	Limit indexation enumeration //
    define("_SETCOUNT_", 0);
    define("_ROWFIRST_", 1);
    define("_ROWLIMIT_", 2);
    define("_ROWCOUNT_", 3);
    define("_PREVPAGE_", 4);
    define("_NEXTPAGE_", 5);
    define("_THISPAGE_", 6);
    define("_NUMPAGES_", 7);
    //	Page navigation type enumeration
    define("PAGENONE", 0);
    define("PAGENEXT", 1);
    define("PAGEBOTH", 3);
    define("PAGEPREV", 4);
    //
    define("NEXTBLOCK", "next");
    define("PREVBLOCK", "prev");
    //-------------------------------------------------------------------------
    //	include
    @require_once (FRAMEWORK_DIR . "dataobject.php");
    //-------------------------------------------------------------------------
    //	Class
    class DataSet extends DataObject {
        //---------------------------------------------------------------------
        //	Attributes
        
        /**
         * Number of rows in result set
         * @var	integer
         * @see
         */
        var $m_rowCount = 0;
        /**
         * Cursor cursor in result set
         * @var	integer
         * @see
         */
        var $m_cursorId = 0;
        /**
         * Page navigation
         * @var	integer
         * @see
         */
        var $m_limitDef = 0;
        /**
         * Page navigation
         * @var	array
         * @see
         */
        var $m_limitSet = array();
        //---------------------------------------------------------------------
        //	Constructor
        /*
        */
        function DataSet() {
        }
        //---------------------------------------------------------------------
        //	Properties
        //
        //	_Cursor property
        //
        /**
         *
         * @param	object	data source
         * @param	integer cursor
         * @return	boolean
         */
        function set_cursor_value($datasource, $value = 0) {
            //
            if (($value >= 0) && ($value < $this->m_rowCount)) {
                if ($datasource->moveToCursor($this->m_resultId, $value) == true) {
                    $this->m_cursorId = $value;
                    return (true);
                }
            }
            //
            $this->m_queryErr = true;
            return (false);
        }
        //
        //	rowCount property
        //
        /**
         *
         * @return	integer
         */
        function rowCount() {
            return ($this->m_rowCount);
        }
        //
        //	_Limit property
        //
        /**
         *
         * @param	integer cursor
         * @return	boolean
         */
        function set_limit_value($limit) {
            //
            if (empty($limit)) return (false);
            //
            if (is_array($limit)) {
                if (!isset($limit[ROWOFFSET]) || !isset($limit[ROWNUMBER])) return (false);
                //
                $this->m_limitSet[_ROWFIRST_] = $limit[ROWOFFSET];
                $this->m_limitSet[_ROWLIMIT_] = $limit[ROWNUMBER];
                $this->m_limitSet[_ROWCOUNT_] = $limit[ALLNUMBER];
            } else {
                //
                $this->m_limitSet[_ROWFIRST_] = 0;
                $this->m_limitSet[_ROWLIMIT_] = $limit;
                $this->m_limitSet[_ROWCOUNT_] = 0;
            }
            //
            return (true);
        }
        /**
         *
         * @return	integer
         */
        function get_limit_value() {
            //
            $this->m_limitSet[_SETCOUNT_] = $this->m_rowCount;
            //
            if (!isset($this->m_limitSet[_ROWFIRST_]) || !isset($this->m_limitSet[_ROWLIMIT_])) return (PAGENONE);
            //
            $setCount = $this->m_limitSet[_SETCOUNT_];
            $rowFirst = $this->m_limitSet[_ROWFIRST_];
            $rowLimit = $this->m_limitSet[_ROWLIMIT_];
            if (empty($this->m_limitSet[_ROWCOUNT_])) $rowCount = $setCount;
            else $rowCount = $this->m_limitSet[_ROWCOUNT_];
            $thispage = ($rowCount/$rowLimit);
            $numpages = ($rowCount/$rowLimit);
            //
            if ($rowFirst == 0) {
                //
                if ($rowCount < $rowLimit) return (PAGENONE);
                else {
                    $this->m_limitSet[_PREVPAGE_] = 0;
                    $this->m_limitSet[_NEXTPAGE_] = $rowFirst+$rowLimit;
                    $this->m_limitSet[_THISPAGE_] = $thispage;
                    $this->m_limitSet[_NUMPAGES_] = $numpages;
                    //
                    return (PAGENEXT);
                }
            }
            //
            if ($rowCount > ($rowLimit+$rowFirst)) {
                //
                $this->m_limitSet[_PREVPAGE_] = $rowFirst-$rowLimit;
                $this->m_limitSet[_NEXTPAGE_] = $rowFirst+$rowLimit;
                $this->m_limitSet[_THISPAGE_] = $thispage;
                $this->m_limitSet[_NUMPAGES_] = $numpages;
                //
                return (PAGEBOTH);
            } else {
                // prev page
                $this->m_limitSet[_PREVPAGE_] = $rowFirst-$rowLimit;
                $this->m_limitSet[_NEXTPAGE_] = 0;
                $this->m_limitSet[_THISPAGE_] = $thispage;
                $this->m_limitSet[_NUMPAGES_] = $numpages;
                //
                return (PAGEPREV);
            }
        }
        //
        //---------------------------------------------------------------------
        //	Methods
        //
        //	Data Command Interface
        //
        
        /**
         * Builds an SQL INSERT query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		insertSet
         *
         */
        function getInsertQuery($indexType = FOREIGNKEY) {
            //
            return ("");
        }
        /**
         * Builds an SQL UPDATE query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		updateSet
         *
         */
        function getUpdateQuery($indexType = FOREIGNKEY) {
            //
            return ("");
        }
        /**
         * Builds an SQL DELETE query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		deleteSet
         *
         */
        function getDeleteQuery($indexType = FOREIGNKEY) {
            //
            $table = $this->m_tableSet[PRIMARYKEY];
            //
            switch ($indexType) {
                case FOREIGNKEY:
                    //
                    # $index = FOREIGNKEY
                    $field = $this->m_indexSet[FOREIGNKEY];
                    $index = $this->m_querySet[$field];
                    if ($index != 0) return ("DELETE FROM $table WHERE $field=$index");
                    //
                    break;
                case REFLECTKEY:
                    //
                    # $index = REFLECTKEY
                    $field = $this->m_indexSet[REFLECTKEY];
                    $index = $this->m_querySet[$field];
                    if ($index != 0) return ("DELETE FROM $table WHERE $field=$index");
                    //
                    break;
                }
                //
                return ("");
            }
        /**
         * Builds an SQL SELECT query
         *
         * @access	protected
         *
         * @param	integer	Index type
         * @param	integer	Order Type
         * @param	string	Order Mode "" Or "DESC"
         * @return	STRING	SQL query
         *
         * @see		selectSet
         *
         */
        function getSelectQuery($indexType = FOREIGNKEY, $orderType = NATURALKEY, $orderMode = "") {
            //
            $table = $this->m_tableSet[PRIMARYKEY];
            //
            switch ($indexType) {
                case FOREIGNKEY:
                    //
                    # $index = FOREIGNKEY
                    $field = $this->m_indexSet["$indexType"];
                    $index = $this->m_querySet[$field];
                    if ($index != 0) {
                        //
                        $selectSQL = "SELECT * FROM $table WHERE $field=$index";
                        //
                        $field = $this->m_indexSet[$orderType];
                        if (!empty($field)) $selectSQL.= " ORDER BY $field $orderMode";
                        //
                        return ($selectSQL);
                    }
                    //
                break;
            }
            //
            return ("");
        }
        //
        //	Data Access Interface
        //
            
        /**
         * Performs an SQL INSERT query run
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	Index Type
         * @return	boolean
         */
        function insertSet($datasource, $indexType = FOREIGNKEY) {
            //
            if (false !== $this->m_resultId)
                $datasource->freeStatement($this->m_resultId);
            $this->m_resultId = false;
            $this->m_queryErr = false;
            //
            $sqlQuery = $this->getInsertQuery($indexType);
            if ("" != $sqlQuery) {
                $this->m_resultId = $datasource->executeQuery($sqlQuery);
                if (false === $this->m_resultId) {
                    $this->m_queryErr = true;
                    return (false);
                }
                if ($datasource->rowsAffected($this->m_resultId) > 0) 
                    return (true);
            }
            //
            return (false);
        }
        /**
         * Performs an SQL UPDATE query run
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	Index Type
         * @return	boolean	Result flag
         */
        function updateSet($datasource, $indexType = FOREIGNKEY) {
            //
            if (false !== $this->m_resultId)
                $datasource->freeStatement($this->m_resultId);
            $this->m_resultId = false;
            $this->m_queryErr = false;
            //
            $sqlQuery = $this->getUpdateQuery($indexType);
            if ("" != $sqlQuery) {
                $this->m_resultId = $datasource->executeQuery($sqlQuery);
                if (false === $this->m_resultId) {
                    $this->m_queryErr = true;
                    return (false);
                }
                if ($datasource->rowsAffected($this->m_resultId) > 0)
                    return (true);
            }
            //
            return (false);
        }
        /**
         * Performs an SQL DELETE query run
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	Index Type
         * @return	boolean
         */
        function deleteSet($datasource, $indexType = FOREIGNKEY) {
            //
            if (false !== $this->m_resultId)
                $datasource->freeStatement($this->m_resultId);
            $this->m_resultId = false;
            $this->m_queryErr = false;
            //
            $sqlQuery = $this->getDeleteQuery($indexType);
            if ("" != $sqlQuery) {
                $this->m_resultId = $datasource->executeQuery($sqlQuery);
                if (false === $this->m_resultId) {
                    $this->m_queryErr = true;
                    return (false);
                }
                if ($datasource->rowsAffected($this->m_resultId) >= 0) 
                    return (true);
            }
            //
            return (false);
        }
        /**
         * Performs an SQL SELECT query run
         *
         * @access	public
         * 
         * @param	object	Data source
         * @param	integer	Index Type
         * @param	integer	Order Type
         * @param	string	Order Mode
         * @param	integer	Limit
         * @return	boolean
         */
        function selectSet($datasource, $indexType = FOREIGNKEY, $orderType = NATURALKEY, $orderMode = "", $limit = 0) {
            //
            if (false !== $this->m_resultId)
                $datasource->freeStatement($this->m_resultId);
            $this->m_resultId = false;
            $this->m_queryErr = false;
            $this->m_rowCount = 0;
            $this->m_cursorId = 0;
            //
            $this->set_limit_value($limit);
            //
            $sqlQuery = $this->getSelectQuery($indexType, $orderType, $orderMode, $limit);
            if ("" != $sqlQuery) {
                $this->m_resultId = $datasource->executeQuery($sqlQuery, $this->m_limitSet);
                if (false === $this->m_resultId) {
                    $this->m_queryErr = true;
                    return (false);
                }
                $this->m_rowCount = $datasource->rowsSelected($this->m_resultId);
                if ($this->m_rowCount > 0) {
                    //
                    $this->m_limitDef = $this->get_limit_value();
                    //
                    return (true);
                }
            }
            //
            return (false);
        }
        //
        //	Data Render Interface
        //
            
        /**
         * select a layout template
         * @param	object	Query manager
         * @param	object	Print manager
         * @param	string	Layout name
         * @param	string	Block name
         * @param	array	Sub Query Parameters
         * @param	boolean	Output mode
         * @return	boolean
         */
        function outputRecordSet(&$datasource, &$template, $layout, $block, $query = NULL, $mode = OUTPUT) {
            //
            if (($this->m_rowCount == 0) || ($this->m_resultId == 0))
                return (false);
            //
            if (!is_object($template)) return (false);
            //
            if ($template->define($layout) == false) return (false);
            //
            $template->assign($this->m_querySet);
            //
            # List items
            for ($i = 0;$i < $this->m_rowCount;$i++) {
                //
                if ($this->fetchCursorRow($datasource) == false) return (false);
                //
                $template->select($block);
                $template->assign($this->m_fieldSet);
                $template->render($block);
            }
            //
            $template->assign($this->m_fieldSet);
            $template->assign($this->m_limitSet);
            //	Page navigation
            if (($this->m_limitDef == PAGEBOTH) || ($this->m_limitDef == PAGENEXT)) {
                $template->select(NEXTBLOCK);
                $template->render(NEXTBLOCK);
            }
            if (($this->m_limitDef == PAGEBOTH) || ($this->m_limitDef == PAGEPREV)) {
                $template->select(PREVBLOCK);
                $template->render(PREVBLOCK);
            }
            //
            $template->output($mode);
            //
            return (true);
        }
        //
        //	Data Getter Interface
        //
            
        /**
         * Fetch The cursor row
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	cursor index
         * @return	boolean
         */
        function fetchCursorRow($datasource, $cursor = -1) {
            //
            if (($cursor >= 0) && ($cursor < $this->m_rowCount)) {
                $this->m_cursorId = $cursor;
                if ($datasource->moveToCursor($this->m_resultId, $this->m_cursorId) == false) {
                    $this->m_cursorId = $this->m_rowCount;
                    $this->m_queryErr = true;
                    return (false);
                }
            }
            //
            if ($this->m_fieldSet = $datasource->fetchDataRow($this->m_resultId)) {
                $this->m_cursorId++;
                return (true);
            }
            //
            return (false);
        }
        /**
         * Fetch The result set
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	cursor index
         * @return	boolean
         */
        function fetchResultSet($datasource, $cursor = -1) {
            //
            if (($cursor >= 0) && ($cursor < $this->m_rowCount)) {
                $this->m_cursorId = $cursor;
                if ($datasource->moveToCursor($this->m_resultId, $this->m_cursorId) == false) {
                    $this->m_cursorId = $this->m_rowCount;
                    $this->m_queryErr = true;
                    return (false);
                }
            }
            //
            if ($this->m_fieldSet = $datasource->fetchDataSet($this->m_resultId)) {
                $this->m_cursorId++;
                return (true);
            }
            //
            return (false);
        }
        };
        //	Class
        //-------------------------------------------------------------------------
        
    }
    //	namespace
    //-----------------------------------------------------------------------------
    
?>
Return current item: Entier Studio