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

Source for file mcpear_class.php

Documentation is available at mcpear_class.php

  1. <?php
  2. /**
  3.  * Simple PEAR DB wrapper class.
  4.  * This file contains the {@link mcpear} class. It is based on the standard
  5.  * PHP PEAR DB library. For more information, see {@link http://pear.php.net}
  6.  * @package HRDIODB
  7.  */
  8.  
  9. /**
  10.  * This file is part of HRDIODB.
  11.  *
  12.  * HRDIODB is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * HRDIODB is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with HRDIODB; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25.  */
  26.  
  27. /** 
  28.  * A PEAR DB based class for interacting with a SQL database.
  29.  * Generic PEAR DB function wrappers used for various DB operations. PEAR is
  30.  * a very handy and easy to use library, but having some wrappers makes using
  31.  * it even easier and faster. This also allows for central error checking and
  32.  * handy utility functions. For more information about PEAR, please see
  33.  * {@link http://pear.php.net}.
  34.  * @package HRDIODB
  35.  */
  36. class mcpear {
  37.   /**
  38.    * Constructor.
  39.    * Establishes database connection.
  40.    * @param string $dsn Data Source Name
  41.    */
  42.   function mcpear ($dsn FALSE{
  43.     $this->dsn = $dsn;
  44.     $this->db $this->db_connect();
  45.  
  46.     if ($this->db{
  47.       // Set default fetch mode to associative
  48.       $this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  49.     else {
  50.       exit(1);
  51.     }
  52.   }
  53.   
  54.   /**
  55.    * Initializes a PEAR database connection.
  56.    * Textbook PEAR database connection function.
  57.    * @return mixed PEAR DB Database connection
  58.    */
  59.   function db_connect ({
  60.     if(!$this->dsn){
  61.       return error("Your DB class constructor must set \$this->dsn");
  62.     }
  63.  
  64.     /* Include the PEAR DB library. It should be available with every PHP
  65.        installation, unless it was specifically excluded. On package-based
  66.        Linux distributions, check to see if the PHP PEAR package is installed.
  67.        For more information, see http://pear.php.net */
  68.     require_once('DB.php');
  69.       
  70.     // Initialize the PEAR connection
  71.     $db DB::connect($this->dsn);
  72.  
  73.     // Check to make sure we didn't encounter an error
  74.     return $this->verify($db);
  75.   }
  76.                                                                                                    
  77.   /**
  78.    * General database query method.
  79.    * Textbook PEAR database query. We use getuserinfo() for error
  80.    * information.
  81.    * @param string $sql SQL query
  82.    * @return mixed Query result resource or FALSE on error
  83.    */
  84.   function db_query ($sql){
  85.     $q =$this->db->query($sql);
  86.     return $this->verify($q);
  87.   }
  88.  
  89.   /**
  90.    * Retrieves a row of data.
  91.    * Returns a row of data for the given SQL query.
  92.    * @param string $sql SQL query
  93.    * @return mixed Row of data or FALSE on error
  94.    */
  95.   function get_row ($sql{
  96.     $row =$this->db->getRow($sql);
  97.     return $this->verify($row);
  98.   }
  99.  
  100.   /**
  101.    * Retrieves a list of data.
  102.    * Returns a list of data for the given SQL query.
  103.    * @param string $sql SQL query
  104.    * @return mixed List of results or FALSE on error
  105.    */
  106.   function get_list ($sql{
  107.     $results =$this->db->getAll($sql);
  108.     return $this->verify($results);
  109.   }
  110.  
  111.   /**
  112.    * Retrieves a column of data.
  113.    * Retrieves a column of data composed of the first row of the results for
  114.    * the given SQL query.
  115.    * @param string $sql SQL query
  116.    * @return mixed Column of results or FALSE on error
  117.    */
  118.   function get_column ($sql{
  119.     $results =$this->db->getCol($sql);
  120.     return $this->verify($results);
  121.   
  122.  
  123.   /**
  124.    * Retrieves a single value.
  125.    * Retrieves the value of the first result for the given SQL query.
  126.    * @param string $sql SQL query
  127.    * @return mixed Value of the first result or FALSE on error
  128.    */
  129.   function get_one ($sql{
  130.     $results =$this->db->getOne($sql);
  131.     return $this->verify($results);
  132.   }
  133.   
  134.   /**
  135.    * Retrieves an array of field names.
  136.    * Retrieves an array of field names in a given table.
  137.    * @param string $table SQL query
  138.    * @return mixed An array of field names for a given table or FALSE on error
  139.    */
  140.   function get_field_names ($table{
  141.     $results $this->db->tableInfo($table);
  142.     $tableinfo $this->verify($results);
  143.     if($tableinfo){
  144.       foreach($tableinfo as $t){
  145.         $field_names[$t['name'];
  146.       }
  147.       return $field_names;
  148.     }
  149.     return FALSE;
  150.   }
  151.  
  152.   /**
  153.    * Returns table row count.
  154.    * Returns the total number of rows in a given table.
  155.    * @param string $table Table name
  156.    * @return mixed Number of rows in the table or FALSE on error
  157.    */
  158.   function get_total ($table{
  159.     $sql "SELECT COUNT(*)
  160.             AS total
  161.             FROM $table"
  162.     $total =$this->db->getOne($sql);
  163.     return $this->verify($total);
  164.   }
  165.   
  166.   /**
  167.    * Returns table contents.
  168.    * Returns an array of associative arrays for the given SQL query.
  169.    * @param string $table Table name
  170.    * @return mixed Associative array of results or FALSE on error
  171.    */
  172.   function get_table ($table{
  173.     $sql "SELECT *
  174.             FROM $table"
  175.     return $this->get_list($sql);
  176.   }
  177.  
  178.   /**
  179.    * Returns next insert id.
  180.    * Returns the next insert id for the sequence $name. Depending on the
  181.    * database you're using, native sequences may or may not exist. PEAR tries
  182.    * to handle things equally by using native sequences in databases which
  183.    * support them, or creating sequence tables in those who don't. By default,
  184.    * PEAR will add the string "_seq" to any sequence name. Keep that in mind
  185.    * when asking it for the next sequence number.
  186.    * @param string $name The name of the sequence
  187.    * @return integer Next id sequence number
  188.    */
  189.   function next_id ($name{
  190.     $id =$this->db->nextId($name);
  191.     return $this->verify($id);
  192.   }
  193.  
  194.   /**
  195.    * Adds a table row.
  196.    * Adds a tabe row to the specific $table using the keys of the $values
  197.    * array as field names and the values as values for those fields.
  198.    * @param array $values Associative array of keys and values
  199.    * @param string $table Table name
  200.    * @return mixed Query result resource or FALSE on error
  201.    */
  202.   function add ($values$table{
  203.     if (is_array($values)) {
  204.       // Unset any values which should be NULL
  205.       foreach($values as $key=>$value){
  206.         if($value == ""){
  207.           unset($values[$key]);
  208.         }
  209.       }
  210.  
  211.       // Auto-execute the INSERT query
  212.       $q $this->db->autoExecute($table$valuesDB_AUTOQUERY_INSERT);
  213.       return $this->verify($q);
  214.     else {
  215.       return error("Add expects an array of data");
  216.     }
  217.   }
  218.  
  219.   /**
  220.    * Updates table rows.
  221.    * Updates table rows for the specific $table using the keys of the $values
  222.    * array as field names and the values as values for those fields.
  223.    * @param array $values Associative array of keys and values
  224.    * @param string $table Table name
  225.    * @param string $where_key A key into the $values array used for the WHERE
  226.    *  clause
  227.    * @return mixed Query result resource or FALSE on error
  228.    */
  229.   function update ($values$table$where_key{
  230.     // Create the WHERE clause
  231.     $update_key "$where_key = '{$values[$where_key]}'";
  232.       
  233.     // Set Null any values that say NULL
  234.       foreach($values as $key=>$value){
  235.         if($value == "NULL"){
  236.           $values[$key]=NULL;
  237.         }
  238.       }
  239.     
  240.     // Auto-execute the UPDATE query
  241.     $q $this->db->autoExecute($table$valuesDB_AUTOQUERY_UPDATE$update_key);
  242.     return $this->verify($q);
  243.   }
  244.  
  245.   /**
  246.    * Deletes table rows.
  247.    * Deletes table rows from a specific $table using the value of the $values
  248.    * array's $where_key key as field name and value as value for the WHERE
  249.    * clause.
  250.    * @param array $values Associative array of keys and values
  251.    * @param string $table Table name
  252.    * @param string $where_key A key into the $values array used for the WHERE
  253.    *  clause
  254.    * @return mixed Query result resource or FALSE on error
  255.    */
  256.   function delete ($values$table$where_key{
  257.     $sql "DELETE FROM $table
  258.             WHERE $where_key = '{$values[$where_key]}'";
  259.     return $this->db_query($sql);
  260.   }
  261.  
  262.   /**
  263.    * Returns the result or prints an error.
  264.    * This function is meant to be used in the 'return' statement of all mcpear
  265.    * functions. It checks to see if there is a valid result or a DB error. If
  266.    * the result is valid, it is returned. If there is a DB error, it is printed
  267.    * and FALSE is returned.
  268.    * @param mixed $result DB result
  269.    * @return mixed Query result or FALSE on error
  270.    */
  271.   function &verify (&$result{
  272.     if (DB::isError($result)){
  273.       return error($result->getMessage());
  274.     else {
  275.       return $result;
  276.     }
  277.   }
  278. }
  279. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:49 -0500 by phpDocumentor 1.3.0RC6