noalyss Version-9
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 *
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 *
87 */
88#[AllowDynamicProperties]
89abstract class Table_Data_SQL extends Data_SQL
90{
91
92
93 function __construct($p_cn, $p_id=-1)
94 {
95 parent::__construct($p_cn, $p_id);
96
97 }
98
99
100 public function insert()
101 {
102 $this->verify();
103 $sql="insert into ".$this->table." ( ";
104 $sep="";
105 $par="";
106 $idx=1;
107 $array=array();
108 foreach ($this->name as $key=> $value)
109 {
110 if (isset($this->default[$value])&&$this->default[$value]=="auto"&&$this->$value==null)
111 continue;
112 // prob. with table when the pk is not auto.
113 if ($value==$this->primary_key && $this->$value==-1 && isset($this->default[$value]) )
114 continue;
115 $sql.=$sep.$value;
116 switch ($this->type[$value])
117 {
118 case "date":
119 if ($this->date_format=="")
120 throw new Exception('Format Date invalide');
121 $par .=$sep.'to_timestamp($'.$idx.",'".$this->date_format."')";
122 break;
123 default:
124 $par .= $sep."$".$idx;
125 }
126
127 $array[]=$this->$value;
128 $sep=",";
129 $idx++;
130 }
131 $sql.=") values (".$par.") returning ".$this->primary_key;
133 $returning=$this->cn->get_value($sql, $array);
134 $this->$pk=$returning;
135 }
136
137 public function delete()
138 {
140 $sql=" delete from ".$this->table." where ".$this->primary_key."= $1";
141 $this->cn->exec_sql($sql,array($this->$pk));
142 }
143 /**
144 * @brief update the value of a column with an expression for $value for the current record
145 * @param $column_exp string like column = $1 or column=function($1)
146 * @param $value value replacing $1
147 * @note you can use it for entering a date with the hour and minute like this
148 * $this->column_update("field_date = to_date($1,'DD.MM.YY HH24:MI'),date('d.m.Y H:i'))
149 *
150 * @return Table_Data_SQL
151 */
152 function column_update($column_expr,$value) {
154 $sql="update ".$this->table." set {$column_expr} where {$this->primary_key} = $2";
155 $this->cn->exec_sql($sql,[$value,$this->$pk]);
156 return $this;
157
158 }
159 public function update()
160 {
161 $this->verify();
163 $sql="update ".$this->table." ";
164 $sep="";
165 $idx=1;
166 $array=array();
167 $set=" set ";
168 foreach ($this->name as $key=> $value) {
169 if (isset($this->default[$value])&&$this->default[$value]=="auto")
170 continue;
171 switch ($this->type[$value])
172 {
173 case "date":
174 $par=$value.'=to_timestamp($'.$idx.",'".$this->date_format."')";
175 break;
176 default:
177 $par=$value."= $".$idx;
178 }
179 $sql.=$sep." $set ".$par;
180 $array[]=$this->$value;
181 $sep=",";
182 $set="";
183 $idx++;
184 }
185 $array[]=$this->$pk;
186 $sql.=" where ".$this->primary_key." = $".$idx;
187 $this->cn->exec_sql($sql, $array);
188 }
189 /***
190 * @brief load a row , corresponding to the primary key
191 */
192 public function load():bool
193 {
194 $sql=$this->build_query();
196 // primary cannot be null or empty
197 if (trim($this->$pk??"")==="" || $this->$pk===null) {
198 $this->pk=-1;
199 return false;
200 }
201
202 $result=$this->cn->get_array($sql,array ($this->$pk));
203 if ($this->cn->count()==0)
204 {
205 $this->$pk=-1;
206 return false;
207 }
208
209 foreach ($result[0] as $key=> $value)
210 {
211 $this->$key=$value;
212 }
213 return true;
214 }
215
216
217 /**
218 * @brief retrieve array of object thanks a condition
219 * @param $cond condition (where clause) (optional by default all the rows are fetched)
220 * you can use this parameter for the order or subselect
221 * @param $p_array array for the SQL stmt
222 * @see Database::exec_sql get_object Database::num_row
223 * @return the return value of exec_sql
224 */
225 function seek($cond='', $p_array=null)
226 {
227 $sql="select * from ".$this->table." $cond";
228 $ret=$this->cn->exec_sql($sql, $p_array);
229 return $ret;
230 }
231
232
233 /**
234 * @brief return the number of count in the table corresponding to the where condition
235 * @param string $p_where the condition appended to the SQL select query , where must be given
236 * @param array $p_array variable from the $p_where condition
237 * @return type
238 */
239 public function count($p_where="",$p_array=null) {
240 $count=$this->cn->get_value("select count(*) from $this->table ".$p_where,$p_array);
241 return $count;
242 }
243 /**
244 *@brief Count the number of record with the id ,
245 * @return integer 0 doesn't exist , 1 exists
246 */
247 public function exist() {
249 $count=$this->cn->get_value("select count(*) from ".$this->table." where ".$this->primary_key."=$1",array($this->$pk));
250 return $count;
251 }
252
253 /**
254 * @brief Build the SQL select statement for querying the object and returns it
255 * @return string Query of the object
256 */
257 public function build_query()
258 {
259 $sql=" select ";
260 $sep="";
261 foreach ($this->name as $key) {
262 switch ($this->type[$key])
263 {
264 case "date":
265 $sql .= $sep.'to_char('.$key.",'".$this->date_format."') as ".$key;
266 break;
267 default:
268 $sql.=$sep.$key;
269 }
270 $sep=",";
271 }
273 $sql.=" from ".$this->table;
274
275 $sql.=" where ".$this->primary_key." = $1";
276
277 return $sql;
278 }
279 /**
280 * @brief Get all the row and use the p_key_code are the key value of array.
281 * The key column is usually the primary key or any unique key.
282 * the returns looks like
283 * @code
284 [ID1]=>array( ["PRIMARYKEY"=>"ID1"
285 , "VALUE" => 2]);
286 [ID2]=>array( ["PRIMARYKEY"=>"ID2"
287 , "VALUE" => 2]);
288 @endcode
289 * @note It should be used only for small tables: the array is build row by row
290 * @param string $p_key_col existing and unique key
291 * @param string $p_cond sql cond
292 * @param array $p_array array of value for the SQL condition
293 */
294 public function get_all_to_array($p_key_col,$p_cond="",$p_array=NULL)
295 {
296 $ret=$this->seek($p_cond, $p_array);
297 if ($ret==FALSE)
298 return array();
299 $a_array=Database::fetch_all($ret);
300 $nb_array=count($a_array);
301 $a_result=array();
302 try
303 {
304 for ($i=0; $i<$nb_array; $i++)
305 {
306 if (!isset($a_array[$i][$p_key_col]))
307 {
308 throw new Exception("col not found ".$p_key_col);
309 }
310 $key=$a_array[$i][$p_key_col];
311 if ( isset ($a_result[$key]) ){
312 throw new Exception ("duplicate found col : $key");
313 }
314 $a_result[$key]=$a_array[$i];
315 }
316 }
317 catch (Exception $exc)
318 {
319 echo $exc->getMessage();
320 record_log($exc->getMessage());
321 record_log($exc->getTraceAsString());
322 throw $exc;
323 }
324 return $a_result;
325 }
326
327}
328
329?>
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
$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.
static fetch_all($ret)
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...
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 the current row return false if not found.
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