Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
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
00083 foreach ($this->name as $key )
00084 {
00085 $this->$key= null;
00086 }
00087 $this->$pk= $p_id;
00088
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
00236
00237
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
00256
00257
00258
00259
00260
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
00270
00271
00272
00273
00274
00275
00276 public function next($ret,$i) {
00277 $array=$this->cn->fetch_array($ret,$i);
00278 return $this->from_array($array);
00279 }
00280
00281
00282
00283 public function get_object($p_ret,$idx)
00284 {
00285 return $this->next($p_ret, $idx);
00286 }
00287 }
00288
00289
00290 ?>