noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_phpcompta_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
00026  * this wrapper is used to created easily a wrapper to a table
00027  *
00028  *@class
00029  * Match a table into an object, you need to add the code for each table
00030  *@note : the primary key must be an integer
00031  *
00032  * @code
00033 class table_name_sql extends phpcompta_sql
00034 {
00035 
00036         function __construct($p_id=-1)
00037         {
00038                 $this->table = "schema.table";
00039                 $this->primary_key = "o_id";
00040 
00041                 $this->name=array(
00042                         "id"=>"o_id",
00043                         "dolibarr"=>"o_doli",
00044                         "date"=>"o_date",
00045                         "qcode"=>"o_qcode",
00046                         "fiche"=>"f_id",
00047 
00048 
00049                 );
00050 
00051                 $this->type = array(
00052                         "o_id"=>"numeric",
00053                         "o_doli"=>"numeric",
00054                         "o_date"=>"date",
00055                         "o_qcode"=>"text",
00056                         "f_id"=>"numeric",
00057 
00058                         );
00059 
00060                 $this->default = array(
00061                         "o_id" => "auto",
00062                 );
00063                 $this->date_format = "DD.MM.YYYY";
00064                 global $cn;
00065 
00066                 parent::__construct($cn,$p_id);
00067         }
00068 
00069 }
00070  * @endcode
00071  *
00072  */
00073 class Phpcompta_SQL
00074 {
00075 
00076         function __construct(&$p_cn, $p_id = -1)
00077         {
00078                 $this->cn = $p_cn;
00079                 $pk=$this->primary_key;
00080                 $this->$pk= $p_id;
00081 
00082                         /* Initialize an empty object */
00083                         foreach ($this->name as $key )
00084                         {
00085                                 $this->$key= null;
00086                         }
00087                         $this->$pk= $p_id;
00088                         /* load it */
00089                         $this->load();
00090         }
00091         public function save() {
00092             $pk=$this->primary_key;
00093             if (  $this->$pk== -1 )
00094                   $this->insert();
00095                 else
00096                         $this->update();
00097   }
00098         public function getp($p_string)
00099         {
00100                 if (array_key_exists( $p_string,$this->name))
00101                 {
00102                         $idx = $this->name[$p_string];
00103                         return $this->$idx;
00104                 }
00105                 else
00106                         throw new Exception(__FILE__ . ":" . __LINE__ . $p_string . 'Erreur attribut inexistant '.$p_string);
00107         }
00108 
00109         public function setp($p_string, $p_value)
00110         {
00111                 if (array_key_exists( $p_string,$this->name))
00112                 {
00113                         $idx = $this->name[$p_string];
00114                         $this->$idx = $p_value;
00115                 }
00116                 else
00117                         throw new Exception(__FILE__ . ":" . __LINE__ . $p_string . 'Erreur attribut inexistant '.$p_string);
00118         }
00119 
00120         public function insert()
00121         {
00122                 $this->verify();
00123                 $sql = "insert into " . $this->table . " ( ";
00124                 $sep = "";
00125                 $par = "";
00126                 $idx = 1;
00127                 $array = array();
00128                 foreach ($this->name as $key=>$value)
00129                 {
00130                         if (isset($this->default[$value]) && $this->default[$value] == "auto" && $this->$value ==null )
00131                                 continue;
00132                         if ( $value==$this->primary_key && $this->$value == -1 ) continue;
00133                         $sql.=$sep.$value;
00134                         switch ($this->type[$value])
00135                         {
00136                                 case "date":
00137                                         if ($this->date_format=="")                                             throw new Exception('Format Date invalide');
00138                                         $par .=$sep. 'to_date($' . $idx . ",'" . $this->date_format . "')" ;
00139                                         break;
00140                                 default:
00141                                         $par .= $sep."$" . $idx ;
00142                         }
00143 
00144                         $array[] = $this->$value;
00145                         $sep = ",";
00146                         $idx++;
00147                 }
00148                 $sql.=") values (" . $par . ") returning " . $this->primary_key;
00149                 $pk=$this->primary_key;
00150                 $this->$pk = $this->cn->get_value($sql, $array);
00151         }
00152 
00153         public function delete()
00154         {
00155                 $pk=$this->primary_key;
00156                 $sql = " delete from " . $this->table . " where " . $this->primary_key . "=" . sql_string($this->$pk);
00157                 $this->cn->exec_sql($sql);
00158         }
00159 
00160         public function update()
00161         {
00162                 $this->verify();
00163                 $pk=$this->primary_key;
00164                 $sql = "update " . $this->table . "  ";
00165                 $sep = "";
00166                 $idx = 1;
00167                 $array = array();
00168                 $set=" set ";
00169                 foreach ($this->name as $key=>$value)
00170                 {
00171                         if (isset($this->default[$value]) && $this->default[$value] == "auto"  )
00172                                 continue;
00173                         switch ($this->type[$value])
00174                         {
00175                                 case "date":
00176                                         $par =$value. '=to_date($' . $idx . ",'" . $this->date_format . "')" ;
00177                                         break;
00178                                 default:
00179                                         $par = $value."= $" . $idx ;
00180                         }
00181                         $sql.=$sep." $set " . $par ;
00182                         $array[] = $this->$value;
00183                         $sep = ",";$set="";$idx++;
00184                 }
00185                 $sql.=" where " . $this->primary_key . " =" . $this->$pk;
00186             $this->cn->exec_sql($sql, $array);
00187 
00188         }
00189 
00190         public function load()
00191         {
00192                 $sql = " select ";
00193                 $sep="";$par="";
00194 
00195                 foreach ($this->name as $key)
00196                 {
00197 
00198                         switch ($this->type[$key])
00199                         {
00200                                 case "date":
00201                                         $sql .= $sep.'to_char(' . $key . ",'" . $this->date_format . "') as ".$key ;
00202                                         break;
00203                                 default:
00204                                         $sql.=$sep.$key ;
00205                         }
00206                         $sep = ",";
00207                 }
00208                 $pk=$this->primary_key;
00209                 $sql.=" from ".$this->table;
00210                 $sql.=" where " . $this->primary_key . " = " . $this->$pk;
00211                 $result = $this->cn->get_array($sql);
00212                 if ($this->cn->count() == 0 ) {
00213                         $this->$pk=-1;
00214                         return ;
00215                 }
00216 
00217                 foreach ($result[0] as $key=>$value) {
00218                         $this->$key=$value;
00219                 }
00220         }
00221 
00222         public function get_info()
00223         {
00224                 return var_export($this, true);
00225         }
00226 
00227         public function verify()
00228         {
00229                 foreach($this->name as $key){
00230                         if ( trim($this->$key)=='') $this->$key=null;
00231                 }
00232                 return 0;
00233         }
00234         /**
00235          * Transform an array into object
00236          * @param type $p_array
00237          * @return object
00238          */
00239         public function from_array($p_array)
00240         {
00241                 foreach ($this->name as $key=>$value)
00242                 {
00243                         if ( isset ($p_array[$value]))
00244                         {
00245                                 $this->$value=$p_array[$value];
00246                         }
00247                         else
00248                         {
00249                                 $this->$value=null;
00250                         }
00251                 }
00252                 return $this;
00253         }
00254           /**
00255    *@brief retrieve array of object thanks a condition
00256    *@param $cond condition (where clause) (optional by default all the rows are fetched)
00257    * you can use this parameter for the order or subselect
00258    *@param $p_array array for the SQL stmt
00259    *@see Database::exec_sql get_object  Database::num_row
00260    *@return the return value of exec_sql
00261    */
00262          function seek($cond='', $p_array=null)
00263         {
00264                 $sql = "select * from ".$this->table."  $cond";
00265                 $ret = $this->cn->exec_sql($sql, $p_array);
00266                 return $ret;
00267         }
00268          /**
00269     *get_seek return the next object, the return of the query must have all the column
00270     * of the object
00271     *@param $p_ret is the return value of an exec_sql
00272     *@param $idx is the index
00273     *@see seek
00274     *@return object
00275     */
00276     public function next($ret,$i) {
00277                 $array=$this->cn->fetch_array($ret,$i);
00278                 return $this->from_array($array);
00279         }
00280         /**
00281          *@see next
00282          */
00283  public function get_object($p_ret,$idx)
00284     {
00285     return $this->next($p_ret, $idx);
00286    }
00287 }
00288 
00289 
00290 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations