dolibarr  7.0.0-beta
DoliDB.class.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2013-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
4  * Copyright (C) 2014-2015 Laurent Destailleur <eldy@users.sourceforge.net>
5  *
6  * This program 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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
25 require_once DOL_DOCUMENT_ROOT .'/core/db/Database.interface.php';
26 
30 abstract class DoliDB implements Database
31 {
33  public $db;
35  public $type;
37  public $forcecharset='utf8';
39  public $forcecollate='utf8_unicode_ci';
41  private $_results;
43  public $connected;
45  public $database_selected;
47  public $database_name;
49  public $database_user;
51  public $database_host;
53  public $database_port;
55  public $transaction_opened;
57  public $lastquery;
59  public $lastqueryerror;
61  public $lasterror;
63  public $lasterrno;
64 
66  public $ok;
68  public $error;
69 
78  function ifsql($test,$resok,$resko)
79  {
80  return 'IF('.$test.','.$resok.','.$resko.')';
81  }
82 
90  function idate($param)
91  {
92  // TODO GMT $param should be gmt, so we should add tzouptut to 'gmt'
93  return dol_print_date($param,"%Y-%m-%d %H:%M:%S");
94  }
95 
101  function lasterrno()
102  {
103  return $this->lasterrno;
104  }
105 
111  function begin()
112  {
113  if (! $this->transaction_opened)
114  {
115  $ret=$this->query("BEGIN");
116  if ($ret)
117  {
118  $this->transaction_opened++;
119  dol_syslog("BEGIN Transaction",LOG_DEBUG);
120  dol_syslog('',0,1);
121  }
122  return $ret;
123  }
124  else
125  {
126  $this->transaction_opened++;
127  dol_syslog('',0,1);
128  return 1;
129  }
130  }
131 
138  function commit($log='')
139  {
140  dol_syslog('',0,-1);
141  if ($this->transaction_opened<=1)
142  {
143  $ret=$this->query("COMMIT");
144  if ($ret)
145  {
146  $this->transaction_opened=0;
147  dol_syslog("COMMIT Transaction".($log?' '.$log:''),LOG_DEBUG);
148  return 1;
149  }
150  else
151  {
152  return 0;
153  }
154  }
155  else
156  {
157  $this->transaction_opened--;
158  return 1;
159  }
160  }
161 
168  function rollback($log='')
169  {
170  dol_syslog('',0,-1);
171  if ($this->transaction_opened<=1)
172  {
173  $ret=$this->query("ROLLBACK");
174  $this->transaction_opened=0;
175  dol_syslog("ROLLBACK Transaction".($log?' '.$log:''),LOG_DEBUG);
176  return $ret;
177  }
178  else
179  {
180  $this->transaction_opened--;
181  return 1;
182  }
183  }
184 
192  function plimit($limit=0,$offset=0)
193  {
194  global $conf;
195  if (empty($limit)) return "";
196  if ($limit < 0) $limit=$conf->liste_limit;
197  if ($offset > 0) return " LIMIT $offset,$limit ";
198  else return " LIMIT $limit ";
199  }
200 
206  function getVersionArray()
207  {
208  return preg_split("/[\.,-]/",$this->getVersion());
209  }
210 
216  function lastquery()
217  {
218  return $this->lastquery;
219  }
220 
228  function order($sortfield=null,$sortorder=null)
229  {
230  if (! empty($sortfield))
231  {
232  $return='';
233  $fields=explode(',',$sortfield);
234  $orders=explode(',',$sortorder);
235  $i=0;
236  foreach($fields as $val)
237  {
238  if (! $return) $return.=' ORDER BY ';
239  else $return.=', ';
240 
241  $return.=preg_replace('/[^0-9a-z_\.]/i','',$val);
242 
243  $tmpsortorder = trim($orders[$i]);
244 
245  // Only ASC and DESC values are valid SQL
246  if (strtoupper($tmpsortorder) === 'ASC') {
247  $return .= ' ASC';
248  } elseif (strtoupper($tmpsortorder) === 'DESC') {
249  $return .= ' DESC';
250  }
251 
252  $i++;
253  }
254  return $return;
255  }
256  else
257  {
258  return '';
259  }
260  }
261 
267  function lasterror()
268  {
269  return $this->lasterror;
270  }
271 
281  function jdate($string, $gm=false)
282  {
283  // TODO GMT must set param gm to true by default
284  if ($string==0 || $string=="0000-00-00 00:00:00") return '';
285  $string=preg_replace('/([^0-9])/i','',$string);
286  $tmp=$string.'000000';
287  $date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
288  return $date;
289  }
290 
296  function lastqueryerror()
297  {
298  return $this->lastqueryerror;
299  }
300 }
301 
Class to manage Dolibarr database access for an SQL database.
lastqueryerror()
Return last query in error.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
lastquery()
Return last request executed with query()
Class to manage Dolibarr database access.
commit($log='')
Validate a database transaction.
ifsql($test, $resok, $resko)
Format a SQL IF.
idate($param)
Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date fiel...
lasterror()
Return last error label.
begin()
Start transaction.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
jdate($string, $gm=false)
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
query($query, $usesavepoint=0, $type= 'auto')
Execute a SQL request and return the resultset.
getVersion()
Return version of database server.
plimit($limit=0, $offset=0)
Define limits and offset of request.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getVersionArray()
Return version of database server into an array.
rollback($log='')
Annulation d'une transaction et retour aux anciennes valeurs.
lasterrno()
Return last error code.
order($sortfield=null, $sortorder=null)
Define sort criteria of request.