dolibarr  13.0.2
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 <https://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  public function ifsql($test, $resok, $resko)
79  {
80  return 'IF('.$test.','.$resok.','.$resko.')';
81  }
82 
91  public function idate($param, $gm = 'tzserver')
92  {
93  // TODO $param should be gmt, so we should add $gm to 'gmt' instead of default 'tzserver'
94  return dol_print_date($param, "%Y-%m-%d %H:%M:%S", $gm);
95  }
96 
102  public function lasterrno()
103  {
104  return $this->lasterrno;
105  }
106 
114  public function sanitize($stringtosanitize, $allowsimplequote = 0)
115  {
116  if ($allowsimplequote) {
117  return preg_replace('/[^a-z0-9_\-\.,\']/i', '', $stringtosanitize);
118  } else {
119  return preg_replace('/[^a-z0-9_\-\.,]/i', '', $stringtosanitize);
120  }
121  }
122 
128  public function begin()
129  {
130  if (!$this->transaction_opened)
131  {
132  $ret = $this->query("BEGIN");
133  if ($ret)
134  {
135  $this->transaction_opened++;
136  dol_syslog("BEGIN Transaction", LOG_DEBUG);
137  dol_syslog('', 0, 1);
138  }
139  return $ret;
140  } else {
141  $this->transaction_opened++;
142  dol_syslog('', 0, 1);
143  return 1;
144  }
145  }
146 
153  public function commit($log = '')
154  {
155  dol_syslog('', 0, -1);
156  if ($this->transaction_opened <= 1)
157  {
158  $ret = $this->query("COMMIT");
159  if ($ret)
160  {
161  $this->transaction_opened = 0;
162  dol_syslog("COMMIT Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
163  return 1;
164  } else {
165  return 0;
166  }
167  } else {
168  $this->transaction_opened--;
169  return 1;
170  }
171  }
172 
179  public function rollback($log = '')
180  {
181  dol_syslog('', 0, -1);
182  if ($this->transaction_opened <= 1)
183  {
184  $ret = $this->query("ROLLBACK");
185  $this->transaction_opened = 0;
186  dol_syslog("ROLLBACK Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
187  return $ret;
188  } else {
189  $this->transaction_opened--;
190  return 1;
191  }
192  }
193 
201  public function plimit($limit = 0, $offset = 0)
202  {
203  global $conf;
204  if (empty($limit)) return "";
205  if ($limit < 0) $limit = $conf->liste_limit;
206  if ($offset > 0) return " LIMIT $offset,$limit ";
207  else return " LIMIT $limit ";
208  }
209 
215  public function getVersionArray()
216  {
217  return preg_split("/[\.,-]/", $this->getVersion());
218  }
219 
225  public function lastquery()
226  {
227  return $this->lastquery;
228  }
229 
237  public function order($sortfield = null, $sortorder = null)
238  {
239  if (!empty($sortfield))
240  {
241  $oldsortorder = '';
242  $return = '';
243  $fields = explode(',', $sortfield);
244  $orders = explode(',', $sortorder);
245  $i = 0;
246  foreach ($fields as $val) {
247  if (!$return) $return .= ' ORDER BY ';
248  else $return .= ', ';
249 
250  $return .= preg_replace('/[^0-9a-z_\.]/i', '', $val); // Add field
251 
252  $tmpsortorder = (empty($orders[$i]) ? '' : trim($orders[$i]));
253 
254  // Only ASC and DESC values are valid SQL
255  if (strtoupper($tmpsortorder) === 'ASC') {
256  $oldsortorder = 'ASC';
257  $return .= ' ASC';
258  } elseif (strtoupper($tmpsortorder) === 'DESC') {
259  $oldsortorder = 'DESC';
260  $return .= ' DESC';
261  } else {
262  $return .= ' '.($oldsortorder ? $oldsortorder : 'ASC');
263  }
264 
265  $i++;
266  }
267  return $return;
268  } else {
269  return '';
270  }
271  }
272 
278  public function lasterror()
279  {
280  return $this->lasterror;
281  }
282 
292  public function jdate($string, $gm = 'tzserver')
293  {
294  // TODO $string should be converted into a GMT timestamp, so param gm should be set to true by default instead of false
295  if ($string == 0 || $string == "0000-00-00 00:00:00") return '';
296  $string = preg_replace('/([^0-9])/i', '', $string);
297  $tmp = $string.'000000';
298  $date = dol_mktime((int) substr($tmp, 8, 2), (int) substr($tmp, 10, 2), (int) substr($tmp, 12, 2), (int) substr($tmp, 4, 2), (int) substr($tmp, 6, 2), (int) substr($tmp, 0, 4), $gm);
299  return $date;
300  }
301 
307  public function lastqueryerror()
308  {
309  return $this->lastqueryerror;
310  }
311 
319  public function getRow($sql)
320  {
321  $sql .= ' LIMIT 1;';
322 
323  $res = $this->query($sql);
324  if ($res)
325  {
326  return $this->fetch_object($res);
327  }
328 
329  return false;
330  }
331 
339  public function getRows($sql)
340  {
341  $res = $this->query($sql);
342  if ($res)
343  {
344  $results = array();
345  if ($this->num_rows($res) > 0) {
346  while ($obj = $this->fetch_object($res)) {
347  $results[] = $obj;
348  }
349  }
350  return $results;
351  }
352 
353  return false;
354  }
355 }
getRow($sql)
Return first result from query as object Note : This method executes a given SQL query and retrieves ...
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= 'auto', $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.
ifsql($test, $resok, $resko)
Format a SQL IF.
lasterror()
Return last error label.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
begin()
Start transaction.
num_rows($resultset)
Return number of lines for result of a SELECT.
getRows($sql)
return all results from query as an array of objects Note : This method executes a given SQL query an...
commit($log= '')
Validate a database transaction.
query($query, $usesavepoint=0, $type= 'auto')
Execute a SQL request and return the resultset.
sanitize($stringtosanitize, $allowsimplequote=0)
Sanitize a string for SQL forging.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getVersion()
Return version of database server.
jdate($string, $gm= 'tzserver')
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
plimit($limit=0, $offset=0)
Define limits and offset of request.
getVersionArray()
Return version of database server into an array.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
lasterrno()
Return last error code.
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
rollback($log= '')
Cancel a transaction and go back to initial data values.
idate($param, $gm= 'tzserver')
Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date fiel...