Go to the documentation of this file.
25 require_once DOL_DOCUMENT_ROOT.
'/core/db/Database.interface.php';
37 public $forcecharset =
'utf8';
39 public $forcecollate =
'utf8_unicode_ci';
45 public $database_selected;
47 public $database_name;
49 public $database_user;
51 public $database_host;
53 public $database_port;
55 public $transaction_opened;
59 public $lastqueryerror;
82 return (empty($this->prefix_db) ? MAIN_DB_PREFIX : $this->prefix_db);
93 public function ifsql($test, $resok, $resko)
96 return '(CASE WHEN '.$test.
' THEN '.$resok.
' ELSE '.$resko.
' END)';
119 public function regexpsql($subject, $pattern, $sqlstring =
false)
122 return "(". $subject .
" REGEXP '" . $pattern .
"')";
125 return "('". $subject .
"' REGEXP '" . $pattern .
"')";
137 public function idate($param, $gm =
'tzserver')
150 return $this->lasterrno;
161 public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0)
163 return preg_replace(
'/[^a-z0-9_\-\.,'.($allowsequals ?
'=' :
'').($allowsimplequote ?
"\'" :
'').
']/i',
'', $stringtosanitize);
172 public function begin($textinlog =
'')
174 if (!$this->transaction_opened) {
175 $ret = $this->
query(
"BEGIN");
177 $this->transaction_opened++;
178 dol_syslog(
"BEGIN Transaction".($textinlog ?
' '.$textinlog :
''), LOG_DEBUG);
183 $this->transaction_opened++;
198 if ($this->transaction_opened <= 1) {
199 $ret = $this->
query(
"COMMIT");
201 $this->transaction_opened = 0;
202 dol_syslog(
"COMMIT Transaction".($log ?
' '.$log :
''), LOG_DEBUG);
208 $this->transaction_opened--;
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);
228 $this->transaction_opened--;
240 public function plimit($limit = 0, $offset = 0)
247 $limit = $conf->liste_limit;
250 return " LIMIT ".((int) $offset).
",".((int) $limit).
" ";
252 return " LIMIT ".((int) $limit).
" ";
263 return preg_split(
"/[\.,-]/", $this->
getVersion());
273 return $this->lastquery;
283 public function order($sortfield =
null, $sortorder =
null)
285 if (!empty($sortfield)) {
288 $fields = explode(
',', $sortfield);
289 $orders = explode(
',', $sortorder);
291 foreach ($fields as $val) {
293 $return .=
' ORDER BY ';
298 $return .= preg_replace(
'/[^0-9a-z_\.]/i',
'', $val);
300 $tmpsortorder = (empty($orders[$i]) ?
'' : trim($orders[$i]));
303 if (strtoupper($tmpsortorder) ===
'ASC') {
304 $oldsortorder =
'ASC';
306 } elseif (strtoupper($tmpsortorder) ===
'DESC') {
307 $oldsortorder =
'DESC';
310 $return .=
' '.($oldsortorder ? $oldsortorder :
'ASC');
328 return $this->lasterror;
340 public function jdate($string, $gm =
'tzserver')
343 if ($string == 0 || $string ==
"0000-00-00 00:00:00") {
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);
359 return $this->lastqueryerror;
374 $res = $this->
query($sql);
398 $res = $this->
query($sql);
prefix()
Return the DB prefix.
commit($log='')
Validate a database transaction.
lasterror()
Return last error label.
getVersion()
Return version of database server.
Class to manage Dolibarr database access.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
lastquery()
Return last request executed with query()
rollback($log='')
Cancel a transaction and go back to initial data values.
sanitize($stringtosanitize, $allowsimplequote=0, $allowsequals=0)
Sanitize a string for SQL forging.
hintindex($nameofindex)
Return SQL string to force an index.
ifsql($test, $resok, $resko)
Format a SQL IF.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
lasterrno()
Return last error code.
getVersionArray()
Return version of database server into an array.
getRow($sql)
Return first result from query as object Note : This method executes a given SQL query and retrieves ...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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...
begin($textinlog='')
Start transaction.
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
lastqueryerror()
Return last query in error.
Class to manage Dolibarr database access for an SQL database.
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 -...
plimit($limit=0, $offset=0)
Define limits and offset of request.
num_rows($resultset)
Return number of lines for result of a SELECT.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
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...
getRows($sql)
Return all results from query as an array of objects Note : This method executes a given SQL query an...