noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
table_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 * @file
24 * @brief interface : for creating ORM Object
25 */
26/**
27 * @class Table_Data_SQL
28 * @brief Interface : this wrapper is used to created easily a wrapper to a table (ORM)
29 * You must create a class extending this one, in the constructor
30 * these variables have to be defined
31 *
32 * - table = name of the view or empty
33 * - select = name of the select
34 * - name = array of column name, match between logic and actual name
35 * - type = array , match between column and type of data
36 * - default = array of column with a default value
37 * - date_format = format of the date
38 * - virtual_col
39 * After you call the parent constructor
40 * @note the view or the table must include an unique key, otherwise the load
41 * doesn't work.
42 *
43 * @class Table_Data_SQL
44 * Match a table or a view into an object, you need to add the code for each table
45 * @note : the primary key must be an integer
46 *
47 * @code
48 class table_name_sql extends Table_Data_SQL
49 {
50
51 function __construct($p_id=-1)
52 {
53 $this->table = "schema.table";
54 $this->primary_key = "o_id";
55
56 $this->name=array(
57 "id"=>"o_id",
58 "program"=>"o_prog",
59 "date"=>"o_date",
60 "qcode"=>"o_qcode",
61 "fiche"=>"f_id",
62
63
64 );
65
66 $this->type = array(
67 "o_id"=>"numeric",
68 "o_prog"=>"numeric",
69 "o_date"=>"date",
70 "o_qcode"=>"text",
71 "f_id"=>"numeric",
72
73 );
74
75 $this->default = array(
76 "o_id" => "auto",
77 );
78 $this->date_format = "DD.MM.YYYY";
79 global $cn;
80
81 parent::__construct($cn,$p_id);
82 }
83
84 }
85 * @endcode
86 * it is also possible to use set_virtual_col to format data directly in the
87 * SQL
88 * @see Data_SQL
89 @code
90 $jrn->set_virtual_col("str_date", " to_char(jr_tech_date,'DD/MM/YY HH24:MI')");
91 @endcode
92 */
93#[AllowDynamicProperties]
94abstract class Table_Data_SQL extends Data_SQL
95{
96
97
98 function __construct($p_cn, $p_id=-1)
99 {
100 parent::__construct($p_cn, $p_id);
101
102 }
103 public function __toString(): string
104 {
105 $ret=" members : ";
106 foreach ($this->name as $name) {
107 $ret.="[ $name => {$this->$name} ]";
108 }
109
110 $ret.="| type ".var_export($this->type,true);
111 $ret.="| default ".var_export($this->default,true);
112 $ret.="| primary key ".$this->primary_key;
113 $ret.="| date_format ".$this->date_format;
114 $ret.="| a_virtual_col".var_export($this->a_virtual_col,true);
115 return $ret;
116 }
117
118
119 public function insert()
120 {
121 $this->verify();
122 $sql="insert into ".$this->table." ( ";
123 $sep="";
124 $par="";
125 $idx=1;
126 $array=array();
127 foreach ($this->name as $key=> $value)
128 {
129 if (isset($this->default[$value])&&$this->default[$value]=="auto"&&$this->$value==null)
130 continue;
131 // prob. with table when the pk is not auto.
132 if ($value==$this->primary_key && $this->$value==-1 && isset($this->default[$value]) )
133 continue;
134 $sql.=$sep.$value;
135 switch ($this->type[$value])
136 {
137 case "date":
138 if ($this->date_format=="")
139 throw new Exception('Format Date invalide');
140 $par .=$sep.'to_timestamp($'.$idx.",'".$this->date_format."')";
141 break;
142 default:
143 $par .= $sep."$".$idx;
144 }
145
146 $array[]=$this->$value;
147 $sep=",";
148 $idx++;
149 }
150 $sql.=") values (".$par.") returning ".$this->primary_key;
152 $returning=$this->cn->get_value($sql, $array);
153 $this->$pk=$returning;
154 }
155
156 public function delete()
157 {
159 $sql=" delete from ".$this->table." where ".$this->primary_key."= $1";
160 $this->cn->exec_sql($sql,array($this->$pk));
161 }
162 /**
163 * @brief update the value of a column with an expression for $value for the current record
164 * @param $column_exp string like column = $1 or column=function($1)
165 * @param $value value replacing $1
166 * @note you can use it for entering a date with the hour and minute like this
167 * $this->column_update("field_date = to_date($1,'DD.MM.YY HH24:MI'),date('d.m.Y H:i'))
168 *
169 * @return Table_Data_SQL
170 */
171 function column_update($column_expr,$value) {
173 $sql="update ".$this->table." set {$column_expr} where {$this->primary_key} = $2";
174 $this->cn->exec_sql($sql,[$value,$this->$pk]);
175 return $this;
176
177 }
178 /**
179 * @brief update the row but not the column with a default value
180 */
181 public function update()
182 {
183 $this->verify();
185 $sql="update ".$this->table." ";
186 $sep="";
187 $idx=1;
188 $array=array();
189 $set=" set ";
190 foreach ($this->name as $key=> $value) {
191 if (isset($this->default[$value])&&$this->default[$value]=="auto")
192 continue;
193 switch ($this->type[$value])
194 {
195 case "date":
196 $par=$value.'=to_timestamp($'.$idx.",'".$this->date_format."')";
197 break;
198 default:
199 $par=$value."= $".$idx;
200 }
201 $sql.=$sep." $set ".$par;
202 $array[]=$this->$value;
203 $sep=",";
204 $set="";
205 $idx++;
206 }
207 $array[]=$this->$pk;
208 $sql.=" where ".$this->primary_key." = $".$idx;
209 $this->cn->exec_sql($sql, $array);
210 }
211 /***
212 * @brief load a row , corresponding to the primary key
213 */
214 public function load():bool
215 {
216
218 // primary cannot be null or empty
219 if (trim($this->$pk??"")==="" || $this->$pk===null) {
220 $this->$pk=-1;
221 return false;
222 }
223 $sql=$this->build_query();
224 $sql.=" where ".$this->primary_key." = $1";
225 $result=$this->cn->get_row($sql,array ($this->$pk));
226 if ($this->cn->count()==0)
227 {
228 $this->$pk=-1;
229 return false;
230 }
231
232 foreach ($result as $key=> $value)
233 {
234 $this->$key=$value;
235 }
236 return true;
237 }
238
239
240 /**
241 * @brief retrieve array of object thanks a condition
242 * @param $cond condition (where clause) (optional by default all the rows are fetched)
243 * you can use this parameter for the order or subselect
244 * @param $p_array array for the SQL stmt
245 * @see Database::exec_sql get_object Database::num_row
246 * @return the return value of exec_sql
247 */
248 function seek($cond='', $p_array=null)
249 {
250 $sql=$this->build_query();
251 $sql.=" $cond ";
252 $ret=$this->cn->exec_sql($sql, $p_array);
253 return $ret;
254 }
255
256
257 /**
258 * @brief return the number of count in the table corresponding to the where condition
259 * @param string $p_where the condition appended to the SQL select query , where must be given
260 * @param array $p_array variable from the $p_where condition
261 * @return type
262 */
263 public function count($p_where="",$p_array=null) {
264 $count=$this->cn->get_value("select count(*) from $this->table ".$p_where,$p_array);
265 return $count;
266 }
267 /**
268 *@brief Count the number of record with the id ,
269 * @return integer 0 doesn't exist , 1 exists
270 */
271 public function exist() {
273 $count=$this->cn->get_value("select count(*) from ".$this->table." where ".$this->primary_key."=$1",array($this->$pk));
274 return $count;
275 }
276
277 /**
278 * @brief Build the SQL select statement for querying the object and returns it
279 * @return string Query of the object
280 */
281 public function build_query()
282 {
283 $sql=" select ";
284 $sep="";
285 foreach ($this->name as $key) {
286 switch ($this->type[$key])
287 {
288 case "date":
289 $sql .= $sep.'to_char('.$key.",'".$this->date_format."') as ".$key;
290 break;
291 default:
292 $sql.=$sep.$key;
293 }
294 $sep=",";
295 }
297 /**
298 * add virtual column
299 */
300 if ( ! empty( $this->a_virtual_col)){
301 $nb_virtual_col=count($this->a_virtual_col);
302 $a_col= array_keys($this->a_virtual_col);
303 for ($x=0;$x<$nb_virtual_col ;$x++) {
304 $col=$a_col[$x];
305 $expr=sprintf("$sep %s as %s ",$this->a_virtual_col[$col], $col);
306 $sql.=" $expr ";
307 }
308 }
309 $sql.=" from ".$this->table;
310 return $sql;
311 }
312
313 /**
314 * @brief Get all the row and use the p_key_code are the key value of array.
315 * The key column is usually the primary key or any unique key.
316 * the returns looks like
317 * @code
318 [ID1]=>array( ["PRIMARYKEY"=>"ID1"
319 , "VALUE" => 2]);
320 [ID2]=>array( ["PRIMARYKEY"=>"ID2"
321 , "VALUE" => 2]);
322 @endcode
323 * @note It should be used only for small tables: the array is build row by row
324 * @param string $p_key_col existing and unique key
325 * @param string $p_cond sql cond
326 * @param array $p_array array of value for the SQL condition
327 */
328 public function get_all_to_array($p_key_col,$p_cond="",$p_array=NULL)
329 {
330 $ret=$this->seek($p_cond, $p_array);
331 if ($ret==FALSE)
332 return array();
333 $a_array=Database::fetch_all($ret);
334 $nb_array=count($a_array);
335 $a_result=array();
336 try
337 {
338 for ($i=0; $i<$nb_array; $i++)
339 {
340 if (!isset($a_array[$i][$p_key_col]))
341 {
342 throw new Exception("col not found ".$p_key_col);
343 }
344 $key=$a_array[$i][$p_key_col];
345 if ( isset ($a_result[$key]) ){
346 throw new Exception ("duplicate found col : $key");
347 }
348 $a_result[$key]=$a_array[$i];
349 }
350 }
351 catch (Exception $exc)
352 {
353 record_log($exc);
354 throw $exc;
355 }
356 return $a_result;
357 }
358
359}
360
361?>
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
$input_from cn
$from_poste name
$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.
$name
Database connection.
static fetch_all($ret, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_all
Interface : this wrapper is used to created easily a wrapper to a table (ORM) You must create a class...
update()
update the row but not the column with a default value
count($p_where="", $p_array=null)
return the number of count in the table corresponding to the where condition
build_query()
Build the SQL select statement for querying the object and returns it.
load()
load a row , corresponding to the primary key
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
exist()
Count the number of record with the id ,.
__construct($p_cn, $p_id=-1)
get_all_to_array($p_key_col, $p_cond="", $p_array=NULL)
Get all the row and use the p_key_code are the key value of array.
column_update($column_expr, $value)
update the value of a column with an expression for $value for the current record
$all table
$count