PHP Classes

File: Xyndravandria/Dyverath/Database.php

Recommend this page to a friend!
  Packages of Mauro Di Girolamo   Xyndravandria Dyverath   Xyndravandria/Dyverath/Database.php   Download  
File: Xyndravandria/Dyverath/Database.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Xyndravandria Dyverath
Compose and execute MySQL database queries
Author: By
Last change:
Date: 13 years ago
Size: 7,837 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;

use Xyndravandria\Dyverath\Query\Query;

/// A class representing a @ref Server "Server's"
/// Database.
class Database extends ExtendedRepresentingClass {
   
   /// A reference to a @ref Database "Database's" Server.
   /// <dl class = "type"><dt><b>%Type:</b></dt>
   /// <dd>Server</dd></dl>
   /// @private 
   private $Server = null;
   
   /// Returns Database::$Server.
   /// @public 
   /// @returns Server
   public function Server( ) {
      return $this->Server;
   }
   
   /// Creates a new Database.
   /// @public 
   /// @param string $Name: The name of the Database.
   /// @param Server $Server: The @ref Database
   /// "Database's" Server.
   public function __construct( $Name, Server $Server ) {
      parent::__construct( $Name );
      $this->Server = $Server;
      return;
   }
   
   /// Accesses one of a @ref Database "Database's" @ref
   /// Table "Tables".
   /// @public
   /// @param string $Name: The @ref Table "Table's" name.
   /// @param mixed $PrimaryKeyValue: If you directly
   /// pass a value for the @ref Table "Table's" primary
   /// key, you will be returned a Dataset instead.
   /// @note $PrimaryKeyValue is an optional parameter. @n
   /// Besides, a primary key can consist of more than
   /// one column and thus, $PrimaryKeyValue can also be
   /// an array of mixed.
   public function Table( $Name, $PrimaryKeyValue = '' ) {
      //\settype( $Name, 'string' );
      //\settype( $PrimaryKeyValue, 'string' ); // TODO: $PrimaryKeyValue always a string (integers etc. will be converted)?
      $Table = new Table( $Name, $this, $PrimaryKeyValue );
      if( ( $TableSaved = Table::Cache( )->Get( $Table->UniqueIdentifier( ) ) ) ) {
         if( empty( $PrimaryKeyValue ) )
            return $TableSaved;
         else
            return $TableSaved->DatasetByPrimaryKey( $PrimaryKeyValue );
      } elseif( \mysql_num_rows( $this->Server->ExecuteQuery( new Query( 'SELECT TRUE FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLES`.`TABLE_SCHEMA` = \'' . \mysql_real_escape_string( $this->Name ) . '\' AND `TABLES`.`TABLE_NAME` = \'' . \mysql_real_escape_string( $Table->Name( ) ) . '\' LIMIT 1' ) ) ) == 0 )
         throw new XyndravandriaDyverathException( 'Requested unknown table \'' . $Table->Name( ) . '\' in database \'' . $this->Name . '\'.' );
      else if( Table::Configuration( ) & Table::CacheEnabled ) {
         if( empty( $PrimaryKeyValue ) )
            return Table::Cache( )->Add( $Table );
         else
            return Table::Cache( )->Add( $Table )->DatasetByPrimaryKey( $PrimaryKeyValue );
      } else {
         if( empty( $PrimaryKeyValue ) )
            return $Table;
         else
            return $Table->DatasetByPrimaryKey( $PrimaryKeyValue );
      }
      return;
   }
   
   /// Alias of Database::Table( ).
   public function __get( $Table ) {
      return $this->Table( $Table );
   }

   /// Alias of Database::Table( ).
   public function __call( $Table, $PrimaryKeyValue ) {
      return empty( $PrimaryKeyValue ) ? $this->Table( $Table ) : $this->Table( $Table, $PrimaryKeyValue[ 0 ] );
   }
   
   /// Returns all this @ref Database "Database's" @ref
   /// Table "Tables".
   /// @public
   /// @returns array of Table
   public function Tables( ) {
      $Tables = array( );
      if( ! ( $Result = $this->Server->ExecuteQuery( 'SELECT `TABLES`.`TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLES`.`TABLE_SCHEMA` = \'' . \mysql_real_escape_string( $this->Name ) . '\'' ) ) )
         throw new XyndravandriaDyverathException( 'Unable to read out the tables of the database \'' . $this->Name . '\' from the information schema.' );
      else
         while( $Dataset = \mysql_fetch_object( $Result ) ) {
            $Table = new Table( $Dataset->TABLE_NAME, $this );
            if( ( $TableSaved = Table::Cache( )->Get( $Table->UniqueIdentifier( ) ) ) )
               $Tables[ ] = $TableSaved;
            elseif( Table::Configuration( ) & Table::CacheEnabled )
               $Tables[ ] = Table::Cache( )->Add( $Table );
            else
               $Tables[ ] = $Table;
         }
      return $Tables;
   }
   
   /// Alias of Database::Cache( )->CurrentObject( ).
   /// @public
   /// @static
   /// @returns Database
   public static function Current( ) {
      return self::Cache( )->CurrentObject( );
   }
   
   /// Returns this @ref Database "Database's" unique
   /// identifier.
   /// @public
   /// @returns string
   /// @note Overrode ExtendedRepresentingClass::UniqueIdentifier( ).
   public function UniqueIdentifier( ) {
      return $this->Server->Name( ) . '->' . $this->Name;
   }
   
   /// Returns the declared name of this class.
   /// @public      
   /// @static      
   /// @returns string
   /// @note Required by the CacheAble interface.
   public static function ClassName( ) {
      return __CLASS__;
   }

}
?>