noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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, or
38 * only an array of columns
39 * - type = array , match between column and type of data
40 * - default = array of column with a default value
41 * - date_format = format of the date
42 *
43 * 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
44 * different PK of the tables
45 * 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
46 * pk cannot be null and must be unique ! For SQL , the value is computed , so you need a subselect like this
47 @code
48 select
49 ssw.sg_id::text||'-'||vfp.f_id::text id,
50 vfp.f_id,vfp.f_enable,vfp.person_name ,vfp.person_fname ,vfp.person_qcode ,ssw.sg_id
51 from rash.vw_fiche_person vfp
52 join rash.security_social_worker ssw using(f_id) ;
53 @endcode
54 *
55 * For SQL , the value is computed , so you need a subselect like this
56@code
57 select * from (
58 select
59 ssw.sg_id::text||'-'||vfp.f_id::text id,
60 vfp.f_id,vfp.f_enable,vfp.person_name ,vfp.person_fname ,vfp.person_qcode ,ssw.sg_id
61 from rash.vw_fiche_person vfp
62 join rash.security_social_worker ssw using(f_id))sub1
63@encode
64 *
65 *
66 *
67 * After you call the parent constructor
68 * @note the view or the table must include an unique key, otherwise the load
69 * doesn't work.
70 *
71 * @pnote
72 * Name is an array the key is the logical name and the value is the name of the column
73 @code
74 $this->name=array(
75 "id"=>"o_id",
76 "program"=>"o_prog",
77 "date"=>"o_date",
78 "qcode"=>"o_qcode",
79 "fiche"=>"f_id",
80 );
81@endcode
82 *
83 the type is an array , key = column name , value = type
84
85 * @code
86 $this->type = array(
87 "o_id"=>"numeric",
88 "o_prog"=>"numeric",
89 "o_date"=>"date",
90 "o_qcode"=>"text",
91 "f_id"=>"numeric",
92 );
93@endcode
94 *
95 */
96
97#[\AllowDynamicProperties]
98abstract class Data_SQL
99{
100 var $cn; //! Database connection
101 var $name; //! Array of logical and real name
102 var $primary_key; //! Column name of the primary key
103 var $type; //! Type of the data
104 var $date_format; //! defaullt date format
107 protected $a_virtual_col;
108 public function __toString(): string
109 {
110 $ret=" members: ";
111 foreach ($this->name as $name) {
112 $ret.="[ $name => {$this->$name} ]";
113 }
114
115 $ret.="| type ".print_r($this->type,true);
116 $ret.="| default ".print_r($this->default,true);
117 $ret.="| primary key ".$this->primary_key;
118 $ret.="| date_format ".$this->date_format;
119 $ret.="| a_virtual_col".var_export($this->a_virtual_col,true);
120 return $ret;
121 }
122
123
124 function __construct(DatabaseCore $p_cn, $p_id=-1)
125 {
126 $this->cn=$p_cn;
128 $this->$pk=$p_id;
129 $this->a_virtual_col=array();
130 // check that the definition is correct
131 if (count($this->name) != count($this->type) ){
132 throw new Exception (__FILE__." $this->table Cannot instantiate");
133 }
134 // forbid the use of a column named type , date_format, name or primary_key to avoid conflict
135
136 /* Initialize an empty object */
137 foreach ($this->name as $key)
138 {
139 if ( in_array($key,['name','type','format_date','cn','date_format','default'] ) ) {
140 throw new Exception ('DATASQL-94 invalid column name'.$key,94);
141 }
142 $this->$key=null;
143 }
144 $this->$pk=$p_id;
145 /* load it , if the pk id doesn't exist, it will be turned into -1 */
146 if ($p_id != -1 )$this->load();
147 if ( empty($this->date_format) ) $this->date_format="DD.MM.YYYY";
148 }
149/**
150 *@brief Insert or update : if the row already exists, update otherwise insert
151 */
152 public function save()
153 {
154 $count = $this->exist();
155
156 if ($count == 0)
157 $this->insert();
158 else
159 $this->update();
160 }
161 /**
162 * @brief return array of virtual cols (alias calculated, formatted cols)
163 * @return array
164 */
165 public function get_a_virtual_col() {
167 }
168 /**
169 * @brief add a virtual column (formatted column, sum of 2 col, ...)
170 * @param $col_name (string) name of the column , will be use in get , getp
171 * @param $sql_expression (string) SQL Expression for the column
172 * like "to_char(col1,'DD.MM.YY HH24:MI:SS')", col1+col2, ...
173 * @note sql expression and col_name must be valid , there is no futher
174 * check
175 * @returns void
176 */
177 public function set_virtual_col($col_name,$sql_expression) {
178 $this->a_virtual_col[$col_name]=$sql_expression;
179 }
180 /**
181 *@brief get the value thanks the colum name and not the alias (name).
182 *@see getp
183 */
184 public function get($cols)
185 {
186 if (array_key_exists($cols, $this->type)) {
187 return $this->$cols;
188 }
189 if (array_key_exists($cols, $this->a_virtual_col))
190 {
191 return $this->$cols;
192 }
193 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
194 }
195
196 /**
197 *@brief set the value thanks the colum name and not the alias (name)
198 *@see setp
199 */
200 public function set($cols, $p_value)
201 {
202 if (array_key_exists($cols, $this->type)) {
203 $this->$cols=$p_value;
204 return $this;
205 } else
206 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
207
208 }
209
210 /**
211 *@brief set the value thanks the alias name instead of the colum name
212 * if not found try the column name
213 *@see get
214 */
215 public function getp($cols)
216 {
217 if (array_key_exists($cols, $this->name)) {
218 $idx=$this->name[$cols];
219 return $this->$idx;
220 }
221 if (array_key_exists($cols, $this->type)) {
222 return $this->$cols;
223 }
224 if (array_key_exists($cols, $this->a_virtual_col))
225 {
226 return $this->$cols;
227 }
228
229 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
230 }
231
232 /**
233 *@brief set the value thanks the alias name instead of the colum name,
234 * if not found try the column name
235 *@see set
236 */
237 public function setp($cols, $p_value)
238 {
239 if (array_key_exists($cols, $this->name)) {
240 $idx=$this->name[$cols];
241 $this->$idx=$p_value;
242 return $this;
243 }
244 if (array_key_exists($cols, $this->type)) {
245 $this->$cols=$p_value;
246 return $this;
247 }
248
249 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
250 }
251
252 public function __set($cols,$p_value) {
253 if (array_key_exists($cols, $this->type)) {
254 $this->$cols=$p_value;
255 return $this;
256 }
257 if ( ! empty ($this->a_virtual_col) && array_key_exists($cols, $this->a_virtual_col))
258 {
259 $this->$cols=$p_value;
260 return $this;
261 }
262
263 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
264 }
265 public function __get($cols) {
266 if (array_key_exists($cols, $this->type)) {
267 return $this->$cols;
268 }
269 if (! empty ($this->a_virtual_col) && array_key_exists($cols, $this->a_virtual_col))
270 {
271 return $this->$cols;
272 }
273 throw new \Exception (" unknow cols [$cols] =".$this,EXC_DATA_SQL);
274 }
275 abstract function insert();
276
277 abstract function delete();
278
279 abstract function update();
280
281 public function set_pk_value($p_value)
282 {
284 $this->$pk=$p_value;
285 }
286 public function get_pk_value()
287 {
289 return $this->$pk;
290 }
291
292 /**
293 * @brief Load the current row return false if not found
294 * @code
295 $pk=$this->primary_key;
296 if ( $this->get_limit_fiche_qcode() != 0 )
297 {
298 $sql=sprintf($this->sql," limit ".$this->get_limit_fiche_qcode());
299 } else
300 {
301 $sql=sprintf($this->sql," ");
302 }
303 $result=$this->cn->get_array($sql. " where id=$1",array ($this->$pk));
304 if ($this->cn->count()==0)
305 {
306 $this->$pk=-1;
307 return false;
308 }
309
310 foreach ($result[0] as $key=> $value)
311 {
312 $this->$key=$value;
313 }
314 return true;
315 *
316 * @endcode
317 * @return bool
318 */
319 abstract function load():bool;
320
321 public function get_info()
322 {
323 return var_export($this, true);
324 }
325/**
326 * @todo ajout vérification type (date, text ou numeric)
327 * @return int
328 */
329 public function verify()
330 {
331 foreach ($this->name as $key)
332 {
333 if (noalyss_trim($this->$key)=='')
334 $this->$key=null;
335 }
336 return 0;
337 }
338
339 /**
340 * @brief Transform an array into object
341 * @param type $p_array
342 * @return object
343 */
344 public function from_array($p_array)
345 {
346 foreach ($this->name as $key=> $value)
347 {
348 if (isset($p_array[$value]))
349 {
350 $this->$value=$p_array[$value];
351 }
352 else
353 {
354 $this->$value=null;
355 }
356 }
357 return $this;
358 }
359 /**
360 *
361 * @brief Turn an object (row) into an array, and the key could be prefixed with $prefix
362 * @param string $prefix before the key
363 * @return array
364 */
365 public function to_array($prefix="")
366 {
367 $array=array();
368 foreach ($this->name as $key=> $value)
369 {
370 $nkey=$prefix.$key;
371 $array[$key]=$this->$key;
372 }
373 if ( ! empty ($this->a_virtual_col )) {
374 $a_column= array_keys($this->a_virtual_col);
375 foreach( $a_column as $column){
376 $array[$column]=$this->a_virtual_col[$column];
377 }
378 }
379 return $array;
380 }
381
382 /**
383 * @brief update the data member of current object with the value from the array.
384 * includes the virtual column, usefull if need to update several columns in once
385 * @param $p_array (array) associative key = column_vale, value new value for this col.
386 * @return void
387 */
388 public function to_row($p_array) {
389 foreach ($this->name as $name) {
390 if (isset ($p_array[$name])) {
391 $this->$name=$p_array[$name];
392 }
393 }
394 if ( ! empty ($this->a_virtual_col )) {
395 $a_column= array_keys($this->a_virtual_col);
396 foreach( $a_column as $column){
397 if ( isset ($p_array[$column])) {
398 $this->$column = $p_array[$column];
399 }
400 }
401 }
402 }
403 /**
404 * @brief retrieve array of object thanks a condition
405 * @param $cond condition (where clause) (optional by default all the rows are fetched)
406 * you can use this parameter for the order or subselect
407 * @param $p_array array for the SQL stmt
408 * @see Database::exec_sql get_object Database::num_row
409 * @return the return value of exec_sql
410 */
411 abstract function seek($cond='', $p_array=null);
412
413 /**
414 * @brief get_seek return the next object, the return of the query must have
415 * all the column of the object including the virtual columns
416 * @param $p_ret is the return value of an exec_sql
417 * @param $idx is the index
418 * @see seek
419 * @return object
420 */
421 public function next($ret, $i)
422 {
423 $array=$this->cn->fetch_array($ret, $i);
424 $this->from_array($array);
425 if ( ! empty ($this->a_virtual_col )) {
426 $a_column= array_keys($this->a_virtual_col);
427 foreach( $a_column as $column){
428 if ( isset ($array[$column] )) {
429 $this->$column = $array[$column];
430 } else {
431 $this->$column = null;
432 }
433 }
434 }
435 return $this;
436 }
437
438 /**
439 * @see next
440 */
441 public function get_object($p_ret, $idx)
442 {
443 return $this->next($p_ret, $idx);
444 }
445
446 /**
447 * @brief return an array of objects.
448 * Do not use this function if they are too many objects, it takes a lot of memory,
449 * and could slow down your application.
450 * @param $cond condition, order...
451 * @param $p_array array to use for a condition
452 * @note this function could slow down your application.
453 */
454 function collect_objects($cond='', $p_array=null)
455 {
456 if ($p_array != null && ! is_array($p_array) )
457 {
458 throw new Exception(_("Erreur : exec_sql attend un array"));
459 }
460 $ret=$this->seek($cond, $p_array);
462 $a_return=array();
463 for ($i=0; $i<$max; $i++)
464 {
465 $x=clone $this->next($ret, $i);
466
467 $a_return[$i]=$x;
468 }
469 return $a_return;
470 }
471 abstract function count($p_where="",$p_array=null) ;
472
473 /**
474 * Count the number of record with the id ,
475 * @return integer 0 doesn't exist , 1 exists
476 */
477 abstract function exist() ;
478 public function get_cn()
479 {
480 return $this->cn;
481 }
482
483 public function get_name()
484 {
485 return $this->name;
486 }
487
488 public function get_primary_key()
489 {
490 return $this->primary_key;
491 }
492
493 public function get_type()
494 {
495 return $this->type;
496 }
497
498 public function set_cn($cn)
499 {
500 $this->cn=$cn;
501 return $this;
502 }
503 /**
504 *
505 * @param string $name
506 * @return $this
507 */
508 public function set_name($name)
509 {
510 $this->name=$name;
511 return $this;
512 }
513 /**
514 *
515 * @param string $primary_key
516 * @return $this
517 */
519 {
520 $this->primary_key=$primary_key;
521 return $this;
522 }
523 /**
524 *
525 * @param array $type
526 * @return $this
527 */
528 public function set_type($type)
529 {
530 $this->type=$type;
531 return $this;
532 }
533
534
535}
536
537?>
noalyss_trim($p_string)
$input_from cn
$from_poste name
_("actif, passif,charge,...")
$input_from type
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.
count($p_where="", $p_array=null)
load()
Load the current row return false if not found.
to_row($p_array)
update the data member of current object with the value from the array.
get_object($p_ret, $idx)
set_virtual_col($col_name, $sql_expression)
add a virtual column (formatted column, sum of 2 col, ...)
$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 inclu...
getp($cols)
set the value thanks the alias name instead of the colum name if not found try the column name
$name
Database connection.
save()
Insert or update : if the row already exists, update otherwise insert.
__set($cols, $p_value)
$default
defaullt date format
$type
Column name of the primary key.
get_a_virtual_col()
return array of virtual cols (alias calculated, formatted cols)
setp($cols, $p_value)
set the value thanks the alias name instead of the colum name, if not found try the column name
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
const EXC_DATA_SQL
Definition constant.php:360
$count