25require_once DOL_DOCUMENT_ROOT.
'/core/db/Database.interface.php';
37 public $forcecharset =
'utf8';
39 public $forcecollate =
'utf8_unicode_ci';
47 public $database_selected;
49 public $database_name;
51 public $database_user;
53 public $database_host;
55 public $database_port;
57 public $transaction_opened;
61 public $lastqueryerror;
85 return (empty($this->prefix_db) ? MAIN_DB_PREFIX : $this->prefix_db);
96 public function ifsql($test, $resok, $resko)
99 return '(CASE WHEN '.$test.
' THEN '.$resok.
' ELSE '.$resko.
' END)';
122 public function regexpsql($subject, $pattern, $sqlstring = 0)
125 return "(". $subject .
" REGEXP '" . $pattern .
"')";
128 return "('". $subject .
"' REGEXP '" . $pattern .
"')";
140 public function idate($param, $gm =
'tzserver')
153 return $this->lasterrno;
165 public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0, $allowsspace = 0)
167 return preg_replace(
'/[^a-z0-9_\-\.,'.($allowsequals ?
'=' :
'').($allowsimplequote ?
"\'" :
'').($allowsspace ?
' ' :
'').
']/i',
'', $stringtosanitize);
176 public function begin($textinlog =
'')
178 if (!$this->transaction_opened) {
179 $ret = $this->
query(
"BEGIN");
181 $this->transaction_opened++;
182 dol_syslog(
"BEGIN Transaction".($textinlog ?
' '.$textinlog :
''), LOG_DEBUG);
189 $this->transaction_opened++;
204 if ($this->transaction_opened <= 1) {
205 $ret = $this->
query(
"COMMIT");
207 $this->transaction_opened = 0;
208 dol_syslog(
"COMMIT Transaction".($log ?
' '.$log :
''), LOG_DEBUG);
214 $this->transaction_opened--;
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);
234 $this->transaction_opened--;
246 public function plimit($limit = 0, $offset = 0)
253 $limit = $conf->liste_limit;
256 return " LIMIT ".((int) $offset).
",".((int) $limit).
" ";
258 return " LIMIT ".((int) $limit).
" ";
269 return preg_split(
"/[\.,-]/", $this->
getVersion());
279 return $this->lastquery;
289 public function order($sortfield =
'', $sortorder =
'')
291 if (!empty($sortfield)) {
294 $fields = explode(
',', $sortfield);
295 $orders = explode(
',', $sortorder);
297 foreach ($fields as $val) {
299 $return .=
' ORDER BY ';
304 $return .= preg_replace(
'/[^0-9a-z_\.]/i',
'', $val);
306 $tmpsortorder = (empty($orders[$i]) ?
'' : trim($orders[$i]));
309 if (strtoupper($tmpsortorder) ===
'ASC') {
310 $oldsortorder =
'ASC';
312 } elseif (strtoupper($tmpsortorder) ===
'DESC') {
313 $oldsortorder =
'DESC';
316 $return .=
' '.($oldsortorder ? $oldsortorder :
'ASC');
334 return $this->lasterror;
346 public function jdate($string, $gm =
'tzserver')
349 if ($string == 0 || $string ==
"0000-00-00 00:00:00") {
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);
365 return $this->lastqueryerror;
380 $res = $this->
query($sql);
404 $res = $this->
query($sql);
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.