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

Source for file db.php

Documentation is available at db.php

  1. <?php
  2. /** 
  3.   * Basic Database Handling System
  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.   * Basic database interface
  36.   *
  37.   * This class is further extended by the database jet selected. Using this method,
  38.   * any kind of database can be used.
  39.   *
  40.   * @package GloryLands
  41.   * @subpackage Database
  42.   */
  43. abstract class DBJet {
  44.  
  45.     /**
  46.       * Initialize Database jet, using the configuration provided
  47.       * @param    array    $config    The Jet configuration (Structure depends on the Jet)
  48.       */
  49.     abstract public function init($config);
  50.  
  51.     /**
  52.       * Insert a new data row on the database
  53.       *
  54.       * @param    string    $table            The name of the table that will receive the new row
  55.       * @param array $data                An array that contains the field names (as keys) and the field values to add
  56.       * @return bool|resource           Returns false in case of error or the resultset of the executed query
  57.       */
  58.     abstract public function insert($table$data);
  59.  
  60.     /**
  61.       * Delete a data row from the database
  62.       *
  63.       * @param    string    $table             The name of the table that contain the data
  64.       * @param array $index_data        An array that holds the indexing information of the data being removed (Ex. [name] => 'user')
  65.       * @return bool|resource           Returns false in case of error or the resultset of the executed query
  66.       */
  67.     abstract public function delete($table$index_data=false);
  68.  
  69.     /**
  70.       * Replace a data row into the database
  71.       *
  72.       * @param    string    $table    The name of the table that will receive the data
  73.       * @param array $data        An array that contains the field names and values. If one or more field names are indexing keys, they will define the row that will be updated.
  74.       * @return bool|resource           Returns false in case of error or the resultset of the executed query
  75.       */
  76.     abstract public function replace($table$data);
  77.  
  78.     /**
  79.       * Select a row from the database
  80.       *
  81.       * @param    string    $table            The name of the database table that the data resides on
  82.       * @param    array    $index_data        [Optional] The indexing fields that define the rows to fetch (ex. [name] => 'user', [password] => '1234')
  83.       * @param    array    $return_data    [Optional] The names of the fields that should be returned (if ommited, all of them)
  84.       * @param    array    $order_by        [Optional] The name of the sorting field
  85.       * @param    array    $sort_order        [Optional] The sort order: 'ASC' for ascending or 'DESC' for descending
  86.       * @return bool|resource           Returns false in case of error or the resultset of the executed query
  87.       */    
  88.     abstract public function select($table$index_data=false$return_data=false$order_by=false$sort_order=false);
  89.  
  90.     /**
  91.       * Update a row on the database
  92.       *
  93.       * @param    string    $table            The name of the database table that the data resides on
  94.       * @param    array    $index_data        The indexing fields that define the row(s) to update (ex. [name] => 'user', [password] => '1234')
  95.       * @param    array    $new_data        The new data
  96.       * @return bool|resource           Returns false in case of error or the resultset of the executed query
  97.       */    
  98.     abstract public function update($table$index_data$new_data);
  99.     
  100.     /**
  101.       * Fetch a row from the resultset
  102.       *
  103.       * @param    int    $mode                [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
  104.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  105.       * @return bool|array               Returns false in case of error or the data array if successfull
  106.       */    
  107.     abstract public function fetch_array($mode=false$resultset=false);
  108.  
  109.     /**
  110.       * Fetch all the rows of the resultset
  111.       *
  112.       * @param    int    $mode                [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
  113.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  114.       * @return bool|array               Returns false in case of error or the data array if successfull
  115.       */    
  116.     abstract public function fetch_array_all($mode=false$resultset=false);
  117.  
  118.     /**
  119.       * Fetch a single value from the resultset
  120.       *
  121.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  122.       * @return bool|array               Returns false in case of error or the data array if successfull
  123.       */    
  124.     abstract public function fetch_value($resultset=false);
  125.  
  126.     /**
  127.       * Get the number of rows in the resultset
  128.       *
  129.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  130.       * @return int                        Returns the number of the rows in the resultset
  131.       */    
  132.     abstract public function num_rows($resultset=false);
  133.  
  134.     /**
  135.       * Get the number of changed rows in the resultset
  136.       *
  137.       * @return int                        Returns the number of the rows that were changed, on the last resultset
  138.       */    
  139.     abstract public function num_changed();
  140.  
  141.     /**
  142.       * Get the last error message
  143.       *
  144.       * @param    bool    $formatted        If TRUE, the message will be properly, HTML formatted
  145.       * @return int                        Returns the HTML or text result
  146.       */    
  147.     abstract public function get_error_message($formatted=true);
  148.     
  149.     // Structure functions
  150.  
  151.     /**
  152.       * Get the names of all the tables in the database
  153.       *
  154.       * @return bool|array               Returns the database tables as array, or FALSE on error
  155.       */    
  156.     abstract public function get_tables();
  157.  
  158.     /**
  159.       * Get a table structure
  160.       *
  161.       * @return bool|array               Returns the table structure as an associative array, with key each field's name
  162.       */    
  163.     abstract public function get_table_structure($table);
  164.     
  165.     // Advanced functions    
  166.  
  167.     /**
  168.       * Manually execute a complex SQL query
  169.       *
  170.       * This function should be avoided when possible, because there could be jets that do not support
  171.       * SQL quering!
  172.       *
  173.       * @param    string    $text            The query to execute
  174.       * @return bool|resource           Returns false in case of error or the resultset of the executed query
  175.       */
  176.     abstract public function query($text);
  177.  
  178.     /**
  179.       * Set the current resultset pointer into a different row
  180.       *
  181.       * @param    int    $position            The new position
  182.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  183.       * @return bool                    Returns false in case of error or TRUE if successfull
  184.       */
  185.     abstract public function set_row($position$resultset=false);
  186.  
  187.     // Checking functions
  188.     
  189.     /**
  190.       * Check if the select query will return data
  191.       *
  192.       * @param    string    $table            The name of the database table that the data resides on
  193.       * @param    array    $index_data        [Optional] The indexing fields that define the rows to fetch (ex. [name] => 'user', [password] => '1234')
  194.       * @param    array    $return_data    [Optional] The names of the fields that should be returned (if ommited, all of them)
  195.       * @param    array    $order_by        [Optional] The name of the sorting field
  196.       * @param    array    $sort_order        [Optional] The sort order: 'ASC' for ascending or 'DESC' for descending
  197.       * @return bool                    Returns TRUE if data were received or FALSE if the result was empty
  198.       */    
  199.     public function check_select($table$index_data=false$return_data=false$order_by=false$sort_order=false{
  200.         $ans $this->select($table$index_data$return_data$order_by$sort_order);
  201.         if (!$ansreturn false;
  202.         if ($this->num_rows($ans1return false;
  203.         return true;
  204.     }
  205.     
  206.     // Local functions
  207.     
  208.     /**
  209.       * Select data and fetch the first row
  210.       *
  211.       * @param    string    $table            The name of the database table that the data resides on
  212.       * @param    array    $index_data        [Optional] The indexing fields that define the rows to fetch (ex. [name] => 'user', [password] => '1234')
  213.       * @param    array    $return_data    [Optional] The names of the fields that should be returned (if ommited, all of them)
  214.       * @param    array    $order_by        [Optional] The name of the sorting field
  215.       * @param    array    $sort_order        [Optional] The sort order: 'ASC' for ascending or 'DESC' for descending
  216.       * @return bool|array               Returns false in case of error or the data array if successfull
  217.       */    
  218.     public function select_and_get($table$index_data=false$return_data=false$order_by=false$sort_order=false{
  219.         $resultset $this->select($table$index_data$return_data$order_by$sort_order);
  220.         $rows $this->num_rows($resultset);
  221.         if ($rows 0{
  222.             $row $this->fetch_array($resultset);
  223.             return $row;
  224.         else {
  225.             return false;
  226.         }
  227.     }
  228.     
  229.     /**
  230.       * Perform an SQ query and return the first row
  231.       *
  232.       * This function should be avoided when possible, because there could be jets that do not support
  233.       * SQL quering!
  234.       *
  235.       * @param    string    $text            The query to execute
  236.       * @return bool|array               Returns false in case of error or the data array if successfull
  237.       */    
  238.     public function query_and_get($text{
  239.         $resultset $this->query($text);
  240.         $rows $this->num_rows($resultset);
  241.         if ($rows 0{
  242.             $row $this->fetch_array($resultset);
  243.             return $row;
  244.         else {
  245.             return false;
  246.         }
  247.     }
  248.     
  249.     /**
  250.       * Check if the resultset is empty
  251.       *
  252.       * @return bool                    Returns TRUE if the resultset is empty, or FALSE otherways
  253.       */    
  254.     public function empty_results({
  255.         $rows $this->num_rows();
  256.         return ($rows == 0);
  257.     }
  258.  
  259.     /**
  260.       * Fetch a row from the resultset using an extended field
  261.       *
  262.       * The extended field is a field in the resultset that contains PHP serialized data. Theese
  263.       * data will be automatically expanded and inserted in the returning array
  264.       *
  265.       * @param    int    $mode                [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
  266.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  267.       * @param    string    $extrafield        [Optional] The name of the extra field (defaults to 'extra')
  268.       * @return bool|array               Returns false in case of error or the data array if successfull
  269.       */    
  270.     public function fetch_array_ex($mode=false$resultset=false$extrafield='extra'{
  271.         $row $this->fetch_array($mode$resultset);
  272.         if (isset($row[$extrafield])) {
  273.             if ($row[$extrafield!= ''{
  274.                 $data unserialize($row[$extrafield]);
  275.                 unset($row[$extrafield]);
  276.                 $row array_merge($row$data);
  277.             }
  278.         }
  279.         return $row;
  280.     }
  281.     
  282.     /**
  283.       * Fetch all the rows from the resultset using an extended field
  284.       *
  285.       * The extended field is a field in the resultset that contains PHP serialized data. Theese
  286.       * data will be automatically expanded and inserted in the returning array
  287.       *
  288.       * @param    int    $mode                [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
  289.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  290.       * @param    string    $extrafield        [Optional] The name of the extra field (defaults to 'extra')
  291.       * @return bool|array               Returns false in case of error or the data array if successfull
  292.       */    
  293.     public function fetch_array_all_ex($mode=false$resultset=false$extrafield='extra'{
  294.         $rows=$this->fetch_array_all($mode$resultset);
  295.         foreach ($rows as $id => $row{
  296.             if (isset($row[$extrafield])) {
  297.                 if ($row[$extrafield!= ''{
  298.                     $data unserialize($row[$extrafield]);
  299.                     unset($row[$extrafield]);
  300.                     $rows[$idarray_merge($row$data);
  301.                 }
  302.             }
  303.         }
  304.         return $rows;
  305.     }
  306.     
  307.     /**
  308.       * The maximum number of rows per page in the paging system
  309.       * @internal
  310.       * @var int 
  311.       */
  312.     private $paging_max;
  313.  
  314.     /**
  315.       * Sets to TRUE if the paging procedure is completed
  316.       * @internal
  317.       * @var bool 
  318.       */
  319.     private $paging_done;
  320.  
  321.     /**
  322.       * The total pages in the paging system
  323.       * @internal
  324.       * @var int 
  325.       */
  326.     private $paging_pages;
  327.     
  328.     /**
  329.       * Start the automated data pager
  330.       *
  331.       * The paging system allows you to group the results into pages, quickly and efficiently.
  332.       * In order to use the paging system, you should first call the paging_start() function and then
  333.       * use the paging_fetch_array() function instead of fetch_array()
  334.       *
  335.       * @param    int    $page                The active page (Starts from 0)
  336.       * @param    int    $rows_per_page        The maximum numbers of rows that will be allowed on each page
  337.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  338.       */    
  339.     public function paging_start($page$rows_per_page$resultset=false{
  340.         $count $this->num_rows($resultset);
  341.         $this->paging_done 0;
  342.         $this->paging_max $rows_per_page;
  343.         $this->paging_pages ceil($count $rows_per_page);
  344.         
  345.         $start $page $rows_per_page;
  346.         if ($start $count{
  347.             $start=$count-1;
  348.             $this->paging_done $this->paging_max;
  349.         }
  350.         if ($start 0$start=0;
  351.         
  352.         if ($count 0{
  353.             $this->set_row($start$resultset);
  354.         }
  355.     }
  356.     
  357.     /**
  358.       * Get the total ammount of pages that are used by the pager
  359.       * @return int        The number of pages
  360.       */
  361.     public function num_pages({
  362.         return $this->paging_pages;
  363.     }
  364.     
  365.     /**
  366.       * Fetch a row from the resultset, using the paging mechanism
  367.       *
  368.       * @param    int    $mode                [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
  369.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  370.       * @return bool|array               Returns false in case of error or when reached the end or the data array if successfull
  371.       */    
  372.     public function paging_fetch_array($mode=false$resultset=false{
  373.         $this->paging_done++;
  374.         if ($this->paging_done $this->paging_maxreturn false;
  375.         return $this->fetch_array($mode$resultset);
  376.     }
  377.  
  378.     /**
  379.       * Fetch all the rows of the resultset, using the paging mechanism
  380.       *
  381.       * @param    int    $mode                [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
  382.       * @param    int    $resultset            [Optional] The resultset to use. If not specified, the last action's resultset will be used.
  383.       * @return bool|array               Returns false in case of error or when reached the end or the data array if successfull
  384.       */    
  385.     public function paging_fetch_array_ex($mode=false$resultset=false$extrafield='extra'{
  386.         $this->paging_done++;
  387.         if ($this->paging_done $this->paging_maxreturn false;
  388.         return $this->fetch_array_ex($mode$resultset$extrafield);
  389.     }
  390. }
  391.  
  392. //
  393. // Try to load the appropriate jet handlers to run this database
  394. // and initialize the database store
  395. //
  396.  
  397. class GLDB {
  398.     
  399.     static public function initialize({
  400.         $db GLConfig::$general->database;
  401.         if (is_file(GLConfig::$engine->file('lib/db/jet/db.'.$db['jet'].'.php'))) {
  402.             include_once(GLConfig::$engine->file('lib/db/jet/db.'.$db['jet'].'.php'));
  403.             $db_name $db['jet'].'_jet';
  404.             GL::$db new $db_name();
  405.             $ans GL::$db->init($db['config']);
  406.             if (!$ans{
  407.                 GLError::trigger('Database jet '.$db['jet'].' could not be initialized!');
  408.             }
  409.         else {
  410.             GLError::trigger('Database jet '.$db['jet'].' was not found!');
  411.         }
  412.         
  413.     }
  414.     
  415. }
  416.  
  417. ?>

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