00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 require_once('class_database.php');
00013 require_once('ac_common.php');
00014
00015
00016
00017
00018
00019 class Profile_sql
00020 {
00021
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
00034 foreach ($this->variable as $key=>$value) $this->$value=null;
00035 $this->p_id=$p_id;
00036 } else {
00037
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
00061
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
00071 if ( $this->p_id == -1 )
00072 $this->insert();
00073 else
00074 $this->update();
00075 }
00076
00077
00078
00079
00080
00081
00082
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
00093
00094
00095
00096
00097
00098
00099 public function get_object($p_ret,$idx)
00100 {
00101
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
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
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
00174
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
00184 $res=$this->cn->get_array(
00185 $sql,
00186 array($this->p_id)
00187 );
00188
00189 if ( count($res) == 0 ) {
00190
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
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
00240 ?>