dolibarr  16.0.5
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 
118  public function idate($param, $gm = 'tzserver')
119  {
120  // TODO $param should be gmt, so we should have default $gm to 'gmt' instead of default 'tzserver'
121  return dol_print_date($param, "%Y-%m-%d %H:%M:%S", $gm);
122  }
123 
129  public function lasterrno()
130  {
131  return $this->lasterrno;
132  }
133 
141  public function sanitize($stringtosanitize, $allowsimplequote = 0)
142  {
143  if ($allowsimplequote) {
144  return preg_replace('/[^a-z0-9_\-\.,\']/i', '', $stringtosanitize);
145  } else {
146  return preg_replace('/[^a-z0-9_\-\.,]/i', '', $stringtosanitize);
147  }
148  }
149 
155  public function begin()
156  {
157  if (!$this->transaction_opened) {
158  $ret = $this->query("BEGIN");
159  if ($ret) {
160  $this->transaction_opened++;
161  dol_syslog("BEGIN Transaction", LOG_DEBUG);
162  dol_syslog('', 0, 1);
163  }
164  return $ret;
165  } else {
166  $this->transaction_opened++;
167  dol_syslog('', 0, 1);
168  return 1;
169  }
170  }
171 
178  public function commit($log = '')
179  {
180  dol_syslog('', 0, -1);
181  if ($this->transaction_opened <= 1) {
182  $ret = $this->query("COMMIT");
183  if ($ret) {
184  $this->transaction_opened = 0;
185  dol_syslog("COMMIT Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
186  return 1;
187  } else {
188  return 0;
189  }
190  } else {
191  $this->transaction_opened--;
192  return 1;
193  }
194  }
195 
202  public function rollback($log = '')
203  {
204  dol_syslog('', 0, -1);
205  if ($this->transaction_opened <= 1) {
206  $ret = $this->query("ROLLBACK");
207  $this->transaction_opened = 0;
208  dol_syslog("ROLLBACK Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
209  return $ret;
210  } else {
211  $this->transaction_opened--;
212  return 1;
213  }
214  }
215 
223  public function plimit($limit = 0, $offset = 0)
224  {
225  global $conf;
226  if (empty($limit)) {
227  return "";
228  }
229  if ($limit < 0) {
230  $limit = $conf->liste_limit;
231  }
232  if ($offset > 0) {
233  return " LIMIT ".((int) $offset).",".((int) $limit)." ";
234  } else {
235  return " LIMIT ".((int) $limit)." ";
236  }
237  }
238 
244  public function getVersionArray()
245  {
246  return preg_split("/[\.,-]/", $this->getVersion());
247  }
248 
254  public function lastquery()
255  {
256  return $this->lastquery;
257  }
258 
266  public function order($sortfield = null, $sortorder = null)
267  {
268  if (!empty($sortfield)) {
269  $oldsortorder = '';
270  $return = '';
271  $fields = explode(',', $sortfield);
272  $orders = explode(',', $sortorder);
273  $i = 0;
274  foreach ($fields as $val) {
275  if (!$return) {
276  $return .= ' ORDER BY ';
277  } else {
278  $return .= ', ';
279  }
280 
281  $return .= preg_replace('/[^0-9a-z_\.]/i', '', $val); // Add field
282 
283  $tmpsortorder = (empty($orders[$i]) ? '' : trim($orders[$i]));
284 
285  // Only ASC and DESC values are valid SQL
286  if (strtoupper($tmpsortorder) === 'ASC') {
287  $oldsortorder = 'ASC';
288  $return .= ' ASC';
289  } elseif (strtoupper($tmpsortorder) === 'DESC') {
290  $oldsortorder = 'DESC';
291  $return .= ' DESC';
292  } else {
293  $return .= ' '.($oldsortorder ? $oldsortorder : 'ASC');
294  }
295 
296  $i++;
297  }
298  return $return;
299  } else {
300  return '';
301  }
302  }
303 
309  public function lasterror()
310  {
311  return $this->lasterror;
312  }
313 
323  public function jdate($string, $gm = 'tzserver')
324  {
325  // TODO $string should be converted into a GMT timestamp, so param gm should be set to true by default instead of false
326  if ($string == 0 || $string == "0000-00-00 00:00:00") {
327  return '';
328  }
329  $string = preg_replace('/([^0-9])/i', '', $string);
330  $tmp = $string.'000000';
331  $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);
332  return $date;
333  }
334 
340  public function lastqueryerror()
341  {
342  return $this->lastqueryerror;
343  }
344 
353  public function getRow($sql)
354  {
355  $sql .= ' LIMIT 1';
356 
357  $res = $this->query($sql);
358  if ($res) {
359  $obj = $this->fetch_object($res);
360  if ($obj) {
361  return $obj;
362  } else {
363  return 0;
364  }
365  }
366 
367  return false;
368  }
369 
379  public function getRows($sql)
380  {
381  $res = $this->query($sql);
382  if ($res) {
383  $results = array();
384  if ($this->num_rows($res) > 0) {
385  while ($obj = $this->fetch_object($res)) {
386  $results[] = $obj;
387  }
388  }
389  return $results;
390  }
391 
392  return false;
393  }
394 }
DoliDB\prefix
prefix()
Return the DB prefix.
Definition: DoliDB.class.php:80
DoliDB\commit
commit($log='')
Validate a database transaction.
Definition: DoliDB.class.php:178
DoliDB\lasterror
lasterror()
Return last error label.
Definition: DoliDB.class.php:309
Database\getVersion
getVersion()
Return version of database server.
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
DoliDB\lastquery
lastquery()
Return last request executed with query()
Definition: DoliDB.class.php:254
DoliDB\rollback
rollback($log='')
Cancel a transaction and go back to initial data values.
Definition: DoliDB.class.php:202
DoliDB\hintindex
hintindex($nameofindex)
Return SQL string to force an index.
Definition: DoliDB.class.php:105
DoliDB\ifsql
ifsql($test, $resok, $resko)
Format a SQL IF.
Definition: DoliDB.class.php:93
Database\fetch_object
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
DoliDB\lasterrno
lasterrno()
Return last error code.
Definition: DoliDB.class.php:129
DoliDB\getVersionArray
getVersionArray()
Return version of database server into an array.
Definition: DoliDB.class.php:244
DoliDB\getRow
getRow($sql)
Return first result from query as object Note : This method executes a given SQL query and retrieves ...
Definition: DoliDB.class.php:353
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
DoliDB\idate
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...
Definition: DoliDB.class.php:118
DoliDB\order
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
Definition: DoliDB.class.php:266
DoliDB\lastqueryerror
lastqueryerror()
Return last query in error.
Definition: DoliDB.class.php:340
Database
Class to manage Dolibarr database access for an SQL database.
Definition: Database.interface.php:26
DoliDB\begin
begin()
Start transaction.
Definition: DoliDB.class.php:155
DoliDB\jdate
jdate($string, $gm='tzserver')
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
Definition: DoliDB.class.php:323
DoliDB\plimit
plimit($limit=0, $offset=0)
Define limits and offset of request.
Definition: DoliDB.class.php:223
Database\num_rows
num_rows($resultset)
Return number of lines for result of a SELECT.
Database\query
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
dol_mktime
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...
Definition: functions.lib.php:2757
DoliDB\getRows
getRows($sql)
Return all results from query as an array of objects Note : This method executes a given SQL query an...
Definition: DoliDB.class.php:379
DoliDB\sanitize
sanitize($stringtosanitize, $allowsimplequote=0)
Sanitize a string for SQL forging.
Definition: DoliDB.class.php:141