29require_once DOL_DOCUMENT_ROOT.
'/core/db/DoliDB.class.php';
42 const LABEL =
'MySQL or MariaDB';
65 if (!empty($conf->db->character_set)) {
66 $this->forcecharset = $conf->db->character_set;
68 if (!empty($conf->db->dolibarr_main_db_collation)) {
69 $this->forcecollate = $conf->db->dolibarr_main_db_collation;
72 $this->database_user = $user;
73 $this->database_host = $host;
74 $this->database_port = $port;
76 $this->transaction_opened = 0;
80 if (!class_exists(
'mysqli')) {
81 $this->connected =
false;
83 $this->
error =
"Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver.";
84 dol_syslog(get_class($this).
"::DoliDBMysqli : Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver.", LOG_ERR);
88 $this->connected =
false;
90 $this->
error = $langs->trans(
"ErrorWrongHostParameter");
91 dol_syslog(get_class($this).
"::DoliDBMysqli : Connect error, wrong host parameters", LOG_ERR);
96 $this->db = $this->
connect($host, $user, $pass,
'', $port);
98 if ($this->db && empty($this->db->connect_errno)) {
99 $this->connected =
true;
102 $this->connected =
false;
104 $this->
error = empty($this->db) ?
'Failed to connect' : $this->db->connect_error;
105 dol_syslog(get_class($this).
"::DoliDBMysqli Connect error: ".$this->
error, LOG_ERR);
108 $disableforcecharset = 0;
111 if ($this->connected && $name) {
113 $this->database_selected =
true;
114 $this->database_name = $name;
118 $clientmustbe = empty($conf->db->character_set) ?
'utf8' : (string) $conf->db->character_set;
119 if (preg_match(
'/latin1/', $clientmustbe)) {
120 $clientmustbe =
'utf8';
123 if (empty($disableforcecharset) && $this->db->character_set_name() != $clientmustbe) {
125 dol_syslog(get_class($this).
"::DoliDBMysqli You should set the \$dolibarr_main_db_character_set and \$dolibarr_main_db_collation for the PHP to the same as the database default, so to ".$this->db->character_set_name().
" or upgrade database default to ".$clientmustbe.
".", LOG_WARNING);
132 $this->db->set_charset($clientmustbe);
134 print
'Failed to force character_set_client to '.$clientmustbe.
" (according to setup) to match the one of the server database.<br>\n";
135 print $e->getMessage();
137 if ($clientmustbe !=
'utf8') {
138 print
'Edit conf/conf.php file to set a charset "utf8"';
139 if ($clientmustbe !=
'utf8mb4') {
140 print
' or "utf8mb4"';
142 print
' instead of "'.$clientmustbe.
'".'.
"\n";
147 $collation = (empty($conf) ?
'utf8_unicode_ci' : $conf->db->dolibarr_main_db_collation);
148 if (preg_match(
'/latin1/', $collation)) {
149 $collation =
'utf8_unicode_ci';
152 if (!preg_match(
'/general/', $collation)) {
153 $this->db->query(
"SET collation_connection = ".$collation);
157 $this->database_selected =
false;
158 $this->database_name =
'';
161 dol_syslog(get_class($this).
"::DoliDBMysqli : Select_db error ".$this->
error, LOG_ERR);
165 $this->database_selected =
false;
167 if ($this->connected) {
169 $clientmustbe = empty($conf->db->character_set) ?
'utf8' : (string) $conf->db->character_set;
170 if (preg_match(
'/latin1/', $clientmustbe)) {
171 $clientmustbe =
'utf8';
173 if (preg_match(
'/utf8mb4/', $clientmustbe)) {
174 $clientmustbe =
'utf8';
177 if (empty($disableforcecharset) && $this->db->character_set_name() != $clientmustbe) {
178 $this->db->set_charset($clientmustbe);
180 $collation = $conf->db->dolibarr_main_db_collation;
181 if (preg_match(
'/latin1/', $collation)) {
182 $collation =
'utf8_unicode_ci';
184 if (preg_match(
'/utf8mb4/', $collation)) {
185 $collation =
'utf8_unicode_ci';
188 if (!preg_match(
'/general/', $collation)) {
189 $this->db->query(
"SET collation_connection = ".$collation);
205 return " FORCE INDEX(".preg_replace(
'/[^a-z0-9_]/',
'', $nameofindex).
")";
232 dol_syslog(get_class($this).
"::select_db database=".$database, LOG_DEBUG);
235 $result = $this->db->select_db($database);
254 public function connect($host, $login, $passwd, $name, $port = 0)
256 dol_syslog(get_class($this).
"::connect host=$host, port=$port, login=$login, passwd=--hidden--, name=$name", LOG_DEBUG);
262 if (!class_exists(
'mysqli')) {
266 if (strpos($host,
'ssl://') === 0) {
267 $tmp =
new mysqliDoli($host, $login, $passwd, $name, $port);
269 $tmp =
new mysqli($host, $login, $passwd, $name, $port);
272 dol_syslog(get_class($this).
"::connect failed", LOG_DEBUG);
284 return $this->db->server_info;
294 return $this->db->client_info;
307 if ($this->transaction_opened > 0) {
308 dol_syslog(get_class($this).
"::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
310 $this->connected =
false;
311 return $this->db->close();
328 public function query($query, $usesavepoint = 0,
$type =
'auto', $result_mode = 0)
330 global $dolibarr_main_db_readonly;
332 $query = trim($query);
334 if (!in_array($query, array(
'BEGIN',
'COMMIT',
'ROLLBACK'))) {
335 $SYSLOG_SQL_LIMIT = 10000;
336 dol_syslog(
'sql='.substr($query, 0, $SYSLOG_SQL_LIMIT), LOG_DEBUG);
342 if (!empty($dolibarr_main_db_readonly)) {
343 if (preg_match(
'/^(INSERT|UPDATE|REPLACE|DELETE|CREATE|ALTER|TRUNCATE|DROP)/i', $query)) {
344 $this->
lasterror =
'Application in read-only mode';
352 $ret = $this->db->query($query, $result_mode);
354 dol_syslog(get_class($this).
"::query Exception in query instead of returning an error: ".$e->getMessage(), LOG_ERR);
358 if (!preg_match(
"/^COMMIT/i", $query) && !preg_match(
"/^ROLLBACK/i", $query)) {
366 dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
368 dol_syslog(get_class($this).
"::query SQL Error message: ".$this->
lasterrno.
" ".$this->lasterror.self::getCallerInfoString(), LOG_ERR);
372 $this->_results = $ret;
385 $backtrace = debug_backtrace();
387 if (count($backtrace) >= 1) {
388 $trace = $backtrace[1];
389 if (isset($trace[
'file'], $trace[
'line'])) {
390 $msg =
" From {$trace['file']}:{$trace['line']}.";
407 if (!is_object($resultset)) {
408 $resultset = $this->_results;
410 return $resultset->fetch_object();
425 if (!is_object($resultset)) {
426 $resultset = $this->_results;
428 return $resultset->fetch_array();
442 if (!is_bool($resultset)) {
443 if (!is_object($resultset)) {
444 $resultset = $this->_results;
446 return $resultset->fetch_row();
465 if (!is_object($resultset)) {
466 $resultset = $this->_results;
468 return isset($resultset->num_rows) ? $resultset->num_rows : 0;
483 if (!is_object($resultset)) {
484 $resultset = $this->_results;
488 return $this->db->affected_rows;
497 public function free($resultset =
null)
500 if (!is_object($resultset)) {
501 $resultset = $this->_results;
504 if (is_object($resultset)) {
505 $resultset->free_result();
517 return $this->db->real_escape_string((
string) $stringtoencode);
529 return str_replace(array(
'\\',
'_',
'%'), array(
'\\\\',
'\_',
'\%'), (
string) $stringtoencode);
539 if (!$this->connected) {
541 return 'DB_ERROR_FAILED_TO_CONNECT';
544 $errorcode_map = array(
545 1004 =>
'DB_ERROR_CANNOT_CREATE',
546 1005 =>
'DB_ERROR_CANNOT_CREATE',
547 1006 =>
'DB_ERROR_CANNOT_CREATE',
548 1007 =>
'DB_ERROR_ALREADY_EXISTS',
549 1008 =>
'DB_ERROR_CANNOT_DROP',
550 1022 =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
551 1025 =>
'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
552 1044 =>
'DB_ERROR_ACCESSDENIED',
553 1046 =>
'DB_ERROR_NODBSELECTED',
554 1048 =>
'DB_ERROR_CONSTRAINT',
555 1050 =>
'DB_ERROR_TABLE_ALREADY_EXISTS',
556 1051 =>
'DB_ERROR_NOSUCHTABLE',
557 1054 =>
'DB_ERROR_NOSUCHFIELD',
558 1060 =>
'DB_ERROR_COLUMN_ALREADY_EXISTS',
559 1061 =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
560 1062 =>
'DB_ERROR_RECORD_ALREADY_EXISTS',
561 1064 =>
'DB_ERROR_SYNTAX',
562 1068 =>
'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
563 1075 =>
'DB_ERROR_CANT_DROP_PRIMARY_KEY',
564 1091 =>
'DB_ERROR_NOSUCHFIELD',
565 1100 =>
'DB_ERROR_NOT_LOCKED',
566 1136 =>
'DB_ERROR_VALUE_COUNT_ON_ROW',
567 1146 =>
'DB_ERROR_NOSUCHTABLE',
568 1215 =>
'DB_ERROR_CANNOT_ADD_FOREIGN_KEY_CONSTRAINT',
569 1216 =>
'DB_ERROR_NO_PARENT',
570 1217 =>
'DB_ERROR_CHILD_EXISTS',
571 1396 =>
'DB_ERROR_USER_ALREADY_EXISTS',
572 1451 =>
'DB_ERROR_CHILD_EXISTS',
573 1826 =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS'
576 if (isset($errorcode_map[$this->db->errno])) {
577 return $errorcode_map[$this->db->errno];
579 $errno = $this->db->errno;
580 return ($errno ?
'DB_ERROR_'.$errno :
'0');
591 if (!$this->connected) {
593 return 'Not connected. Check setup parameters in conf/conf.php file and your mysql client and server versions';
595 return $this->db->error;
610 return $this->db->insert_id;
621 public function encrypt($fieldorvalue, $withQuotes = 1)
626 $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
629 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
631 $escapedstringwithquotes = ($withQuotes ?
"'" :
"").$this->
escape($fieldorvalue).($withQuotes ?
"'" :
"");
633 if ($cryptType && !empty($cryptKey)) {
634 if ($cryptType == 2) {
635 $escapedstringwithquotes =
"AES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
636 } elseif ($cryptType == 1) {
637 $escapedstringwithquotes =
"DES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
641 return $escapedstringwithquotes;
655 $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
658 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
662 if ($cryptType && !empty($cryptKey)) {
663 if ($cryptType == 2) {
664 $return =
'AES_DECRYPT('.$value.
',\''.$cryptKey.
'\')
';
665 } elseif ($cryptType == 1) {
666 $return = 'DES_DECRYPT(
'.$value.',\
''.$cryptKey.
'\')
';
674 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
680 public function DDLGetConnectId()
683 $resql = $this->query('SELECT CONNECTION_ID()
');
685 $row = $this->fetch_row($resql);
692 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
704 public function DDLCreateDb($database, $charset = '
', $collation = '', $owner = '')
707 if (empty($charset)) {
708 $charset = $this->forcecharset;
710 if (empty($collation)) {
711 $collation = $this->forcecollate;
714 // ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
715 $sql = "CREATE DATABASE `".$this->escape($database)."`";
716 $sql .= " DEFAULT CHARACTER SET `".$this->escape($charset)."` DEFAULT COLLATE `".$this->escape($collation)."`";
718 dol_syslog($sql, LOG_DEBUG);
719 $ret = $this->query($sql);
721 // We try again for compatibility with Mysql < 4.1.1
722 $sql = "CREATE DATABASE `".$this->escape($database)."`";
723 dol_syslog($sql, LOG_DEBUG);
724 $ret = $this->query($sql);
730 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
738 public function DDLListTables($database, $table = '
')
741 $listtables = array();
745 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
747 $like = "LIKE '".$this->escape($tmptable)."'";
749 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
751 $sql = "SHOW TABLES FROM `".$tmpdatabase."` ".$like.";";
753 $result = $this->query($sql);
755 while ($row = $this->fetch_row($result)) {
756 $listtables[] = $row[0];
762 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
770 public function DDLListTablesFull($database, $table = '
')
773 $listtables = array();
777 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
779 $like = "LIKE '".$this->escape($tmptable)."'";
781 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
783 $sql = "SHOW FULL TABLES FROM `".$tmpdatabase."` ".$like.";";
785 $result = $this->query($sql);
787 while ($row = $this->fetch_row($result)) {
788 $listtables[] = $row;
794 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
801 public function DDLInfoTable($table)
804 $infotables = array();
806 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
808 $sql = "SHOW FULL COLUMNS FROM ".$tmptable.";";
810 dol_syslog($sql, LOG_DEBUG);
811 $result = $this->query($sql);
813 while ($row = $this->fetch_row($result)) {
814 $infotables[] = $row;
820 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
833 public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
836 // @TODO: $fulltext_keys parameter is unused
846 // Keys found into the array $fields: type,value,attribute,null,default,extra
847 // ex. : $fields['rowid'] = array(
848 // 'type'=>'int' or 'integer
',
850 // 'null'=>'not
null',
851 // 'extra
'=> 'auto_increment
'
853 $sql = "CREATE TABLE ".$this->sanitize($table)."(";
855 $sqlfields = array();
856 foreach ($fields as $field_name => $field_desc) {
857 $sqlfields[$i] = $this->sanitize($field_name)." ";
858 $sqlfields[$i] .= $this->sanitize($field_desc['type']);
859 if (isset($field_desc['value
']) && $field_desc['value
'] !== '') {
860 $sqlfields[$i] .= "(".$this->sanitize($field_desc['value
']).")";
862 if (isset($field_desc['attribute
']) && $field_desc['attribute
'] !== '') {
863 $sqlfields[$i] .= " ".$this->sanitize($field_desc['attribute
'], 0, 0, 1); // Allow space to accept attributes like "ON UPDATE CURRENT_TIMESTAMP"
865 if (isset($field_desc['default']) && $field_desc['default'] !== '') {
866 if (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
867 $sqlfields[$i] .= " DEFAULT ".((float) $field_desc['default']);
868 } elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP
') {
869 $sqlfields[$i] .= " DEFAULT ".$this->sanitize($field_desc['default']);
871 $sqlfields[$i] .= " DEFAULT '".$this->escape($field_desc['default'])."'";
874 if (isset($field_desc['null']) && $field_desc['null'] !== '') {
875 $sqlfields[$i] .= " ".$this->sanitize($field_desc['null'], 0, 0, 1);
877 if (isset($field_desc['extra
']) && $field_desc['extra
'] !== '') {
878 $sqlfields[$i] .= " ".$this->sanitize($field_desc['extra
'], 0, 0, 1);
880 if (!empty($primary_key) && $primary_key == $field_name) {
881 $sqlfields[$i] .= " AUTO_INCREMENT PRIMARY KEY"; // mysql instruction that will be converted by driver late
886 if (is_array($unique_keys)) {
888 foreach ($unique_keys as $key => $value) {
889 $sqluq[$i] = "UNIQUE KEY '".$this->sanitize($key)."' ('".$this->escape($value)."')";
893 if (is_array($keys)) {
895 foreach ($keys as $key => $value) {
896 $sqlk[$i] = "KEY ".$this->sanitize($key)." (".$value.")";
900 $sql .= implode(',
', $sqlfields);
901 if ($unique_keys != "") {
902 $sql .= ",".implode(',
', $sqluq);
904 if (is_array($keys)) {
905 $sql .= ",".implode(',
', $sqlk);
908 $sql .= " engine=".$this->sanitize($type);
910 if (!$this->query($sql)) {
917 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
924 public function DDLDropTable($table)
927 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
929 $sql = "DROP TABLE ".$this->sanitize($tmptable);
931 if (!$this->query($sql)) {
938 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
946 public function DDLDescTable($table, $field = "")
949 $sql = "DESC ".$this->sanitize($table)." ".$this->sanitize($field);
951 dol_syslog(get_class($this)."::DDLDescTable ".$sql, LOG_DEBUG);
952 $this->_results = $this->query($sql);
953 return $this->_results;
956 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
966 public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
969 // cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
970 // ex. : $field_desc = array('
type'=>'int','value
'=>'11
','null'=>'not
null','extra
'=> 'auto_increment
');
971 $sql = "ALTER TABLE ".$this->sanitize($table)." ADD ".$this->sanitize($field_name)." ";
972 $sql .= $this->sanitize($field_desc['type']);
973 if (isset($field_desc['value
']) && preg_match("/^[^\s]/i", $field_desc['value
'])) {
974 if (!in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'date
', 'datetime
')) && $field_desc['value
']) {
975 $sql .= "(".$this->sanitize($field_desc['value
']).")";
978 if (isset($field_desc['attribute
']) && preg_match("/^[^\s]/i", $field_desc['attribute
'])) {
979 $sql .= " ".$this->sanitize($field_desc['attribute
']);
981 if (isset($field_desc['null']) && preg_match("/^[^\s]/i", $field_desc['null'])) {
982 $sql .= " ".$field_desc['null'];
984 if (isset($field_desc['default']) && preg_match("/^[^\s]/i", $field_desc['default'])) {
985 if (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
986 $sql .= " DEFAULT ".((float) $field_desc['default']);
987 } elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP
') {
988 $sql .= " DEFAULT ".$this->sanitize($field_desc['default']);
990 $sql .= " DEFAULT '".$this->escape($field_desc['default'])."'";
993 if (isset($field_desc['extra
']) && preg_match("/^[^\s]/i", $field_desc['extra
'])) {
994 $sql .= " ".$this->sanitize($field_desc['extra
'], 0, 0, 1);
996 $sql .= " ".$this->sanitize($field_position, 0, 0, 1);
998 dol_syslog(get_class($this)."::DDLAddField ".$sql, LOG_DEBUG);
999 if ($this->query($sql)) {
1005 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1014 public function DDLUpdateField($table, $field_name, $field_desc)
1017 $sql = "ALTER TABLE ".$this->sanitize($table);
1018 $sql .= " MODIFY COLUMN ".$this->sanitize($field_name)." ".$this->sanitize($field_desc['
type']);
1019 if (in_array($field_desc['type'], array('double', 'tinyint
', 'int', 'varchar
')) && $field_desc['value
']) {
1020 $sql .= "(".$this->sanitize($field_desc['value
']).")";
1022 if (isset($field_desc['value
']) && ($field_desc['null'] == 'not
null' || $field_desc['null'] == 'NOT NULL
')) {
1023 // We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL
1024 if ($field_desc['type'] == 'varchar
' || $field_desc['type'] == 'text
') {
1025 $sqlbis = "UPDATE ".$this->sanitize($table)." SET ".$this->sanitize($field_name)." = '".$this->escape(isset($field_desc['default']) ? $field_desc['default'] : '')."' WHERE ".$this->sanitize($field_name)." IS NULL";
1026 $this->query($sqlbis);
1027 } elseif (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
1028 $sqlbis = "UPDATE ".$this->sanitize($table)." SET ".$this->sanitize($field_name)." = ".((float) $this->escape(isset($field_desc['default']) ? $field_desc['default'] : 0))." WHERE ".$this->sanitize($field_name)." IS NULL";
1029 $this->query($sqlbis);
1032 $sql .= " NOT NULL";
1035 if (isset($field_desc['default']) && $field_desc['default'] != '') {
1036 if (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
1037 $sql .= " DEFAULT ".((float) $field_desc['default']);
1038 } elseif ($field_desc['type'] != 'text
') {
1039 $sql .= " DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
1043 dol_syslog(get_class($this)."::DDLUpdateField ".$sql, LOG_DEBUG);
1044 if (!$this->query($sql)) {
1051 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1059 public function DDLDropField($table, $field_name)
1062 $tmp_field_name = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $field_name);
1064 $sql = "ALTER TABLE ".$this->sanitize($table)." DROP COLUMN `".$this->sanitize($tmp_field_name)."`";
1065 if ($this->query($sql)) {
1068 $this->error = $this->lasterror();
1073 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1083 public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
1086 $sql = "CREATE USER '
".$this->escape($dolibarr_main_db_user)."' IDENTIFIED BY '".$this->escape($dolibarr_main_db_pass)."'";
1087 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1088 $resql = $this->query($sql);
1090 if ($this->lasterrno != 'DB_ERROR_USER_ALREADY_EXISTS
') {
1093 // If user already exists, we continue to set permissions
1094 dol_syslog(get_class($this)."::DDLCreateUser sql=".$sql, LOG_WARNING);
1098 // Redo with localhost forced (sometimes user is created on %)
1099 $sql = "CREATE USER '".$this->escape($dolibarr_main_db_user)."'@'localhost
' IDENTIFIED BY '".$this->escape($dolibarr_main_db_pass)."'";
1100 $resql = $this->query($sql);
1102 $sql = "GRANT ALL PRIVILEGES ON ".$this->escape($dolibarr_main_db_name).".* TO '".$this->escape($dolibarr_main_db_user)."'@'".$this->escape($dolibarr_main_db_host)."'";
1103 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1104 $resql = $this->query($sql);
1106 $this->error = "Connected user not allowed to GRANT ALL PRIVILEGES ON ".$this->escape($dolibarr_main_db_name).".* TO '".$this->escape($dolibarr_main_db_user)."'@'".$this->escape($dolibarr_main_db_host)."'";
1110 $sql = "FLUSH Privileges";
1112 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
1113 $resql = $this->query($sql);
1128 public function getDefaultCharacterSetDatabase()
1130 $resql = $this->query("SHOW VARIABLES LIKE 'character_set_database
'");
1132 // version Mysql < 4.1.1
1133 return $this->forcecharset;
1135 $liste = $this->fetch_array($resql);
1136 $tmpval = $liste['Value
'];
1146 public function getListOfCharacterSet()
1148 $resql = $this->query('SHOW CHARSET
');
1152 while ($obj = $this->fetch_object($resql)) {
1153 $liste[$i]['charset
'] = $obj->Charset;
1157 $this->free($resql);
1159 // version Mysql < 4.1.1
1171 public function getDefaultCollationDatabase()
1173 $resql = $this->query("SHOW VARIABLES LIKE 'collation_database
'");
1175 // version Mysql < 4.1.1
1176 return $this->forcecollate;
1178 $liste = $this->fetch_array($resql);
1179 $tmpval = $liste['Value
'];
1189 public function getListOfCollation()
1191 $resql = $this->query('SHOW COLLATION
');
1195 while ($obj = $this->fetch_object($resql)) {
1196 $liste[$i]['collation
'] = $obj->Collation;
1199 $this->free($resql);
1201 // version Mysql < 4.1.1
1212 public function getPathOfDump()
1214 $fullpathofdump = '/pathtomysqldump/mysqldump
';
1216 $resql = $this->query("SHOW VARIABLES LIKE 'basedir
'");
1218 $liste = $this->fetch_array($resql);
1219 $basedir = $liste['Value
'];
1220 $fullpathofdump = $basedir.(preg_match('/\/$/
', $basedir) ? '' : '/
').'bin/mysqldump
';
1222 return $fullpathofdump;
1230 public function getPathOfRestore()
1232 $fullpathofimport = '/pathtomysql/mysql
';
1234 $resql = $this->query("SHOW VARIABLES LIKE 'basedir
'");
1236 $liste = $this->fetch_array($resql);
1237 $basedir = $liste['Value
'];
1238 $fullpathofimport = $basedir.(preg_match('/\/$/
', $basedir) ? '' : '/
').'bin/mysql
';
1240 return $fullpathofimport;
1249 public function getServerParametersValues($filter = '
')
1253 $sql = 'SHOW VARIABLES
';
1255 $sql .= " LIKE '".$this->escape($filter)."'";
1257 $resql = $this->query($sql);
1259 while ($obj = $this->fetch_object($resql)) {
1260 $result[$obj->Variable_name] = $obj->Value;
1273 public function getServerStatusValues($filter = '
')
1277 $sql = 'SHOW STATUS
';
1279 $sql .= " LIKE '".$this->escape($filter)."'";
1281 $resql = $this->query($sql);
1283 while ($obj = $this->fetch_object($resql)) {
1284 $result[$obj->Variable_name] = $obj->Value;
1293if (class_exists('myslqi
')) {
1297 class mysqliDoli extends mysqli
1310 public function __construct($host, $user, $pass, $name, $port = 0, $socket = "")
1313 if (PHP_VERSION_ID >= 80100) {
1314 parent::__construct();
1316 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
1319 if (strpos($host, 'ssl:
1320 $host = substr($host, 6);
1321 parent::options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT,
false);
1323 parent::ssl_set(
null,
null,
"",
null,
null);
1324 $flags = MYSQLI_CLIENT_SSL;
1326 parent::real_connect($host, $user, $pass, $name, $port, $socket, $flags);
Class to manage Dolibarr database access.
lastqueryerror()
Return last query in error.
lasterror()
Return last error label.
lasterrno()
Return last error code.
lastquery()
Return last request executed with query()
Class to manage Dolibarr database access for a MySQL database using the MySQLi extension.
fetch_array($resultset)
Return datas as an array.
__construct($type, $host, $user, $pass, $name='', $port=0)
Constructor.
free($resultset=null)
Libere le dernier resultset utilise sur cette connection.
escapeforlike($stringtoencode)
Escape a string to insert data into a like.
num_rows($resultset)
Return number of lines for result of a SELECT.
const VERSIONMIN
Version min database.
error()
Return description of last error.
escape($stringtoencode)
Escape a string to insert data.
getVersion()
Return version of database server.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
encrypt($fieldorvalue, $withQuotes=1)
Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple ...
convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
affected_rows($resultset)
Return the number of lines in the result of a request INSERT, DELETE or UPDATE.
select_db($database)
Select a database.
decrypt($value)
Decrypt sensitive data in database.
fetch_row($resultset)
Return datas as an array.
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.
const LABEL
Database label.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
hintindex($nameofindex)
Return SQL string to force an index.
connect($host, $login, $passwd, $name, $port=0)
Connect to server.
errno()
Return generic error code of last operation.
static getCallerInfoString()
Get caller info.
getDriverInfo()
Return version of database client driver.
close()
Close database connection.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e rowid