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);
109 if ($this->connected && $name) {
111 $this->database_selected =
true;
112 $this->database_name = $name;
116 $clientmustbe = empty($conf->db->character_set) ?
'utf8' : (string) $conf->db->character_set;
117 if (preg_match(
'/latin1/', $clientmustbe)) {
118 $clientmustbe =
'utf8';
121 $disableforcecharset = 0;
122 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 one of the database ".$this->db->character_set_name(), LOG_WARNING);
126 $this->db->set_charset($clientmustbe);
128 print
'Failed to force character_set_client to '.$clientmustbe.
" (according to setup) to match the one of the server database.<br>\n";
129 print $e->getMessage();
131 if ($clientmustbe !=
'utf8') {
132 print
'Edit conf/conf.php file to set a charset "utf8"';
133 if ($clientmustbe !=
'utf8mb4') {
134 print
' or "utf8mb4"';
136 print
' instead of "'.$clientmustbe.
'".'.
"\n";
141 $collation = (empty($conf) ?
'utf8_unicode_ci' : $conf->db->dolibarr_main_db_collation);
142 if (preg_match(
'/latin1/', $collation)) {
143 $collation =
'utf8_unicode_ci';
146 if (!preg_match(
'/general/', $collation)) {
147 $this->db->query(
"SET collation_connection = ".$collation);
151 $this->database_selected =
false;
152 $this->database_name =
'';
155 dol_syslog(get_class($this).
"::DoliDBMysqli : Select_db error ".$this->
error, LOG_ERR);
159 $this->database_selected =
false;
161 if ($this->connected) {
163 $clientmustbe = empty($conf->db->character_set) ?
'utf8' : (string) $conf->db->character_set;
164 if (preg_match(
'/latin1/', $clientmustbe)) {
165 $clientmustbe =
'utf8';
167 if (preg_match(
'/utf8mb4/', $clientmustbe)) {
168 $clientmustbe =
'utf8';
171 if ($this->db->character_set_name() != $clientmustbe) {
172 $this->db->set_charset($clientmustbe);
174 $collation = $conf->db->dolibarr_main_db_collation;
175 if (preg_match(
'/latin1/', $collation)) {
176 $collation =
'utf8_unicode_ci';
178 if (preg_match(
'/utf8mb4/', $collation)) {
179 $collation =
'utf8_unicode_ci';
182 if (!preg_match(
'/general/', $collation)) {
183 $this->db->query(
"SET collation_connection = ".$collation);
199 return " FORCE INDEX(".preg_replace(
'/[^a-z0-9_]/',
'', $nameofindex).
")";
226 dol_syslog(get_class($this).
"::select_db database=".$database, LOG_DEBUG);
229 $result = $this->db->select_db($database);
248 public function connect($host, $login, $passwd, $name, $port = 0)
250 dol_syslog(get_class($this).
"::connect host=$host, port=$port, login=$login, passwd=--hidden--, name=$name", LOG_DEBUG);
256 if (!class_exists(
'mysqli')) {
259 if (strpos($host,
'ssl://') === 0) {
260 $tmp =
new mysqliDoli($host, $login, $passwd, $name, $port);
262 $tmp =
new mysqli($host, $login, $passwd, $name, $port);
265 dol_syslog(get_class($this).
"::connect failed", LOG_DEBUG);
277 return $this->db->server_info;
287 return $this->db->client_info;
300 if ($this->transaction_opened > 0) {
301 dol_syslog(get_class($this).
"::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
303 $this->connected =
false;
304 return $this->db->close();
321 public function query($query, $usesavepoint = 0,
$type =
'auto', $result_mode = 0)
323 global $dolibarr_main_db_readonly;
325 $query = trim($query);
327 if (!in_array($query, array(
'BEGIN',
'COMMIT',
'ROLLBACK'))) {
328 $SYSLOG_SQL_LIMIT = 10000;
329 dol_syslog(
'sql='.substr($query, 0, $SYSLOG_SQL_LIMIT), LOG_DEBUG);
335 if (!empty($dolibarr_main_db_readonly)) {
336 if (preg_match(
'/^(INSERT|UPDATE|REPLACE|DELETE|CREATE|ALTER|TRUNCATE|DROP)/i', $query)) {
337 $this->
lasterror =
'Application in read-only mode';
345 $ret = $this->db->query($query, $result_mode);
347 dol_syslog(get_class($this).
"::query Exception in query instead of returning an error: ".$e->getMessage(), LOG_ERR);
351 if (!preg_match(
"/^COMMIT/i", $query) && !preg_match(
"/^ROLLBACK/i", $query)) {
359 dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
361 dol_syslog(get_class($this).
"::query SQL Error message: ".$this->
lasterrno.
" ".$this->lasterror.self::getCallerInfoString(), LOG_ERR);
365 $this->_results = $ret;
378 $backtrace = debug_backtrace();
380 if (count($backtrace) >= 1) {
381 $trace = $backtrace[1];
382 if (isset($trace[
'file'], $trace[
'line'])) {
383 $msg =
" From {$trace['file']}:{$trace['line']}.";
400 if (!is_object($resultset)) {
401 $resultset = $this->_results;
403 return $resultset->fetch_object();
418 if (!is_object($resultset)) {
419 $resultset = $this->_results;
421 return $resultset->fetch_array();
435 if (!is_bool($resultset)) {
436 if (!is_object($resultset)) {
437 $resultset = $this->_results;
439 return $resultset->fetch_row();
458 if (!is_object($resultset)) {
459 $resultset = $this->_results;
461 return isset($resultset->num_rows) ? $resultset->num_rows : 0;
476 if (!is_object($resultset)) {
477 $resultset = $this->_results;
481 return $this->db->affected_rows;
490 public function free($resultset =
null)
493 if (!is_object($resultset)) {
494 $resultset = $this->_results;
497 if (is_object($resultset)) {
498 $resultset->free_result();
510 return $this->db->real_escape_string((
string) $stringtoencode);
522 return str_replace(array(
'\\',
'_',
'%'), array(
'\\\\',
'\_',
'\%'), (
string) $stringtoencode);
532 if (!$this->connected) {
534 return 'DB_ERROR_FAILED_TO_CONNECT';
537 $errorcode_map = array(
538 1004 =>
'DB_ERROR_CANNOT_CREATE',
539 1005 =>
'DB_ERROR_CANNOT_CREATE',
540 1006 =>
'DB_ERROR_CANNOT_CREATE',
541 1007 =>
'DB_ERROR_ALREADY_EXISTS',
542 1008 =>
'DB_ERROR_CANNOT_DROP',
543 1022 =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
544 1025 =>
'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
545 1044 =>
'DB_ERROR_ACCESSDENIED',
546 1046 =>
'DB_ERROR_NODBSELECTED',
547 1048 =>
'DB_ERROR_CONSTRAINT',
548 1050 =>
'DB_ERROR_TABLE_ALREADY_EXISTS',
549 1051 =>
'DB_ERROR_NOSUCHTABLE',
550 1054 =>
'DB_ERROR_NOSUCHFIELD',
551 1060 =>
'DB_ERROR_COLUMN_ALREADY_EXISTS',
552 1061 =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
553 1062 =>
'DB_ERROR_RECORD_ALREADY_EXISTS',
554 1064 =>
'DB_ERROR_SYNTAX',
555 1068 =>
'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
556 1075 =>
'DB_ERROR_CANT_DROP_PRIMARY_KEY',
557 1091 =>
'DB_ERROR_NOSUCHFIELD',
558 1100 =>
'DB_ERROR_NOT_LOCKED',
559 1136 =>
'DB_ERROR_VALUE_COUNT_ON_ROW',
560 1146 =>
'DB_ERROR_NOSUCHTABLE',
561 1215 =>
'DB_ERROR_CANNOT_ADD_FOREIGN_KEY_CONSTRAINT',
562 1216 =>
'DB_ERROR_NO_PARENT',
563 1217 =>
'DB_ERROR_CHILD_EXISTS',
564 1396 =>
'DB_ERROR_USER_ALREADY_EXISTS',
565 1451 =>
'DB_ERROR_CHILD_EXISTS',
566 1826 =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS'
569 if (isset($errorcode_map[$this->db->errno])) {
570 return $errorcode_map[$this->db->errno];
572 $errno = $this->db->errno;
573 return ($errno ?
'DB_ERROR_'.$errno :
'0');
584 if (!$this->connected) {
586 return 'Not connected. Check setup parameters in conf/conf.php file and your mysql client and server versions';
588 return $this->db->error;
603 return $this->db->insert_id;
614 public function encrypt($fieldorvalue, $withQuotes = 1)
619 $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
622 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
624 $escapedstringwithquotes = ($withQuotes ?
"'" :
"").$this->
escape($fieldorvalue).($withQuotes ?
"'" :
"");
626 if ($cryptType && !empty($cryptKey)) {
627 if ($cryptType == 2) {
628 $escapedstringwithquotes =
"AES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
629 } elseif ($cryptType == 1) {
630 $escapedstringwithquotes =
"DES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
634 return $escapedstringwithquotes;
648 $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
651 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
655 if ($cryptType && !empty($cryptKey)) {
656 if ($cryptType == 2) {
657 $return =
'AES_DECRYPT('.$value.
',\''.$cryptKey.
'\')
';
658 } elseif ($cryptType == 1) {
659 $return = 'DES_DECRYPT(
'.$value.',\
''.$cryptKey.
'\')
';
667 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
673 public function DDLGetConnectId()
676 $resql = $this->query('SELECT CONNECTION_ID()
');
678 $row = $this->fetch_row($resql);
685 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
697 public function DDLCreateDb($database, $charset = '
', $collation = '', $owner = '')
700 if (empty($charset)) {
701 $charset = $this->forcecharset;
703 if (empty($collation)) {
704 $collation = $this->forcecollate;
707 // ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
708 $sql = "CREATE DATABASE `".$this->escape($database)."`";
709 $sql .= " DEFAULT CHARACTER SET `".$this->escape($charset)."` DEFAULT COLLATE `".$this->escape($collation)."`";
711 dol_syslog($sql, LOG_DEBUG);
712 $ret = $this->query($sql);
714 // We try again for compatibility with Mysql < 4.1.1
715 $sql = "CREATE DATABASE `".$this->escape($database)."`";
716 dol_syslog($sql, LOG_DEBUG);
717 $ret = $this->query($sql);
723 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
731 public function DDLListTables($database, $table = '
')
734 $listtables = array();
738 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
740 $like = "LIKE '".$this->escape($tmptable)."'";
742 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
744 $sql = "SHOW TABLES FROM `".$tmpdatabase."` ".$like.";";
746 $result = $this->query($sql);
748 while ($row = $this->fetch_row($result)) {
749 $listtables[] = $row[0];
755 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
763 public function DDLListTablesFull($database, $table = '
')
766 $listtables = array();
770 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
772 $like = "LIKE '".$this->escape($tmptable)."'";
774 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
776 $sql = "SHOW FULL TABLES FROM `".$tmpdatabase."` ".$like.";";
778 $result = $this->query($sql);
780 while ($row = $this->fetch_row($result)) {
781 $listtables[] = $row;
787 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
794 public function DDLInfoTable($table)
797 $infotables = array();
799 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
801 $sql = "SHOW FULL COLUMNS FROM ".$tmptable.";";
803 dol_syslog($sql, LOG_DEBUG);
804 $result = $this->query($sql);
806 while ($row = $this->fetch_row($result)) {
807 $infotables[] = $row;
813 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
826 public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
829 // @TODO: $fulltext_keys parameter is unused
839 // Keys found into the array $fields: type,value,attribute,null,default,extra
840 // ex. : $fields['rowid'] = array(
841 // 'type'=>'int' or 'integer
',
843 // 'null'=>'not
null',
844 // 'extra
'=> 'auto_increment
'
846 $sql = "CREATE TABLE ".$this->sanitize($table)."(";
848 $sqlfields = array();
849 foreach ($fields as $field_name => $field_desc) {
850 $sqlfields[$i] = $this->sanitize($field_name)." ";
851 $sqlfields[$i] .= $this->sanitize($field_desc['type']);
852 if (isset($field_desc['value
']) && $field_desc['value
'] !== '') {
853 $sqlfields[$i] .= "(".$this->sanitize($field_desc['value
']).")";
855 if (isset($field_desc['attribute
']) && $field_desc['attribute
'] !== '') {
856 $sqlfields[$i] .= " ".$this->sanitize($field_desc['attribute
'], 0, 0, 1); // Allow space to accept attributes like "ON UPDATE CURRENT_TIMESTAMP"
858 if (isset($field_desc['default']) && $field_desc['default'] !== '') {
859 if (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
860 $sqlfields[$i] .= " DEFAULT ".((float) $field_desc['default']);
861 } elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP
') {
862 $sqlfields[$i] .= " DEFAULT ".$this->sanitize($field_desc['default']);
864 $sqlfields[$i] .= " DEFAULT '".$this->escape($field_desc['default'])."'";
867 if (isset($field_desc['null']) && $field_desc['null'] !== '') {
868 $sqlfields[$i] .= " ".$this->sanitize($field_desc['null'], 0, 0, 1);
870 if (isset($field_desc['extra
']) && $field_desc['extra
'] !== '') {
871 $sqlfields[$i] .= " ".$this->sanitize($field_desc['extra
'], 0, 0, 1);
873 if (!empty($primary_key) && $primary_key == $field_name) {
874 $sqlfields[$i] .= " AUTO_INCREMENT PRIMARY KEY"; // mysql instruction that will be converted by driver late
879 if (is_array($unique_keys)) {
881 foreach ($unique_keys as $key => $value) {
882 $sqluq[$i] = "UNIQUE KEY '".$this->sanitize($key)."' ('".$this->escape($value)."')";
886 if (is_array($keys)) {
888 foreach ($keys as $key => $value) {
889 $sqlk[$i] = "KEY ".$this->sanitize($key)." (".$value.")";
893 $sql .= implode(',
', $sqlfields);
894 if ($unique_keys != "") {
895 $sql .= ",".implode(',
', $sqluq);
897 if (is_array($keys)) {
898 $sql .= ",".implode(',
', $sqlk);
901 $sql .= " engine=".$this->sanitize($type);
903 if (!$this->query($sql)) {
910 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
917 public function DDLDropTable($table)
920 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
922 $sql = "DROP TABLE ".$this->sanitize($tmptable);
924 if (!$this->query($sql)) {
931 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
939 public function DDLDescTable($table, $field = "")
942 $sql = "DESC ".$this->sanitize($table)." ".$this->sanitize($field);
944 dol_syslog(get_class($this)."::DDLDescTable ".$sql, LOG_DEBUG);
945 $this->_results = $this->query($sql);
946 return $this->_results;
949 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
959 public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
962 // cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
963 // ex. : $field_desc = array('
type'=>'int','value
'=>'11
','null'=>'not
null','extra
'=> 'auto_increment
');
964 $sql = "ALTER TABLE ".$this->sanitize($table)." ADD ".$this->sanitize($field_name)." ";
965 $sql .= $this->sanitize($field_desc['type']);
966 if (isset($field_desc['value
']) && preg_match("/^[^\s]/i", $field_desc['value
'])) {
967 if (!in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'date
', 'datetime
')) && $field_desc['value
']) {
968 $sql .= "(".$this->sanitize($field_desc['value
']).")";
971 if (isset($field_desc['attribute
']) && preg_match("/^[^\s]/i", $field_desc['attribute
'])) {
972 $sql .= " ".$this->sanitize($field_desc['attribute
']);
974 if (isset($field_desc['null']) && preg_match("/^[^\s]/i", $field_desc['null'])) {
975 $sql .= " ".$field_desc['null'];
977 if (isset($field_desc['default']) && preg_match("/^[^\s]/i", $field_desc['default'])) {
978 if (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
979 $sql .= " DEFAULT ".((float) $field_desc['default']);
980 } elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP
') {
981 $sql .= " DEFAULT ".$this->sanitize($field_desc['default']);
983 $sql .= " DEFAULT '".$this->escape($field_desc['default'])."'";
986 if (isset($field_desc['extra
']) && preg_match("/^[^\s]/i", $field_desc['extra
'])) {
987 $sql .= " ".$this->sanitize($field_desc['extra
'], 0, 0, 1);
989 $sql .= " ".$this->sanitize($field_position, 0, 0, 1);
991 dol_syslog(get_class($this)."::DDLAddField ".$sql, LOG_DEBUG);
992 if ($this->query($sql)) {
998 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1007 public function DDLUpdateField($table, $field_name, $field_desc)
1010 $sql = "ALTER TABLE ".$this->sanitize($table);
1011 $sql .= " MODIFY COLUMN ".$this->sanitize($field_name)." ".$this->sanitize($field_desc['
type']);
1012 if (in_array($field_desc['type'], array('double', 'tinyint
', 'int', 'varchar
')) && $field_desc['value
']) {
1013 $sql .= "(".$this->sanitize($field_desc['value
']).")";
1015 if (isset($field_desc['value
']) && ($field_desc['null'] == 'not
null' || $field_desc['null'] == 'NOT NULL
')) {
1016 // 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
1017 if ($field_desc['type'] == 'varchar
' || $field_desc['type'] == 'text
') {
1018 $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";
1019 $this->query($sqlbis);
1020 } elseif (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
1021 $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";
1022 $this->query($sqlbis);
1025 $sql .= " NOT NULL";
1028 if (isset($field_desc['default']) && $field_desc['default'] != '') {
1029 if (in_array($field_desc['type'], array('tinyint
', 'smallint
', 'int', 'double'))) {
1030 $sql .= " DEFAULT ".((float) $field_desc['default']);
1031 } elseif ($field_desc['type'] != 'text
') {
1032 $sql .= " DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
1036 dol_syslog(get_class($this)."::DDLUpdateField ".$sql, LOG_DEBUG);
1037 if (!$this->query($sql)) {
1044 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1052 public function DDLDropField($table, $field_name)
1055 $tmp_field_name = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $field_name);
1057 $sql = "ALTER TABLE ".$this->sanitize($table)." DROP COLUMN `".$this->sanitize($tmp_field_name)."`";
1058 if ($this->query($sql)) {
1061 $this->error = $this->lasterror();
1066 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1076 public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
1079 $sql = "CREATE USER '
".$this->escape($dolibarr_main_db_user)."' IDENTIFIED BY '".$this->escape($dolibarr_main_db_pass)."'";
1080 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1081 $resql = $this->query($sql);
1083 if ($this->lasterrno != 'DB_ERROR_USER_ALREADY_EXISTS
') {
1086 // If user already exists, we continue to set permissions
1087 dol_syslog(get_class($this)."::DDLCreateUser sql=".$sql, LOG_WARNING);
1091 // Redo with localhost forced (sometimes user is created on %)
1092 $sql = "CREATE USER '".$this->escape($dolibarr_main_db_user)."'@'localhost
' IDENTIFIED BY '".$this->escape($dolibarr_main_db_pass)."'";
1093 $resql = $this->query($sql);
1095 $sql = "GRANT ALL PRIVILEGES ON ".$this->escape($dolibarr_main_db_name).".* TO '".$this->escape($dolibarr_main_db_user)."'@'".$this->escape($dolibarr_main_db_host)."'";
1096 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1097 $resql = $this->query($sql);
1099 $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)."'";
1103 $sql = "FLUSH Privileges";
1105 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
1106 $resql = $this->query($sql);
1121 public function getDefaultCharacterSetDatabase()
1123 $resql = $this->query("SHOW VARIABLES LIKE 'character_set_database
'");
1125 // version Mysql < 4.1.1
1126 return $this->forcecharset;
1128 $liste = $this->fetch_array($resql);
1129 $tmpval = $liste['Value
'];
1139 public function getListOfCharacterSet()
1141 $resql = $this->query('SHOW CHARSET
');
1145 while ($obj = $this->fetch_object($resql)) {
1146 $liste[$i]['charset
'] = $obj->Charset;
1150 $this->free($resql);
1152 // version Mysql < 4.1.1
1164 public function getDefaultCollationDatabase()
1166 $resql = $this->query("SHOW VARIABLES LIKE 'collation_database
'");
1168 // version Mysql < 4.1.1
1169 return $this->forcecollate;
1171 $liste = $this->fetch_array($resql);
1172 $tmpval = $liste['Value
'];
1182 public function getListOfCollation()
1184 $resql = $this->query('SHOW COLLATION
');
1188 while ($obj = $this->fetch_object($resql)) {
1189 $liste[$i]['collation
'] = $obj->Collation;
1192 $this->free($resql);
1194 // version Mysql < 4.1.1
1205 public function getPathOfDump()
1207 $fullpathofdump = '/pathtomysqldump/mysqldump
';
1209 $resql = $this->query("SHOW VARIABLES LIKE 'basedir
'");
1211 $liste = $this->fetch_array($resql);
1212 $basedir = $liste['Value
'];
1213 $fullpathofdump = $basedir.(preg_match('/\/$/
', $basedir) ? '' : '/
').'bin/mysqldump
';
1215 return $fullpathofdump;
1223 public function getPathOfRestore()
1225 $fullpathofimport = '/pathtomysql/mysql
';
1227 $resql = $this->query("SHOW VARIABLES LIKE 'basedir
'");
1229 $liste = $this->fetch_array($resql);
1230 $basedir = $liste['Value
'];
1231 $fullpathofimport = $basedir.(preg_match('/\/$/
', $basedir) ? '' : '/
').'bin/mysql
';
1233 return $fullpathofimport;
1242 public function getServerParametersValues($filter = '
')
1246 $sql = 'SHOW VARIABLES
';
1248 $sql .= " LIKE '".$this->escape($filter)."'";
1250 $resql = $this->query($sql);
1252 while ($obj = $this->fetch_object($resql)) {
1253 $result[$obj->Variable_name] = $obj->Value;
1266 public function getServerStatusValues($filter = '
')
1270 $sql = 'SHOW STATUS
';
1272 $sql .= " LIKE '".$this->escape($filter)."'";
1274 $resql = $this->query($sql);
1276 while ($obj = $this->fetch_object($resql)) {
1277 $result[$obj->Variable_name] = $obj->Value;
1288class mysqliDoli extends mysqli
1301 public function __construct($host, $user, $pass, $name, $port = 0, $socket = "")
1304 if (PHP_VERSION_ID >= 80100) {
1305 parent::__construct();
1307 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
1310 if (strpos($host, 'ssl:
1311 $host = substr($host, 6);
1312 parent::options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT,
false);
1314 parent::ssl_set(
null,
null,
"",
null,
null);
1315 $flags = MYSQLI_CLIENT_SSL;
1317 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.
Class to make SSL 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