Location: PHPKode > projects > Entier Studio > entierstudio/framework/datarow.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: datarow.php 81 2008-01-17 23:08:21Z yannromefort $
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefDataRow")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefDataRow", "1");
    //
    define(INSERT, 0);
    define(SELECT, 1);
    define(EXPAND, 2);
    define(DEPEND, 3);
    define(EXTEND, 4);
    define(EXPORT, 5);
    define(IMPORT, 6);
    //-------------------------------------------------------------------------
    //	Include
    @require_once (FRAMEWORK_DIR . "dataobject.php");
    //-------------------------------------------------------------------------
    //	Class
    class DataRow extends DataObject {
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * DataRow constructor.
         *
         * @access	public
         */
        function DataRow() {
        }
        //---------------------------------------------------------------------
        //	Methods
        //
        //	Data Command Interface
        //
        
        /**
         * Builds a "by default" SQL INSERT query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		insertRow
         *
         */
        function getInsertQuery($indexType = FOREIGNKEY) {
            //
            $table = $this->m_tableSet[PRIMARYKEY];
            $index = $this->m_indexSet["$indexType"];
            //
            $insertSQL = "INSERT INTO $table ( ";
            $valuesSQL = "VALUES ( ";
            //
            while (list($field, $value) = each($this->m_fieldSet)) {
                //
                if (!empty($value)) {
                    //
                    if (!is_numeric($value)) $value = "'$value'";
                    //
                    if ($field == $index) {
                        $insertSQL.= "  $field";
                        $valuesSQL.= "  $value";
                    } else {
                        $insertSQL.= " ,$field";
                        $valuesSQL.= " ,$value";
                    }
                }
            }
            //
            return ($insertSQL . ") " . $valuesSQL . ")");
        }
        /**
         * Builds a a "by default" SQL UPDATE query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		updateRow
         *
         */
        function getUpdateQuery($indexType = PRIMARYKEY) {
            //
            return ("");
        }
        /**
         * Builds a "by default" SQL DELETE query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		deleteRow
         *
         */
        function getDeleteQuery($indexType = PRIMARYKEY) {
            //
            $table = $this->m_tableSet[PRIMARYKEY];
            //
            switch ($indexType) {
                case PRIMARYKEY:
                    //
                    # index = Pkid
                    $field = $this->m_indexSet[PRIMARYKEY];
                    $index = $this->m_fieldSet["$field"];
                    if ($index != 0) return ("DELETE FROM $table WHERE $field=$index");
                    //
                    break;
                }
                //
                return ("");
        }
        /**
         * Builds a "by default" SQL SELECT query
         *
         * @access	protected
         *
         * @param	integer	Index Type
         * @return	STRING	SQL query
         *
         * @see		selectRow
         *
         */
        function getSelectQuery($indexType = PRIMARYKEY) {
            //
            $table = $this->m_tableSet[PRIMARYKEY];
            //
            switch ($indexType) {
                case PRIMARYKEY:
                    //
                    # index = Pkid
                    $field = $this->m_indexSet[PRIMARYKEY];
                    $index = $this->m_fieldSet["$field"];
                    if ($index != 0) return ("SELECT * FROM $table WHERE $field=$index");
                    //
                    break;
                }
                //
                return ("");
        }
        //
        //	Data Access Interface
        //
        
        /**
         * Performs an SQL INSERT query run
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	Index Type
         * @return	boolean	Result flag
         */
        function insertRow($dataSource, $indexType = FOREIGNKEY) {
            //
            if ($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) == 1) {
                    if (isset($this->m_indexSet[PRIMARYKEY])) {
                        $primaryKey = $this->m_indexSet[PRIMARYKEY];
                        $this->m_fieldSet[$primaryKey] = $dataSource->lastInsertedID($this->m_resultId);
                    }
                    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 updateRow($dataSource, $indexType = PRIMARYKEY) {
            //
            if ($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) <= 1) return (true);
            }
            //
            return (false);
        }
        /**
         * Performs an SQL DELETE query run
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	Index Type
         * @return	boolean	Result flag
         */
        function deleteRow($dataSource, $indexType = PRIMARYKEY) {
            //
            if ($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) == 1) return (true);
            }
            //
            return (false);
        }
        /**
         * Performs an SQL SELECT query run
         *
         * @access	public
         *
         * @param	object	Data source
         * @param	integer	Index Type
         * @return	boolean	Result flag
         */
        function selectRow($dataSource, $indexType = PRIMARYKEY) {
            //
            if ($this->m_resultId) $dataSource->freeStatement($this->m_resultId);
            $this->m_resultId = false;
            $this->m_queryErr = false;
            //
            $sqlQuery = $this->getSelectQuery($indexType);
            if ($sqlQuery != "") {
                $this->m_resultId = $dataSource->executeQuery($sqlQuery);
                if (false === $this->m_resultId) {
                    $this->m_queryErr = true;
                    return (false);
                }
                if ($dataSource->rowsSelected($this->m_resultId) == 1) {
                    $this->m_fieldSet = $dataSource->fetchDataRow($this->m_resultId);
                    return (true);
                }
            }
            //
            return (false);
        }
        //
        //	Data Render Interface
        //
        
        /**
         *
         * @param	object	Query manager reference
         * @param	object	Print manager reference
         * @param	string	Template layout name
         * @param	integer	View type selector
         * @return	boolean	Result flag
         * @access	public
         */
        function outputDataRow(&$datasource, &$template, $layout, $mode = OUTPUT) {
            //
            if (!is_object($template)) return (false);
            //
            if ($template->define($layout) == false) return (false);
            //
            $template->assign($this->m_fieldSet);
            $template->output($mode);
            //
            return (true);
        }
    };
    //	Class
    //-------------------------------------------------------------------------
    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio