noalyss  Version-6.9.1.8
 All Data Structures Namespaces Files Functions Variables Pages
class_noalyss_sql.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of NOALYSS.
5  *
6  * NOALYSS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * NOALYSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with NOALYSS; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 // Copyright Author Dany De Bontridder danydb@aevalys.eu
22 
23 /**
24  * @file
25  * @brief this wrapper is used to created easily a wrapper to a table
26  *
27  * @class Noalyss_SQL
28  * Match a table into an object, you need to add the code for each table
29  * @note : the primary key must be an integer
30  *
31  * @code
32  class table_name_sql extends Noalyss_SQL
33  {
34 
35  function __construct($p_id=-1)
36  {
37  $this->table = "schema.table";
38  $this->primary_key = "o_id";
39 
40  $this->name=array(
41  "id"=>"o_id",
42  "dolibarr"=>"o_doli",
43  "date"=>"o_date",
44  "qcode"=>"o_qcode",
45  "fiche"=>"f_id",
46 
47 
48  );
49 
50  $this->type = array(
51  "o_id"=>"numeric",
52  "o_doli"=>"numeric",
53  "o_date"=>"date",
54  "o_qcode"=>"text",
55  "f_id"=>"numeric",
56 
57  );
58 
59  $this->default = array(
60  "o_id" => "auto",
61  );
62  $this->date_format = "DD.MM.YYYY";
63  global $cn;
64 
65  parent::__construct($cn,$p_id);
66  }
67 
68  }
69  * @endcode
70  *
71  */
72 abstract class Noalyss_SQL
73 {
74 
75  function __construct(&$p_cn, $p_id=-1)
76  {
77  $this->cn=$p_cn;
78  $pk=$this->primary_key;
79  $this->$pk=$p_id;
80  // check that the definition is correct
81  if (count($this->name) != count($this->type) ){
82  throw new Exception (__FILE__." $this->table Cannot instantiate");
83  }
84  /* Initialize an empty object */
85  foreach ($this->name as $key)
86  {
87  $this->$key=null;
88  }
89  $this->$pk=$p_id;
90  /* load it */
91  if ($p_id != -1 )$this->load();
92  }
93 /**
94  * Insert or update : if the row already exists, update otherwise insert
95  */
96  public function save()
97  {
98  $pk=$this->primary_key;
99  $count=$this->cn->get_value('select count(*) from '.$this->table.' where '.$this->primary_key.'=$1',array($this->$pk));
100 
101  if ($count == 0)
102  $this->insert();
103  else
104  $this->update();
105  }
106  /**
107  *@brief get the value thanks the colum name and not the alias (name).
108  *@see getp
109  */
110  public function get($p_string)
111  {
112  if (array_key_exists($p_string, $this->type)) {
113  return $this->$p_string;
114  }
115  else
116  throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
117  }
118 
119  /**
120  *@brief set the value thanks the colum name and not the alias (name)
121  *@see setp
122  */
123  public function set($p_string, $p_value)
124  {
125  if (array_key_exists($p_string, $this->type)) {
126  $this->$idx=$p_value;
127  } else
128  throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
129  }
130 
131  /**
132  *@brief set the value thanks the alias name instead of the colum name
133  *@see get
134  */
135  public function getp($p_string)
136  {
137  if (array_key_exists($p_string, $this->name)) {
138  $idx=$this->name[$p_string];
139  return $this->$idx;
140  }
141  else
142  throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
143  }
144 
145  /**
146  *@brief set the value thanks the alias name instead of the colum name
147  *@see set
148  */
149  public function setp($p_string, $p_value)
150  {
151  if (array_key_exists($p_string, $this->name)) {
152  $idx=$this->name[$p_string];
153  $this->$idx=$p_value;
154  } else
155  throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
156  }
157 
158  public function insert()
159  {
160  $this->verify();
161  $sql="insert into ".$this->table." ( ";
162  $sep="";
163  $par="";
164  $idx=1;
165  $array=array();
166  foreach ($this->name as $key=> $value)
167  {
168  if (isset($this->default[$value])&&$this->default[$value]=="auto"&&$this->$value==null)
169  continue;
170  if ($value==$this->primary_key&&$this->$value==-1)
171  continue;
172  $sql.=$sep.$value;
173  switch ($this->type[$value])
174  {
175  case "date":
176  if ($this->date_format=="")
177  throw new Exception('Format Date invalide');
178  $par .=$sep.'to_timestamp($'.$idx.",'".$this->date_format."')";
179  break;
180  default:
181  $par .= $sep."$".$idx;
182  }
183 
184  $array[]=$this->$value;
185  $sep=",";
186  $idx++;
187  }
188  $sql.=") values (".$par.") returning ".$this->primary_key;
189  $pk=$this->primary_key;
190  $this->$pk=$this->cn->get_value($sql, $array);
191  }
192 
193  public function delete()
194  {
195  $pk=$this->primary_key;
196  $sql=" delete from ".$this->table." where ".$this->primary_key."= $1";
197  $this->cn->exec_sql($sql,array($this->$pk));
198  }
199 
200  public function update()
201  {
202  $this->verify();
203  $pk=$this->primary_key;
204  $sql="update ".$this->table." ";
205  $sep="";
206  $idx=1;
207  $array=array();
208  $set=" set ";
209  foreach ($this->name as $key=> $value) {
210  if (isset($this->default[$value])&&$this->default[$value]=="auto")
211  continue;
212  switch ($this->type[$value])
213  {
214  case "date":
215  $par=$value.'=to_timestamp($'.$idx.",'".$this->date_format."')";
216  break;
217  default:
218  $par=$value."= $".$idx;
219  }
220  $sql.=$sep." $set ".$par;
221  $array[]=$this->$value;
222  $sep=",";
223  $set="";
224  $idx++;
225  }
226  $array[]=$this->$pk;
227  $sql.=" where ".$this->primary_key." = $".$idx;
228  $this->cn->exec_sql($sql, $array);
229  }
230  public function set_pk_value($p_value)
231  {
232  $pk=$this->primary_key;
233  $this->$pk=$p_value;
234  }
235  public function get_pk_value()
236  {
237  $pk=$this->primary_key;
238  return $this->$pk;
239  }
240 
241  public function load()
242  {
243  $sql=" select ";
244  $sep="";
245  foreach ($this->name as $key) {
246  switch ($this->type[$key])
247  {
248  case "date":
249  $sql .= $sep.'to_char('.$key.",'".$this->date_format."') as ".$key;
250  break;
251  default:
252  $sql.=$sep.$key;
253  }
254  $sep=",";
255  }
256  $pk=$this->primary_key;
257  $sql.=" from ".$this->table;
258 
259  $sql.=" where ".$this->primary_key." = $1";
260 
261  $result=$this->cn->get_array($sql,array ($this->$pk));
262  if ($this->cn->count()==0)
263  {
264  $this->$pk=-1;
265  return;
266  }
267 
268  foreach ($result[0] as $key=> $value)
269  {
270  $this->$key=$value;
271  }
272  }
273 
274  public function get_info()
275  {
276  return var_export($this, true);
277  }
278 /**
279  * @todo ajout vérification type (date, text ou numeric)
280  * @return int
281  */
282  public function verify()
283  {
284  foreach ($this->name as $key)
285  {
286  if (trim($this->$key)=='')
287  $this->$key=null;
288  }
289  return 0;
290  }
291 
292  /**
293  * Transform an array into object
294  * @param type $p_array
295  * @return object
296  */
297  public function from_array($p_array)
298  {
299  foreach ($this->name as $key=> $value)
300  {
301  if (isset($p_array[$value]))
302  {
303  $this->$value=$p_array[$value];
304  }
305  else
306  {
307  $this->$value=null;
308  }
309  }
310  return $this;
311  }
312  /**
313  * Turn an object (row) into an array
314  * @return array
315  */
316  public function to_array()
317  {
318  $array=array();
319  foreach ($this->name as $key=> $value)
320  {
321 
322  $array[$key]=$this->$key;
323  }
324  return $array;
325  }
326 
327  /**
328  * @brief retrieve array of object thanks a condition
329  * @param $cond condition (where clause) (optional by default all the rows are fetched)
330  * you can use this parameter for the order or subselect
331  * @param $p_array array for the SQL stmt
332  * @see Database::exec_sql get_object Database::num_row
333  * @return the return value of exec_sql
334  */
335  function seek($cond='', $p_array=null)
336  {
337  $sql="select * from ".$this->table." $cond";
338  $ret=$this->cn->exec_sql($sql, $p_array);
339  return $ret;
340  }
341 
342  /**
343  * get_seek return the next object, the return of the query must have all the column
344  * of the object
345  * @param $p_ret is the return value of an exec_sql
346  * @param $idx is the index
347  * @see seek
348  * @return object
349  */
350  public function next($ret, $i)
351  {
352  $array=$this->cn->fetch_array($ret, $i);
353  return $this->from_array($array);
354  }
355 
356  /**
357  * @see next
358  */
359  public function get_object($p_ret, $idx)
360  {
361  return $this->next($p_ret, $idx);
362  }
363 
364  /**
365  * @brief return an array of objects. Do not use this function if they are too many objects, it takes a lot of memory,
366  * and could slow down your application.
367  * @param $cond condition, order...
368  * @param $p_array array to use for a condition
369  * @note this function could slow down your application.
370  */
371  function collect_objects($cond='', $p_array=null)
372  {
373  if ($p_array != null && ! is_array($p_array) )
374  {
375  throw new Exception(_("Erreur : exec_sql attend un array"));
376  }
377  $ret=$this->seek($cond, $p_array);
379  $a_return=array();
380  for ($i=0; $i<$max; $i++)
381  {
382  $a_return[$i]=clone $this->next($ret, $i);
383  }
384  return $a_return;
385  }
386  public function count($p_where="",$p_array=null) {
387  $count=$this->cn->get_value("select count(*) from $this->table".$p_where,$p_array);
388  return $count;
389  }
390  /**
391  * Count the number of record with the id ,
392  * @return integer 0 doesn't exist , 1 exists
393  */
394  public function exist() {
395  $pk=$this->primary_key;
396  $count=$this->cn->get_value("select count(*) from ".$this->table." where ".$this->primary_key."=$1",array($this->$pk));
397  return $count;
398  }
399 }
400 
401 ?>
to_array()
Turn an object (row) into an array.
save()
Insert or update : if the row already exists, update otherwise insert.
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
static num_row($ret)
wrapper for the function pg_NumRows
get_object($p_ret, $idx)
Match a table into an object, you need to add the code for each table.
set($p_string, $p_value)
set the value thanks the colum name and not the alias (name)
$value
__construct(&$p_cn, $p_id=-1)
$idx
set_pk_value($p_value)
from_array($p_array)
Transform an array into object.
function clone(object)
$from_poste name
count($p_where="", $p_array=null)
function trim(s)
remove trailing and heading space
Definition: scripts.js:95
next($ret, $i)
get_seek return the next object, the return of the query must have all the column of the object ...
$input_from cn
Definition: balance.inc.php:71
collect_objects($cond='', $p_array=null)
return an array of objects.
setp($p_string, $p_value)
set the value thanks the alias name instead of the colum name
$count
Definition: modele.inc.php:255
$input_from type
Definition: balance.inc.php:70
exist()
Count the number of record with the id ,.
$select_type table
getp($p_string)
set the value thanks the alias name instead of the colum name