PHP Classes

File: Xyndravandria/Dyverath/Query/Component/Type/Column.php

Recommend this page to a friend!
  Packages of Mauro Di Girolamo   Xyndravandria Dyverath   Xyndravandria/Dyverath/Query/Component/Type/Column.php   Download  
File: Xyndravandria/Dyverath/Query/Component/Type/Column.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Xyndravandria Dyverath
Compose and execute MySQL database queries
Author: By
Last change: Alpha 0.1.0
Date: 13 years ago
Size: 8,018 bytes
 

 

Contents

Class file image Download
<?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\Query\Component\Type;

use Xyndravandria\Dyverath\Table;
use Xyndravandria\Dyverath\XyndravandriaDyverathException;
use Xyndravandria\Dyverath\Query\Component\Statement\StatementType;

// TODO: Stricter validations?
// TODO: Aliases are conflicting the work of Data and Dataset, since the real column names are lost.
// TODO: Aliases in other queries than Select are invalid.
// - Check whether column exists in table
// - Check ColumnArray structure
/// @brief A class representing one or more columns used in a Query. @n
/// @details A class representing one or more columns used in a Query. @n
/// There are four different usages of the
/// Column class:
/// <table>
/// <tr>
///    <th>Usage</th>
///    <th>Example</th>
///    <th>Output</th>
/// </tr>
/// <tr>
///    <td>One or more normal columns</td>
///    <td>@verbatim new Column( 'Name' )
///new Column( array( 'Name', 'Age' ) ) @endverbatim</td>
///    <td> @verbatim `Table`.`Name`
///`Table`.`Name` , `Table`.`Age` @endverbatim</td>
/// </tr>
/// <tr>
///    <td>All columns of a table</td>
///    <td>@verbatim new Column( Column::AllColumns ) @endverbatim</td>
///    <td>@verbatim * @endverbatim</td>
/// </tr>
/// <tr>
///    <td>Aliases for one or more columns</td>
///    <td>@verbatim new Column( array( 'Name' => 'TheName' ) )
///new Column( array( 'Name', 'Age' => 'TheAge' ) ) @endverbatim</td>
///    <td>@verbatim `Table`.`Name` AS `TheName` 
///`Table`.`Name` , `Table`.`Age` AS `TheAge` @endverbatim</td>
/// </tr>
/// <tr>
///    <td>With ORDER BY clause</td>
///    <td>@verbatim new Column( array( 'Name' => Column::Ascending ) )
///new Column( array( 'Name', 'Age' => Column::Descending ) ) @endverbatim</td>
///    <td>@verbatim ORDER BY `Name` ASC
///ORDER BY `Name` , `Age` DESC @endverbatim</td>
/// </tr>
/// </table>
/// @abstract
class Column extends Type implements StatementType {
   
   /// The column(s) represented as an array.
   /// <dl class = "type"><dt><b>%Type:</b></dt>
   /// <dd>array of mixed</dd></dl>
   /// @private 
   private $ColumnArray = null;
   
   /// The columns' table.
   /// <dl class = "type"><dt><b>%Type:</b></dt>
   /// <dd>Table</dd></dl>
   /// @private 
   private $Table;
   
   /// Optimises a Column.
   /// @public 
   /// @param Table $Table: Description
   public function Optimise( Table $Table ) {
      $this->Table = $Table;
      $this->CreateString( );
      return;
   }
   
   /// Used to tell that all columns of a Table are
   /// affected.
   const AllColumns = 1;
   
   /// Used to tell that the values of one column should
   /// be sorted in ascending order.
   const Ascending = 2;
   
   /// Used to tell that the values of one column should
   /// be sorted in descending order.
   const Descending = 3;
   
   /// Returns Column::$ColumnArray.
   /// @public 
   /// @returns array of string
   public function BlankColumn( ) {
      return $this->ColumnArray;
   }
   
   // TODO: Select SQL Functions.
   /// @public 
   /// Creates a new Column.
   /// @param $ColumnArray: The column(s) represented as
   /// an array.
   public function __construct( $ColumnArray ) { // TODO: Type check $ColumnArray?
      \is_array( $ColumnArray ) || $ColumnArray = array( $ColumnArray );
      $this->ColumnArray = $ColumnArray;
      $this->CreateString( );
      return;
   }
   
   /// The Column as a string to be used as a component
   /// of a Query.
   /// <dl class = "type"><dt><b>%Type:</b></dt>
   /// <dd>string</dd></dl>
   /// @private 
   private $String;
   
   /// Turns the Column into a string and saves it into
   /// Column::$String.
   /// @public
   public function CreateString( ) {
      $this->String = '';
      if( isset( $this->ColumnArray[ 0 ] ) && $this->ColumnArray[ 0 ] == self::AllColumns )
         $this->String = '*';
      else
         foreach( $this->ColumnArray as $Index => $Column )
            if( \is_numeric( $Index ) ) // Normal column
               $this->String .= ( $this->String == '' ? '' : ', ' ) . ( \is_null( $this->Table ) ? '' : '`' . $this->Table->Name( ) . '`.' ) . '`' . $Column . '`';
            elseif( $Column == self::Ascending || $Column == self::Descending ) // With ASC or DESC
               $this->String .= ( $this->String == '' ? '' : ', ' ) . ( \is_null( $this->Table ) ? '' : '`' . $this->Table->Name( ) . '`.' ) . '`' . $Index . '` ' . ( $Column == self::Ascending ? 'ASC' : 'DESC' );
            elseif( !\is_numeric( $Index ) && !\is_numeric( $Column ) ) // With alias
               $this->String .= ( $this->String == '' ? '' : ', ' ) . ( \is_null( $this->Table ) ? '' : '`' . $this->Table->Name( ) . '`.' ) . '`' . $Index . '` AS `' . $Column . '`';
            else
               throw new XyndravandriaDyverathException( 'Wrong parameters passed to Column::__construct( ). Maybe associative array?'  );
      return;
   }

   /// Returns Column::$String.
   /// @public 
   /// @returns string
   /// @note Required by the Component class.
   public function __ToString( ) {
      return $this->String;
   }
   
}
?>