LiveUser_Admin
[ class tree: LiveUser_Admin ] [ index: LiveUser_Admin ] [ all elements ]

Source for file MDB.php

Documentation is available at MDB.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A framework for authentication and authorization in PHP applications
  6.  *
  7.  * LiveUser_Admin is meant to be used with the LiveUser package.
  8.  * It is composed of all the classes necessary to administrate
  9.  * data used by LiveUser.
  10.  *
  11.  * You'll be able to add/edit/delete/get things like:
  12.  * * Rights
  13.  * * Users
  14.  * * Groups
  15.  * * Areas
  16.  * * Applications
  17.  * * Subgroups
  18.  * * ImpliedRights
  19.  *
  20.  * And all other entities within LiveUser.
  21.  *
  22.  * At the moment we support the following storage containers:
  23.  * * DB
  24.  * * MDB
  25.  * * MDB2
  26.  *
  27.  * But it takes no time to write up your own storage container,
  28.  * so if you like to use native mysql functions straight, then it's possible
  29.  * to do so in under a hour!
  30.  *
  31.  * PHP version 4 and 5
  32.  *
  33.  * LICENSE: This library is free software; you can redistribute it and/or
  34.  * modify it under the terms of the GNU Lesser General Public
  35.  * License as published by the Free Software Foundation; either
  36.  * version 2.1 of the License, or (at your option) any later version.
  37.  *
  38.  * This library is distributed in the hope that it will be useful,
  39.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  41.  * Lesser General Public License for more details.
  42.  *
  43.  * You should have received a copy of the GNU Lesser General Public
  44.  * License along with this library; if not, write to the Free Software
  45.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  46.  * MA  02111-1307  USA
  47.  *
  48.  *
  49.  * @category authentication
  50.  * @package LiveUser_Admin
  51.  * @author  Markus Wolff <wolff@21st.de>
  52.  * @author  Helgi Þormar Þorbjörnsson <dufuz@php.net>
  53.  * @author  Lukas Smith <smith@pooteeweet.org>
  54.  * @author  Arnaud Limbourg <arnaud@php.net>
  55.  * @author  Christian Dickmann <dickmann@php.net>
  56.  * @author  Matt Scifo <mscifo@php.net>
  57.  * @author  Bjoern Kraus <krausbn@php.net>
  58.  * @copyright 2002-2006 Markus Wolff
  59.  * @license http://www.gnu.org/licenses/lgpl.txt
  60.  * @version CVS: $Id: MDB.php 213700 2006-05-25 08:20:34Z lsmith $
  61.  * @link http://pear.php.net/LiveUser_Admin
  62.  */
  63.  
  64. /**
  65.  * Require parent class definition.
  66.  */
  67. require_once 'LiveUser/Admin/Storage/SQL.php';
  68. require_once 'MDB.php';
  69.  
  70. /**
  71.  * This is a PEAR::MDB backend driver for the LiveUser class.
  72.  * A PEAR::MDB connection object can be passed to the constructor to reuse an
  73.  * existing connection. Alternatively, a DSN can be passed to open a new one.
  74.  *
  75.  * Requirements:
  76.  * - File "Liveuser/Admin.php" (contains the parent class "LiveUser_Admin")
  77.  * - Array of connection options or a PEAR::MDB connection object must be
  78.  *   passed to the init() method
  79.  *   Example: array('dsn' => 'mysql://user:pass@host/db_name')
  80.  *              OR
  81.  *            array('dbc' => &$conn) ($conn is a PEAR::MDB connection object)
  82.  *
  83.  * @category authentication
  84.  * @package LiveUser_Admin
  85.  * @author  Lukas Smith <smith@pooteeweet.org>
  86.  * @author  Bjoern Kraus <krausbn@php.net>
  87.  * @copyright 2002-2006 Markus Wolff
  88.  * @license http://www.gnu.org/licenses/lgpl.txt
  89.  * @version Release: @package_version@
  90.  * @link http://pear.php.net/LiveUser_Admin
  91.  */
  92. {
  93.     /**
  94.      * Database connection functions
  95.      *
  96.      * @var    object 
  97.      * @access private
  98.      */
  99.     var $function 'connect';
  100.  
  101.     /**
  102.      * Initializes database storage container.
  103.      * Connects to database or uses existing database connection.
  104.      *
  105.      * @param array Storage Configuration
  106.      * @param array containing the database structure (tables, fields, alias)
  107.      * @return bool true on success and false on failure
  108.      *
  109.      * @access public
  110.      */
  111.     function init(&$storageConf$structure)
  112.     {
  113.         parent::init($storageConf$structure);
  114.  
  115.         if (!MDB::isConnection($this->dbc&& !is_null($this->dsn)) {
  116.             $this->options['optimize''portability';
  117.             if ($this->function == 'singleton'{
  118.                 $dbc =MDB::singleton($this->dsn$this->options);
  119.             else {
  120.                 $dbc =MDB::connect($this->dsn$this->options);
  121.             }
  122.             if (PEAR::isError($dbc)) {
  123.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  124.                     array('container' => 'could not connect: '.$dbc->getMessage(),
  125.                     'debug' => $dbc->getUserInfo()));
  126.                 return false;
  127.             }
  128.             $this->dbc =$dbc;
  129.         }
  130.  
  131.         if (!MDB::isConnection($this->dbc)) {
  132.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  133.                 array('container' => 'storage layer configuration missing'));
  134.             return false;
  135.         }
  136.  
  137.         return true;
  138.     }
  139.  
  140.     /**
  141.      * Convert a text value into a DBMS specific format that is suitable to
  142.      * compose query statements.
  143.      *
  144.      * @param string text string value that is intended to be converted.
  145.      * @param string type to which the value should be converted to
  146.      * @return stringtext string that represents the given argument value in
  147.      *        a DBMS specific format.
  148.      *
  149.      * @access public
  150.      * @uses MDB::getValue
  151.      */
  152.     function quote($value$type)
  153.     {
  154.         return $this->dbc->getValue($type$value);
  155.     }
  156.  
  157.     /**
  158.      * Apply a type to all values of an array and return as a comma
  159.      * seperated string useful for generating IN statements
  160.      *
  161.      * @param array data array
  162.      * @param string determines type of the field
  163.      * @return string comma seperated values
  164.      *
  165.      * @access public
  166.      * @uses MDB::getValue
  167.      */
  168.     function implodeArray($array$type)
  169.     {
  170.         if (!is_array($array|| empty($array)) {
  171.             return 'NULL';
  172.         }
  173.         foreach ($array as $value{
  174.             $return[$this->dbc->getValue($type$value);
  175.         }
  176.         return implode(', '$return);
  177.     }
  178.  
  179.     /**
  180.      *  Sets the range of the next query
  181.      *
  182.      * @param string number of rows to select
  183.      * @param string first row to select
  184.      *
  185.      * @return bool true on success and false on failure
  186.      *
  187.      * @access public
  188.      * @uses MDB::setSelectedRowRange
  189.      */
  190.     function setLimit($limit$offset)
  191.     {
  192.         if ($limit || $offset{
  193.             $result $this->dbc->setSelectedRowRange($offset$limit);
  194.             if (PEAR::isError($result)) {
  195.                 $this->stack->push(
  196.                     LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  197.                     array('reason' => $result->getMessage('-' $result->getUserInfo())
  198.                 );
  199.                 return false;
  200.             }
  201.         }
  202.         return true;
  203.     }
  204.  
  205.     /**
  206.      * Execute DML query
  207.      *
  208.      * @param string DML query
  209.      * @return bool|intof the affected rows
  210.      *
  211.      * @access public
  212.      * @uses MDB::query MDB::affectedRows
  213.      */
  214.     function exec($query)
  215.     {
  216.         $result $this->dbc->query($query);
  217.         if (PEAR::isError($result)) {
  218.             $this->stack->push(
  219.                 LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  220.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  221.             );
  222.             return false;
  223.         }
  224.         return $this->dbc->affectedRows();
  225.     }
  226.  
  227.     /**
  228.      * Execute the specified query, fetch the value from the first column of
  229.      * the first row of the result set and then frees the result set.
  230.      *
  231.      * @param string the SELECT query statement to be executed.
  232.      * @param string argument that specifies the expected datatype of the
  233.      *        result set field, so that an eventual conversion may be performed.
  234.      *        The default datatype is text, meaning no conversion is performed.
  235.      * @return bool|string
  236.      *
  237.      * @access public
  238.      * @uses MDB::queryOne
  239.      */
  240.     function queryOne($query$type)
  241.     {
  242.         if (is_array($type)) {
  243.             $type reset($type);
  244.         }
  245.  
  246.         $result $this->dbc->queryOne($query$type);
  247.         if (PEAR::isError($result)) {
  248.             $this->stack->push(
  249.                 LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  250.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  251.             );
  252.             return false;
  253.         }
  254.         return $result;
  255.     }
  256.  
  257.     /**
  258.      * Execute the specified query, fetch the values from the first
  259.      * row of the result set into an array and then frees
  260.      * the result set.
  261.      *
  262.      * @param string the SELECT query statement to be executed.
  263.      * @param array argument that specifies a list of expected datatypes
  264.      *        of theresult set columns, so that the conversions may be performed.
  265.      *        The default datatype is text, meaning no conversion is performed.
  266.      * @return bool|array
  267.      *
  268.      * @access public
  269.      * @uses MDB::queryRow
  270.      */
  271.     function queryRow($query$type)
  272.     {
  273.         $result $this->dbc->queryRow($query$typeMDB_FETCHMODE_ASSOC);
  274.         if (PEAR::isError($result)) {
  275.             $this->stack->push(
  276.                 LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  277.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  278.             );
  279.             return false;
  280.         }
  281.         return $result;
  282.     }
  283.  
  284.     /**
  285.      * Execute the specified query, fetch the value from the first column of
  286.      * each row of the result set into an array and then frees the result set.
  287.      *
  288.      * @param string the SELECT query statement to be executed.
  289.      * @param string argument that specifies the expected datatype of the
  290.      *        result set field, so that an eventual conversion may be performed.
  291.      *        The default datatype is text, meaning no conversion is performed.
  292.      * @return bool|array
  293.      *
  294.      * @access public
  295.      * @uses MDB::queryCol
  296.      */
  297.     function queryCol($query$type)
  298.     {
  299.         if (is_array($type)) {
  300.             $type reset($type);
  301.         }
  302.  
  303.         $result $this->dbc->queryCol($query$type);
  304.         if (PEAR::isError($result)) {
  305.             $this->stack->push(
  306.                 LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  307.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  308.             );
  309.             return false;
  310.         }
  311.         return $result;
  312.     }
  313.  
  314.     /**
  315.      * Execute the specified query, fetch all the rows of the result set into
  316.      * a two dimensional array and then frees the result set.
  317.      *
  318.      * @param string the SELECT query statement to be executed.
  319.      * @param array argument that specifies a list of expected datatypes
  320.      *        of theresult set columns, so that the conversions may be performed.
  321.      *        The default datatype is text, meaning no conversion is performed.
  322.      * @param bool if set to true, returned array will have the first
  323.      *        column as its first dimension
  324.      * @param bool if set to true and $rekey is set to true, then
  325.      *       all values with the same first column will be wrapped in an array
  326.      * @return bool|array
  327.      *
  328.      * @access public
  329.      * @uses MDB::queryAll
  330.      */
  331.     function queryAll($query$types$rekey$group)
  332.     {
  333.         $result $this->dbc->queryAll($query$typesMDB_FETCHMODE_ASSOC$rekeyfalse$group);
  334.         if (PEAR::isError($result)) {
  335.             $this->stack->push(
  336.                 LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  337.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  338.             );
  339.             return false;
  340.         }
  341.         return $result;
  342.     }
  343.  
  344.     /**
  345.      * returns the next free id of a sequence
  346.      *
  347.      * @param string name of the sequence
  348.      * @param bool when true the seqence is
  349.      *                            automatic created, if it not exists
  350.      * @return bool|intfalse on failure or next id for the table
  351.      *
  352.      * @access public
  353.      * @uses MDB::nextId
  354.      */
  355.     function nextId($seqname$ondemand = true)
  356.     {
  357.         $result $this->dbc->nextId($seqname$ondemand);
  358.         if (PEAR::isError($result)) {
  359.             $this->stack->push(
  360.                 LIVEUSER_ADMIN_ERROR_QUERY_BUILDER'exception',
  361.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  362.             );
  363.             return false;
  364.         }
  365.         return $result;
  366.     }
  367.  
  368.     /**
  369.      * Since MDB does not support determining if auto increment is supported,
  370.      * the call is redirected to nextID()
  371.      *
  372.      * @param string name of the table into which a new row was inserted
  373.      * @param string name of the field into which a new row was inserted
  374.      * @param bool when true the seqence is automatic created, if it not exists
  375.      * @return bool|int
  376.      *
  377.      * @access public
  378.      * @uses MDB2::nextId MDB2_Extended::getBeforeId
  379.      */
  380.     function getBeforeId($table$field$ondemand = true)
  381.     {
  382.         $seq $table.(empty($field'' '_'.$field);
  383.         return $this->nextId($seq$ondemand);
  384.     }
  385.  
  386.     /**
  387.      * Since MDB does not support determining if auto increment is supported,
  388.      * the call just returns the $id parameter
  389.      *
  390.      * @param string value as returned by getBeforeId()
  391.      * @param string name of the table into which a new row was inserted
  392.      * @param string name of the field into which a new row was inserted
  393.      * @return  bool|intreturns the id that the users passed via params
  394.      *
  395.      * @access public
  396.      * @uses MDB2_Extended::getAfterId
  397.      */
  398.     function getAfterId($id$table$field)
  399.     {
  400.         return $id;
  401.     }
  402. }
  403. ?>

Documentation generated on Tue, 05 Oct 2010 16:30:10 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.