go-database
[ class tree: go-database ] [ index: go-database ] [ all elements ]

Source for file base_db.class.inc.php

Documentation is available at base_db.class.inc.php

  1. <?php
  2. /**
  3.  * Copyright Intermesh
  4.  *
  5.  * This file is part of Group-Office. You should have received a copy of the
  6.  * Group-Office license along with Group-Office. See the file /LICENSE.TXT
  7.  *
  8.  * If you have questions write an e-mail to info@intermesh.nl
  9.  *
  10.  * @copyright Copyright Intermesh
  11.  * @version $Id: about.php 1088 2008-10-07 13:02:06Z mschering $
  12.  * @author Merijn Schering <mschering@intermesh.nl>
  13.  */
  14.  
  15. /**
  16.  * Class that connects to MySQL using the MySQLi extension
  17.  *
  18.  * @version $Id: imap.class.inc 1201 2008-10-22 18:23:34Z mschering $
  19.  * @author Merijn Schering <mschering@intermesh.nl>
  20.  * @package go.database
  21.  * @access public
  22.  */
  23.  
  24. class base_db{
  25.  
  26.     /**
  27.      * The database host
  28.      *
  29.      * @var string 
  30.      */
  31.     var $host = "";
  32.  
  33.     /**
  34.      * The database name
  35.      *
  36.      * @var string 
  37.      */
  38.     var $database = "";
  39.  
  40.     /**
  41.      * The database username
  42.      *
  43.      * @var string 
  44.      */
  45.     var $user = "";
  46.  
  47.     /**
  48.      * The database password
  49.      *
  50.      * @var string 
  51.      */
  52.     var $password = "";
  53.  
  54.     /**
  55.      * Set to true for debugging messages.
  56.      *
  57.      * @var bool 
  58.      */
  59.     var $debug = false;
  60.  
  61.     /**
  62.      * "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
  63.      *
  64.      * @var string 
  65.      */
  66.     var $halt_on_error = "yes";
  67.  
  68.     /**
  69.      * The sequence table to use for autoincrementing numbers.
  70.      *
  71.      * @var unknown_type 
  72.      */
  73.     var $seq_table = "go_db_sequence";
  74.  
  75.     /**
  76.      * The current record from a select query
  77.      *
  78.      * @var array 
  79.      */
  80.     var $record = array();
  81.  
  82.     /**
  83.      * The current row index when waling through a result set
  84.      *
  85.      * @var unknown_type 
  86.      */
  87.     var $row;
  88.  
  89.     /**
  90.      * Database error number
  91.      *
  92.      * @var int 
  93.      */
  94.     var $errno = 0;
  95.  
  96.     /**
  97.      * Database error message
  98.      *
  99.      * @var string 
  100.      */
  101.     var $error = "";
  102.  
  103.     /**
  104.      * Type of database connector
  105.      *
  106.      * @var string 
  107.      */
  108.     var $type = "mysqli";
  109.  
  110.     /**
  111.      * The database connection link identifier
  112.      *
  113.      * @var resource 
  114.      */
  115.     var $link = false;
  116.  
  117.     /**
  118.      * The result object from a query
  119.      *
  120.      * @var resource 
  121.      */
  122.     var $result = false;
  123.  
  124.     /**
  125.      * True when a table is locked
  126.      *
  127.      * @var bool 
  128.      */
  129.     var $locked = false;
  130.  
  131.     /**
  132.      * Constructor a config object with db_host, db_pass, db_user and db_name
  133.      * may be passed so it can connect to a different database then the default.
  134.      *
  135.      * @param unknown_type $config 
  136.      * @return db 
  137.      */
  138.     public function __construct($config=null)
  139.     {
  140.         $this->set_config($config);
  141.     }
  142.  
  143.     /**
  144.      * Set's the connection parameters. A config object with db_host, db_pass, db_user and db_name
  145.      * may be passed so it can connect to a different database then the default.
  146.      *
  147.      * @param object $config 
  148.      */
  149.  
  150.     public function set_config($config=null)
  151.     {
  152.         global $GO_CONFIG;
  153.             
  154.         if(!isset($config&& isset($GO_CONFIG))
  155.         {
  156.             $config $GO_CONFIG;
  157.         }
  158.             
  159.         if(isset($config))
  160.         {
  161.             if (isset($config->db_host)) {
  162.                 $this->host = $config->db_host;
  163.             }
  164.             if (isset($config->db_name)) {
  165.                 $this->database = $config->db_name;
  166.             }
  167.             if (isset($config->db_user)) {
  168.                 $this->user = $config->db_user;
  169.             }
  170.             if (isset($config->db_pass)) {
  171.                 $this->password = $config->db_pass;
  172.             }
  173.         }
  174.     }
  175.     
  176.     /**
  177.      * Set the connection parameters manually
  178.      *
  179.      * @param string $host 
  180.      * @param string $database 
  181.      * @param string $user 
  182.      * @param string $pass 
  183.      */
  184.     
  185.     public function set_parameters($host$database$user$pass)
  186.     {
  187.         $this->host = $host;
  188.         $this->database = $database;
  189.         $this->user = $user;
  190.         $this->password = $pass;
  191.     }
  192.  
  193.     /**
  194.      * Connnects to the database
  195.      *
  196.      * @return resource The connection link identifier
  197.      */
  198.  
  199.     public function connect()
  200.     {
  201.     }
  202.  
  203.     /**
  204.      * Frees the memory associated with a result
  205.      * return void
  206.      */
  207.     function free({
  208.     }
  209.  
  210.     /**
  211.      * Queries the database
  212.      *
  213.      * @param string $sql 
  214.      * @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
  215.      * @param mixed $params If a single or an array of parameters are given in the statement will be prepared
  216.      *
  217.      * @return object The result object
  218.      */
  219.     public function query($sql$types=''$params=array())
  220.     {
  221.     }
  222.  
  223.     /**
  224.      * Returns the number of rows found when you have used
  225.      * SELECT SQL_CALC_FOUND_ROWS
  226.      *
  227.      * @return unknown 
  228.      */
  229.  
  230.     public function found_rows(){
  231.     }
  232.  
  233.  
  234.     /**
  235.      * Walk the reseult set from a select query
  236.      *
  237.      * @param int $result_type DB_ASSOC, DB_BOTH or DB_NUM
  238.      * @return unknown 
  239.      */
  240.     public function next_record($result_type=DB_ASSOC{
  241.     }
  242.  
  243.  
  244.     /**
  245.      * Lock a table
  246.      *
  247.      * @param string $table 
  248.      * @param string $mode Modes are: "read", "read local", "write", "low priority write"
  249.      * @return unknown 
  250.      */
  251.     public function lock($table$mode "write"{
  252.         $query "lock tables ";
  253.         if(is_array($table)) {
  254.             while(list($key,$valueeach($table)) {
  255.                 if(is_int($key)) $key $mode;
  256.                 if(strpos($value",")) {
  257.                     $query .= str_replace(","" $key"$value" $key";
  258.                 else {
  259.                     $query .= "$value $key";
  260.                 }
  261.             }
  262.             $query substr($query0-2);
  263.         elseif(strpos($table",")) {
  264.             $query .= str_replace(","" $mode"$table" $mode";
  265.         else {
  266.             $query .= "$table $mode";
  267.         }
  268.         if(!$this->query($query)) {
  269.             $this->halt("lock() failed.");
  270.             return false;
  271.         }
  272.         $this->locked = true;
  273.         return true;
  274.     }
  275.  
  276.     /**
  277.      * Unlock tables
  278.      *
  279.      * @return bool True on success
  280.      */
  281.  
  282.     public function unlock({
  283.         // set before unlock to avoid potential loop
  284.         $this->locked = false;
  285.  
  286.         if(!$this->query("unlock tables")) {
  287.             $this->halt("unlock() failed.");
  288.             return false;
  289.         }
  290.         return true;
  291.     }
  292.  
  293.     /**
  294.      * Fetch a single field from a result record
  295.      *
  296.      * @param string $name Field name or index
  297.      * @return mixed the field value
  298.      */
  299.  
  300.     public function f($name{
  301.         if (isset($this->record[$name])) {
  302.             return $this->record[$name];
  303.         }
  304.     }
  305.  
  306.     /**
  307.      * Print a single field from a result record.
  308.      *
  309.      * @param string $name Field name or index
  310.      * @return mixed the field value
  311.      */
  312.     public function p($name{
  313.         if (isset($this->record[$name])) {
  314.             print $this->record[$name];
  315.         }
  316.     }
  317.  
  318.     /**
  319.      * Get a next unique ID for a table. Used instead of auto increment
  320.      * so that we have better support for different database backends.
  321.      *
  322.      * @param string $seq_name 
  323.      * @return int the next unique ID
  324.      */
  325.     public function nextid($seq_name{
  326.         /* if no current lock, lock sequence table */
  327.         if(!$this->locked{
  328.             if($this->lock($this->seq_table)) {
  329.                 $locked true;
  330.             else {
  331.                 $this->halt("cannot lock ".$this->seq_table." - has it been created?");
  332.                 return 0;
  333.             }
  334.         }
  335.  
  336.         /* get sequence number and increment */
  337.         $q sprintf("select nextid from %s where seq_name = '%s'",
  338.         $this->seq_table,
  339.         $seq_name);
  340.         if(!$this->query($q)) {
  341.             $this->halt('query failed in nextid: '.$q);
  342.             return 0;
  343.         }
  344.  
  345.         /* No current value, make one */
  346.         if(!$this->next_record()) {
  347.             $currentid 0;
  348.             $q sprintf("insert into %s values('%s', %s)",
  349.             $this->seq_table,
  350.             $seq_name,
  351.             $currentid);
  352.             if(!$this->query($q)) {
  353.                 $this->halt('query failed in nextid: '.$q);
  354.                 return 0;
  355.             }
  356.         else {
  357.             $currentid $this->f("nextid");
  358.         }
  359.         $nextid $currentid 1;
  360.         $q sprintf("update %s set nextid = '%s' where seq_name = '%s'",
  361.         $this->seq_table,
  362.         $nextid,
  363.         $seq_name);
  364.         if(!$this->query($q)) {
  365.             $this->halt('query failed in nextid: '.$q);
  366.             return 0;
  367.         }
  368.  
  369.         /* if nextid() locked the sequence table, unlock it */
  370.         if($locked{
  371.             $this->unlock();
  372.         }
  373.  
  374.         return $nextid;
  375.     }
  376.  
  377.     /**
  378.      * Return the number of rows found in the last select statement
  379.      *
  380.      * @return int Number of rows
  381.      */
  382.  
  383.     public function num_rows({
  384.     }
  385.  
  386.     /**
  387.      * Gets the number of affected rows in a previous MySQL operatio
  388.      *
  389.      * @return int 
  390.      */
  391.     function affected_rows({
  392.     }
  393.  
  394.  
  395.     /**
  396.      * Get the number of fields in a result
  397.      *
  398.      * @return int 
  399.      */
  400.     function num_fields({
  401.     }
  402.  
  403.     /**
  404.      * Updates a row from a table
  405.      *
  406.      * @param string $table The table name
  407.      * @param string $index The field name to select on ( WHERE '$index'=1). This field must be present in the $fields parameter
  408.      * @param array $fields An associative array with fieldname=>value
  409.      * @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
  410.      * @param bool $trim Trim values in the array
  411.      * @return bool 
  412.      */
  413.  
  414.     public function update_row($table$index$fields$types=''$trim=true)
  415.     {
  416.         if(!is_array($fields))
  417.         {
  418.             $this->halt('Invalid update row called');
  419.             return false;
  420.         }
  421.         if(!is_array($index))
  422.         {
  423.             $index array($index);
  424.         }
  425.         if(empty($types))
  426.         {
  427.             $types str_repeat('s'count($fields));
  428.         }
  429.  
  430.         $field_types='';
  431.         $index_types='';
  432.         $count=0;
  433.         $indexes=array();
  434.  
  435.         foreach($fields as $key => $value)
  436.         {
  437.             if(!in_array($key$index))
  438.             {
  439.                 $updates["`$key`=?";
  440.                 $field_values[$value;
  441.                 $field_types.=$types[$count];
  442.             }else
  443.             {
  444.                 $indexes[]="`$key`=?";
  445.                 $index_types.=$types[$count];
  446.                 $index_values[$value;
  447.             }
  448.             $count++;
  449.         }
  450.         if(isset($updates))
  451.         {
  452.             $sql "UPDATE `$table` SET ".implode(',',$updates)." WHERE ".implode(' AND '$indexes);
  453.                 
  454.             foreach($index_values as $index_value)
  455.             {
  456.                 $field_values[]=$index_value;
  457.             }
  458.             $field_types .= $index_types;
  459.  
  460.             if(!$this->query($sql$field_types$field_values))
  461.             {
  462.                 if($this->halt_on_error=='yes')
  463.                 {
  464.                     throw new DatabaseUpdateException();
  465.                 }
  466.             }else
  467.             {
  468.                 return true;
  469.             }
  470.         }
  471.             
  472.         return false;
  473.     }
  474.  
  475.     /**
  476.      * Inserts a row in a table
  477.      *
  478.      * @param string $table The table name
  479.      * @param array $fields An associative array with fieldname=>value
  480.      * @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
  481.      * @param bool $trim Trim values in the array
  482.      * @return bool 
  483.      */
  484.  
  485.     public function insert_row($table$fields$types=''$trim=true$replace=false)
  486.     {
  487.         if(!is_array($fields))
  488.         {
  489.             $this->halt('Invalid insert row called');
  490.             return false;
  491.         }
  492.  
  493.         foreach($fields as $key => $value)
  494.         {
  495.             $field_names[$key;
  496.             $field_values[$value;
  497.         }
  498.         if(isset($field_names))
  499.         {
  500.             $sql $replace 'REPLACE' 'INSERT';
  501.             $sql .= " INTO `$table` (`".implode('`,`'$field_names)."`) VALUES ".
  502.                       "(".str_repeat('?,'count($field_values)-1)."?)";
  503.                 
  504.             if(empty($types))
  505.             {
  506.                 $types str_repeat('s',count($field_values));
  507.             }
  508.                 
  509.             if(!$this->query($sql$types$field_values))
  510.             {
  511.                 if($this->halt_on_error=='yes')
  512.                 {
  513.                     throw new DatabaseInsertException();
  514.                 }
  515.             }else
  516.             {
  517.                 return true;
  518.             }
  519.         }else
  520.         {
  521.             throw new DatabaseInsertException();
  522.         }            
  523.     }
  524.  
  525.     /**
  526.      * Replaces a row in a table
  527.      *
  528.      * @param string $table The table name
  529.      * @param string $index The field name to select on ( WHERE '$index'=1). This field must be present in the $fields parameter
  530.      * @param array $fields An associative array with fieldname=>value
  531.      * @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
  532.      * @param bool $trim Trim values in the array
  533.      * @return bool 
  534.      */
  535.  
  536.     public function replace_row($table$fields$types$trim=true)
  537.     {
  538.         return $this->insert_row($table$fields$types,$trimtrue);
  539.     }
  540.  
  541.     /**
  542.      * Escapes a value to make it safe to send to MySQL
  543.      *
  544.      * @param mixed $value 
  545.      * @param bool $trim Trim the value
  546.      * @return mixed the escaped value.
  547.      */
  548.     public function escape($value$trim=true)
  549.     {
  550.     }
  551.  
  552.     /**
  553.      * Returns the auto generated id used in the last query
  554.      *
  555.      * @return int 
  556.      */
  557.     public function insert_id()
  558.     {
  559.     }
  560.  
  561.     /**
  562.      * Sets the error and errno property
  563.      *
  564.      * @return void 
  565.      */
  566.     protected function set_error()
  567.     {
  568.  
  569.     }
  570.  
  571.     /**
  572.      * Halts when an error occurs
  573.      *
  574.      * @param unknown_type $msg 
  575.      */
  576.     protected function halt($msg{
  577.  
  578.         $this->set_error();
  579.  
  580.         if ($this->locked{
  581.             $this->unlock();
  582.         }
  583.  
  584.         go_log(LOG_DEBUGsprintf("<b>Database error:</b> %s<br>\n<b>MySQL Error</b>: %s (%s)<br>\n",
  585.         $msg,
  586.         $this->errno,
  587.         $this->error));
  588.  
  589.         if($this->halt_on_error=='yes')
  590.         {
  591.             throw new Exception(sprintf("<b>Database error:</b> %s<br>\n<b>MySQL Error</b>: %s (%s)<br>\n",
  592.             $msg,
  593.             $this->errno,
  594.             $this->error));
  595.         }elseif($this->halt_on_error=='report')
  596.         {
  597.             echo sprintf("<b>Database error:</b> %s<br>\n<b>MySQL Error</b>: %s (%s)<br>\n",
  598.             $msg,
  599.             $this->errno,
  600.             $this->error);
  601.         }
  602.     }
  603.  
  604. }

Documentation generated on Thu, 30 Oct 2008 14:13:23 +0100 by phpDocumentor 1.4.0