27require_once DOL_DOCUMENT_ROOT.
'/core/db/Database.interface.php';
45 public $forcecharset =
'utf8';
47 public $forcecollate =
'utf8_unicode_ci';
55 public $database_selected;
57 public $database_name;
59 public $database_user;
61 public $database_host;
63 public $database_port;
65 public $transaction_opened;
69 public $lastqueryerror;
93 return (empty($this->prefix_db) ? MAIN_DB_PREFIX : $this->prefix_db);
104 public function ifsql($test, $resok, $resko)
107 return '(CASE WHEN '.$test.
' THEN '.$resok.
' ELSE '.$resko.
' END)';
118 return 'STDDEV_POP('.$nameoffield.
')';
142 public function regexpsql($subject, $pattern, $sqlstring = 0)
145 return "(". $subject .
" REGEXP '" . $this->
escape($pattern) .
"')";
148 return "('". $this->
escape($subject) .
"' REGEXP '" . $this->
escape($pattern) .
"')";
160 public function idate($param, $gm =
'tzserver')
173 return $this->lasterrno;
186 public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0, $allowsspace = 0, $allowschars = 1)
188 return preg_replace(
'/[^0-9_\-\.,'.($allowschars ?
'a-z' :
'').($allowsequals ?
'=' :
'').($allowsimplequote ?
"\'" :
'').($allowsspace ?
' ' :
'').
']/i',
'', $stringtosanitize);
197 public function begin($textinlog =
'')
199 if (!$this->transaction_opened) {
200 $ret = $this->
query(
"BEGIN");
202 $this->transaction_opened++;
203 dol_syslog(
"BEGIN Transaction".($textinlog ?
' '.$textinlog :
''), LOG_DEBUG);
210 $this->transaction_opened++;
225 if ($this->transaction_opened <= 1) {
226 $ret = $this->
query(
"COMMIT");
228 $this->transaction_opened = 0;
229 dol_syslog(
"COMMIT Transaction".($log ?
' '.$log :
''), LOG_DEBUG);
235 $this->transaction_opened--;
249 if ($this->transaction_opened <= 1) {
250 $ret = $this->
query(
"ROLLBACK");
251 $this->transaction_opened = 0;
252 dol_syslog(
"ROLLBACK Transaction".($log ?
' '.$log :
''), LOG_DEBUG);
255 $this->transaction_opened--;
267 public function plimit($limit = 0, $offset = 0)
274 $limit =
$conf->liste_limit;
277 return " LIMIT ".((int) $offset).
",".((int) $limit).
" ";
279 return " LIMIT ".((int) $limit).
" ";
290 return preg_split(
"/[\.,-]/", $this->
getVersion());
300 return $this->lastquery;
310 public function order($sortfield =
'', $sortorder =
'')
312 if (!empty($sortfield)) {
315 $fields = explode(
',', $sortfield);
316 $orders = (!empty($sortorder) ? explode(
',', $sortorder) : array());
318 foreach ($fields as $val) {
320 $return .=
' ORDER BY ';
325 $return .= preg_replace(
'/[^0-9a-z_\.]/i',
'', $val);
327 $tmpsortorder = (empty($orders[$i]) ?
'' : trim($orders[$i]));
330 if (strtoupper($tmpsortorder) ===
'ASC') {
331 $oldsortorder =
'ASC';
333 } elseif (strtoupper($tmpsortorder) ===
'DESC') {
334 $oldsortorder =
'DESC';
337 $return .=
' '.($oldsortorder ? $oldsortorder :
'ASC');
355 return $this->lasterror;
367 public function jdate($string, $gm =
'tzserver')
370 if ($string == 0 || $string ==
"0000-00-00 00:00:00") {
373 $string = preg_replace(
'/([^0-9])/i',
'', $string);
374 $tmp = $string.
'000000';
375 $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);
386 return $this->lastqueryerror;
401 $resql = $this->
query($sql);
425 if (!preg_match(
'/LIMIT \d+(?:(?:,\ *\d*)|(?:\ +OFFSET\ +\d*))?\ *;?$/', $sql)) {
426 trigger_error(__CLASS__ .
'::'.__FUNCTION__.
'() query must have a LIMIT clause', E_USER_ERROR);
429 $resql = $this->
query($sql);
Class to manage Dolibarr database access.
commit($log='')
Validate a database transaction.
hintindex($nameofindex, $mode=1)
Return SQL string to force an index.
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...
const LABEL
Force subclass to implement LABEL - description of DB type.
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.
stddevpop($nameoffield)
Return SQL string to aggregate using the Standard Deviation of population.
prefix()
Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=....
sanitize($stringtosanitize, $allowsimplequote=0, $allowsequals=0, $allowsspace=0, $allowschars=1)
Sanitize a string for SQL forging.
regexpsql($subject, $pattern, $sqlstring=0)
Format a SQL REGEXP.
getRows($sql)
Return all results from query as an array of objects.
order($sortfield='', $sortorder='')
Define sort criteria of request.
const VERSIONMIN
Force subclass to implement VERSIONMIN - required DB version.
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 information (by default a local PHP server timestamp) Rep...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $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.
escape($stringtoencode)
Escape a string to insert data.
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.
free($resultset=null)
Free last resultset used.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...