<?php
/*
=============================================================================================================================================
| This file is part of a project released under the terms of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt). |
| |
| You should be given a copy of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt) within the same directory as the README.md; |
| if not, you can get a copy at http://Xyndravandria.ohost.de/XyndravandriaPHPLicense.txt . |
| |
| The copyright (c) of this project is owned by Mauro Di Girolamo <maurodigirolamo@.web.de>. |
============================================================================================================================================|
Xyndravandria Dyverath
----------------------
Alpha 0.0.0
Xyndravandria is the name of a collection of projects designed and developed by Mauro Di Girolamo (maurodigirolamo@web.de); he is therefore the copyright (c) owner of Xyndravandria itself and all of its projects.
Xyndravandria Dyverath is released under the terms of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt). You should be given a copy of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt) within the same directory as the README.md; if not, you can get a copy at http://Xyndravandria.ohost.de/XyndravandriaPHPLicense.txt . There might be a release under a freer license for a later, more stable version.
The documentation is either included in ./admin_media/Documentation/ or can be read at http://Xyndravandria.ohost.de/Dyverath/Documentation/.
All projects:
Xyndravandria Averazain
http://github.com/MauroDiGirolamo/Xyndravandria_Averazain
PHP
Averazain is an Ajax framework supporting also JavaScript disabled clients perfectly - including search engines like Google.
Xyndravandria Dyverath
http://github.com/MauroDiGirolamo/Xyndravandria_Dyverath
PHP
Dyverath is a database access wrapper.
Xyndravandria Erozaver
http://github.com/MauroDiGirolamo/Xyndravandria_Erozaver
PHP
Erozaver is a class extending the type hinting given by the PHP engine (additional support for basic type hinting and size constraints).
Xyndravandria Mondraviel
http://github.com/MauroDiGirolamo/Xyndravandria_Mondraviel
PHP
Mondraviel is a class used to separate HTML from PHP code by firstly register models - files containing place holders embedded in HTML code - and then later fill them dynamically with content by passing values for the place holders.
*/
namespace Xyndravandria\Dyverath;
use Xyndravandria\Dyverath\Query\Select;
use Xyndravandria\Dyverath\Query\Update;
use Xyndravandria\Dyverath\Query\Component\Statement\StatementListing\Where;
use Xyndravandria\Dyverath\Query\Component\Statement\StatementListing\CarelessWhere;
use Xyndravandria\Dyverath\Query\Component\Statement\StatementListing\UpdateStatement;
use Xyndravandria\Dyverath\Query\Component\Statement\StatementListing\InsertIntoStatement;
use Xyndravandria\Dyverath\Query\Component\Statement\Statement;
use Xyndravandria\Dyverath\Query\Component\Type\Column;
use Xyndravandria\Dyverath\Query\Component\Type\Limit;
/// A class representing Data inside of a Table.
class Data extends RepresentingClass implements \Iterator {
/// Whether the primary key should always be read out
/// of the Table. @n
/// The @ref Dataset "Datasets" have methods, for
/// instance Dataset::__set( ), which require the
/// primary key to clearly spot themselves from the
/// Table. @n
/// If this is not possible, you will not be able to
/// work with @ref Dataset "Datasets" properly.
const AlwaysSelectPrimaryKey = 1;
/// Returns the default configuration of a ExtendedRepresentingClass.
/// @private
/// @static
/// @returns integer
/// @note Overrode
/// RepresentingClass::DefaultConfiguration( ).
private static function DefaultConfiguration( ) {
return self::AlwaysSelectPrimaryKey;
}
/// The @ref Dataset "Datasets" inside a Data.
/// <dl class = "type"><dt><b>%Type:</b></dt>
/// <dd>array of Dataset</dd></dl>
/// @private
private $Datasets = array( );
/// Returns either one element of Data::$Dataset or
/// the whole attribute.
/// @public
/// @param integer $Index: Index of an
/// element inside the Data::$Dataset array.
/// @returns array of Dataset or
/// Dataset or null
/// @note $Index is an optional parameter. @n
/// If not passed, Data::$Datasets will be returned.
public function Dataset( $Index = -1 ) {
//\settype( $Index, 'integer' );
if( $this->ExceptionIfDeleted( ) )
return;
elseif( $Index == -1 )
// TODO: If Data::Dataset( ) is called if the Data->$Table has no primary keys, the Datasets returned here will be orphaned.
return $this->Datasets;
else
return isset( $this->Datasets[ $Index ] ) ? $this->Datasets[ $Index ] : null;
}
/// Returns count( Data::$Datasets ).
/// @public
/// @returns integer
public function Length( ) {
return \count( $this->Datasets );
}
/// A reference to the Table a Data is inside of.
/// <dl class = "type"><dt><b>%Type:</b></dt>
/// <dd>Table</dd></dl>
/// @private
private $Table = null;
/// Returns Data::$Table.
/// @public
/// @returns Table
public function Table( ) {
return $this->Table;
}
/// The @ref Column "Column(s)" a Data contains @ref
/// Value "Values" of.
/// <dl class = "type"><dt><b>%Type:</b></dt>
/// <dd>Column</dd></dl>
/// @private
private $Column = null;
/// The Where needed to read a Data out of its Table.
/// <dl class = "type"><dt><b>%Type:</b></dt>
/// <dd>Where</dd></dl>
/// @private
private $Where = null;
/// The Limit used within the
/// Query to read out Data.
/// <dl class = "return"><dt><b>%Type:</b></dt>
/// <dd>Limit</dd></dl>
/// @private
private $Limit;
/// The ORDER BY clause used within the Query to read
/// out Data.
/// <dl class = "return"><dt><b>%Type:</b></dt>
/// <dd>Column</dd></dl>
/// @private
private $OrderBy;
/// Creates a new instance of a Data.
/// @public
/// @param Table $Table: The Table a Data is inside of.
/// @param Column $Column: The @ref Column "Column(s)"
/// a Data contains @ref Value "Values" of.
/// @param Where $Where: The Where needed to read a
/// Data out of its Table.
/// @param Column $OrderBy: The OrderBy used within
/// the Query to read out Data.
/// @param Limit $Limit: The Limit used within the
/// Query to read out Data.
/// @note $Where, $Limit and $OrderBy are optional
/// parameters. @n
/// If $Where is not passed, the Data will be read out
/// without any constraint.
public function __construct( Table $Table, Column $Column, Where $Where = null, Column $OrderBy = null, Limit $Limit = null ) {
$this->Table = $Table;
$this->Column = $Column;
$this->Where = \is_null( $Where ) ? new CarelessWhere( 'TRUE' ) : $Where;
$this->Limit = $Limit;
$this->OrderBy = $OrderBy;
$this->SelectDatasets( );
return;
}
/// Executes a Query to read the @ref Dataset
/// "Datasets" out using its Where and inserts them
/// into @ref Dataset "Datasets".
/// @public
public function SelectDatasets( ) {
if( ! $this->ExceptionIfDeleted( ) ) {
$Result = $this->Table->Database( )->Server( )->ExecuteQuery( new Select( $this->Column, $this->Table, $this->Where, $this->OrderBy, $this->Limit ) );
$this->Datasets = array( );
while( $Object = \mysql_fetch_object( $Result ) )
$this->Datasets[ ] = new Dataset( $Object, $this );
}
return;
}
/// Deletes a Dataset from its Data.
/// @public
/// @param Dataset $Dataset: The Dataset to be removed.
/// @param boolean $CallDelete: Whether
/// $Datatset->Delete( ) should be called or not.
/// @note $CallDete is necessary, because
/// Data::DeleteDatatset( ) can be called by
/// Dataset::Delete( ). @n
/// Its default value is true, so you will not notice
/// anything when using it manually to delete a
/// certain Dataset from a Data.
public function DeleteDataset( Dataset $DatasetToBeRemoved, $CallDelete = true ) {
//\settype( $CallDelete, 'boolean' );
if( ! $this->ExceptionIfDeleted( ) ) {
foreach( $this->Datasets as $Index => &$Dataset )
if( $Dataset == $DatasetToBeRemoved ) {
$CallDelete && $this->Datatsets[ $Index ]->Delete( );
unset( $this->Datasets[ $Index ] );
$this->Datasets = \array_values( $this->Datasets ); // TODO: Reording indexes here might be a conflict when iterating over this object.
break;
}
}
return;
}
/// Alias of Data::UpdateColumnTotal( ).
public function __set( $ColumnName, $Value ) {
$this->UpdateColumnTotal( $ColumnName, $Value );
return;
}
/// Sets a new value for a column changing all @ref
/// Dataset "Datasets".
/// @public
/// @param string $ColumnName: The column to be
/// changed.
/// @param mixed $Value: The new value.
public function UpdateColumnTotal( $ColumnName, $Value ) {
//\settype( $ColumnName, 'string' );
//\settype( $Value, 'string' );
if( $this->ExceptionIfDeleted( ) )
return;
elseif( ! \in_array( $ColumnName, $this->Column->BlankColumn( ) ) )
throw new XyndravandriaDyverathException( 'Column \'' . $ColumnName . '\' in $ColumnName is not in this Data.' );
else {
$Column = new Column( $ColumnName );
$Column->Optimise( $this );
$this->Table->Database( )->Server( )->ExecuteQuery( new Update( $this, new UpdateStatement( array( new Statement( $Column, new Operator( '=' ), new Value( $Value ) ) ) ), $this->Where ) );
$this->SelectDatasets( );
}
return;
}
/// Deletes a Data.
/// @public
public function Delete( ) {
if( $this->ExceptionIfDeleted( ) )
return;
else {
$this->Table->Database( )->Server( )->ExecuteQuery( new DeleteFrom( $this, $this->Where ) );
$this->Table = null;
}
return;
}
/// Alias of Data::ColumnValue( ).
public function __get( $Column ) {
return $this->ColumnValue( $Column );
}
/// Puts all values of a certain column from all @ref
/// Dataset "Datasets" into an array an returns it.
/// @public
/// @param $Column: The certain column.
/// @returns array of mixed
public function ColumnValue( $Column ) {
$Value = array( );
if( ! $this->ExceptionIfDeleted( ) && $this->Length( ) > 0 ) {
if( \is_null( $this->Datasets[ 0 ]->$Column ) )
throw new XyndravandriaDyverathException( 'Column \'' . $Column . '\' has not been found in the Datasets. Either it is now column of the Table or it has not been read out.' );
else
foreach( $this->Datasets as $Dataset )
$Value[ ] = $Dataset->$Column;
}
return $Value;
}
/// Throws an exception if the Data is deleted.
/// @public
/// @returns boolean
private function ExceptionIfDeleted( ) {
if( $this->Table == null ) {
throw new XyndravandriaDyverathException( 'This data has already been deleted.' );
return true;
} else
return false;
}
/// The current position.
/// <dl class = "return"><dt><b>%Type:</b></dt>
/// <dd>integer</dd></dl>
/// @private
/// @note Required by the Iterator interface.
private $Position = 0;
/// Returns the current Dataset.
/// @public
/// @return Dataset or null
/// @note Required by the Iterator interface.
public function current( ) {
if( $this->ExceptionIfDeleted( ) )
return;
else
return isset( $this->Datasets[ $this->Position ] ) ? $this->Datasets[ $this->Position ] : null;
}
/// Returns the current position.
/// @public
/// @returns integer
/// @note Required by the Iterator interface.
public function key( ) {
if( $this->ExceptionIfDeleted( ) )
return;
else
return $this->Position;
}
/// Moves forward to next element.
/// @public
/// @note Required by the Iterator interface.
public function next( ) {
$this->ExceptionIfDeleted( ) || $this->Position < \count( $this->Datasets ) && ++$this->Position;
return;
}
/// Rewinds the Iterator to the first element.
/// @public
/// @note Required by the Iterator interface.
public function rewind( ) {
$this->ExceptionIfDeleted( ) || $this->Position = 0;
return;
}
/// Checks if current position is valid.
/// @public
/// @return boolean
/// @note Required by the Iterator interface.
public function valid( ) {
if( $this->ExceptionIfDeleted( ) )
return;
else
return isset( $this->Datasets[ $this->Position ] );
}
}
?>
|