Class: dataQuery

Attributes:
Inherits:
Implements:

Contents:

Introduction

Represents an OXRM representation of a Query, SQL SELECT statement for an entity

Meta Data

authorBashkim Isai
packageOXRM

Constants

Constant NameConstant Value
RETURN_TYPE_FIELDfield
RETURN_TYPE_ENTITYentity

Properties

Methods

Also See

Class Syntax

In file D:\oblib-core\php\oblib\dataOXRM\dataQuery.class.php between lines 51 and 503.

<?php
 
    
/**
     * Represents an OXRM representation of a Query,
     * SQL SELECT statement for an entity
     *
     * @author    Bashkim Isai
     * @package    OXRM
     * @see        Object [data]
     * @see        Object [dataSampleQuery]
     */

    
class dataQuery implements dataSampleQuery
    
{
        
        
/**
         * The OXRM document which holds the information for this Query
         * 
         * @type    Object [dataOXRM]
         */
        
        
public $_doxOXRM;
        
        
/**
         * XPath Object which allows us to Traverse the OXRM document
         * 
         * @type    Object [DOMXPath]
         */
        
        
public $_dxpOXRM;
        
        
/**
         * The DOMNode which speficies the constraints for this Query
         * 
         * @type    Object [DOMNode]
         */
        
        
public $_dnoQuery;
        
        
/**
         * The Name of the Query
         * 
         * @type    String
         */
        
        
private $_strName;
        
        
/**
         * The List of Entities that are associated with this Query
         * 
         * @type    Object [DOMNodeList]
         */
        
        
private $_dnlEntity;
        
        
/**
         * The conditions in the WHERE clause for this Query
         * 
         * @type    Object [DOMNode]
         */
        
        
private $_dnoWhere;
        
        
/**
         * The GROUP BY conditions for this Statement
         * 
         * @type    Object [DOMNodeList]
         */
        
        
private $_dnlGroupBy;
        
        
/**
         * Represents how the information should be Returned
         * 
         * @type    Object [DOMNode]
         */
        
        
private $_dnoReturn;
        
        
/**
         * An array which represents the associated entities for this Query.
         * The key of this array represents the 'AS' field (of the Table Alias)
         * and the value of the array is the associated dataEntities object
         * 
         * @type    Array [String => Object [dataEntities]]
         */
        
        
private $_arrEntity    = Array ();
        
        
/**
         * The params that were passed through for the generation of this Query
         * The key represents the Param's attribute name
         * 
         * @type    Array [String => Mix]
         */
        
        
private $_arrParam    = Array ();
        
        
/**
         * The WHERE clause builder
         * 
         * @type    Object [dataQueryBranch]
         */
        
        
private $_eqbWhere;
        
        
/**
         * The length of the Sample being returned. If NULL, no LIMIT will be applied
         * 
         * @type    Integer
         */
        
        
private $_intSampleLength;
        
        
/**
         * The page number that is currently being previewed.
         * Does nothing unless the Sample Length is defined
         * 
         * @type    Integer
         */
        
        
private $_intSamplePage;
        
        
/**
         * This constant represents that the result will be return as a 'field' type result
         */
        
        
const RETURN_TYPE_FIELD        'field';
        
        
/**
         * This constant represents that the result will be return as a 'entity' type result
         */
        
        
const RETURN_TYPE_ENTITY    'entity';
        
        
/**
         * Creates all the required information to generate the Query. Puts all the
         * Node information in objects and stores it for later use. Also retrieves and
         * stores any Entities which will be needed for this statement
         * 
         * @return    Void
         */
        
        
final function __construct (dataOXRM &$doxOXRM, &$dnoQuery)
        {
            
$this->_doxOXRM            =& $doxOXRM;
            
$this->_dxpOXRM            =& $doxOXRM->_dxpOXRM;
            
$this->_dnoQuery        =& $dnoQuery;
            
            
$this->_strName            $this->_dxpOXRM->Query ('./name'$dnoQuery)->item (0)->nodeValue;
            
$this->_dnlEntity        $this->_dxpOXRM->Query ('./with-entity'$dnoQuery);
            
$this->_dnoWhere        $this->_dxpOXRM->Query ('./where'$dnoQuery)->item (0);
            
$this->_dnlGroupBy        $this->_dxpOXRM->Query ('./groupby'$dnoQuery);
            
$this->_dnoReturn        $this->_dxpOXRM->Query ('./return'$dnoQuery)->item (0);
            
            
// Setup the Where Clause
            
$this->_eqbWhere = new dataQueryBranch;
            
            
// Setup Entity Information
            
foreach ($this->_dnlEntity as $dnoEntity)
            {
                if (isset (
$this->_arrEntity [$dnoEntity->getAttribute ('as')]))
                {
                    throw new 
ExceptionCore ('The alias you are attempting to assign already exists: ' $dnoEntity->getAttribute ('as'));
                }
                
                
$objEntity $this->_doxOXRM->Entity ($dnoEntity->nodeValue);
                
$this->_arrEntity [$dnoEntity->getAttribute ('as')] = $objEntity;
            }
        }
        
        
/**
         * Sets the Parameters for this Constructed Query
         * 
         * @return    Void
         */
        
        
public function Param ($arrParam)
        {
            
$this->_arrParam $arrParam;
        }
        
        
/**
         * Define the LIMIT Clause of the generated Statement (Sample Length, Page Number)
         * 
         * @return    Void
         */
        
        
public function Sample ($intSampleLength=NULL$intSamplePage=1)
        {
            
$this->_intSampleLength    = (int) $intSampleLength;
            
$this->_intSamplePage    = (int) $intSamplePage;
        }
        
        
/**
         * Asks the dataSample object to run so that the information can be traversed
         * 
         * @return    Object [dataSample]
         */
        
        
public function ___Render ()
        {
            return new 
dataSample ($this);
        }
        
        
        
        
        
/**
         * The Following Lines relate specifically to generating SELECT queries
         */
        
        
        
        
        /**
         * The tagName of the Sample Element
         * 
         * @return    String
         * @see        Class [dataSample]
         */
        
        
public function QuerySelect_Name ()
        {
            return 
$this->_strName;
        }
        
        
/**
         * The Function that will be called to handle results. 
         * If the 'return type' of this statement is dataQuery::RETURN_TYPE_FIELD
         * then NULL will be returned - otherwise if the 'return type' of this
         * field is dataQuery::RETURN_TYPE_ENTITY, then the Result Handler will be
         * callbacked
         * 
         * @return    Callback
         * @see        Class [dataSample]
         */
        
        
public function QuerySelect_Result ()
        {
            switch (
$this->_dnoReturn->getAttribute ('type'))
            {
                case 
self::RETURN_TYPE_FIELD:
                    return 
NULL;
                    
                case 
self::RETURN_TYPE_ENTITY:
                    return Array (
$this'QuerySelect_ResultHandler');
            }
        }
        
        
/**
         * This result handler is for when the 'return type' is
         * dataQuery::RETURN_TYPE_ENTITY. It will generate the required
         * dataEntity object and return it to the dataSample handler
         * 
         * @return    Object [typeOf dataEntities::_strClassOne]
         * @see        Class [dataSample]
         */
        
        
public function QuerySelect_ResultHandler ($arrData)
        {
            
$strEntity $this->_dnoReturn->getAttribute ('entity');
            
$objEntity $this->_arrEntity [$strEntity];
            
            return new 
$objEntity->_strClassOne ($objEntity$arrData);
        }
        
        
/**
         * Generates the fields to return for the SELECT statement. If we are
         * dealing with a 'return type' of dataQuery::RETURN_TYPE_FIELD then 
         * the returned string will be generated from the OXRM Query information,
         * otherwise dataQuery::RETURN_TYPE_ENTITY will handle the fields as 
         * entities and return the default SELECT statement for that entity
         * 
         * @return    String
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_Field ()
        {
            
$strReturnType $this->_dnoReturn->getAttribute ('type');
            
            switch (
$strReturnType)
            {
                case 
self::RETURN_TYPE_FIELD:
                    
$dnlReturnField $this->_dxpOXRM->Query ('./field'$this->_dnoReturn);
                    
$arrReturnField = Array ();
                    
                    foreach (
$dnlReturnField as $dnoReturnField)
                    {
                        
$strReturnField $dnoReturnField->nodeValue;
                        
                        if (
$dnoReturnField->hasAttribute ('as'))
                        {
                            
$strReturnField .= ' as ';
                            
$strReturnField .= $dnoReturnField->getAttribute ('as');
                        }
                        
                        
$arrReturnField [] = $strReturnField;
                    }
                    
                    
$strSelectField implode (', '$arrReturnField);
                    
                    break;
                    
                case 
self::RETURN_TYPE_ENTITY:
                    
$strSelectField 
                        
$this->_arrEntity [$this->_dnoReturn->getAttribute ('entity')]->___PropertySelect (
                            
$this->_dnoReturn->getAttribute ('entity')
                        );
                    
                    break;
            }
            
            return 
$strSelectField;
        }
        
        
/**
         * Gets the name and alias to use for the FROM clause
         * Also assigns appropriate 'JOIN' syntaxes
         * 
         * @return    String
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_Table ()
        {
            
$strSelectTable '';
            
$strParentAlias NULL;
            
            foreach (
$this->_dnlEntity as $dnoEntity)
            {
                
$objEntity $this->_arrEntity [$dnoEntity->getAttribute ('as')];
                
$strTableName '(`' $objEntity->___Table () . '` `' $dnoEntity->getAttribute ('as') . '`)';
                
                if (
$strParentAlias === NULL)
                {
                    
$strSelectTable $strTableName;
                }
                else
                {
                    switch (
strtoupper ($dnoEntity->getAttribute ('join')))
                    {
                        case 
'LEFT':
                            
$strSelectTable  '(' $strSelectTable ' ';
                            
$strSelectTable .= 'LEFT JOIN ' $strTableName ' ON (';
                            
$strSelectTable .= '`' $strParentAlias '`.`' $this->_dnlEntity->___Primary () . '`=';
                            
$strSelectTable .= '`' $dnoEntity->getAttribute ('as') . '`.`' $dnoEntity->getAttribute ('on') . '`';
                            
$strSelectTable .= '))';
                            
                            break;
                            
                        case 
'RIGHT':
                            
$strSelectTable  '(' $strSelectTable ' ';
                            
$strSelectTable .= 'RIGHT JOIN ' $strTableName ' ON (';
                            
$strSelectTable .= '`' $strParentAlias '`.`' $this->_dnlEntity->___Primary () . '`=';
                            
$strSelectTable .= '`' $dnoEntity->getAttribute ('as') . '`.`' $dnoEntity->getAttribute ('on') . '`';
                            
$strSelectTable .= '))';
                            
                            break;
                            
                        case 
'INNER':
                            
$strSelectTable  '(' $strSelectTable ' ';
                            
$strSelectTable .= 'INNER JOIN ' $strTableName ' ON (';
                            
$strSelectTable .= '`' $strParentAlias '`.`' $this->_dnlEntity->___Primary () . '`=';
                            
$strSelectTable .= '`' $dnoEntity->getAttribute ('as') . '`.`' $dnoEntity->getAttribute ('on') . '`';
                            
$strSelectTable .= '))';
                            
                            break;
                    }
                }
                
                
$strParentAlias $dnoEntity->getAttribute ('as');
            }
            
            return 
$strSelectTable;
        }
        
        
/**
         * Returns the WHERE clause as an Object to be traversed manually
         * 
         * @return    String
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_Where ()
        {
            if (
$this->_dnoWhere)
            {
                
$this->_eqbWhere->OXRM ($this->_dnoWhere$this->_arrParam);
            }
            
            return 
$this->_eqbWhere;
        }
        
        
/**
         * Generates the GROUP BY Clause
         * 
         * @return    String
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_GroupBy ()
        {
            if (
$this->_dnlGroupBy->length == 0)
            {
                return;
            }
            
            
// Group By Clause
            
$arrGroupBy = Array ();
            foreach (
$this->_dnlGroupBy as $dnoGroupBy)
            {
                
$arrGroupBy [] = $dnoGroupBy->nodeValue;
            }
            
            return 
implode (', '$arrGroupBy);
        }
        
        
/**
         * Generates the ORDER BY Clause. As there is no ORDER BY allowed for dataQuery objects at the moment,
         * a blank value is returned
         * 
         * @return    String
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_OrderBy ()
        {
            return;
        }
        
        
/**
         * Accessor method for the Sample Length
         * 
         * @return    String
         * @see        Property [dataQuery _intSamplePage]
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_SampleLength ()
        {
            return 
$this->_intSampleLength;
        }
        
        
/**
         * Accessor method for the Sample Page
         * 
         * @return    String
         * @see        Property [dataQuery _intSamplePage]
         * @see        Class [dataSample]
         * @see        Class [dataEntities]
         */
        
        
public function QuerySelect_SamplePage ()
        {
            return 
$this->_intSamplePage;
        }
    }

?>