| author | Bashkim Isai |
|---|---|
| package | OXRM |
In file D:\oblib-core\php\oblib\dataOXRM\dataOXRM.class.php between lines 49 and 361.
<?php
/**
* Traverses an OXRM XML File
*
* @author Bashkim Isai
* @package OXRM
* @see Object [data]
*/
class dataOXRM
{
/**
* The DOMDocument containing the OXRM information
*
* @type Object [DOMDocument]
*/
public $_domOXRM;
/**
* The XPath Object which will traverse the DOMDocument
*
* @type Object [DOMXPath]
*/
public $_dxpOXRM;
/**
* Keeps an Array of Entities which have already been
* loaded so as not to load the same dataEntities class twice
*
* @type Array [String => dataEntities]
*/
public $_arrEntity;
/**
* Keeps an Array of Queries which have already been
* loaded so as not to load the same dataQuery class twice
*
* @type Array [String => dataQuery]
*/
public $_arrQuery;
/**
* Builds the basics for an OXRM document to be traversed (including
* loading the file in the DOMDocument and creating a XPath object
*
* @see Object [DOMDocument]
* @see Object [DOMXPath]
* @return Void
*/
function __construct ($strFile)
{
// Load the OXRM file
$this->_domOXRM = new DOMDocument;
$this->_domOXRM->Load ($strFile);
$this->_dxpOXRM = new DOMXPath ($this->_domOXRM);
}
/**
* Gets OXRM information for a Query and starts a dataQuery object for it
*
* @see Class [dataQuery]
* @return Object [dataQuery]
*/
public function &Query ($strQueryName)
{
// If the DOM Node has already been loaded
// Return the preloaded object
if (isset ($this->_arrQuery [$strQueryName]))
{
return $this->_arrQuery [$strQueryName];
}
// Get the Entity object in the OXRM document
$dnqQuery = $this->_dxpOXRM->Query ('/oxrm/query[@name=' . escapeshellarg ($strQueryName) . ']');
if ($dnqQuery->length == 0)
{
// If there is no entity by the name called, throw and exception
throw new ExceptionCore ('There is no Entity Definition for the following Entity: ' . $strQueryName);
}
else if ($dnqQuery->length > 1)
{
// If the name is ambiguous, then throw and exception
throw new ExceptionCore ('There are multiple Entity Definitions for the following Entity: ' . $strQueryName);
}
// Get the Entity
$dnoQuery = $dnqQuery->item (0);
$this->_arrQuery [$strQueryName] = new dataQuery ($this, $dnoQuery);
return $this->_arrQuery [$strQueryName];
}
/**
* Gets OXRM information for an Entity and starts a dataEntities object for it
*
* @see Class [dataEntities]
* @return Object [dataEntities]
*/
public function &Entity ($strEntityName)
{
// If the DOM Node has already been loaded
// Return the preloaded object
if (isset ($this->_arrEntity [$strEntityName]))
{
return $this->_arrEntity [$strEntityName];
}
// Get the Entity object in the OXRM document
$dnqEntity = $this->_dxpOXRM->Query ('/oxrm/entity[@name=' . escapeshellarg ($strEntityName) . ']');
if ($dnqEntity->length == 0)
{
// If there is no entity by the name called, throw and exception
throw new ExceptionCore ('There is no Entity Definition for the following Entity: ' . $strEntityName);
}
else if ($dnqEntity->length > 1)
{
// If the name is ambiguous, then throw and exception
throw new ExceptionCore ('There are multiple Entity Definitions for the following Entity: ' . $strEntityName);
}
// Get the Entity
$dnoEntity = $dnqEntity->item (0);
// Include any desired files
$dnlClassFile = $this->_dxpOXRM->Query ('./file', $dnoEntity);
if ($dnlClassFile->length >= 1)
{
foreach ($dnlClassFile as $dnoClassFile)
{
$strFileName = $dnoClassFile->nodeValue;
if (!file_exists (DIR_ENTITY . '/' . $strFileName))
{
throw new ExceptionCore (
'The file you attempted to include (' . $strFileName . ') does not exist ' .
'(using directory ' . DIR_ENTITY . ')'
);
}
require_once (DIR_ENTITY . $strFileName);
}
}
// Get the 'many' class
$dnlClassMany = $this->_dxpOXRM->Query ('./class[@multiplicity="*"]', $dnoEntity);
if ($dnlClassMany->length != 1)
{
throw new ExceptionCore ('No many/plural/* class was defined for the entity: ' . $strEntityName);
}
$strClassMany = $dnlClassMany->item (0)->nodeValue;
if (!class_exists ($strClassMany))
{
throw new ExceptionCore ('Could not start a dataEntity because class does not exist: ' . $strClassMany);
}
if (!is_subclass_of ($strClassMany, 'dataEntities'))
{
throw new ExceptionCore ('Could not start new entity because class does not inherit dataEntities: ' . $strClassMany);
}
// Instantiate the 'many' class and return it
$this->_arrEntity [$strEntityName] = new $strClassMany ($this, $dnoEntity);
return $this->_arrEntity [$strEntityName];
}
/**
* Executes a custom SQL statement and returns the result as an Array
* with the popuplated database information for manual PHP traversing
*
* @see Class [dataEntities]
* @return Array [] [String => Mix]
*/
public function RenderSqlAsStatement ($strStatement, $arrParam)
{
// This is what the Information will be outputted into
$arrResponse = Array ();
// Build the Response
$smtStatement = $GLOBALS ['MySQL']->Prepare ($strStatement);
if (!$smtStatement)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
if ($arrParam)
{
call_user_func_array (
Array ($smtStatement, 'bind_param'),
$arrParam
);
}
$smtStatement->execute ();
// Put the Database response into an ObLib object
$rmdMetaData = $smtStatement->result_metadata ();
$arrData = Array ();
$arrReference = Array ();
foreach ($rmdMetaData->fetch_fields () as $objField)
{
$arrData [$objField->name] = '';
$arrReference [] =& $arrData [$objField->name];
}
call_user_func_array (
Array ($smtStatement, 'bind_result'),
$arrReference
);
while ($smtStatement->Fetch ())
{
$arrResult = new stdClass;
foreach ($arrData as $strKey => $mixValue)
{
$arrResult->$strKey = $mixValue;
}
$arrResponse [] = $arrResult;
}
$smtStatement->free_result ();
// Return the Response
return $arrResponse;
}
/**
* Executes a custom SQL statement and returns the new row's ID.
* Used for custom INSERT statements
*
* @see Class [dataEntities]
* @return Integer
*/
public function RenderSqlAsInsert ($strStatement, $arrParam)
{
// Prepare and Execute the Insert Statement
$smtStatement = $GLOBALS ['MySQL']->Prepare ($strStatement);
if (!$smtStatement)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
if ($arrParam)
{
call_user_func_array (
Array ($smtStatement, 'bind_param'),
$arrParam
);
}
$bolResponse = $smtStatement->execute ();
$smtStatement->free_result ();
// If there was a failure, tell us
if (!$bolResponse)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
return $GLOBALS ['MySQL']->insert_id;
}
/**
* Executes a custom SQL statement and returns the number of affected rows.
* Used for custom UPDATE and DELETE statements
*
* @see Class [dataEntities]
* @return Integer
*/
public function RenderSqlAsAffect ($strStatement, $arrParam)
{
$smtStatement = $GLOBALS ['MySQL']->Prepare ($strStatement);
if (!$smtStatement)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
call_user_func_array (
Array ($smtStatement, 'bind_param'),
$arrParam
);
$bolResponse = $smtStatement->execute ();
$smtStatement->free_result ();
// If there was a failure, tell us
if (!$bolResponse)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
return $GLOBALS ['MySQL']->affected_rows;
}
}
?>