YAP  2.5
 All Data Structures Namespaces Files Functions Variables
CYap_ODBC.php
Go to the documentation of this file.
1 <?PHP
10 /*
11  * +-------------------------------------------------------------------------+
12  * | Yap, Version 2.5.0 |
13  * +-------------------------------------------------------------------------+
14  * | Copyright (c) 2003-2013 Andrioli Darvin |
15  * | Email <darvin (inside) andrioli (dot) com> |
16  * | Web http://www.andrioli.com/en/yap.html |
17  * | Download http://www.phpclasses.org/browse.html/package/1391.html |
18  * | |
19  * +-------------------------------------------------------------------------+
20  * | This library is free software; you can redistribute it and/or modify |
21  * | it under the terms of the GNU Lesser General Public License as |
22  * | published by the Free Software Foundation; either version 2 of the |
23  * | License, or (at your option) any later version. |
24  * | |
25  * | This library is distributed in the hope that it will be useful, but |
26  * | WITHOUT ANY WARRANTY; without even the implied warranty of |
27  * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
28  * | Lesser General Public License for more details. |
29  * | |
30  * | You should have received a copy of the GNU Lesser General Public |
31  * | License along with this library; if not, write to the Free Software |
32  * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
33  * +-------------------------------------------------------------------------+
34  */
38 class CYap_odbc extends CYapDB {
77 
78 function CYap_odbc()
79 {
81 $this->UseLimit=false;
82 $this->LimitStart=0;
83 $this->LastRow=array();
84 $this->CurrentRow=array();
85 }
93 function OpenDb($DBSystem,$DBUser="",$DBPasswd="")
94 {
95 if($this->Db_Open)
96  {
97  $this->txtError='Database already open';
98  return(false);
99  }
100 $this->SaveUser=$DBUser;
101 $this->SavePasswd=$DBPasswd;
102 return(true);
103 }
110 function SelectDb($DBname)
111 {
112 if($this->Db_Open)
113  {
114  $this->txtError='Database already open';
115  return(false);
116  }
117 if(($this->DbConn=odbc_connect($DBname,$this->SaveUser,$this->SavePasswd))===FALSE)
118  {
119  // Errore di select del db
120  $this->txtError="Error during connection to the server ".$DBSystem;
121  return(false);
122  }
123 $this->Db_Open=true;
124 return(true);
125 }
131 function DbExecSql($sql)
132 {
133 $this->UseLimit=FALSE;
134 $this->LimitStart=0;
135 if(!$this->Db_Open)
136  {
137  $this->txtError='No database open';
138  return(false);
139  }
140 if(($this->DbResult=odbc_exec($this->DbConn,$sql))===false)
141  {
142  // Errore di select del db
143  $this->txtError="Error into sql statement. Sql string: ".$sql.".Err:". odbc_errormsg() . ": " . mysql_error();
144  return(false);
145  }
146 $this->CurrentRow[$this->DbResult]=1;
147 $this->LastQuery[$this->DbResult]=$sql;
148 return($this->DbResult);
149 }
150 
159 function QueryLimit($select,$start=0,$many=-1)
160 {
161 // If I got an error, exit. The error text is set by DbExecSql
162 // and the caller should know what to do.
163 if(($res=$this->DbExecSql($select))===FALSE)
164  return($res);
165 $this->UseLimit=TRUE;
166 // yap start limi is zero based, the odbc start from 1
167 $this->LimitStart=$start+1;
168 if($many!=-1)
169  {
170  $this->LimitToEnd=$many;
171  }
172 else
173  $this->LimitToEnd=999999; // I hope that no one perform a select that returns 1000000 rows!
174 $this->LimitEnd=$this->LimitToEnd;
175 $this->CurrentRow[$res]=$this->LimitStart;
176 return($res);
177 }
183 function DbGetNumRow($result)
184 {
185 $nRows=odbc_num_rows($result);
186 if($nRows==-1)
187  {
188  // ODBC without numrows support. Try to guess it
189  $nRows=$this->CalculateNumRows($result);
190  }
191 if($this->UseLimit)
192  {
193  $newRows=$nRows-$this->LimitStart;
194  $nRows=($newRows>$this->LimitEnd)?$this->LimitEnd:$newRows;
195  }
196 return($nRows);
197 }
198 
199 
200 function CalculateNumRows($result)
201 {
202 $min=1;
203 $max=60000; // no more then 60000 rows are returned
204 if(odbc_fetch_into($result,$tmp,$max))
205  return($max);
206 if(!odbc_fetch_into($result,$tmp,$min))
207  return(0); // No row
208 $fine=false;
209 $gap=($min-$max)/2;
210 $corrente=$max;
211 while(!(abs($gap)<1))
212  {
213  $corrente+=floor($gap);
214  if(!odbc_fetch_into($result,$tmp,$corrente))
215  {
216  $gap=($min-$corrente)/2;
217  $max=$corrente;
218  }
219  else
220  {
221  $min=$corrente;
222  $gap=($max-$min)/2;
223  }
224  }
225 odbc_fetch_into($result,$tmp,1);
226 return($min);
227 }
233 function DbGetNumFields($result)
234 {
235 $tmp=odbc_fetch_array($result,1);
236 return(count($tmp));
237 }
245 function DbFetchField($result,$FieldNo)
246 {
247 $meta=new DbMeta();
248 $meta->name=odbc_field_name($result,$FieldNo+1);
249 $type=odbc_field_type($result,$FieldNo+1);
250 $meta->max_length=odbc_field_len($result,$FieldNo+1);
251 switch($type)
252  {
253  case 'LONGBINARY': // Ole object
254  $meta->type='IGNORE'; // Ignore this field
255  $meta->numeric=0;
256  break;
257  case 'DATETIME': // Date format
258  case 'LONGCHAR': // memo fields
259  case 'CHAR' :
260  case 'VARCHAR' :
261  $meta->type=$type;
262  $meta->numeric=0;
263  break;
264  case 'BIT': // Boolean
265  $meta->type='BOOL';
266  $meta->numeric=0;
267  break;
268  case 'BYTE' :
269  $meta->type='BIT';
270  $meta->numeric=0;
271  break;
272  case 'CURRENCY' :
273  $meta->type='DOUBLE';
274  $meta->numeric=1;
275  break;
276  case 'SMALLINT' :
277  case 'INTEGER' :
278  case 'DOUBLE' :
279  $meta->type=$type;
280  $meta->numeric=1;
281  break;
282  }
283 $meta->table='';
284 return($meta);
285 }
292 function DbFieldFlags($result,$FieldNo)
293 {
294 $type=odbc_field_type($result,$FieldNo+1);
295 if($type=='COUNTER')
296  $txt='auto_increment';
297 else
298  $txt='';
299 return($txt);
300 }
301 
302 
309 function DbFreeResult($result)
310 {
311 if(!($es=odbc_free_result($result)))
312  $this->txtError=odbc_errormsg();
313 $this->LastRow[$result]=array();
314 return($es);
315 }
321 function DbGetValue($result)
322 {
323 if(!$this->Db_Open)
324  {
325  $this->txtError='No database open';
326  return(false);
327  }
328 if($this->UseLimit&&!$this->LimitToEnd)
329  return(FALSE);
330 if($this->UseLimit)
331  $this->LimitToEnd--;
332 $this->LastRow[$result]=odbc_fetch_array($result,$this->CurrentRow[$result]);
333 $this->CurrentRow[$result]++;
334 return($this->LastRow[$result]);
335 }
340 function Db_Close()
341 {
342 if(!$this->Db_Open)
343  {
344  $this->txtError='No database open';
345  return(false);
346  }
347 odbc_close($this->DbConn);
348 $this->Db_Open=false;
349 return(true);
350 }
351 
352 
361 function DbaddSql($row,$fieldsType,$KeyField)
362 {
363 $Count=0;
364 $fName='';
365 $fValue='';
366 foreach($row as $Key => $Value)
367  {
368  // I Can't update the row key
369  if($Key!=$KeyField)
370  {
371  if($Count)
372  {
373  $fName.=', ';
374  $fValue.=', ';
375  }
376  $fName.='['.$Key.']';
377  $apice=$fieldsType[$Key]['apice'];
378  $fValue.=$apice.$this->DBEscape($Value).$apice;
379  $Count++;
380  }
381  }
382 $OutText='('.$fName.') VALUES ('.$fValue.') ';
383 return($OutText);
384 }
385 
393 function DbmodifySql($row,$fieldsType)
394 {
395 $OutText=' set ';
396 $c=0;
397 foreach($row as $Key => $Value)
398  {
399  if($c) $OutText.=', ';
400  $apice=$fieldsType[$Key]['apice'];
401  $OutText.='['.$Key.'] = '.$apice.$this->DBEscape($Value).$apice.' ';
402  $c++;
403  }
404 return($OutText);
405 }
414 function DbRegexCond($FieldName,$pattern)
415 {
416 return($FieldName.' like \'%'.$pattern.'%\'');
417 }
418 
427 function DbEscape($value)
428 {
429 $quote=(bool)ini_get('magic_quotes_gpc');
430 if(!$quote)
431  $ret=str_replace("'","''",$value);
432 else
433  $ret=str_replace("\\","'",$value);
434 return($ret);
435 }
445 function DbBetweenCond($FieldName,$ValueFrom,$ValueTo,$ValueType)
446 {
447 if($ValueType=='DB_NUMBER')
448  return($FieldName.' >= '.$ValueFrom.' and '.$FieldName.' <= '.$ValueTo);
449 else
450  return($FieldName.' >= \''.$ValueFrom.'\' and '.$FieldName.' <= \''.$ValueTo.'\'');
451 }
452 }
453 ?>