28 require_once DOL_DOCUMENT_ROOT.
'/core/db/DoliDB.class.php';
47 const WEEK_MONDAY_FIRST = 1;
49 const WEEK_FIRST_WEEKDAY = 4;
68 if (!empty($conf->db->character_set)) {
69 $this->forcecharset = $conf->db->character_set;
71 if (!empty($conf->db->dolibarr_main_db_collation)) {
72 $this->forcecollate = $conf->db->dolibarr_main_db_collation;
75 $this->database_user = $user;
76 $this->database_host = $host;
77 $this->database_port = $port;
79 $this->transaction_opened = 0;
103 $this->
db = $this->
connect($host, $user, $pass, $name, $port);
106 $this->connected =
true;
108 $this->database_selected =
true;
109 $this->database_name = $name;
122 $this->connected =
false;
124 $this->database_selected =
false;
125 $this->database_name =
'';
127 dol_syslog(get_class($this).
"::DoliDBSqlite3 : Error Connect ".$this->
error, LOG_ERR);
144 if (preg_match(
'/^--\s\$Id/i', $line)) {
148 if (preg_match(
'/^#/i', $line) || preg_match(
'/^$/i', $line) || preg_match(
'/^--/i', $line)) {
152 if (
$type ==
'auto') {
153 if (preg_match(
'/ALTER TABLE/i', $line)) {
155 } elseif (preg_match(
'/CREATE TABLE/i', $line)) {
157 } elseif (preg_match(
'/DROP TABLE/i', $line)) {
162 if (
$type ==
'dml') {
163 $line = preg_replace(
'/\s/',
' ', $line);
166 if (preg_match(
'/(ISAM|innodb)/i', $line)) {
167 $line = preg_replace(
'/\)[\s\t]*type[\s\t]*=[\s\t]*(MyISAM|innodb);/i',
');', $line);
168 $line = preg_replace(
'/\)[\s\t]*engine[\s\t]*=[\s\t]*(MyISAM|innodb);/i',
');', $line);
169 $line = preg_replace(
'/,$/',
'', $line);
173 if (preg_match(
'/[\s\t\(]*(\w*)[\s\t]+int.*auto_increment/i', $line, $reg)) {
174 $newline = preg_replace(
'/([\s\t\(]*)([a-zA-Z_0-9]*)[\s\t]+int.*auto_increment[^,]*/i',
'\\1 \\2 integer PRIMARY KEY AUTOINCREMENT', $line);
180 $line = str_replace(
'tinyint',
'smallint', $line);
183 $line = preg_replace(
'/(int\w+|smallint)\s+unsigned/i',
'\\1', $line);
186 $line = preg_replace(
'/\w*blob/i',
'text', $line);
189 $line = preg_replace(
'/tinytext/i',
'text', $line);
190 $line = preg_replace(
'/mediumtext/i',
'text', $line);
194 $line = preg_replace(
'/datetime not null/i',
'datetime', $line);
195 $line = preg_replace(
'/datetime/i',
'timestamp', $line);
198 $line = preg_replace(
'/^double/i',
'numeric', $line);
199 $line = preg_replace(
'/(\s*)double/i',
'\\1numeric', $line);
201 $line = preg_replace(
'/^float/i',
'numeric', $line);
202 $line = preg_replace(
'/(\s*)float/i',
'\\1numeric', $line);
205 if (preg_match(
'/unique index\s*\((\w+\s*,\s*\w+)\)/i', $line)) {
206 $line = preg_replace(
'/unique index\s*\((\w+\s*,\s*\w+)\)/i',
'UNIQUE\(\\1\)', $line);
210 $line = preg_replace(
'/AFTER [a-z0-9_]+/i',
'', $line);
213 $line = preg_replace(
'/ALTER TABLE [a-z0-9_]+ DROP INDEX/i',
'DROP INDEX', $line);
216 if (preg_match(
'/ALTER TABLE ([a-z0-9_]+) CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i', $line, $reg)) {
217 $line =
"-- ".$line.
" replaced by --\n";
218 $line .=
"ALTER TABLE ".$reg[1].
" RENAME COLUMN ".$reg[2].
" TO ".$reg[3];
222 if (preg_match(
'/ALTER TABLE ([a-z0-9_]+) MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i', $line, $reg)) {
223 $line =
"-- ".$line.
" replaced by --\n";
225 $newreg3 = preg_replace(
'/ DEFAULT NULL/i',
'', $newreg3);
226 $newreg3 = preg_replace(
'/ NOT NULL/i',
'', $newreg3);
227 $newreg3 = preg_replace(
'/ NULL/i',
'', $newreg3);
228 $newreg3 = preg_replace(
'/ DEFAULT 0/i',
'', $newreg3);
229 $newreg3 = preg_replace(
'/ DEFAULT \'[0-9a-zA-Z_@]*\'/i',
'', $newreg3);
230 $line .=
"ALTER TABLE ".$reg[1].
" ALTER COLUMN ".$reg[2].
" TYPE ".$newreg3;
236 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i', $line, $reg)) {
237 $line =
"-- ".$line.
" replaced by --\n";
238 $line .=
"CREATE UNIQUE INDEX ".$reg[2].
" ON ".$reg[1].
"(".$reg[3];
243 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i', $line, $reg)) {
244 $line =
"-- ".$line.
" replaced by --\n";
245 $line .=
"ALTER TABLE ".$reg[1].
" DROP CONSTRAINT ".$reg[2];
250 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i', $line, $reg)) {
251 $fieldlist = $reg[4];
253 $tablename = $reg[1];
254 $line =
"-- ".$line.
" replaced by --\n";
255 $line .=
"CREATE ".(preg_match(
'/UNIQUE/', $reg[2]) ?
'UNIQUE ' :
'').
"INDEX ".$idxname.
" ON ".$tablename.
" (".$fieldlist.
")";
257 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+CONSTRAINT\s+(.*)\s*FOREIGN\s+KEY\s*\(([\w,\s]+)\)\s*REFERENCES\s+(\w+)\s*\(([\w,\s]+)\)/i', $line, $reg)) {
259 dol_syslog(get_class().
'::query line emptied');
270 if (preg_match(
'/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i', $line, $reg)) {
271 if ($reg[1] == $reg[2]) {
272 $line = preg_replace(
'/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i',
'DELETE FROM \\1 USING \\3', $line);
277 $line = preg_replace(
'/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i',
'FROM \\1', $line);
281 $line = preg_replace(
'/FROM\s*\(([a-z_]+\s+as\s+[a-z_]+)\s*,\s*([a-z_]+\s+as\s+[a-z_]+\s*)\)/i',
'FROM \\1, \\2', $line);
285 $line = preg_replace(
'/FROM\s*\(([a-z_]+\s+as\s+[a-z_]+)\s*,\s*([a-z_]+\s+as\s+[a-z_]+\s*),\s*([a-z_]+\s+as\s+[a-z_]+\s*)\)/i',
'FROM \\1, \\2, \\3', $line);
304 dol_syslog(get_class($this).
"::select_db database=".$database, LOG_DEBUG);
322 public function connect($host, $login, $passwd, $name, $port = 0)
324 global $main_data_dir;
326 dol_syslog(get_class($this).
"::connect name=".$name, LOG_DEBUG);
328 $dir = $main_data_dir;
330 $dir = DOL_DATA_ROOT;
334 $database_name = $dir.
'/database_'.$name.
'.sdb';
338 $this->
db =
new SQLite3($database_name);
341 $this->
error = self::LABEL.
' '.$e->getMessage().
' current dir='.$database_name;
357 $tmp = $this->
db->version();
358 return $tmp[
'versionString'];
368 return 'sqlite3 php driver';
381 if ($this->transaction_opened > 0) {
382 dol_syslog(get_class($this).
"::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
384 $this->connected =
false;
402 public function query($query, $usesavepoint = 0,
$type =
'auto', $result_mode = 0)
404 global $conf, $dolibarr_main_db_readonly;
408 $query = trim($query);
414 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+CONSTRAINT\s+(.*)\s*FOREIGN\s+KEY\s*\(([\w,\s]+)\)\s*REFERENCES\s+(\w+)\s*\(([\w,\s]+)\)/i', $query, $reg)) {
419 $foreignFields = $reg[5];
420 $foreignTable = $reg[4];
421 $localfields = $reg[3];
422 $constraintname = trim($reg[2]);
423 $tablename = trim($reg[1]);
425 $descTable = $this->
db->querySingle(
"SELECT sql FROM sqlite_master WHERE name='".$this->
escape($tablename).
"'");
428 $this->
query(
"ALTER TABLE ".$tablename.
" RENAME TO tmp_".$tablename);
433 $descTable = substr($descTable, 0, strlen($descTable) - 1);
434 $descTable .=
", CONSTRAINT ".$constraintname.
" FOREIGN KEY (".$localfields.
") REFERENCES ".$foreignTable.
"(".$foreignFields.
")";
440 $this->
query($descTable);
443 $this->
query(
"INSERT INTO ".$tablename.
" SELECT * FROM tmp_".$tablename);
446 $this->
query(
"DROP TABLE tmp_".$tablename);
455 if (!in_array($query, array(
'BEGIN',
'COMMIT',
'ROLLBACK'))) {
456 $SYSLOG_SQL_LIMIT = 10000;
457 dol_syslog(
'sql='.substr($query, 0, $SYSLOG_SQL_LIMIT), LOG_DEBUG);
463 if (!empty($dolibarr_main_db_readonly)) {
464 if (preg_match(
'/^(INSERT|UPDATE|REPLACE|DELETE|CREATE|ALTER|TRUNCATE|DROP)/i', $query)) {
465 $this->
lasterror =
'Application in read-only mode';
475 $ret = $this->
db->query($query);
477 $ret->queryString = $query;
480 $this->
error = $this->
db->lastErrorMsg();
483 if (!preg_match(
"/^COMMIT/i", $query) && !preg_match(
"/^ROLLBACK/i", $query)) {
485 if (!is_object($ret) || $this->
error) {
490 dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
492 $errormsg = get_class($this).
"::query SQL Error message: ".$this->lasterror;
494 if (preg_match(
'/[0-9]/', $this->
lasterrno)) {
495 $errormsg .=
' ('.$this->lasterrno.
')';
498 if ($conf->global->SYSLOG_LEVEL < LOG_DEBUG) {
499 dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
501 dol_syslog(get_class($this).
"::query SQL Error message: ".$errormsg, LOG_ERR);
504 $this->_results = $ret;
521 if (!is_object($resultset)) {
522 $resultset = $this->_results;
525 $ret = $resultset->fetchArray(SQLITE3_ASSOC);
527 return (
object) $ret;
544 if (!is_object($resultset)) {
545 $resultset = $this->_results;
548 $ret = $resultset->fetchArray(SQLITE3_ASSOC);
563 if (!is_bool($resultset)) {
564 if (!is_object($resultset)) {
565 $resultset = $this->_results;
567 return $resultset->fetchArray(SQLITE3_NUM);
588 if (!is_object($resultset)) {
589 $resultset = $this->_results;
591 if (preg_match(
"/^SELECT/i", $resultset->queryString)) {
592 return $this->
db->querySingle(
"SELECT count(*) FROM (".$resultset->queryString.
") q");
611 if (!is_object($resultset)) {
612 $resultset = $this->_results;
614 if (preg_match(
"/^SELECT/i", $resultset->queryString)) {
619 return $this->
db->changes();
629 public function free($resultset =
null)
632 if (!is_object($resultset)) {
633 $resultset = $this->_results;
636 if ($resultset && is_object($resultset)) {
637 $resultset->finalize();
649 return Sqlite3::escapeString($stringtoencode);
660 return str_replace(array(
'_',
'\\',
'%'), array(
'\_',
'\\\\',
'\%'), (
string) $stringtoencode);
670 if (!$this->connected) {
672 return 'DB_ERROR_FAILED_TO_CONNECT';
707 $errno = $this->
db->lastErrorCode();
708 if ($errno ==
'HY000' || $errno == 0) {
709 if (preg_match(
'/table.*already exists/i', $this->
error)) {
710 return 'DB_ERROR_TABLE_ALREADY_EXISTS';
711 } elseif (preg_match(
'/index.*already exists/i', $this->
error)) {
712 return 'DB_ERROR_KEY_NAME_ALREADY_EXISTS';
713 } elseif (preg_match(
'/syntax error/i', $this->
error)) {
714 return 'DB_ERROR_SYNTAX';
717 if ($errno ==
'23000') {
718 if (preg_match(
'/column.* not unique/i', $this->
error)) {
719 return 'DB_ERROR_RECORD_ALREADY_EXISTS';
720 } elseif (preg_match(
'/PRIMARY KEY must be unique/i', $this->
error)) {
721 return 'DB_ERROR_RECORD_ALREADY_EXISTS';
728 return ($errno ?
'DB_ERROR_'.$errno :
'0');
739 if (!$this->connected) {
741 return 'Not connected. Check setup parameters in conf/conf.php file and your sqlite version';
758 return $this->
db->lastInsertRowId();
769 public function encrypt($fieldorvalue, $withQuotes = 1)
774 $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
777 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
779 $escapedstringwithquotes = ($withQuotes ?
"'" :
"").$this->
escape($fieldorvalue).($withQuotes ?
"'" :
"");
781 if ($cryptType && !empty($cryptKey)) {
782 if ($cryptType == 2) {
783 $escapedstringwithquotes =
"AES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
784 } elseif ($cryptType == 1) {
785 $escapedstringwithquotes =
"DES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
789 return $escapedstringwithquotes;
803 $cryptType = ($conf->db->dolibarr_main_db_encryption ? $conf->db->dolibarr_main_db_encryption : 0);
806 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
810 if ($cryptType && !empty($cryptKey)) {
811 if ($cryptType == 2) {
812 $return =
'AES_DECRYPT('.$value.
',\''.$cryptKey.
'\')
';
813 } elseif ($cryptType == 1) {
814 $return = 'DES_DECRYPT(
'.$value.',\
''.$cryptKey.
'\')
';
822 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
828 public function DDLGetConnectId()
835 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
847 public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
850 if (empty($charset)) {
851 $charset = $this->forcecharset;
853 if (empty($collation)) {
854 $collation = $this->forcecollate;
857 // ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
858 $sql = "CREATE DATABASE ".$this->escape($database);
859 $sql .= " DEFAULT CHARACTER SET ".$this->escape($charset)." DEFAULT COLLATE ".$this->escape($collation);
861 dol_syslog($sql, LOG_DEBUG);
862 $ret = $this->query($sql);
867 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
875 public function DDLListTables($database, $table = '')
878 $listtables = array();
882 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
884 $like = "LIKE '".$this->escape($tmptable)."'";
886 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
888 $sql = "SHOW TABLES FROM ".$tmpdatabase." ".$like.";";
890 $result = $this->query($sql);
892 while ($row = $this->fetch_row($result)) {
893 $listtables[] = $row[0];
899 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
907 public function DDLInfoTable($table)
910 $infotables = array();
912 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
914 $sql = "SHOW FULL COLUMNS FROM ".$tmptable.";";
916 dol_syslog($sql, LOG_DEBUG);
917 $result = $this->query($sql);
919 while ($row = $this->fetch_row($result)) {
920 $infotables[] = $row;
926 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
939 public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
942 // FIXME: $fulltext_keys parameter is unused
944 // cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
945 // ex. : $fields['rowid'] = array('type'=>'int','value
'=>'11
','null'=>'not
null','extra
'=> 'auto_increment
');
946 $sql = "create table ".$table."(";
948 foreach ($fields as $field_name => $field_desc) {
949 $sqlfields[$i] = $field_name." ";
950 $sqlfields[$i] .= $field_desc['type'];
951 if (preg_match("/^[^\s]/i", $field_desc['value
'])) {
952 $sqlfields[$i] .= "(".$field_desc['value
'].")";
953 } elseif (preg_match("/^[^\s]/i", $field_desc['attribute
'])) {
954 $sqlfields[$i] .= " ".$field_desc['attribute
'];
955 } elseif (preg_match("/^[^\s]/i", $field_desc['default'])) {
956 if (preg_match("/null/i", $field_desc['default'])) {
957 $sqlfields[$i] .= " default ".$field_desc['default'];
959 $sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'";
961 } elseif (preg_match("/^[^\s]/i", $field_desc['null'])) {
962 $sqlfields[$i] .= " ".$field_desc['null'];
963 } elseif (preg_match("/^[^\s]/i", $field_desc['extra
'])) {
964 $sqlfields[$i] .= " ".$field_desc['extra
'];
968 if ($primary_key != "") {
969 $pk = "primary key(".$primary_key.")";
972 if (is_array($unique_keys)) {
974 foreach ($unique_keys as $key => $value) {
975 $sqluq[$i] = "UNIQUE KEY '".$key."' ('".$this->escape($value)."')";
979 if (is_array($keys)) {
981 foreach ($keys as $key => $value) {
982 $sqlk[$i] = "KEY ".$key." (".$value.")";
986 $sql .= implode(',
', $sqlfields);
987 if ($primary_key != "") {
990 if (is_array($unique_keys)) {
991 $sql .= ",".implode(',
', $sqluq);
993 if (is_array($keys)) {
994 $sql .= ",".implode(',
', $sqlk);
996 $sql .= ") type=".$type;
998 dol_syslog($sql, LOG_DEBUG);
999 if (!$this -> query($sql)) {
1005 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1012 public function DDLDropTable($table)
1015 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
1017 $sql = "DROP TABLE ".$tmptable;
1019 if (!$this->query($sql)) {
1026 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1034 public function DDLDescTable($table, $field = "")
1037 $sql = "DESC ".$table." ".$field;
1039 dol_syslog(get_class($this)."::DDLDescTable ".$sql, LOG_DEBUG);
1040 $this->_results = $this->query($sql);
1041 return $this->_results;
1044 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1054 public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
1057 // cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
1058 // ex. : $field_desc = array('type'=>'int','value
'=>'11
','null'=>'not
null','extra
'=> 'auto_increment
');
1059 $sql = "ALTER TABLE ".$table." ADD ".$field_name." ";
1060 $sql .= $field_desc['type'];
1061 if (preg_match("/^[^\s]/i", $field_desc['value
'])) {
1062 if (!in_array($field_desc['type'], array('date
', 'datetime
'))) {
1063 $sql .= "(".$field_desc['value
'].")";
1066 if (preg_match("/^[^\s]/i", $field_desc['attribute
'])) {
1067 $sql .= " ".$field_desc['attribute
'];
1069 if (preg_match("/^[^\s]/i", $field_desc['null'])) {
1070 $sql .= " ".$field_desc['null'];
1072 if (preg_match("/^[^\s]/i", $field_desc['default'])) {
1073 if (preg_match("/null/i", $field_desc['default'])) {
1074 $sql .= " default ".$field_desc['default'];
1076 $sql .= " default '".$this->escape($field_desc['default'])."'";
1079 if (preg_match("/^[^\s]/i", $field_desc['extra
'])) {
1080 $sql .= " ".$field_desc['extra
'];
1082 $sql .= " ".$field_position;
1084 dol_syslog(get_class($this)."::DDLAddField ".$sql, LOG_DEBUG);
1085 if (!$this->query($sql)) {
1091 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1100 public function DDLUpdateField($table, $field_name, $field_desc)
1103 $sql = "ALTER TABLE ".$table;
1104 $sql .= " MODIFY COLUMN ".$field_name." ".$field_desc['type'];
1105 if ($field_desc['type'] == 'tinyint
' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar
') {
1106 $sql .= "(".$field_desc['value
'].")";
1109 dol_syslog(get_class($this)."::DDLUpdateField ".$sql, LOG_DEBUG);
1110 if (!$this->query($sql)) {
1116 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1124 public function DDLDropField($table, $field_name)
1127 $tmp_field_name = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $field_name);
1129 $sql = "ALTER TABLE ".$table." DROP COLUMN `".$tmp_field_name."`";
1130 if (!$this->query($sql)) {
1131 $this->error = $this->lasterror();
1138 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1148 public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
1151 $sql = "INSERT INTO user ";
1152 $sql .= "(Host,User,password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv)";
1153 $sql .= " VALUES ('".$this->escape($dolibarr_main_db_host)."','".$this->escape($dolibarr_main_db_user)."',password('".addslashes($dolibarr_main_db_pass)."')";
1154 $sql .= ",'Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
')";
1156 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1157 $resql = $this->query($sql);
1162 $sql = "INSERT INTO db ";
1163 $sql .= "(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv)";
1164 $sql .= " VALUES ('".$this->escape($dolibarr_main_db_host)."','".$this->escape($dolibarr_main_db_name)."','".addslashes($dolibarr_main_db_user)."'";
1165 $sql .= ",'Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
')";
1167 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
1168 $resql = $this->query($sql);
1173 $sql = "FLUSH Privileges";
1175 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
1176 $resql = $this->query($sql);
1188 public function getDefaultCharacterSetDatabase()
1198 public function getListOfCharacterSet()
1202 $liste[$i]['charset
'] = 'UTF-8
';
1212 public function getDefaultCollationDatabase()
1222 public function getListOfCollation()
1226 $liste[$i]['charset
'] = 'UTF-8
';
1236 public function getPathOfDump()
1238 // FIXME: not for SQLite
1239 $fullpathofdump = '/pathtomysqldump/mysqldump
';
1241 $resql = $this->query('SHOW VARIABLES LIKE \
'basedir\'');
1244 $basedir = $liste[
'Value'];
1245 $fullpathofdump = $basedir.(preg_match(
'/\/$/', $basedir) ?
'' :
'/').
'bin/mysqldump';
1247 return $fullpathofdump;
1258 $fullpathofimport =
'/pathtomysql/mysql';
1260 $resql = $this->
query(
'SHOW VARIABLES LIKE \'basedir\'');
1263 $basedir = $liste[
'Value'];
1264 $fullpathofimport = $basedir.(preg_match(
'/\/$/', $basedir) ?
'' :
'/').
'bin/mysql';
1266 return $fullpathofimport;
1279 if (!isset($pragmas)) {
1284 'application_id',
'auto_vacuum',
'automatic_index',
'busy_timeout',
'cache_size',
1285 'cache_spill',
'case_sensitive_like',
'checkpoint_fullsync',
'collation_list',
1286 'compile_options',
'data_version',
1287 'defer_foreign_keys',
'encoding',
'foreign_key_check',
'freelist_count',
1288 'full_column_names',
'fullsync',
'ingore_check_constraints',
'integrity_check',
1289 'journal_mode',
'journal_size_limit',
'legacy_file_format',
'locking_mode',
1290 'max_page_count',
'page_count',
'page_size',
'parser_trace',
1291 'query_only',
'quick_check',
'read_uncommitted',
'recursive_triggers',
1292 'reverse_unordered_selects',
'schema_version',
'user_version',
1293 'secure_delete',
'short_column_names',
'shrink_memory',
'soft_heap_limit',
1294 'synchronous',
'temp_store',
'threads',
1295 'vdbe_addoptrace',
'vdbe_debug',
'vdbe_listing',
'vdbe_trace',
1296 'wal_autocheckpoint',
1301 foreach ($pragmas as $var) {
1302 $sql =
"PRAGMA $var";
1307 $result[$var] = $obj[0];
1310 $result[$var] =
'FAIL';
1350 $newname = preg_replace(
'/_/',
'', $name);
1351 $localname = __CLASS__.
'::db'.$newname;
1352 $reflectClass =
new ReflectionClass(__CLASS__);
1353 $reflectFunction = $reflectClass->getMethod(
'db'.$newname);
1354 if ($arg_count < 0) {
1355 $arg_count = $reflectFunction->getNumberOfParameters();
1357 if (!$this->
db->createFunction($name, $localname, $arg_count)) {
1358 $this->
error =
"unable to create custom function '$name'";
1376 if ($y == 0 && $month == 0) {
1379 $num = (365 * $y + 31 * ($month - 1) + $day);
1383 $num -= floor(($month * 4 + 23) / 10);
1385 $temp = floor(($y / 100 + 1) * 3 / 4);
1386 return $num + floor($y / 4) - $temp;
1400 $ret = floor(($daynr + 5 + ($sunday_first_day_of_week ? 1 : 0)) % 7);
1414 return (($year & 3) == 0 && ($year % 100 || ($year % 400 == 0 && $year)) ? 366 : 365);
1428 private static function calc_week($year, $month, $day, $week_behaviour, &$calc_year)
1433 $monday_first = ($week_behaviour & self::WEEK_MONDAY_FIRST) ? 1 : 0;
1434 $week_year = ($week_behaviour & self::WEEK_YEAR) ? 1 : 0;
1435 $first_weekday = ($week_behaviour & self::WEEK_FIRST_WEEKDAY) ? 1 : 0;
1440 if ($month == 1 && $day <= 7 - $weekday) {
1441 if (!$week_year && (($first_weekday && $weekday != 0) || (!$first_weekday && $weekday >= 4))) {
1447 $weekday = ($weekday + 53 * 7 - $days) % 7;
1450 if (($first_weekday && $weekday != 0) || (!$first_weekday && $weekday >= 4)) {
1451 $days = $daynr - ($first_daynr + (7 - $weekday));
1453 $days = $daynr - ($first_daynr - $weekday);
1456 if ($week_year && $days >= 52 * 7) {
1458 if ((!$first_weekday && $weekday < 4) || ($first_weekday && $weekday == 0)) {
1463 return floor($days / 7 + 1);
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 SQLite database.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
escape($stringtoencode)
Escape a string to insert data.
fetch_array($resultset)
Return datas as an array.
error()
Renvoie le texte de l'erreur mysql de l'operation precedente.
fetch_row($resultset)
Return datas as an array.
getPathOfRestore()
Return full path of restore program.
errno()
Renvoie le code erreur generique de l'operation precedente.
const LABEL
Database label.
close()
Close database connexion.
static calc_days_in_year($year)
calc_days_in_year
static calc_daynr($year, $month, $day)
calc_daynr
escapeforlike($stringtoencode)
Escape a string to insert data into a like.
static calc_weekday($daynr, $sunday_first_day_of_week)
calc_weekday
encrypt($fieldorvalue, $withQuotes=1)
Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple ...
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.
affected_rows($resultset)
Return number of lines for result of a SELECT.
decrypt($value)
Decrypt sensitive data in database.
static calc_week($year, $month, $day, $week_behaviour, &$calc_year)
calc_week
getDriverInfo()
Return version of database client driver.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
addCustomFunction($name, $arg_count=-1)
Permet le chargement d'une fonction personnalisee dans le moteur de base de donnees.
select_db($database)
Select a database.
connect($host, $login, $passwd, $name, $port=0)
Connexion to server.
num_rows($resultset)
Return number of lines for result of a SELECT.
static convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
getVersion()
Return version of database server.
getServerParametersValues($filter='')
Return value of server parameters.
free($resultset=null)
Free last resultset used.
__construct($type, $host, $user, $pass, $name='', $port=0)
Constructor.
const VERSIONMIN
Version min database.
getServerStatusValues($filter='')
Return value of server status.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
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
$conf db
API class for accounts.