noalyss Version-9
data_sql.class.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 a abstract class , all the SQL class, like noalyss_sql (table), Acc_Plan_SQL (based on a SQL not a table)
26 */
27
28/**
29 * @brief this an abstract class , all the SQL class, like noalyss_sql (table),
30 * Acc_Plan_SQL (based on a SQL not a table) or a view.
31 *
32 * You must create a class extending this one, in the constructor
33 * these variables have to be defined
34 *
35 * - table = name of the view or empty
36 * - sql = sql statement
37 * - name = array of column name, match between logic and actual name
38 * - type = array , match between column and type of data
39 * - default = array of column with a default value
40 * - date_format = format of the date
41 *
42 * if you give a SQL or a View you have to give a primary key, usually , the best is to use a key composed of
43 * different PK of the tables
44 * Example : in this SQL (or view) the PK is id and it is composed with the PK f_id and sg_id , remember that the
45 * pk cannot be null and must be unique ! For SQL , the value is computed , so you need a subselect like this
46 @code
47 select
48 ssw.sg_id::text||'-'||vfp.f_id::text id,
49 vfp.f_id,vfp.f_enable,vfp.person_name ,vfp.person_fname ,vfp.person_qcode ,ssw.sg_id
50 from rash.vw_fiche_person vfp
51 join rash.security_social_worker ssw using(f_id) ;
52 @endcode
53 *
54 * For SQL , the value is computed , so you need a subselect like this
55@code
56 select * from (
57 select
58 ssw.sg_id::text||'-'||vfp.f_id::text id,
59 vfp.f_id,vfp.f_enable,vfp.person_name ,vfp.person_fname ,vfp.person_qcode ,ssw.sg_id
60 from rash.vw_fiche_person vfp
61 join rash.security_social_worker ssw using(f_id))sub1
62@encode
63 *
64 *
65 *
66 * After you call the parent constructor
67 * @note the view or the table must include an unique key, otherwise the load
68 * doesn't work.
69 *
70 * @pnote
71 * Name is an array the key is the logical name and the value is the name of the column
72 @code
73 $this->name=array(
74 "id"=>"o_id",
75 "program"=>"o_prog",
76 "date"=>"o_date",
77 "qcode"=>"o_qcode",
78 "fiche"=>"f_id",
79 );
80@endcode
81 *
82 the type is an array , key = column name , value = type
83
84 * @code
85 $this->type = array(
86 "o_id"=>"numeric",
87 "o_prog"=>"numeric",
88 "o_date"=>"date",
89 "o_qcode"=>"text",
90 "f_id"=>"numeric",
91 );
92@endcode
93 *
94 */
95#[AllowDynamicProperties]
96abstract class Data_SQL
97{
98 var $cn; //! Database connection
99 var $name; //! Array of logical and real name
100 var $primary_key; //! Column name of the primary key
101 var $type; //! Type of the data
102 var $date_format; //! defaullt date format
105
106 public function __toString(): string
107 {
108 $ret="values ";
109 foreach ($this->name as $name) {
110 $ret.="[ $name => {$this->$name} ]";
111 }
112
113 $ret.="| type ".print_r($this->type,true);
114 $ret.="| default ".print_r($this->default,true);
115 $ret.="| primary key ".$this->primary_key;
116 $ret.="| date_format ".$this->date_format;
117 return $ret;
118 }
119
120
121 function __construct(DatabaseCore $p_cn, $p_id=-1)
122 {
123 $this->cn=$p_cn;
125 $this->$pk=$p_id;
126 // check that the definition is correct
127 if (count($this->name) != count($this->type) ){
128 throw new Exception (__FILE__." $this->table Cannot instantiate");
129 }
130 // forbid the use of a column named type , date_format, name or primary_key to avoid conflict
131
132 /* Initialize an empty object */
133 foreach ($this->name as $key)
134 {
135 if ( in_array($key,['name','type','format_date','cn','date_format','default'] ) ) {
136 throw new Exception ('DATASQL-94 invalid column name'.$key);
137 }
138 $this->$key=null;
139 }
140 $this->$pk=$p_id;
141 /* load it , if the pk id doesn't exist, it will be turned into -1 */
142 if ($p_id != -1 )$this->load();
143 if ( empty($this->date_format) ) $this->date_format="DD.MM.YYYY";
144 }
145/**
146 *@brief Insert or update : if the row already exists, update otherwise insert
147 */
148 public function save()
149 {
150 $count = $this->exist();
151
152 if ($count == 0)
153 $this->insert();
154 else
155 $this->update();
156 }
157 /**
158 *@brief get the value thanks the colum name and not the alias (name).
159 *@see getp
160 */
161 public function get($p_string)
162 {
163 if (array_key_exists($p_string, $this->type)) {
164 return $this->$p_string;
165 }
166 else
167 throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
168 }
169
170 /**
171 *@brief set the value thanks the colum name and not the alias (name)
172 *@see setp
173 */
174 public function set($p_string, $p_value)
175 {
176 if (array_key_exists($p_string, $this->type)) {
177 $this->$p_string=$p_value;
178 return $this;
179 } else
180 throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
181 }
182
183 /**
184 *@brief set the value thanks the alias name instead of the colum name
185 *@see get
186 */
187 public function getp($p_string)
188 {
189 if (array_key_exists($p_string, $this->name)) {
190 $idx=$this->name[$p_string];
191 return $this->$idx;
192 }
193 else
194 throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
195 }
196
197 /**
198 *@brief set the value thanks the alias name instead of the colum name
199 *@see set
200 */
201 public function setp($p_string, $p_value)
202 {
203 if (array_key_exists($p_string, $this->name)) {
204 $idx=$this->name[$p_string];
205 $this->$idx=$p_value;
206 return $this;
207 } else
208 throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string);
209 }
210
211 abstract function insert();
212
213 abstract function delete();
214
215 abstract function update();
216
217 public function set_pk_value($p_value)
218 {
220 $this->$pk=$p_value;
221 }
222 public function get_pk_value()
223 {
225 return $this->$pk;
226 }
227
228 /**
229 * @brief Load the current row return false if not found
230 * @code
231 $pk=$this->primary_key;
232 if ( $this->get_limit_fiche_qcode() != 0 )
233 {
234 $sql=sprintf($this->sql," limit ".$this->get_limit_fiche_qcode());
235 } else
236 {
237 $sql=sprintf($this->sql," ");
238 }
239 $result=$this->cn->get_array($sql. " where id=$1",array ($this->$pk));
240 if ($this->cn->count()==0)
241 {
242 $this->$pk=-1;
243 return false;
244 }
245
246 foreach ($result[0] as $key=> $value)
247 {
248 $this->$key=$value;
249 }
250 return true;
251 *
252 * @endcode
253 * @return bool
254 */
255 abstract function load():bool;
256
257 public function get_info()
258 {
259 return var_export($this, true);
260 }
261/**
262 * @todo ajout vérification type (date, text ou numeric)
263 * @return int
264 */
265 public function verify()
266 {
267 foreach ($this->name as $key)
268 {
269 if (noalyss_trim($this->$key)=='')
270 $this->$key=null;
271 }
272 return 0;
273 }
274
275 /**
276 * Transform an array into object
277 * @param type $p_array
278 * @return object
279 */
280 public function from_array($p_array)
281 {
282 foreach ($this->name as $key=> $value)
283 {
284 if (isset($p_array[$value]))
285 {
286 $this->$value=$p_array[$value];
287 }
288 else
289 {
290 $this->$value=null;
291 }
292 }
293 return $this;
294 }
295 /**
296 *
297 * @brief Turn an object (row) into an array, and the key could be prefixed with $prefix
298 * @param string $prefix before the key
299 * @return array
300 */
301 public function to_array($prefix="")
302 {
303 $array=array();
304 foreach ($this->name as $key=> $value)
305 {
306 $nkey=$prefix.$key;
307 $array[$key]=$this->$key;
308 }
309 return $array;
310 }
311
312 /**
313 * @brief turns a row fetched from the DB into a SQL object in updating all his attribute
314 * @param $p_array
315 * @return void
316 */
317 public function to_row($p_array) {
318 foreach ($this->name as $name) {
319 $this->$name=$p_array[$name];
320 }
321 }
322 /**
323 * @brief retrieve array of object thanks a condition
324 * @param $cond condition (where clause) (optional by default all the rows are fetched)
325 * you can use this parameter for the order or subselect
326 * @param $p_array array for the SQL stmt
327 * @see Database::exec_sql get_object Database::num_row
328 * @return the return value of exec_sql
329 */
330 abstract function seek($cond='', $p_array=null);
331
332 /**
333 * @brief get_seek return the next object, the return of the query must have all the column
334 * of the object
335 * @param $p_ret is the return value of an exec_sql
336 * @param $idx is the index
337 * @see seek
338 * @return object
339 */
340 public function next($ret, $i)
341 {
342 $array=$this->cn->fetch_array($ret, $i);
343 return $this->from_array($array);
344 }
345
346 /**
347 * @see next
348 */
349 public function get_object($p_ret, $idx)
350 {
351 return $this->next($p_ret, $idx);
352 }
353
354 /**
355 * @brief return an array of objects.
356 * Do not use this function if they are too many objects, it takes a lot of memory,
357 * and could slow down your application.
358 * @param $cond condition, order...
359 * @param $p_array array to use for a condition
360 * @note this function could slow down your application.
361 */
362 function collect_objects($cond='', $p_array=null)
363 {
364 if ($p_array != null && ! is_array($p_array) )
365 {
366 throw new Exception(_("Erreur : exec_sql attend un array"));
367 }
368 $ret=$this->seek($cond, $p_array);
370 $a_return=array();
371 for ($i=0; $i<$max; $i++)
372 {
373 $a_return[$i]=clone $this->next($ret, $i);
374 }
375 return $a_return;
376 }
377 abstract function count($p_where="",$p_array=null) ;
378
379 /**
380 * Count the number of record with the id ,
381 * @return integer 0 doesn't exist , 1 exists
382 */
383 abstract function exist() ;
384 public function get_cn()
385 {
386 return $this->cn;
387 }
388
389 public function get_name()
390 {
391 return $this->name;
392 }
393
394 public function get_primary_key()
395 {
396 return $this->primary_key;
397 }
398
399 public function get_type()
400 {
401 return $this->type;
402 }
403
404 public function set_cn($cn)
405 {
406 $this->cn=$cn;
407 return $this;
408 }
409 /**
410 *
411 * @param string $name
412 * @return $this
413 */
414 public function set_name($name)
415 {
416 $this->name=$name;
417 return $this;
418 }
419 /**
420 *
421 * @param string $primary_key
422 * @return $this
423 */
425 {
426 $this->primary_key=$primary_key;
427 return $this;
428 }
429 /**
430 *
431 * @param array $type
432 * @return $this
433 */
434 public function set_type($type)
435 {
436 $this->type=$type;
437 return $this;
438 }
439
440
441}
442
443?>
noalyss_trim($p_string)
Definition: ac_common.php:1545
$idx
$input_from cn
Definition: balance.inc.php:66
$from_poste name
$input_from type
Definition: balance.inc.php:65
this an abstract class , all the SQL class, like noalyss_sql (table), Acc_Plan_SQL (based on a SQL no...
$primary_key
Array of logical and real name.
to_array($prefix="")
Turn an object (row) into an array, and the key could be prefixed with $prefix.
exist()
Count the number of record with the id ,.
__construct(DatabaseCore $p_cn, $p_id=-1)
set_type($type)
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($p_where="", $p_array=null)
load()
Load the current row return false if not found.
to_row($p_array)
turns a row fetched from the DB into a SQL object in updating all his attribute
get_object($p_ret, $idx)
$date_format
Type of the data.
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
from_array($p_array)
Transform an array into object.
set_pk_value($p_value)
set_primary_key($primary_key)
set_name($name)
next($ret, $i)
get_seek return the next object, the return of the query must have all the column of the object
getp($p_string)
set the value thanks the alias name instead of the colum name
$name
Database connection.
save()
Insert or update : if the row already exists, update otherwise insert.
$default
defaullt date format
$type
Column name of the primary key.
This class allow you to connect to the postgresql database, execute sql, retrieve data.
static num_row($ret)
wrapper for the function pg_num_rows
$count