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 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
00082 foreach ($this->name as $key)
00083 {
00084 $this->$key=null;
00085 }
00086 $this->$pk=$p_id;
00087
00088 if ($p_id != -1 )$this->load();
00089 }
00090
00091
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
00235
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
00249
00250
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
00270
00271
00272
00273
00274
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
00285
00286
00287
00288
00289
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
00299
00300 public function get_object($p_ret, $idx)
00301 {
00302 return $this->next($p_ret, $idx);
00303 }
00304
00305
00306
00307
00308
00309
00310
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 ?>