28require_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);
142 if (preg_match(
'/^--\s\$Id/i', $line)) {
146 if (preg_match(
'/^#/i', $line) || preg_match(
'/^$/i', $line) || preg_match(
'/^--/i', $line)) {
150 if (
$type ==
'auto') {
151 if (preg_match(
'/ALTER TABLE/i', $line)) {
153 } elseif (preg_match(
'/CREATE TABLE/i', $line)) {
155 } elseif (preg_match(
'/DROP TABLE/i', $line)) {
160 if (
$type ==
'dml') {
161 $line = preg_replace(
'/\s/',
' ', $line);
164 if (preg_match(
'/(ISAM|innodb)/i', $line)) {
165 $line = preg_replace(
'/\)[\s\t]*type[\s\t]*=[\s\t]*(MyISAM|innodb);/i',
');', $line);
166 $line = preg_replace(
'/\)[\s\t]*engine[\s\t]*=[\s\t]*(MyISAM|innodb);/i',
');', $line);
167 $line = preg_replace(
'/,$/',
'', $line);
171 if (preg_match(
'/[\s\t\(]*(\w*)[\s\t]+int.*auto_increment/i', $line, $reg)) {
172 $newline = preg_replace(
'/([\s\t\(]*)([a-zA-Z_0-9]*)[\s\t]+int.*auto_increment[^,]*/i',
'\\1 \\2 integer PRIMARY KEY AUTOINCREMENT', $line);
178 $line = str_replace(
'tinyint',
'smallint', $line);
181 $line = preg_replace(
'/(int\w+|smallint)\s+unsigned/i',
'\\1', $line);
184 $line = preg_replace(
'/\w*blob/i',
'text', $line);
187 $line = preg_replace(
'/tinytext/i',
'text', $line);
188 $line = preg_replace(
'/mediumtext/i',
'text', $line);
192 $line = preg_replace(
'/datetime not null/i',
'datetime', $line);
193 $line = preg_replace(
'/datetime/i',
'timestamp', $line);
196 $line = preg_replace(
'/^double/i',
'numeric', $line);
197 $line = preg_replace(
'/(\s*)double/i',
'\\1numeric', $line);
199 $line = preg_replace(
'/^float/i',
'numeric', $line);
200 $line = preg_replace(
'/(\s*)float/i',
'\\1numeric', $line);
203 if (preg_match(
'/unique index\s*\((\w+\s*,\s*\w+)\)/i', $line)) {
204 $line = preg_replace(
'/unique index\s*\((\w+\s*,\s*\w+)\)/i',
'UNIQUE\(\\1\)', $line);
208 $line = preg_replace(
'/AFTER [a-z0-9_]+/i',
'', $line);
211 $line = preg_replace(
'/ALTER TABLE [a-z0-9_]+ DROP INDEX/i',
'DROP INDEX', $line);
214 if (preg_match(
'/ALTER TABLE ([a-z0-9_]+) CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i', $line, $reg)) {
215 $line =
"-- ".$line.
" replaced by --\n";
216 $line .=
"ALTER TABLE ".$reg[1].
" RENAME COLUMN ".$reg[2].
" TO ".$reg[3];
220 if (preg_match(
'/ALTER TABLE ([a-z0-9_]+) MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i', $line, $reg)) {
221 $line =
"-- ".$line.
" replaced by --\n";
223 $newreg3 = preg_replace(
'/ DEFAULT NULL/i',
'', $newreg3);
224 $newreg3 = preg_replace(
'/ NOT NULL/i',
'', $newreg3);
225 $newreg3 = preg_replace(
'/ NULL/i',
'', $newreg3);
226 $newreg3 = preg_replace(
'/ DEFAULT 0/i',
'', $newreg3);
227 $newreg3 = preg_replace(
'/ DEFAULT \'[0-9a-zA-Z_@]*\'/i',
'', $newreg3);
228 $line .=
"ALTER TABLE ".$reg[1].
" ALTER COLUMN ".$reg[2].
" TYPE ".$newreg3;
234 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i', $line, $reg)) {
235 $line =
"-- ".$line.
" replaced by --\n";
236 $line .=
"CREATE UNIQUE INDEX ".$reg[2].
" ON ".$reg[1].
"(".$reg[3];
241 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i', $line, $reg)) {
242 $line =
"-- ".$line.
" replaced by --\n";
243 $line .=
"ALTER TABLE ".$reg[1].
" DROP CONSTRAINT ".$reg[2];
248 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i', $line, $reg)) {
249 $fieldlist = $reg[4];
251 $tablename = $reg[1];
252 $line =
"-- ".$line.
" replaced by --\n";
253 $line .=
"CREATE ".(preg_match(
'/UNIQUE/', $reg[2]) ?
'UNIQUE ' :
'').
"INDEX ".$idxname.
" ON ".$tablename.
" (".$fieldlist.
")";
255 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)) {
257 dol_syslog(get_class().
'::query line emptied');
268 if (preg_match(
'/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i', $line, $reg)) {
269 if ($reg[1] == $reg[2]) {
270 $line = preg_replace(
'/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i',
'DELETE FROM \\1 USING \\3', $line);
275 $line = preg_replace(
'/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i',
'FROM \\1', $line);
279 $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);
283 $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);
302 dol_syslog(get_class($this).
"::select_db database=".$database, LOG_DEBUG);
320 public function connect($host, $login, $passwd, $name, $port = 0)
322 global $main_data_dir;
324 dol_syslog(get_class($this).
"::connect name=".$name, LOG_DEBUG);
326 $dir = $main_data_dir;
328 $dir = DOL_DATA_ROOT;
332 $database_name = $dir.
'/database_'.$name.
'.sdb';
336 $this->db =
new SQLite3($database_name);
339 $this->
error = self::LABEL.
' '.$e->getMessage().
' current dir='.$database_name;
355 $tmp = $this->db->version();
356 return $tmp[
'versionString'];
366 return 'sqlite3 php driver';
379 if ($this->transaction_opened > 0) {
380 dol_syslog(get_class($this).
"::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
382 $this->connected =
false;
400 public function query($query, $usesavepoint = 0,
$type =
'auto', $result_mode = 0)
402 global $conf, $dolibarr_main_db_readonly;
406 $query = trim($query);
412 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)) {
417 $foreignFields = $reg[5];
418 $foreignTable = $reg[4];
419 $localfields = $reg[3];
420 $constraintname = trim($reg[2]);
421 $tablename = trim($reg[1]);
423 $descTable = $this->db->querySingle(
"SELECT sql FROM sqlite_master WHERE name='".$this->
escape($tablename).
"'");
426 $this->
query(
"ALTER TABLE ".$tablename.
" RENAME TO tmp_".$tablename);
431 $descTable = substr($descTable, 0, strlen($descTable) - 1);
432 $descTable .=
", CONSTRAINT ".$constraintname.
" FOREIGN KEY (".$localfields.
") REFERENCES ".$foreignTable.
"(".$foreignFields.
")";
438 $this->
query($descTable);
441 $this->
query(
"INSERT INTO ".$tablename.
" SELECT * FROM tmp_".$tablename);
444 $this->
query(
"DROP TABLE tmp_".$tablename);
453 if (!in_array($query, array(
'BEGIN',
'COMMIT',
'ROLLBACK'))) {
454 $SYSLOG_SQL_LIMIT = 10000;
455 dol_syslog(
'sql='.substr($query, 0, $SYSLOG_SQL_LIMIT), LOG_DEBUG);
461 if (!empty($dolibarr_main_db_readonly)) {
462 if (preg_match(
'/^(INSERT|UPDATE|REPLACE|DELETE|CREATE|ALTER|TRUNCATE|DROP)/i', $query)) {
463 $this->
lasterror =
'Application in read-only mode';
473 $ret = $this->db->query($query);
475 $ret->queryString = $query;
478 $this->
error = $this->db->lastErrorMsg();
481 if (!preg_match(
"/^COMMIT/i", $query) && !preg_match(
"/^ROLLBACK/i", $query)) {
483 if (!is_object($ret) || $this->
error) {
488 dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
490 $errormsg = get_class($this).
"::query SQL Error message: ".$this->lasterror;
492 if (preg_match(
'/[0-9]/', $this->
lasterrno)) {
493 $errormsg .=
' ('.$this->lasterrno.
')';
496 if ($conf->global->SYSLOG_LEVEL < LOG_DEBUG) {
497 dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
499 dol_syslog(get_class($this).
"::query SQL Error message: ".$errormsg, LOG_ERR);
502 $this->_results = $ret;
519 if (!is_object($resultset)) {
520 $resultset = $this->_results;
523 $ret = $resultset->fetchArray(SQLITE3_ASSOC);
525 return (
object) $ret;
542 if (!is_object($resultset)) {
543 $resultset = $this->_results;
546 $ret = $resultset->fetchArray(SQLITE3_ASSOC);
561 if (!is_bool($resultset)) {
562 if (!is_object($resultset)) {
563 $resultset = $this->_results;
565 return $resultset->fetchArray(SQLITE3_NUM);
586 if (!is_object($resultset)) {
587 $resultset = $this->_results;
589 if (preg_match(
"/^SELECT/i", $resultset->queryString)) {
590 return $this->db->querySingle(
"SELECT count(*) FROM (".$resultset->queryString.
") q");
609 if (!is_object($resultset)) {
610 $resultset = $this->_results;
612 if (preg_match(
"/^SELECT/i", $resultset->queryString)) {
617 return $this->db->changes();
627 public function free($resultset =
null)
630 if (!is_object($resultset)) {
631 $resultset = $this->_results;
634 if ($resultset && is_object($resultset)) {
635 $resultset->finalize();
647 return Sqlite3::escapeString($stringtoencode);
658 return str_replace(array(
'\\',
'_',
'%'), array(
'\\\\',
'\_',
'\%'), (
string) $stringtoencode);
668 if (!$this->connected) {
670 return 'DB_ERROR_FAILED_TO_CONNECT';
705 $errno = $this->db->lastErrorCode();
706 if ($errno ==
'HY000' || $errno == 0) {
707 if (preg_match(
'/table.*already exists/i', $this->
error)) {
708 return 'DB_ERROR_TABLE_ALREADY_EXISTS';
709 } elseif (preg_match(
'/index.*already exists/i', $this->
error)) {
710 return 'DB_ERROR_KEY_NAME_ALREADY_EXISTS';
711 } elseif (preg_match(
'/syntax error/i', $this->
error)) {
712 return 'DB_ERROR_SYNTAX';
715 if ($errno ==
'23000') {
716 if (preg_match(
'/column.* not unique/i', $this->
error)) {
717 return 'DB_ERROR_RECORD_ALREADY_EXISTS';
718 } elseif (preg_match(
'/PRIMARY KEY must be unique/i', $this->
error)) {
719 return 'DB_ERROR_RECORD_ALREADY_EXISTS';
726 return ($errno ?
'DB_ERROR_'.$errno :
'0');
737 if (!$this->connected) {
739 return 'Not connected. Check setup parameters in conf/conf.php file and your sqlite version';
756 return $this->db->lastInsertRowId();
767 public function encrypt($fieldorvalue, $withQuotes = 1)
772 $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
775 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
777 $escapedstringwithquotes = ($withQuotes ?
"'" :
"").$this->
escape($fieldorvalue).($withQuotes ?
"'" :
"");
779 if ($cryptType && !empty($cryptKey)) {
780 if ($cryptType == 2) {
781 $escapedstringwithquotes =
"AES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
782 } elseif ($cryptType == 1) {
783 $escapedstringwithquotes =
"DES_ENCRYPT(".$escapedstringwithquotes.
", '".$this->
escape($cryptKey).
"')";
787 return $escapedstringwithquotes;
801 $cryptType = ($conf->db->dolibarr_main_db_encryption ? $conf->db->dolibarr_main_db_encryption : 0);
804 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
808 if ($cryptType && !empty($cryptKey)) {
809 if ($cryptType == 2) {
810 $return =
'AES_DECRYPT('.$value.
',\''.$cryptKey.
'\')
';
811 } elseif ($cryptType == 1) {
812 $return = 'DES_DECRYPT(
'.$value.',\
''.$cryptKey.
'\')
';
820 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
826 public function DDLGetConnectId()
833 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
845 public function DDLCreateDb($database, $charset = '
', $collation = '', $owner = '')
848 if (empty($charset)) {
849 $charset = $this->forcecharset;
851 if (empty($collation)) {
852 $collation = $this->forcecollate;
855 // ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
856 $sql = "CREATE DATABASE ".$this->escape($database);
857 $sql .= " DEFAULT CHARACTER SET ".$this->escape($charset)." DEFAULT COLLATE ".$this->escape($collation);
859 dol_syslog($sql, LOG_DEBUG);
860 $ret = $this->query($sql);
865 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
873 public function DDLListTables($database, $table = '
')
876 $listtables = array();
880 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
882 $like = "LIKE '".$this->escape($tmptable)."'";
884 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
886 $sql = "SHOW TABLES FROM ".$tmpdatabase." ".$like.";";
888 $result = $this->query($sql);
890 while ($row = $this->fetch_row($result)) {
891 $listtables[] = $row[0];
897 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
905 public function DDLListTablesFull($database, $table = '
')
908 $listtables = array();
912 $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i
', '', $table);
914 $like = "LIKE '".$this->escape($tmptable)."'";
916 $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $database);
918 $sql = "SHOW FULL TABLES FROM ".$tmpdatabase." ".$like.";";
920 $result = $this->query($sql);
922 while ($row = $this->fetch_row($result)) {
923 $listtables[] = $row;
929 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
937 public function DDLInfoTable($table)
940 $infotables = array();
942 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
944 $sql = "SHOW FULL COLUMNS FROM ".$tmptable.";";
946 dol_syslog($sql, LOG_DEBUG);
947 $result = $this->query($sql);
949 while ($row = $this->fetch_row($result)) {
950 $infotables[] = $row;
956 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
969 public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
972 // FIXME: $fulltext_keys parameter is unused
974 // cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
975 // ex. : $fields['
rowid'] = array('type'=>'int','value
'=>'11
','null'=>'not
null','extra
'=> 'auto_increment
');
976 $sql = "create table ".$table."(";
978 foreach ($fields as $field_name => $field_desc) {
979 $sqlfields[$i] = $field_name." ";
980 $sqlfields[$i] .= $field_desc['type'];
981 if (preg_match("/^[^\s]/i", $field_desc['value
'])) {
982 $sqlfields[$i] .= "(".$field_desc['value
'].")";
983 } elseif (preg_match("/^[^\s]/i", $field_desc['attribute
'])) {
984 $sqlfields[$i] .= " ".$field_desc['attribute
'];
985 } elseif (preg_match("/^[^\s]/i", $field_desc['default'])) {
986 if (preg_match("/null/i", $field_desc['default'])) {
987 $sqlfields[$i] .= " default ".$field_desc['default'];
989 $sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'";
991 } elseif (preg_match("/^[^\s]/i", $field_desc['null'])) {
992 $sqlfields[$i] .= " ".$field_desc['null'];
993 } elseif (preg_match("/^[^\s]/i", $field_desc['extra
'])) {
994 $sqlfields[$i] .= " ".$field_desc['extra
'];
998 if ($primary_key != "") {
999 $pk = "primary key(".$primary_key.")";
1002 if (is_array($unique_keys)) {
1004 foreach ($unique_keys as $key => $value) {
1005 $sqluq[$i] = "UNIQUE KEY '".$key."' ('".$this->escape($value)."')";
1009 if (is_array($keys)) {
1011 foreach ($keys as $key => $value) {
1012 $sqlk[$i] = "KEY ".$key." (".$value.")";
1016 $sql .= implode(',
', $sqlfields);
1017 if ($primary_key != "") {
1020 if (is_array($unique_keys)) {
1021 $sql .= ",".implode(',
', $sqluq);
1023 if (is_array($keys)) {
1024 $sql .= ",".implode(',
', $sqlk);
1026 $sql .= ") type=".$type;
1028 dol_syslog($sql, LOG_DEBUG);
1029 if (!$this -> query($sql)) {
1035 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1042 public function DDLDropTable($table)
1045 $tmptable = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $table);
1047 $sql = "DROP TABLE ".$tmptable;
1049 if (!$this->query($sql)) {
1056 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1064 public function DDLDescTable($table, $field = "")
1067 $sql = "DESC ".$table." ".$field;
1069 dol_syslog(get_class($this)."::DDLDescTable ".$sql, LOG_DEBUG);
1070 $this->_results = $this->query($sql);
1071 return $this->_results;
1074 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1084 public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
1087 // cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
1088 // ex. : $field_desc = array('
type'=>'int','value
'=>'11
','null'=>'not
null','extra
'=> 'auto_increment
');
1089 $sql = "ALTER TABLE ".$table." ADD ".$field_name." ";
1090 $sql .= $field_desc['type'];
1091 if (preg_match("/^[^\s]/i", $field_desc['value
'])) {
1092 if (!in_array($field_desc['type'], array('date
', 'datetime
'))) {
1093 $sql .= "(".$field_desc['value
'].")";
1096 if (preg_match("/^[^\s]/i", $field_desc['attribute
'])) {
1097 $sql .= " ".$field_desc['attribute
'];
1099 if (preg_match("/^[^\s]/i", $field_desc['null'])) {
1100 $sql .= " ".$field_desc['null'];
1102 if (preg_match("/^[^\s]/i", $field_desc['default'])) {
1103 if (preg_match("/null/i", $field_desc['default'])) {
1104 $sql .= " default ".$field_desc['default'];
1106 $sql .= " default '".$this->escape($field_desc['default'])."'";
1109 if (preg_match("/^[^\s]/i", $field_desc['extra
'])) {
1110 $sql .= " ".$field_desc['extra
'];
1112 $sql .= " ".$field_position;
1114 dol_syslog(get_class($this)."::DDLAddField ".$sql, LOG_DEBUG);
1115 if (!$this->query($sql)) {
1121 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1130 public function DDLUpdateField($table, $field_name, $field_desc)
1133 $sql = "ALTER TABLE ".$table;
1134 $sql .= " MODIFY COLUMN ".$field_name." ".$field_desc['
type'];
1135 if ($field_desc['type'] == 'tinyint
' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar
') {
1136 $sql .= "(".$field_desc['value
'].")";
1139 dol_syslog(get_class($this)."::DDLUpdateField ".$sql, LOG_DEBUG);
1140 if (!$this->query($sql)) {
1146 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1154 public function DDLDropField($table, $field_name)
1157 $tmp_field_name = preg_replace('/[^a-z0-9\.\-\_]/i
', '', $field_name);
1159 $sql = "ALTER TABLE ".$table." DROP COLUMN `".$tmp_field_name."`";
1160 if (!$this->query($sql)) {
1161 $this->error = $this->lasterror();
1168 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1178 public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
1181 $sql = "INSERT INTO user ";
1182 $sql .= "(Host,User,password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv)";
1183 $sql .= " VALUES ('
".$this->escape($dolibarr_main_db_host)."','".$this->escape($dolibarr_main_db_user)."',password('".addslashes($dolibarr_main_db_pass)."')";
1184 $sql .= ",'Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
')";
1186 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1187 $resql = $this->query($sql);
1192 $sql = "INSERT INTO db ";
1193 $sql .= "(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv)";
1194 $sql .= " VALUES ('".$this->escape($dolibarr_main_db_host)."','".$this->escape($dolibarr_main_db_name)."','".addslashes($dolibarr_main_db_user)."'";
1195 $sql .= ",'Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
','Y
')";
1197 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
1198 $resql = $this->query($sql);
1203 $sql = "FLUSH Privileges";
1205 dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
1206 $resql = $this->query($sql);
1218 public function getDefaultCharacterSetDatabase()
1228 public function getListOfCharacterSet()
1232 $liste[$i]['charset
'] = 'UTF-8
';
1242 public function getDefaultCollationDatabase()
1252 public function getListOfCollation()
1256 $liste[$i]['charset
'] = 'UTF-8
';
1266 public function getPathOfDump()
1268 // FIXME: not for SQLite
1269 $fullpathofdump = '/pathtomysqldump/mysqldump
';
1271 $resql = $this->query('SHOW VARIABLES LIKE \
'basedir\'');
1274 $basedir = $liste[
'Value'];
1275 $fullpathofdump = $basedir.(preg_match(
'/\/$/', $basedir) ?
'' :
'/').
'bin/mysqldump';
1277 return $fullpathofdump;
1288 $fullpathofimport =
'/pathtomysql/mysql';
1290 $resql = $this->
query(
'SHOW VARIABLES LIKE \'basedir\'');
1293 $basedir = $liste[
'Value'];
1294 $fullpathofimport = $basedir.(preg_match(
'/\/$/', $basedir) ?
'' :
'/').
'bin/mysql';
1296 return $fullpathofimport;
1309 if (!isset($pragmas)) {
1314 'application_id',
'auto_vacuum',
'automatic_index',
'busy_timeout',
'cache_size',
1315 'cache_spill',
'case_sensitive_like',
'checkpoint_fullsync',
'collation_list',
1316 'compile_options',
'data_version',
1317 'defer_foreign_keys',
'encoding',
'foreign_key_check',
'freelist_count',
1318 'full_column_names',
'fullsync',
'ingore_check_constraints',
'integrity_check',
1319 'journal_mode',
'journal_size_limit',
'legacy_file_format',
'locking_mode',
1320 'max_page_count',
'page_count',
'page_size',
'parser_trace',
1321 'query_only',
'quick_check',
'read_uncommitted',
'recursive_triggers',
1322 'reverse_unordered_selects',
'schema_version',
'user_version',
1323 'secure_delete',
'short_column_names',
'shrink_memory',
'soft_heap_limit',
1324 'synchronous',
'temp_store',
'threads',
1325 'vdbe_addoptrace',
'vdbe_debug',
'vdbe_listing',
'vdbe_trace',
1326 'wal_autocheckpoint',
1331 foreach ($pragmas as $var) {
1332 $sql =
"PRAGMA $var";
1333 $resql = $this->
query($sql);
1337 $result[$var] = $obj[0];
1340 $result[$var] =
'FAIL';
1380 $newname = preg_replace(
'/_/',
'', $name);
1381 $localname = __CLASS__.
'::db'.$newname;
1382 $reflectClass =
new ReflectionClass(__CLASS__);
1383 $reflectFunction = $reflectClass->getMethod(
'db'.$newname);
1384 if ($arg_count < 0) {
1385 $arg_count = $reflectFunction->getNumberOfParameters();
1387 if (!$this->db->createFunction($name, $localname, $arg_count)) {
1388 $this->
error =
"unable to create custom function '$name'";
1406 if ($y == 0 && $month == 0) {
1409 $num = (365 * $y + 31 * ($month - 1) + $day);
1413 $num -= floor(($month * 4 + 23) / 10);
1415 $temp = floor(($y / 100 + 1) * 3 / 4);
1416 return $num + floor($y / 4) - $temp;
1430 $ret = floor(($daynr + 5 + ($sunday_first_day_of_week ? 1 : 0)) % 7);
1444 return (($year & 3) == 0 && ($year % 100 || ($year % 400 == 0 && $year)) ? 366 : 365);
1458 private static function calc_week($year, $month, $day, $week_behaviour, &$calc_year)
1463 $monday_first = ($week_behaviour & self::WEEK_MONDAY_FIRST) ? 1 : 0;
1464 $week_year = ($week_behaviour & self::WEEK_YEAR) ? 1 : 0;
1465 $first_weekday = ($week_behaviour & self::WEEK_FIRST_WEEKDAY) ? 1 : 0;
1470 if ($month == 1 && $day <= 7 - $weekday) {
1471 if (!$week_year && (($first_weekday && $weekday != 0) || (!$first_weekday && $weekday >= 4))) {
1477 $weekday = ($weekday + 53 * 7 - $days) % 7;
1480 if (($first_weekday && $weekday != 0) || (!$first_weekday && $weekday >= 4)) {
1481 $days = $daynr - ($first_daynr + (7 - $weekday));
1483 $days = $daynr - ($first_daynr - $weekday);
1486 if ($week_year && $days >= 52 * 7) {
1488 if ((!$first_weekday && $weekday < 4) || ($first_weekday && $weekday == 0)) {
1493 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.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
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.
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.
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.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e rowid
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type