Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_temp_bank_sql.php
Go to the documentation of this file.
1 <?php
2 /*
3  * This file is part of NOALYSS.
4  *
5  * NOALYSS is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * NOALYSS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with NOALYSS; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /**
20  *@file
21  *@brief Manage the table importbank.temp_bank
22  *
23  *
24 Example
25 @code
26 
27 @endcode
28  */
29 require_once NOALYSS_INCLUDE.'/lib/class_database.php';
30 require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
31 
32 
33 /**
34  *@brief Manage the table importbank.temp_bank
35 */
37  {
38  /* example private $variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0); */
39 
40  protected $variable=array("id"=>"id","tp_date"=>"tp_date"
41  ,"jrn_def_id"=>"jrn_def_id"
42  ,"libelle"=>"libelle"
43  ,"amount"=>"amount"
44  ,"ref_operation"=>"ref_operation"
45  ,"status"=>"status"
46  ,"import_id"=>"import_id"
47  ,"tp_third"=>"tp_third"
48  ,"tp_extra"=>"tp_extra"
49  ,"f_id"=>"f_id"
50  ,"tp_rec"=>"tp_rec"
51  ,"tp_error_msg"=>"tp_error_msg"
52  );
53  function __construct ( & $p_cn,$p_id=-1)
54  {
55  $this->cn=$p_cn;
56  $this->id=$p_id;
57 
58  if ( $p_id == -1 )
59  {
60  /* Initialize an empty object */
61  foreach ($this->variable as $key=>$value) $this->$value=null;
62  $this->id=$p_id;
63  }
64  else
65  {
66  /* load it */
67 
68  $this->load();
69  }
70  }
71  public function get_parameter($p_string)
72  {
73  if ( array_key_exists($p_string,$this->variable) )
74  {
75  $idx=$this->variable[$p_string];
76  return $this->$idx;
77  }
78  else
79  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
80  }
81  public function set_parameter($p_string,$p_value)
82  {
83  if ( array_key_exists($p_string,$this->variable) )
84  {
85  $idx=$this->variable[$p_string];
86  $this->$idx=$p_value;
87  }
88  else
89  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
90  }
91  public function get_info()
92  {
93  return var_export($this,true);
94  }
95  public function verify()
96  {
97  // Verify that the elt we want to add is correct
98  /* verify only the datatype */
99  if ( trim($this->tp_date) == '') $this->tp_date=null;
100  if ( trim($this->jrn_def_id) == '') $this->jrn_def_id=null;
101  if ( $this->jrn_def_id!== null && settype($this->jrn_def_id,'float') == false )
102  throw new Exception('DATATYPE jrn_def_id $this->jrn_def_id non numerique');
103  if ( trim($this->libelle) == '') $this->libelle=null;
104  if ( trim($this->amount) == '') $this->amount=null;
105  if ( $this->amount!== null && settype($this->amount,'float') == false )
106  throw new Exception('DATATYPE amount $this->amount non numerique');
107  if ( trim($this->ref_operation) == '') $this->ref_operation=null;
108  if ( trim($this->status) == '') $this->status=null;
109  if ( trim($this->import_id) == '') $this->import_id=null;
110  if ( $this->import_id!== null && settype($this->import_id,'float') == false )
111  throw new Exception('DATATYPE import_id $this->import_id non numerique');
112  if ( trim($this->tp_third) == '') $this->tp_third=null;
113  if ( trim($this->tp_extra) == '') $this->tp_extra=null;
114  if ( trim($this->f_id) == '') $this->f_id=null;
115  if ( $this->f_id!== null && settype($this->f_id,'float') == false )
116  throw new Exception('DATATYPE f_id $this->f_id non numerique');
117 
118 
119  }
120  public function save()
121  {
122  /* please adapt */
123  if ( $this->id == -1 )
124  $this->insert();
125  else
126  $this->update();
127  }
128  /**
129  *@brief retrieve array of object thanks a condition
130  *@param $cond condition (where clause) (optional by default all the rows are fetched)
131  * you can use this parameter for the order or subselect
132  *@param $p_array array for the SQL stmt
133  *@see Database::exec_sql get_object Database::num_row
134  *@return the return value of exec_sql
135  */
136  public function seek($cond='',$p_array=null)
137  {
138  $sql="select * from importbank.temp_bank $cond";
139  $aobj=array();
140  $ret= $this->cn->exec_sql($sql,$p_array);
141  return $ret;
142  }
143  /**
144  *get_seek return the next object, the return of the query must have all the column
145  * of the object
146  *@param $p_ret is the return value of an exec_sql
147  *@param $idx is the index
148  *@see seek
149  *@return object
150  */
151  public function get_object($p_ret,$idx)
152  {
153  // map each row in a object
154  $oobj=new Temp_Bank_sql ($this->cn);
155  $array=Database::fetch_array($p_ret,$idx);
156  foreach ($array as $idx=>$value)
157  {
158  $oobj->$idx=$value;
159  }
160  $aobj[]=clone $oobj;
161 
162  return $aobj;
163  }
164  public function insert()
165  {
166  if ( $this->verify() != 0 ) return;
167  if ( $this->id==-1 )
168  {
169  /* please adapt */
170  $sql="insert into importbank.temp_bank(tp_date
171  ,jrn_def_id
172  ,libelle
173  ,amount
174  ,ref_operation
175  ,status
176  ,import_id
177  ,tp_third
178  ,tp_extra
179  ,f_id
180  ,tp_rec
181  ,tp_error_msg
182  ) values (to_date($1,'DD.MM.YYYY')
183  ,$2
184  ,$3
185  ,$4
186  ,$5
187  ,$6
188  ,$7
189  ,$8
190  ,$9
191  ,$10
192  ,$11
193  ,$12
194 
195  ) returning id";
196 
197  $this->id=$this->cn->get_value(
198  $sql,
199  array( $this->tp_date
200  ,$this->jrn_def_id
201  ,$this->libelle
202  ,$this->amount
203  ,$this->ref_operation
204  ,$this->status
205  ,$this->import_id
206  ,$this->tp_third
207  ,$this->tp_extra
208  ,$this->f_id
209  ,$this->tp_rec
210  ,$this->tp_error_msg
211 
212  )
213  );
214  }
215  else
216  {
217  $sql="insert into importbank.temp_bank(tp_date
218  ,jrn_def_id
219  ,libelle
220  ,amount
221  ,ref_operation
222  ,status
223  ,import_id
224  ,tp_third
225  ,tp_extra
226  ,f_id
227  ,id) values (to_date($1,'DD.MM.YYYY')
228  ,$2
229  ,$3
230  ,$4
231  ,$5
232  ,$6
233  ,$7
234  ,$8
235  ,$9
236  ,$10
237  ,$11
238  ,$12
239  ,$13
240  ) returning id";
241 
242  $this->id=$this->cn->get_value(
243  $sql,
244  array( $this->tp_date
245  ,$this->jrn_def_id
246  ,$this->libelle
247  ,$this->amount
248  ,$this->ref_operation
249  ,$this->status
250  ,$this->import_id
251  ,$this->tp_third
252  ,$this->tp_extra
253  ,$this->f_id
254  ,$this->id
255  ,$this->tp_rec
256  ,$this->tp_error_msg
257  )
258  );
259 
260  }
261 
262  }
263 
264  public function update()
265  {
266 
267  if ( $this->verify() != 0 ) return;
268  /* please adapt */
269  $sql=" update importbank.temp_bank set tp_date =to_date($1,'DD.MM.YYYY')
270  ,jrn_def_id = $2
271  ,libelle = $3
272  ,amount = $4
273  ,ref_operation = $5
274  ,status = $6
275  ,import_id = $7
276  ,tp_third = $8
277  ,tp_extra = $9
278  ,f_id = $10
279  ,tp_rec=$11
280  ,tp_error_msg=$12
281  where id= $13";
282  $res=$this->cn->exec_sql(
283  $sql,
284  array($this->tp_date
285  ,$this->jrn_def_id
286  ,$this->libelle
287  ,$this->amount
288  ,$this->ref_operation
289  ,$this->status
290  ,$this->import_id
291  ,$this->tp_third
292  ,$this->tp_extra
293  ,$this->f_id
294  ,$this->tp_rec
295  ,$this->tp_error_msg
296  ,$this->id)
297  );
298 
299  }
300  /**
301  *@brief load a object
302  *@return 0 on success -1 the object is not found
303  */
304  public function load()
305  {
306 
307  $sql="select to_char(tp_date,'DD.MM.YYYY') as tp_date
308  ,jrn_def_id
309  ,libelle
310  ,amount
311  ,ref_operation
312  ,status
313  ,import_id
314  ,tp_third
315  ,tp_extra
316  ,f_id
317  ,tp_rec
318  ,tp_error_msg
319  from importbank.temp_bank where id=$1";
320  /* please adapt */
321  $res=$this->cn->get_array(
322  $sql,
323  array($this->id)
324  );
325 
326  if ( count($res) == 0 )
327  {
328  /* Initialize an empty object */
329  foreach ($this->variable as $key=>$value) $this->$key='';
330 
331  return -1;
332  }
333  foreach ($res[0] as $idx=>$value)
334  {
335  $this->$idx=$value;
336  }
337  return 0;
338  }
339 
340  public function delete()
341  {
342  $sql="delete from importbank.temp_bank where id=$1";
343  $res=$this->cn->exec_sql($sql,array($this->id));
344  }
345  /**
346  * Unit test for the class
347  */
348  static function test_me()
349  {
350  $cn=new Database(25);
351  $cn->start();
352  echo h2info('Test object vide');
353  $obj=new Temp_Bank_sql($cn);
354  var_dump($obj);
355 
356  echo h2info('Test object NON vide');
357  $obj->set_parameter('j_id',3);
358  $obj->load();
359  var_dump($obj);
360 
361  echo h2info('Update');
362  $obj->set_parameter('j_qcode','NOUVEAU CODE');
363  $obj->save();
364  $obj->load();
365  var_dump($obj);
366 
367  echo h2info('Insert');
368  $obj->set_parameter('j_id',0);
369  $obj->save();
370  $obj->load();
371  var_dump($obj);
372 
373  echo h2info('Delete');
374  $obj->delete();
375  echo (($obj->load()==0)?'Trouve':'non trouve');
376  var_dump($obj);
377  $cn->rollback();
378 
379  }
380 
381  }
382 // Temp_Bank_sql::test_me();
383 ?>
$ret
Manage the table importbank.temp_bank.
get_object($p_ret, $idx)
get_seek return the next object, the return of the query must have all the column of the object ...
__construct(&$p_cn, $p_id=-1)
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
load()
load a object
if(isset($_POST['remove'])) $array
static test_me()
Unit test for the class.
$key
$res
set_parameter($p_string, $p_value)
get_parameter($p_string)
$sql
global $cn