Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_format_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.format_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.format_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","format_name"=>"format_name"
41  ,"jrn_def_id"=>"jrn_def_id"
42  ,"pos_lib"=>"pos_lib"
43  ,"pos_amount"=>"pos_amount"
44  ,"pos_date"=>"pos_date"
45  ,"pos_operation_nb"=>"pos_operation_nb"
46  ,"pos_third"=>"pos_third"
47  ,"pos_extra"=>"pos_extra"
48  ,"sep_decimal"=>"sep_decimal"
49  ,"sep_thousand"=>"sep_thousand"
50  ,"sep_field"=>"sep_field"
51  ,"format_date"=>"format_date"
52  ,"nb_col"=>"nb_col"
53  ,"skip"=>"skip"
54  );
55  function __construct ( & $p_cn,$p_id=-1)
56  {
57  $this->cn=$p_cn;
58  $this->id=$p_id;
59 
60  if ( $p_id == -1 )
61  {
62  /* Initialize an empty object */
63  foreach ($this->variable as $key=>$value) $this->$value=NULL;
64  $this->id=$p_id;
65  }
66  else
67  {
68  /* load it */
69 
70  $this->load();
71  }
72  }
73  public function get_parameter($p_string)
74  {
75  if ( array_key_exists($p_string,$this->variable) )
76  {
77  $idx=$this->variable[$p_string];
78  return $this->$idx;
79  }
80  else
81  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
82  }
83  public function set_parameter($p_string,$p_value)
84  {
85  if ( array_key_exists($p_string,$this->variable) )
86  {
87  $idx=$this->variable[$p_string];
88  $this->$idx=$p_value;
89  }
90  else
91  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
92  }
93  public function get_info()
94  {
95  return var_export($this,true);
96  }
97  public function verify()
98  {
99  // Verify that the elt we want to add is correct
100  /* verify only the datatype */
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 ( $this->pos_lib !== null && settype($this->pos_lib,'float') == false )
104  throw new Exception('DATATYPE pos_lib $this->pos_lib non numerique');
105  if ( $this->pos_amount !== null && settype($this->pos_amount,'float') == false )
106  throw new Exception('DATATYPE pos_amount $this->pos_amount non numerique');
107  if ( $this->pos_date !== null && settype($this->pos_date,'float') == false )
108  throw new Exception('DATATYPE pos_date $this->pos_date non numerique');
109  if ( $this->pos_operation_nb !== null && settype($this->pos_operation_nb,'float') == false )
110  throw new Exception('DATATYPE pos_operation_nb $this->pos_operation_nb non numerique');
111  if ($this->nb_col !== null && settype($this->nb_col,'float') == false )
112  throw new Exception('DATATYPE nb_col $this->nb_col non numerique');
113 
114  }
115  public function save()
116  {
117  /* please adapt */
118  if ( $this->id == -1 )
119  $this->insert();
120  else
121  $this->update();
122  }
123  /**
124  *@brief retrieve array of object thanks a condition
125  *@param $cond condition (where clause) (optional by default all the rows are fetched)
126  * you can use this parameter for the order or subselect
127  *@param $p_array array for the SQL stmt
128  *@see Database::exec_sql get_object Database::num_row
129  *@return the return value of exec_sql
130  */
131  public function seek($cond='',$p_array=null)
132  {
133  $sql="select * from importbank.format_bank $cond";
134  $aobj=array();
135  $ret= $this->cn->exec_sql($sql,$p_array);
136  return $ret;
137  }
138  /**
139  *get_seek return the next object, the return of the query must have all the column
140  * of the object
141  *@param $p_ret is the return value of an exec_sql
142  *@param $idx is the index
143  *@see seek
144  *@return object
145  */
146  public function get_object($p_ret,$idx)
147  {
148  // map each row in a object
149  $oobj=new Format_Bank_sql ($this->cn);
150  $array=Database::fetch_array($p_ret,$idx);
151  foreach ($array as $idx=>$value)
152  {
153  $oobj->$idx=$value;
154  }
155  $aobj[]=clone $oobj;
156 
157  return $aobj;
158  }
159  public function insert()
160  {
161  if ( $this->verify() != 0 ) return;
162  if ( strlen(trim($this->format_name))==0)
163  {
164  $get_id=$this->cn->get_value('select id from importbank.format_bank where format_name=$1',array($this->format_name));
165  if ( $this->cn->size() == 1) {
166  $this->update();
167  return;
168  }
169 
170  }
171  if ( $this->id==-1 )
172  {
173  /* please adapt */
174  $sql="insert into importbank.format_bank(format_name
175  ,jrn_def_id
176  ,pos_lib
177  ,pos_amount
178  ,pos_date
179  ,pos_operation_nb
180  ,pos_third
181  ,pos_extra
182  ,sep_decimal
183  ,sep_thousand
184  ,sep_field
185  ,format_date
186  ,nb_col
187  ,skip
188  ) values ($1
189  ,$2
190  ,$3
191  ,$4
192  ,$5
193  ,$6
194  ,$7
195  ,$8
196  ,$9
197  ,$10
198  ,$11
199  ,$12
200  ,$13
201  ,$14
202  ) returning id";
203 
204  $this->id=$this->cn->get_value(
205  $sql,
206  array( $this->format_name
207  ,$this->jrn_def_id
208  ,$this->pos_lib
209  ,$this->pos_amount
210  ,$this->pos_date
211  ,$this->pos_operation_nb
212  ,$this->pos_third
213  ,$this->pos_extra
214  ,$this->sep_decimal
215  ,$this->sep_thousand
216  ,$this->sep_field
217  ,$this->format_date
218  ,$this->nb_col
219  ,$this->skip
220  )
221  );
222  }
223  else
224  {
225  $sql="insert into importbank.format_bank(format_name
226  ,jrn_def_id
227  ,pos_lib
228  ,pos_amount
229  ,pos_date
230  ,pos_operation_nb
231  ,pos_third
232  ,pos_extra
233  ,sep_decimal
234  ,sep_thousand
235  ,sep_field
236  ,format_date
237  ,nb_col
238  ,id
239  ,skip
240  ) values ($1
241  ,$2
242  ,$3
243  ,$4
244  ,$5
245  ,$6
246  ,$7
247  ,$8
248  ,$9
249  ,$10
250  ,$11
251  ,$12
252  ,$13
253  ,$14
254  ,$15
255  ) returning id";
256 
257  $this->id=$this->cn->get_value(
258  $sql,
259  array( $this->format_name
260  ,$this->jrn_def_id
261  ,$this->pos_lib
262  ,$this->pos_amount
263  ,$this->pos_date
264  ,$this->pos_operation_nb
265  ,$this->pos_third
266  ,$this->pos_extra
267  ,$this->sep_decimal
268  ,$this->sep_thousand
269  ,$this->sep_field
270  ,$this->format_date
271  ,$this->nb_col
272  ,$this->id
273  ,$this->skip
274  )
275  );
276 
277  }
278 
279  }
280 
281  public function update()
282  {
283  if ( $this->verify() != 0 ) return;
284  /* please adapt */
285  $sql=" update importbank.format_bank set format_name = $1
286  ,jrn_def_id = $2
287  ,pos_lib = $3
288  ,pos_amount = $4
289  ,pos_date = $5
290  ,pos_operation_nb = $6
291  ,pos_third = $7
292  ,pos_extra=$8
293  ,sep_decimal = $9
294  ,sep_thousand = $10
295  ,sep_field = $11
296  ,format_date = $12
297  ,nb_col = $13
298  ,skip=$14
299  where id= $15";
300  $res=$this->cn->exec_sql(
301  $sql,
302  array($this->format_name
303  ,$this->jrn_def_id
304  ,$this->pos_lib
305  ,$this->pos_amount
306  ,$this->pos_date
307  ,$this->pos_operation_nb
308  ,$this->pos_third
309  ,$this->pos_extra
310  ,$this->sep_decimal
311  ,$this->sep_thousand
312  ,$this->sep_field
313  ,$this->format_date
314  ,$this->nb_col
315  ,$this->skip
316  ,$this->id)
317  );
318 
319  }
320  /**
321  *@brief load a object
322  *@return 0 on success -1 the object is not found
323  */
324  public function load()
325  {
326 
327  $sql="select format_name
328  ,jrn_def_id
329  ,pos_lib
330  ,pos_amount
331  ,pos_date
332  ,pos_operation_nb
333  ,pos_third
334  ,pos_extra
335  ,sep_decimal
336  ,sep_thousand
337  ,sep_field
338  ,format_date
339  ,nb_col
340  ,skip
341  from importbank.format_bank where id=$1";
342  /* please adapt */
343  $res=$this->cn->get_array(
344  $sql,
345  array($this->id)
346  );
347 
348  if ( count($res) == 0 )
349  {
350  /* Initialize an empty object */
351  foreach ($this->variable as $key=>$value) $this->$key='';
352 
353  return -1;
354  }
355  foreach ($res[0] as $idx=>$value)
356  {
357  $this->$idx=$value;
358  }
359  return 0;
360  }
361 
362  public function delete()
363  {
364  $sql="delete from importbank.format_bank where id=$1";
365  $res=$this->cn->exec_sql($sql,array($this->id));
366  }
367  /**
368  * Unit test for the class
369  */
370  static function test_me()
371  {
372 
373  }
374 
375  }
376 // Format_Bank_sql::test_me();
377 ?>
$ret
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)
static test_me()
Unit test for the class.
Manage the table importbank.format_bank.
if(isset($_POST['remove'])) $array
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
$key
set_parameter($p_string, $p_value)
$res
$sql