noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_noalyss_sql.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  *   This file is part of NOALYSS.
00005  *
00006  *   NOALYSS is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   NOALYSS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with NOALYSS; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00022 
00023 /**
00024  * @file
00025  * @brief this wrapper is used to created easily a wrapper to a table
00026  *
00027  * @class
00028  * Match a table into an object, you need to add the code for each table
00029  * @note : the primary key must be an integer
00030  *
00031  * @code
00032   class table_name_sql extends Noalyss_SQL
00033   {
00034 
00035   function __construct($p_id=-1)
00036   {
00037   $this->table = "schema.table";
00038   $this->primary_key = "o_id";
00039 
00040   $this->name=array(
00041   "id"=>"o_id",
00042   "dolibarr"=>"o_doli",
00043   "date"=>"o_date",
00044   "qcode"=>"o_qcode",
00045   "fiche"=>"f_id",
00046 
00047 
00048   );
00049 
00050   $this->type = array(
00051   "o_id"=>"numeric",
00052   "o_doli"=>"numeric",
00053   "o_date"=>"date",
00054   "o_qcode"=>"text",
00055   "f_id"=>"numeric",
00056 
00057   );
00058 
00059   $this->default = array(
00060   "o_id" => "auto",
00061   );
00062   $this->date_format = "DD.MM.YYYY";
00063   global $cn;
00064 
00065   parent::__construct($cn,$p_id);
00066   }
00067 
00068   }
00069  * @endcode
00070  *
00071  */
00072 abstract class Noalyss_SQL
00073 {
00074 
00075     function __construct(&$p_cn, $p_id=-1)
00076     {
00077         $this->cn=$p_cn;
00078         $pk=$this->primary_key;
00079         $this->$pk=$p_id;
00080 
00081         /* Initialize an empty object */
00082         foreach ($this->name as $key)
00083         {
00084             $this->$key=null;
00085         }
00086         $this->$pk=$p_id;
00087         /* load it */
00088         if ($p_id != -1 )$this->load();
00089     }
00090 /**
00091  * Insert or update : if the row already exists, update otherwise insert
00092  */
00093     public function save()
00094     {
00095         $pk=$this->primary_key;
00096         $count=$this->cn->get_value('select count(*) from '.$this->table.' where '.$this->primary_key.'=$1',array($this->$pk));
00097         
00098         if ($count == 0)
00099             $this->insert();
00100         else
00101             $this->update();
00102     }
00103 
00104     public function getp($p_string)
00105     {
00106         if (array_key_exists($p_string, $this->name)) {
00107             $idx=$this->name[$p_string];
00108             return $this->$idx;
00109         }
00110         else
00111             throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
00112     }
00113 
00114     public function setp($p_string, $p_value)
00115     {
00116         if (array_key_exists($p_string, $this->name))    {
00117             $idx=$this->name[$p_string];
00118             $this->$idx=$p_value;
00119         }        else
00120             throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
00121     }
00122 
00123     public function insert()
00124     {
00125         $this->verify();
00126         $sql="insert into ".$this->table." ( ";
00127         $sep="";
00128         $par="";
00129         $idx=1;
00130         $array=array();
00131         foreach ($this->name as $key=> $value)
00132         {
00133             if (isset($this->default[$value])&&$this->default[$value]=="auto"&&$this->$value==null)
00134                 continue;
00135             if ($value==$this->primary_key&&$this->$value==-1)
00136                 continue;
00137             $sql.=$sep.$value;
00138             switch ($this->type[$value])
00139             {
00140                 case "date":
00141                     if ($this->date_format=="")
00142                         throw new Exception('Format Date invalide');
00143                     $par .=$sep.'to_date($'.$idx.",'".$this->date_format."')";
00144                     break;
00145                 default:
00146                     $par .= $sep."$".$idx;
00147             }
00148 
00149             $array[]=$this->$value;
00150             $sep=",";
00151             $idx++;
00152         }
00153         $sql.=") values (".$par.") returning ".$this->primary_key;
00154         $pk=$this->primary_key;
00155         $this->$pk=$this->cn->get_value($sql, $array);
00156     }
00157 
00158     public function delete()
00159     {
00160         $pk=$this->primary_key;
00161         $sql=" delete from ".$this->table." where ".$this->primary_key."= $1";
00162         $this->cn->exec_sql($sql,array($this->$pk));
00163     }
00164 
00165     public function update()
00166     {
00167         $this->verify();
00168         $pk=$this->primary_key;
00169         $sql="update ".$this->table."  ";
00170         $sep="";
00171         $idx=1;
00172         $array=array();
00173         $set=" set ";
00174         foreach ($this->name as $key=> $value)        {
00175             if (isset($this->default[$value])&&$this->default[$value]=="auto")
00176                 continue;
00177             switch ($this->type[$value])
00178             {
00179                 case "date":
00180                     $par=$value.'=to_date($'.$idx.",'".$this->date_format."')";
00181                     break;
00182                 default:
00183                     $par=$value."= $".$idx;
00184             }
00185             $sql.=$sep." $set ".$par;
00186             $array[]=$this->$value;
00187             $sep=",";
00188             $set="";
00189             $idx++;
00190         }
00191         $array[]=$this->$pk;
00192         $sql.=" where ".$this->primary_key." = $".$idx;
00193         $this->cn->exec_sql($sql, $array);
00194     }
00195 
00196     public function load()
00197     {
00198         $sql=" select ";
00199         $sep="";
00200         foreach ($this->name as $key)       {
00201             switch ($this->type[$key])
00202             {
00203                 case "date":
00204                     $sql .= $sep.'to_char('.$key.",'".$this->date_format."') as ".$key;
00205                     break;
00206                 default:
00207                     $sql.=$sep.$key;
00208             }
00209             $sep=",";
00210         }
00211         $pk=$this->primary_key;
00212         $sql.=" from ".$this->table;
00213         
00214         $sql.=" where ".$this->primary_key." = $1";
00215        
00216         $result=$this->cn->get_array($sql,array ($this->$pk));
00217         if ($this->cn->count()==0)
00218         {
00219             $this->$pk=-1;
00220             return;
00221         }
00222 
00223         foreach ($result[0] as $key=> $value)
00224         {
00225             $this->$key=$value;
00226         }
00227     }
00228 
00229     public function get_info()
00230     {
00231         return var_export($this, true);
00232     }
00233 /**
00234  * @todo ajout vérification type (date, text ou numeric)
00235  * @return int
00236  */
00237     public function verify()
00238     {
00239         foreach ($this->name as $key)
00240         {
00241             if (trim($this->$key)=='')
00242                 $this->$key=null;
00243         }
00244         return 0;
00245     }
00246 
00247     /**
00248      * Transform an array into object
00249      * @param type $p_array
00250      * @return object
00251      */
00252     public function from_array($p_array)
00253     {
00254         foreach ($this->name as $key=> $value)
00255         {
00256             if (isset($p_array[$value]))
00257             {
00258                 $this->$value=$p_array[$value];
00259             }
00260             else
00261             {
00262                 $this->$value=null;
00263             }
00264         }
00265         return $this;
00266     }
00267 
00268     /**
00269      * @brief retrieve array of object thanks a condition
00270      * @param $cond condition (where clause) (optional by default all the rows are fetched)
00271      * you can use this parameter for the order or subselect
00272      * @param $p_array array for the SQL stmt
00273      * @see Database::exec_sql get_object  Database::num_row
00274      * @return the return value of exec_sql
00275      */
00276     function seek($cond='', $p_array=null)
00277     {
00278         $sql="select * from ".$this->table."  $cond";
00279         $ret=$this->cn->exec_sql($sql, $p_array);
00280         return $ret;
00281     }
00282 
00283     /**
00284      * get_seek return the next object, the return of the query must have all the column
00285      * of the object
00286      * @param $p_ret is the return value of an exec_sql
00287      * @param $idx is the index
00288      * @see seek
00289      * @return object
00290      */
00291     public function next($ret, $i)
00292     {
00293         $array=$this->cn->fetch_array($ret, $i);
00294         return $this->from_array($array);
00295     }
00296 
00297     /**
00298      * @see next
00299      */
00300     public function get_object($p_ret, $idx)
00301     {
00302         return $this->next($p_ret, $idx);
00303     }
00304 
00305     /**
00306      * @brief return an array of objects. Do not use this function if they are too many objects, it takes a lot of memory,
00307      * and could slow down your application.
00308      * @param $cond condition, order...
00309      * @param $p_array array to use for a condition
00310      * @note this function could slow down your application.
00311      */
00312     function collect_objects($cond='', $p_array=null)
00313     {
00314         if ($p_array != null && ! is_array($p_array) )
00315         {
00316             throw new Exception(_("Erreur : exec_sql attend un array"));
00317         }
00318         $ret=$this->seek($cond, $p_array);
00319         $max=Database::num_row($ret);
00320         $a_return=array();
00321         for ($i=0; $i<$max; $i++)
00322         {
00323             $a_return[$i]=clone $this->next($ret, $i);
00324         }
00325         return $a_return;
00326     }
00327     public function count($p_where="",$p_array=null) {
00328         $count=$this->cn->get_value("select count(*) from $this->table".$p_where,$p_array);
00329         return $count;
00330     }
00331 }
00332 
00333 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations