YAP  2.5
 All Data Structures Namespaces Files Functions Variables
CYap_Mssql.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_MsSQL extends CYapDB {
77 
78 function CYap_MsSQL()
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 if(($this->DbConn=mssql_connect($DBSystem,$DBUser,$DBPasswd))===FALSE)
103  {
104  // Errore di select del db
105  $this->txtError="Error during connection to the server ".$DBSystem;
106  return(false);
107  }
108 $this->Db_Open=true;
109 return(true);
110 }
117 function SelectDb($DBname)
118 {
119 if(!$this->Db_Open)
120  {
121  $this->txtError='No database open';
122  return(false);
123  }
124 if(!mssql_select_db($DBname,$this->DbConn))
125  {
126  // Errore di select del db
127  $this->txtError="I cannot select the db ".$DBname;
128  return(false);
129  }
130 return(true);
131 }
137 function DbExecSql($sql)
138 {
139 $this->UseLimit=FALSE;
140 $this->LimitStart=0;
141 if(!$this->Db_Open)
142  {
143  $this->txtError='No database open';
144  return(false);
145  }
146 if(($this->DbResult=mssql_query($sql,$this->DbConn))===false)
147  {
148  // Errore di select del db
149  $this->txtError="Error into sql statement. Sql string: ".$sql.".Err:". mssql_get_last_message();
150  return(false);
151  }
152 $this->CurrentRow[$this->DbResult]=0;
153 $this->LastQuery[$this->DbResult]=$sql;
154 return($this->DbResult);
155 }
156 
165 function QueryLimit($select,$start=0,$many=-1)
166 {
167 // If I got an error, exit. The error text is set by DbExecSql
168 // and the caller should know what to do.
169 if(($res=$this->DbExecSql($select))===FALSE)
170  return($res);
171 $this->UseLimit=TRUE;
172 // yap start limi is zero based, the odbc start from 1
173 $this->LimitStart=$start;
174 if($many!=-1)
175  {
176  $this->LimitToEnd=$many;
177  }
178 else
179  $this->LimitToEnd=999999; // I hope that no one perform a select that returns 1000000 rows!
180 $this->LimitEnd=$this->LimitToEnd;
181 $this->CurrentRow[$res]=$this->LimitStart;
182 return($res);
183 }
189 function DbGetNumRow($result)
190 {
191 $nRows=mssql_num_rows($result);
192 if($this->UseLimit)
193  {
194  $newRows=$nRows-$this->LimitStart;
195  $nRows=($newRows>$this->LimitEnd)?$this->LimitEnd:$newRows;
196  }
197 // var_dump($nRows);
198 return($nRows);
199 }
200 
206 function DbGetNumFields($result)
207 {
208 return(mssql_num_fields($result));
209 }
217 function DbFetchField($result,$FieldNo)
218 {
219 $meta=new DbMeta();
220 $meta->name=mssql_field_name($result,$FieldNo);
221 $type=mssql_field_type($result,$FieldNo);
222 $meta->max_length=mssql_field_length($result,$FieldNo);
223 switch($type)
224  {
225  case 'LONGBINARY': // Ole object
226  $meta->type='IGNORE'; // Ignore this field
227  $meta->numeric=0;
228  break;
229  case 'DATETIME': // Date format
230  case 'LONGCHAR': // memo fields
231  case 'CHAR' :
232  case 'VARCHAR' :
233  $meta->type=$type;
234  $meta->numeric=0;
235  break;
236  case 'BIT': // Boolean
237  $meta->type='BOOL';
238  $meta->numeric=0;
239  break;
240  case 'BYTE' :
241  $meta->type='BIT';
242  $meta->numeric=0;
243  break;
244  case 'CURRENCY' :
245  $meta->type='DOUBLE';
246  $meta->numeric=1;
247  break;
248  case 'SMALLINT' :
249  case 'INTEGER' :
250  case 'DOUBLE' :
251  $meta->type=$type;
252  $meta->numeric=1;
253  break;
254  }
255 $meta->table='';
256 return($meta);
257 }
264 function DbFieldFlags($result,$FieldNo)
265 {
266 $type=mssql_field_type($result,$FieldNo);
267 if($type=='COUNTER')
268  $txt='auto_increment';
269 else
270  $txt='';
271 return($txt);
272 }
273 
274 
281 function DbFreeResult($result)
282 {
283 if(!($es=mssql_free_result($result)))
284  $this->txtError=mssql_get_last_message();
285 $this->LastRow[$result]=array();
286 return($es);
287 }
293 function DbGetValue($result)
294 {
295 if(!$this->Db_Open)
296  {
297  $this->txtError='No database open';
298  return(false);
299  }
300 // var_dump($this->UseLimit);
301 // var_dump($this->LimitToEnd);
302 if($this->UseLimit&&!$this->LimitToEnd)
303  return(FALSE);
304 if($this->UseLimit)
305  $this->LimitToEnd--;
306 // var_dump($this->CurrentRow[$result]);
307 if(@mssql_data_seek($result,$this->CurrentRow[$result]))
308  {
309  $this->LastRow[$result]=mssql_fetch_array($result);
310  $this->CurrentRow[$result]++;
311  }
312 else
313  $this->LastRow[$result]=false;
314 return($this->LastRow[$result]);
315 }
320 function Db_Close()
321 {
322 if(!$this->Db_Open)
323  {
324  $this->txtError='No database open';
325  return(false);
326  }
327 mssql_close($this->DbConn);
328 $this->Db_Open=false;
329 return(true);
330 }
331 
332 
341 function DbaddSql($row,$fieldsType,$KeyField)
342 {
343 $Count=0;
344 $fName='';
345 $fValue='';
346 foreach($row as $Key => $Value)
347  {
348  // I Can't update the row key
349  if($Key!=$KeyField)
350  {
351  if($Count)
352  {
353  $fName.=', ';
354  $fValue.=', ';
355  }
356  $fName.='['.$Key.']';
357  $apice=$fieldsType[$Key]['apice'];
358  $fValue.=$apice.$this->DBEscape($Value).$apice;
359  $Count++;
360  }
361  }
362 $OutText='('.$fName.') VALUES ('.$fValue.') ';
363 return($OutText);
364 }
365 
373 function DbmodifySql($row,$fieldsType)
374 {
375 $OutText=' set ';
376 $c=0;
377 foreach($row as $Key => $Value)
378  {
379  if($c) $OutText.=', ';
380  $apice=$fieldsType[$Key]['apice'];
381  $OutText.='['.$Key.'] = '.$apice.$this->DBEscape($Value).$apice.' ';
382  $c++;
383  }
384 return($OutText);
385 }
394 function DbRegexCond($FieldName,$pattern)
395 {
396 return($FieldName.' like \'%'.$pattern.'%\'');
397 }
398 
407 function DbEscape($value)
408 {
409 $quote=(bool)ini_get('magic_quotes_gpc');
410 if(!$quote)
411  $ret=str_replace("'","''",$value);
412 else
413  $ret=str_replace("\\","'",$value);
414 return($ret);
415 }
425 function DbBetweenCond($FieldName,$ValueFrom,$ValueTo,$ValueType)
426 {
427 if($ValueType=='DB_NUMBER')
428  return($FieldName.' between '.$ValueFrom.' and '.$ValueTo);
429 else
430  return($FieldName.' between \''.$ValueFrom.'\' and \''.$ValueTo.'\'');
431 }
432 }
433 ?>