dolibarr 19.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
25require_once DOL_DOCUMENT_ROOT.'/core/db/Database.interface.php';
26
30abstract class DoliDB implements Database
31{
33 public $db;
35 public $type;
37 public $forcecharset = 'utf8';
39 public $forcecollate = 'utf8_unicode_ci';
40
42 private $_results;
43
45 public $connected;
47 public $database_selected;
49 public $database_name;
51 public $database_user;
53 public $database_host;
55 public $database_port;
57 public $transaction_opened;
59 public $lastquery;
61 public $lastqueryerror;
63 public $lasterror;
65 public $lasterrno;
66
68 public $prefix_db;
69
71 public $ok;
73 public $error;
74
75
76
83 public function prefix()
84 {
85 return (empty($this->prefix_db) ? MAIN_DB_PREFIX : $this->prefix_db);
86 }
87
96 public function ifsql($test, $resok, $resko)
97 {
98 //return 'IF('.$test.','.$resok.','.$resko.')'; // Not sql standard
99 return '(CASE WHEN '.$test.' THEN '.$resok.' ELSE '.$resko.' END)';
100 }
101
108 public function hintindex($nameofindex)
109 {
110 return '';
111 }
112
113
122 public function regexpsql($subject, $pattern, $sqlstring = 0)
123 {
124 if ($sqlstring) {
125 return "(". $subject ." REGEXP '" . $pattern . "')";
126 }
127
128 return "('". $subject ."' REGEXP '" . $pattern . "')";
129 }
130
131
140 public function idate($param, $gm = 'tzserver')
141 {
142 // TODO $param should be gmt, so we should have default $gm to 'gmt' instead of default 'tzserver'
143 return dol_print_date($param, "%Y-%m-%d %H:%M:%S", $gm);
144 }
145
151 public function lasterrno()
152 {
153 return $this->lasterrno;
154 }
155
165 public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0, $allowsspace = 0)
166 {
167 return preg_replace('/[^a-z0-9_\-\.,'.($allowsequals ? '=' : '').($allowsimplequote ? "\'" : '').($allowsspace ? ' ' : '').']/i', '', $stringtosanitize);
168 }
169
176 public function begin($textinlog = '')
177 {
178 if (!$this->transaction_opened) {
179 $ret = $this->query("BEGIN");
180 if ($ret) {
181 $this->transaction_opened++;
182 dol_syslog("BEGIN Transaction".($textinlog ? ' '.$textinlog : ''), LOG_DEBUG);
183 dol_syslog('', 0, 1);
184 return 1;
185 } else {
186 return 0;
187 }
188 } else {
189 $this->transaction_opened++;
190 dol_syslog('', 0, 1);
191 return 1;
192 }
193 }
194
201 public function commit($log = '')
202 {
203 dol_syslog('', 0, -1);
204 if ($this->transaction_opened <= 1) {
205 $ret = $this->query("COMMIT");
206 if ($ret) {
207 $this->transaction_opened = 0;
208 dol_syslog("COMMIT Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
209 return 1;
210 } else {
211 return 0;
212 }
213 } else {
214 $this->transaction_opened--;
215 return 1;
216 }
217 }
218
225 public function rollback($log = '')
226 {
227 dol_syslog('', 0, -1);
228 if ($this->transaction_opened <= 1) {
229 $ret = $this->query("ROLLBACK");
230 $this->transaction_opened = 0;
231 dol_syslog("ROLLBACK Transaction".($log ? ' '.$log : ''), LOG_DEBUG);
232 return $ret;
233 } else {
234 $this->transaction_opened--;
235 return 1;
236 }
237 }
238
246 public function plimit($limit = 0, $offset = 0)
247 {
248 global $conf;
249 if (empty($limit)) {
250 return "";
251 }
252 if ($limit < 0) {
253 $limit = $conf->liste_limit;
254 }
255 if ($offset > 0) {
256 return " LIMIT ".((int) $offset).",".((int) $limit)." ";
257 } else {
258 return " LIMIT ".((int) $limit)." ";
259 }
260 }
261
267 public function getVersionArray()
268 {
269 return preg_split("/[\.,-]/", $this->getVersion());
270 }
271
277 public function lastquery()
278 {
279 return $this->lastquery;
280 }
281
289 public function order($sortfield = '', $sortorder = '')
290 {
291 if (!empty($sortfield)) {
292 $oldsortorder = '';
293 $return = '';
294 $fields = explode(',', $sortfield);
295 $orders = explode(',', $sortorder);
296 $i = 0;
297 foreach ($fields as $val) {
298 if (!$return) {
299 $return .= ' ORDER BY ';
300 } else {
301 $return .= ', ';
302 }
303
304 $return .= preg_replace('/[^0-9a-z_\.]/i', '', $val); // Add field
305
306 $tmpsortorder = (empty($orders[$i]) ? '' : trim($orders[$i]));
307
308 // Only ASC and DESC values are valid SQL
309 if (strtoupper($tmpsortorder) === 'ASC') {
310 $oldsortorder = 'ASC';
311 $return .= ' ASC';
312 } elseif (strtoupper($tmpsortorder) === 'DESC') {
313 $oldsortorder = 'DESC';
314 $return .= ' DESC';
315 } else {
316 $return .= ' '.($oldsortorder ? $oldsortorder : 'ASC');
317 }
318
319 $i++;
320 }
321 return $return;
322 } else {
323 return '';
324 }
325 }
326
332 public function lasterror()
333 {
334 return $this->lasterror;
335 }
336
346 public function jdate($string, $gm = 'tzserver')
347 {
348 // TODO $string should be converted into a GMT timestamp, so param gm should be set to true by default instead of false
349 if ($string == 0 || $string == "0000-00-00 00:00:00") {
350 return '';
351 }
352 $string = preg_replace('/([^0-9])/i', '', $string);
353 $tmp = $string.'000000';
354 $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);
355 return $date;
356 }
357
363 public function lastqueryerror()
364 {
365 return $this->lastqueryerror;
366 }
367
376 public function getRow($sql)
377 {
378 $sql .= ' LIMIT 1';
379
380 $res = $this->query($sql);
381 if ($res) {
382 $obj = $this->fetch_object($res);
383 if ($obj) {
384 return $obj;
385 } else {
386 return 0;
387 }
388 }
389
390 return false;
391 }
392
402 public function getRows($sql)
403 {
404 $res = $this->query($sql);
405 if ($res) {
406 $results = array();
407 if ($this->num_rows($res) > 0) {
408 while ($obj = $this->fetch_object($res)) {
409 $results[] = $obj;
410 }
411 }
412 return $results;
413 }
414
415 return false;
416 }
417}
Class to manage Dolibarr database access.
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 found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=....
hintindex($nameofindex)
Return SQL string to force an index.
regexpsql($subject, $pattern, $sqlstring=0)
Format a SQL REGEXP.
sanitize($stringtosanitize, $allowsimplequote=0, $allowsequals=0, $allowsspace=0)
Sanitize a string for SQL forging.
getRows($sql)
Return all results from query as an array of objects Note : This method executes a given SQL query an...
order($sortfield='', $sortorder='')
Define sort criteria of request.
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.