phpDocumentor GloryLands
Database
[ class tree: GloryLands ] [ index: GloryLands ] [ all elements ]

Source for file db.structured.php

Documentation is available at db.structured.php

  1. <?php
  2. /** 
  3.   * Structured database access
  4.   *
  5.   * <pre>
  6.   * GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
  7.   * Copyright (C) 2008-09  John Haralampidis <jïhnys2[at]gmail.cïm>
  8.   *
  9.   * This program is free software: you can redistribute it and/or modify
  10.   * it under the terms of the GNU General Public License as published by
  11.   * the Free Software Foundation, either version 3 of the License, or
  12.   * (at your option) any later version.
  13.   *
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   * GNU General Public License for more details.
  18.   *
  19.   * You should have received a copy of the GNU General Public License
  20.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.   *
  22.   * For any help/suggestions or troubleshooting you can see the
  23.   * project community website at <http://www.glorylands.gr>
  24.   * </pre>
  25.   *
  26.   * @license GNU/GPLv3 GNU General Public License version 3
  27.   * @package GloryLands
  28.   * @subpackage Database
  29.   * @author John Haralampidis <jïhnys2[at]gmail.cïm>
  30.   * @copyright Copyright (C) 2007-2008, John Haralampidis
  31.   * @version 1.0
  32.   */
  33.  
  34. /**
  35.   * Structured database record
  36.   *
  37.   * This class provides record-level structured database access.
  38.   *
  39.   * @package GloryLands
  40.   * @subpackage Database
  41.   */
  42. class GLDBRecord {
  43.     
  44.     protected $_data;
  45.     protected $_index;    
  46.     protected $_changed;
  47.     protected $_parent;
  48.     
  49.     public function __construct($data&$parent{
  50.         $this->_data = $data;
  51.         $this->_parent = &$parent;
  52.         $this->_changed = false;        
  53.         $this->update_index();
  54.     }
  55.     
  56.     public function __destruct({
  57.         $this->save();
  58.     }
  59.  
  60.     private function update_index({
  61.         $this->_index = array();
  62.         foreach ($this->_parent->_index as $i{
  63.             $this->_index[$i$this->_data[$i];
  64.         }
  65.     }
  66.     
  67.     public function save({
  68.         if (!$this->_changedreturn;
  69.         $this->_changed = false;
  70.         GL::$db->update($this->_parent->_table$this->_index$this->_data);
  71.         $this->update_index();
  72.     }
  73.     
  74.     public function __get($var{
  75.         if (isset($this->_data[$var])) {
  76.             return $this->_data[$var];
  77.         else {
  78.             return NULL;
  79.         }
  80.     }
  81.  
  82.     public function __set($var$value{
  83.         if (isset($this->_data[$var])) {
  84.             $this->_data[$var$value;
  85.             $this->_changed = true;
  86.         }
  87.     }
  88.  
  89. }
  90.  
  91. /**
  92.   * Structured database recordset
  93.   *
  94.   * This class provides recordset-level structured database access.
  95.   *
  96.   * @package GloryLands
  97.   * @subpackage Database
  98.   */
  99. class GLDBRecordset {
  100.     
  101.     public $_table;
  102.     public $_index;
  103.  
  104.     protected $_recordset;
  105.     protected $_cursor;
  106.     
  107.     public function __construct($table$index=false{
  108.         $this->_table = $table;
  109.         $this->_cursor = 0;
  110.         if (!$index{
  111.             $this->_index = array('index');
  112.         else {
  113.             $this->_index = $index;
  114.         }
  115.     }
  116.  
  117.     protected function fetch_object($data{
  118.         $record new GLDBRecord($data$this);
  119.         return $record;        
  120.     }
  121.         
  122.     public function select($index_data=false$order_by=false$sort_order=false{
  123.         $this->_recordset = GL::$db->select($this->_table$index_datafalse$order_by$sort_order);
  124.     }
  125.     
  126.     public function rows({
  127.         return GL::$db->num_rows($this->_recordset);
  128.     }
  129.  
  130.     public function last({
  131.         if ($this->rows(== 0return false;
  132.         $this->_cursor = $this->rows()-1;
  133.         GL::$db->set_row(0$this->_recordset);
  134.         $row GL::$db->fetch_array(MYSQL_ASSOC$this->_recordset);
  135.         if (!$rowreturn false;
  136.  
  137.         return $this->fetch_object($row);
  138.     }
  139.  
  140.     public function first({
  141.         if ($this->rows(== 0return false;
  142.         GL::$db->set_row(0$this->_recordset);
  143.         $row GL::$db->fetch_array(MYSQL_ASSOC$this->_recordset);
  144.         if (!$rowreturn false;
  145.         $this->_cursor = 0;
  146.  
  147.         return $this->fetch_object($row);
  148.     }
  149.     
  150.     public function next({
  151.         $row GL::$db->fetch_array(MYSQL_ASSOC$this->_recordset);
  152.         if (!$rowreturn false;
  153.         $this->_cursor++;
  154.  
  155.         return $this->fetch_object($row);
  156.     }
  157.  
  158.     public function previous({
  159.         if ($this->_cursor == 0return false;
  160.         $this->_cursor--;
  161.         GL::$db->set_row(0$this->_recordset);
  162.         $row GL::$db->fetch_array(MYSQL_ASSOC$this->_recordset);
  163.         if (!$rowreturn false;
  164.         GL::$db->set_row(0$this->_recordset);
  165.         
  166.         return $this->fetch_object($row);
  167.     }
  168.     
  169.     public function create({
  170.         return $this->fetch_object(false);
  171.     }
  172.         
  173. }
  174.  
  175. ?>

Documentation generated on Tue, 13 Oct 2009 23:48:52 +0300 by phpDocumentor 1.4.1