dolibarr  17.0.4
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 $prefix_db;
67 
69  public $ok;
71  public $error;
72 
73 
74 
80  public function prefix()
81  {
82  return (empty($this->prefix_db) ? MAIN_DB_PREFIX : $this->prefix_db);
83  }
84 
93  public function ifsql($test, $resok, $resko)
94  {
95  //return 'IF('.$test.','.$resok.','.$resko.')'; // Not sql standard
96  return '(CASE WHEN '.$test.' THEN '.$resok.' ELSE '.$resko.' END)';
97  }
98 
105  public function hintindex($nameofindex)
106  {
107  return '';
108  }
109 
110 
119  public function regexpsql($subject, $pattern, $sqlstring = false)
120  {
121  if ($sqlstring) {
122  return "(". $subject ." REGEXP '" . $pattern . "')";
123  }
124 
125  return "('". $subject ."' REGEXP '" . $pattern . "')";
126  }
127 
128 
137  public function idate($param, $gm = 'tzserver')
138  {
139  // TODO $param should be gmt, so we should have default $gm to 'gmt' instead of default 'tzserver'
140  return dol_print_date($param, "%Y-%m-%d %H:%M:%S", $gm);
141  }
142 
148  public function lasterrno()
149  {
150  return $this->lasterrno;
151  }
152 
161  public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0)
162  {
163  return preg_replace('/[^a-z0-9_\-\.,'.($allowsequals ? '=' : '').($allowsimplequote ? "\'" : '').']/i', '', $stringtosanitize);
164  }
165 
172  public function begin($textinlog = '')
173  {
174  if (!$this->transaction_opened) {
175  $ret = $this->query("BEGIN");
176  if ($ret) {
177  $this->transaction_opened++;
178  dol_syslog("BEGIN Transaction".($textinlog ? ' '.$textinlog : ''), LOG_DEBUG);
179  dol_syslog('', 0, 1);
180  }
181  return $ret;
182  } else {
183  $this->transaction_opened++;
184  dol_syslog('', 0, 1);
185  return 1;
186  }
187  }
188 
195  public function commit($log = '')
196  {
197  dol_syslog('', 0, -1);
198  if ($this->transaction_opened <= 1) {
199  $ret = $this->query("COMMIT");
200  if ($ret) {
201  $this->transaction_opened = 0;
202  dol_syslog("COMMIT Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
203  return 1;
204  } else {
205  return 0;
206  }
207  } else {
208  $this->transaction_opened--;
209  return 1;
210  }
211  }
212 
219  public function rollback($log = '')
220  {
221  dol_syslog('', 0, -1);
222  if ($this->transaction_opened <= 1) {
223  $ret = $this->query("ROLLBACK");
224  $this->transaction_opened = 0;
225  dol_syslog("ROLLBACK Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
226  return $ret;
227  } else {
228  $this->transaction_opened--;
229  return 1;
230  }
231  }
232 
240  public function plimit($limit = 0, $offset = 0)
241  {
242  global $conf;
243  if (empty($limit)) {
244  return "";
245  }
246  if ($limit < 0) {
247  $limit = $conf->liste_limit;
248  }
249  if ($offset > 0) {
250  return " LIMIT ".((int) $offset).",".((int) $limit)." ";
251  } else {
252  return " LIMIT ".((int) $limit)." ";
253  }
254  }
255 
261  public function getVersionArray()
262  {
263  return preg_split("/[\.,-]/", $this->getVersion());
264  }
265 
271  public function lastquery()
272  {
273  return $this->lastquery;
274  }
275 
283  public function order($sortfield = null, $sortorder = null)
284  {
285  if (!empty($sortfield)) {
286  $oldsortorder = '';
287  $return = '';
288  $fields = explode(',', $sortfield);
289  $orders = explode(',', $sortorder);
290  $i = 0;
291  foreach ($fields as $val) {
292  if (!$return) {
293  $return .= ' ORDER BY ';
294  } else {
295  $return .= ', ';
296  }
297 
298  $return .= preg_replace('/[^0-9a-z_\.]/i', '', $val); // Add field
299 
300  $tmpsortorder = (empty($orders[$i]) ? '' : trim($orders[$i]));
301 
302  // Only ASC and DESC values are valid SQL
303  if (strtoupper($tmpsortorder) === 'ASC') {
304  $oldsortorder = 'ASC';
305  $return .= ' ASC';
306  } elseif (strtoupper($tmpsortorder) === 'DESC') {
307  $oldsortorder = 'DESC';
308  $return .= ' DESC';
309  } else {
310  $return .= ' '.($oldsortorder ? $oldsortorder : 'ASC');
311  }
312 
313  $i++;
314  }
315  return $return;
316  } else {
317  return '';
318  }
319  }
320 
326  public function lasterror()
327  {
328  return $this->lasterror;
329  }
330 
340  public function jdate($string, $gm = 'tzserver')
341  {
342  // TODO $string should be converted into a GMT timestamp, so param gm should be set to true by default instead of false
343  if ($string == 0 || $string == "0000-00-00 00:00:00") {
344  return '';
345  }
346  $string = preg_replace('/([^0-9])/i', '', $string);
347  $tmp = $string.'000000';
348  $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);
349  return $date;
350  }
351 
357  public function lastqueryerror()
358  {
359  return $this->lastqueryerror;
360  }
361 
370  public function getRow($sql)
371  {
372  $sql .= ' LIMIT 1';
373 
374  $res = $this->query($sql);
375  if ($res) {
376  $obj = $this->fetch_object($res);
377  if ($obj) {
378  return $obj;
379  } else {
380  return 0;
381  }
382  }
383 
384  return false;
385  }
386 
396  public function getRows($sql)
397  {
398  $res = $this->query($sql);
399  if ($res) {
400  $results = array();
401  if ($this->num_rows($res) > 0) {
402  while ($obj = $this->fetch_object($res)) {
403  $results[] = $obj;
404  }
405  }
406  return $results;
407  }
408 
409  return false;
410  }
411 }
Class to manage Dolibarr database access.
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
sanitize($stringtosanitize, $allowsimplequote=0, $allowsequals=0)
Sanitize a string for SQL forging.
commit($log='')
Validate a database transaction.
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...
ifsql($test, $resok, $resko)
Format a SQL IF.
getVersionArray()
Return version of database server into an array.
lastqueryerror()
Return last query in error.
begin($textinlog='')
Start transaction.
rollback($log='')
Cancel a transaction and go back to initial data values.
lasterror()
Return last error label.
getRow($sql)
Return first result from query as object Note : This method executes a given SQL query and retrieves ...
lasterrno()
Return last error code.
lastquery()
Return last request executed with query()
plimit($limit=0, $offset=0)
Define limits and offset of request.
prefix()
Return the DB prefix.
hintindex($nameofindex)
Return SQL string to force an index.
getRows($sql)
Return all results from query as an array of objects Note : This method executes a given SQL query an...
regexpsql($subject, $pattern, $sqlstring=false)
Format a SQL REGEXP.
jdate($string, $gm='tzserver')
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
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...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Class to manage Dolibarr database access for an SQL database.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
getVersion()
Return version of database server.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
num_rows($resultset)
Return number of lines for result of a SELECT.