class_profile_sql.php

Go to the documentation of this file.
00001 <?php
00002 /**
00003  *@file
00004  *@brief Manage the table public.profile
00005  *
00006  *
00007 Example
00008 @code
00009 
00010 @endcode
00011  */
00012 require_once('class_database.php');
00013 require_once('ac_common.php');
00014 
00015 
00016 /**
00017  *@brief Manage the table public.profile
00018 */
00019 class Profile_sql  
00020 {
00021   /* example private $variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0); */
00022   
00023   protected $variable=array("p_id"=>"p_id","p_name"=>"p_name"
00024 ,"p_desc"=>"p_desc"
00025 ,"with_calc"=>"with_calc"
00026 ,"with_direct_form"=>"with_direct_form"
00027 );
00028   function __construct ( & $p_cn,$p_id=-1) {
00029         $this->cn=$p_cn;
00030         $this->p_id=$p_id;
00031         
00032         if ( $p_id == -1 ) {
00033         /* Initialize an empty object */
00034             foreach ($this->variable as $key=>$value) $this->$value=null;
00035             $this->p_id=$p_id;
00036         } else {
00037          /* load it */
00038 
00039          $this->load();
00040       }
00041   }
00042   public function get_parameter($p_string) {
00043     if ( array_key_exists($p_string,$this->variable) ) {
00044       $idx=$this->variable[$p_string];
00045       return $this->$idx;
00046     }
00047     else 
00048       throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00049   }
00050   public function set_parameter($p_string,$p_value) {
00051     if ( array_key_exists($p_string,$this->variable) ) {
00052       $idx=$this->variable[$p_string];
00053       $this->$idx=$p_value;
00054     }
00055     else 
00056       throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00057   }
00058   public function get_info() {    return var_export($this,true);  }
00059   public function verify() {
00060     // Verify that the elt we want to add is correct
00061     /* verify only the datatype */
00062      if ( trim($this->p_name) == '') $this->p_name=null;
00063  if ( trim($this->p_desc) == '') $this->p_desc=null;
00064  if ( trim($this->with_calc) == '') $this->with_calc=null;
00065  if ( trim($this->with_direct_form) == '') $this->with_direct_form=null;
00066 
00067     
00068   }
00069   public function save() {
00070   /* please adapt */
00071     if (  $this->p_id == -1 ) 
00072       $this->insert();
00073     else
00074       $this->update();
00075   }
00076   /**
00077    *@brief retrieve array of object thanks a condition
00078    *@param $cond condition (where clause) (optional by default all the rows are fetched)
00079    * you can use this parameter for the order or subselect
00080    *@param $p_array array for the SQL stmt
00081    *@see Database::exec_sql get_object  Database::num_row
00082    *@return the return value of exec_sql
00083    */
00084    public function seek($cond='',$p_array=null) 
00085    {
00086      $sql="select * from public.profile  $cond";
00087      $aobj=array();
00088      $ret= $this->cn->exec_sql($sql,$p_array);
00089      return $ret;
00090    }
00091    /**
00092     *get_seek return the next object, the return of the query must have all the column
00093     * of the object
00094     *@param $p_ret is the return value of an exec_sql
00095     *@param $idx is the index
00096     *@see seek
00097     *@return object 
00098     */
00099    public function get_object($p_ret,$idx)
00100     {
00101      // map each row in a object
00102      $oobj=new Profile_sql ($this->cn);
00103      $array=Database::fetch_array($p_ret,$idx);
00104      foreach ($array as $idx=>$value) { $oobj->$idx=$value; }
00105      return $oobj;
00106    }
00107   public function insert() {
00108     if ( $this->verify() != 0 ) return;
00109       if( $this->p_id==-1 ){
00110     /*  please adapt */
00111     $sql="insert into public.profile(p_name
00112 ,p_desc
00113 ,with_calc
00114 ,with_direct_form
00115 ) values ($1
00116 ,$2
00117 ,$3
00118 ,$4
00119 ) returning p_id";
00120     
00121     $this->p_id=$this->cn->get_value(
00122                  $sql,
00123                  array( $this->p_name
00124 ,$this->p_desc
00125 ,$this->with_calc
00126 ,$this->with_direct_form
00127 )
00128                  );
00129           } else {
00130               $sql="insert into public.profile(p_name
00131 ,p_desc
00132 ,with_calc
00133 ,with_direct_form
00134 ,p_id) values ($1
00135 ,$2
00136 ,$3
00137 ,$4
00138 ,$5
00139 ) returning p_id";
00140     
00141     $this->p_id=$this->cn->get_value(
00142                  $sql,
00143                  array( $this->p_name
00144 ,$this->p_desc
00145 ,$this->with_calc
00146 ,$this->with_direct_form
00147 ,$this->p_id)
00148                  );
00149 
00150           }
00151    
00152   }
00153 
00154   public function update() {
00155     if ( $this->verify() != 0 ) return;
00156     /*   please adapt */
00157     $sql=" update public.profile set p_name = $1
00158 ,p_desc = $2
00159 ,with_calc = $3
00160 ,with_direct_form = $4
00161  where p_id= $5";
00162     $res=$this->cn->exec_sql(
00163                  $sql,
00164                  array($this->p_name
00165 ,$this->p_desc
00166 ,$this->with_calc
00167 ,$this->with_direct_form
00168 ,$this->p_id)
00169                  );
00170                  
00171   }
00172 /**
00173  *@brief load a object
00174  *@return 0 on success -1 the object is not found
00175  */
00176   public function load() {
00177 
00178    $sql="select p_name
00179 ,p_desc
00180 ,with_calc
00181 ,with_direct_form
00182  from public.profile where p_id=$1"; 
00183     /* please adapt */
00184     $res=$this->cn->get_array(
00185                  $sql,
00186                  array($this->p_id)
00187                  );
00188                  
00189     if ( count($res) == 0 ) {
00190           /* Initialize an empty object */
00191           foreach ($this->variable as $key=>$value) $this->$key='';
00192 
00193           return -1;
00194           }
00195     foreach ($res[0] as $idx=>$value) { $this->$idx=$value; }
00196     return 0;
00197   }
00198 
00199   public function delete() {
00200     $sql="delete from public.profile where p_id=$1"; 
00201     $res=$this->cn->exec_sql($sql,array($this->p_id));
00202   }
00203   /**
00204    * Unit test for the class
00205    */   
00206   static function test_me() {
00207       $cn=new Database(25);
00208 $cn->start();
00209     echo h2info('Test object vide');
00210     $obj=new Profile_sql($cn);
00211     var_dump($obj);
00212 
00213     echo h2info('Test object NON vide');
00214     $obj->set_parameter('j_id',3);
00215     $obj->load();
00216     var_dump($obj);
00217 
00218     echo h2info('Update');
00219     $obj->set_parameter('j_qcode','NOUVEAU CODE');
00220     $obj->save();
00221     $obj->load();
00222     var_dump($obj);
00223 
00224     echo h2info('Insert');
00225     $obj->set_parameter('j_id',0);
00226     $obj->save();
00227     $obj->load();
00228     var_dump($obj);
00229 
00230     echo h2info('Delete');
00231     $obj->delete();
00232     echo (($obj->load()==0)?'Trouve':'non trouve');
00233     var_dump($obj);
00234 $cn->rollback();
00235 
00236   }
00237   
00238 }
00239 // Profile_sql::test_me();
00240 ?>