31 require_once DOL_DOCUMENT_ROOT.
'/core/db/DoliDB.class.php';
51 public $unescapeslashquot;
52 public $standard_conforming_strings;
70 if (!empty($conf->db->character_set)) $this->forcecharset = $conf->db->character_set;
71 if (!empty($conf->db->dolibarr_main_db_collation)) $this->forcecollate = $conf->db->dolibarr_main_db_collation;
73 $this->database_user = $user;
74 $this->database_host = $host;
75 $this->database_port = $port;
77 $this->transaction_opened = 0;
81 if (!function_exists(
"pg_connect"))
83 $this->connected =
false;
85 $this->
error =
"Pgsql PHP functions are not available in this version of PHP";
86 dol_syslog(get_class($this).
"::DoliDBPgsql : Pgsql PHP functions are not available in this version of PHP", LOG_ERR);
92 $this->connected =
false;
94 $this->
error = $langs->trans(
"ErrorWrongHostParameter");
95 dol_syslog(get_class($this).
"::DoliDBPgsql : Erreur Connect, wrong host parameters", LOG_ERR);
101 $this->
db = $this->
connect($host, $user, $pass, $name, $port);
105 $this->connected =
true;
109 $this->connected =
false;
111 $this->
error =
'Host, login or password incorrect';
112 dol_syslog(get_class($this).
"::DoliDBPgsql : Erreur Connect ".$this->
error, LOG_ERR);
116 if ($this->connected && $name)
120 $this->database_selected =
true;
121 $this->database_name = $name;
124 $this->database_selected =
false;
125 $this->database_name =
'';
128 dol_syslog(get_class($this).
"::DoliDBPgsql : Erreur Select_db ".$this->
error, LOG_ERR);
132 $this->database_selected =
false;
152 if (preg_match(
'/^--\s\$Id/i', $line)) {
156 if (preg_match(
'/^#/i', $line) || preg_match(
'/^$/i', $line) || preg_match(
'/^--/i', $line))
164 $line = preg_replace(
'/GROUP_CONCAT/i',
'STRING_AGG', $line);
165 $line = preg_replace(
'/ SEPARATOR/i',
',', $line);
166 $line = preg_replace(
'/STRING_AGG\(([^,\)]+)\)/i',
'STRING_AGG(\\1, \',\')', $line);
171 if (preg_match(
'/ALTER TABLE/i', $line))
$type =
'dml';
172 elseif (preg_match(
'/CREATE TABLE/i', $line))
$type =
'dml';
173 elseif (preg_match(
'/DROP TABLE/i', $line))
$type =
'dml';
176 $line = preg_replace(
'/ as signed\)/i',
' as integer)', $line);
182 $line = preg_replace(
'/\s/',
' ', $line);
185 if (preg_match(
'/(ISAM|innodb)/i', $line)) {
186 $line = preg_replace(
'/\)[\s\t]*type[\s\t]*=[\s\t]*(MyISAM|innodb).*;/i',
');', $line);
187 $line = preg_replace(
'/\)[\s\t]*engine[\s\t]*=[\s\t]*(MyISAM|innodb).*;/i',
');', $line);
188 $line = preg_replace(
'/,$/',
'', $line);
192 if (preg_match(
'/[\s\t\(]*(\w*)[\s\t]+int.*auto_increment/i', $line, $reg)) {
193 $newline = preg_replace(
'/([\s\t\(]*)([a-zA-Z_0-9]*)[\s\t]+int.*auto_increment[^,]*/i',
'\\1 \\2 SERIAL PRIMARY KEY', $line);
198 if (preg_match(
'/[\s\t\(]*(\w*)[\s\t]+bigint.*auto_increment/i', $line, $reg)) {
199 $newline = preg_replace(
'/([\s\t\(]*)([a-zA-Z_0-9]*)[\s\t]+bigint.*auto_increment[^,]*/i',
'\\1 \\2 BIGSERIAL PRIMARY KEY', $line);
205 $line = preg_replace(
'/tinyint\(?[0-9]*\)?/',
'smallint', $line);
206 $line = preg_replace(
'/tinyint/i',
'smallint', $line);
209 $line = preg_replace(
'/(int\w+|smallint)\s+unsigned/i',
'\\1', $line);
212 $line = preg_replace(
'/\w*blob/i',
'text', $line);
215 $line = preg_replace(
'/tinytext/i',
'text', $line);
216 $line = preg_replace(
'/mediumtext/i',
'text', $line);
217 $line = preg_replace(
'/longtext/i',
'text', $line);
219 $line = preg_replace(
'/text\([0-9]+\)/i',
'text', $line);
223 $line = preg_replace(
'/datetime not null/i',
'datetime', $line);
224 $line = preg_replace(
'/datetime/i',
'timestamp', $line);
227 $line = preg_replace(
'/^double/i',
'numeric', $line);
228 $line = preg_replace(
'/(\s*)double/i',
'\\1numeric', $line);
230 $line = preg_replace(
'/^float/i',
'numeric', $line);
231 $line = preg_replace(
'/(\s*)float/i',
'\\1numeric', $line);
235 $line = preg_replace(
'/(\s*)tms(\s*)timestamp/i',
'\\1tms timestamp without time zone DEFAULT now() NOT NULL', $line);
238 $line = preg_replace(
'/(\s*)DEFAULT(\s*)CURRENT_TIMESTAMP/i',
'\\1', $line);
241 $line = preg_replace(
'/(\s*)ON(\s*)UPDATE(\s*)CURRENT_TIMESTAMP/i',
'\\1', $line);
244 if (preg_match(
'/unique index\s*\((\w+\s*,\s*\w+)\)/i', $line))
246 $line = preg_replace(
'/unique index\s*\((\w+\s*,\s*\w+)\)/i',
'UNIQUE\(\\1\)', $line);
250 $line = preg_replace(
'/\sAFTER [a-z0-9_]+/i',
'', $line);
253 $line = preg_replace(
'/ALTER TABLE [a-z0-9_]+\s+DROP INDEX/i',
'DROP INDEX', $line);
256 if (preg_match(
'/ALTER TABLE ([a-z0-9_]+)\s+CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i', $line, $reg))
258 $line =
"-- ".$line.
" replaced by --\n";
259 $line .=
"ALTER TABLE ".$reg[1].
" RENAME COLUMN ".$reg[2].
" TO ".$reg[3];
263 if (preg_match(
'/ALTER TABLE ([a-z0-9_]+)\s+MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i', $line, $reg))
265 $line =
"-- ".$line.
" replaced by --\n";
267 $newreg3 = preg_replace(
'/ DEFAULT NULL/i',
'', $newreg3);
268 $newreg3 = preg_replace(
'/ NOT NULL/i',
'', $newreg3);
269 $newreg3 = preg_replace(
'/ NULL/i',
'', $newreg3);
270 $newreg3 = preg_replace(
'/ DEFAULT 0/i',
'', $newreg3);
271 $newreg3 = preg_replace(
'/ DEFAULT \'?[0-9a-zA-Z_@]*\'?/i',
'', $newreg3);
272 $line .=
"ALTER TABLE ".$reg[1].
" ALTER COLUMN ".$reg[2].
" TYPE ".$newreg3;
278 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i', $line, $reg))
280 $line =
"-- ".$line.
" replaced by --\n";
281 $line .=
"ALTER TABLE ".$reg[1].
" ADD PRIMARY KEY (".$reg[3];
286 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*DROP\s+PRIMARY\s+KEY\s*([^;]+)$/i', $line, $reg))
288 $line =
"-- ".$line.
" replaced by --\n";
289 $line .=
"ALTER TABLE ".$reg[1].
" DROP CONSTRAINT ".$reg[2];
294 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i', $line, $reg))
296 $line =
"-- ".$line.
" replaced by --\n";
297 $line .=
"ALTER TABLE ".$reg[1].
" DROP CONSTRAINT ".$reg[2];
302 if (preg_match(
'/ALTER\s+TABLE\s+(.*)\s*ADD CONSTRAINT\s+(.*)\s*FOREIGN\s+KEY\s*(.*)$/i', $line, $reg))
304 $line = preg_replace(
'/;$/',
'', $line);
305 $line .=
" DEFERRABLE INITIALLY IMMEDIATE;";
310 if (preg_match(
'/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i', $line, $reg))
312 $fieldlist = $reg[4];
314 $tablename = $reg[1];
315 $line =
"-- ".$line.
" replaced by --\n";
316 $line .=
"CREATE ".(preg_match(
'/UNIQUE/', $reg[2]) ?
'UNIQUE ' :
'').
"INDEX ".$idxname.
" ON ".$tablename.
" (".$fieldlist.
")";
322 $line = str_replace(
' LIKE \'',
' ILIKE \'', $line, $count_like);
323 if (!empty($conf->global->PSQL_USE_UNACCENT) && $count_like > 0)
326 $line = preg_replace(
'/\s+(\(+\s*)([a-zA-Z0-9\-\_\.]+) ILIKE /',
' \1unaccent(\2) ILIKE ', $line);
329 $line = str_replace(
' LIKE BINARY \'',
' LIKE \'', $line);
332 $line = preg_replace(
'/^INSERT IGNORE/',
'INSERT', $line);
336 if (preg_match(
'/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i', $line, $reg))
338 if ($reg[1] == $reg[2])
340 $line = preg_replace(
'/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i',
'DELETE FROM \\1 USING \\3', $line);
345 $line = preg_replace(
'/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i',
'FROM \\1', $line);
349 $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);
353 $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);
357 $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*),\s*([a-z_]+\s+as\s+[a-z_]+\s*)\)/i',
'FROM \\1, \\2, \\3, \\4', $line);
361 $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*),\s*([a-z_]+\s+as\s+[a-z_]+\s*),\s*([a-z_]+\s+as\s+[a-z_]+\s*)\)/i',
'FROM \\1, \\2, \\3, \\4, \\5', $line);
368 if ($unescapeslashquot) $line = preg_replace(
"/\\\'/",
"''", $line);
388 if ($database == $this->database_name) {
406 public function connect($host, $login, $passwd, $name, $port = 0)
413 $host = str_replace(array(
"\\",
"'"), array(
"\\\\",
"\\'"), $host);
414 $login = str_replace(array(
"\\",
"'"), array(
"\\\\",
"\\'"), $login);
415 $passwd = str_replace(array(
"\\",
"'"), array(
"\\\\",
"\\'"), $passwd);
416 $name = str_replace(array(
"\\",
"'"), array(
"\\\\",
"\\'"), $name);
417 $port = str_replace(array(
"\\",
"'"), array(
"\\\\",
"\\'"), $port);
419 if (!$name) $name =
"postgres";
422 if ((!empty($host) && $host ==
"socket") && !defined(
'NOLOCALSOCKETPGCONNECT'))
424 $con_string =
"dbname='".$name.
"' user='".$login.
"' password='".$passwd.
"'";
425 $this->
db = @pg_connect($con_string);
431 if (!$host) $host =
"localhost";
432 if (!$port) $port = 5432;
434 $con_string =
"host='".$host.
"' port='".$port.
"' dbname='".$name.
"' user='".$login.
"' password='".$passwd.
"'";
435 $this->
db = @pg_connect($con_string);
441 $this->database_name = $name;
442 pg_set_error_verbosity($this->
db, PGSQL_ERRORS_VERBOSE);
443 pg_query($this->
db,
"set datestyle = 'ISO, YMD';");
460 return $liste[
'server_version'];
472 return 'pgsql php driver';
485 if ($this->transaction_opened > 0)
dol_syslog(get_class($this).
"::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
486 $this->connected =
false;
487 return pg_close($this->
db);
500 public function query($query, $usesavepoint = 0,
$type =
'auto')
504 $query = trim($query);
507 $query = $this->
convertSQLFromMysql($query,
$type, ($this->unescapeslashquot && $this->standard_conforming_strings));
510 if (!empty($conf->global->MAIN_DB_AUTOFIX_BAD_SQL_REQUEST))
516 if (preg_match(
'/([^\'])([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9])/', $query))
518 $query = preg_replace(
'/([^\'])([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9])/',
'\\1\'\\2\'', $query);
519 dol_syslog(
"Warning: Bad formed request converted into ".$query, LOG_WARNING);
520 }
else $loop =
false;
524 if ($usesavepoint && $this->transaction_opened)
526 @pg_query($this->
db,
'SAVEPOINT mysavepoint');
529 if (!in_array($query, array(
'BEGIN',
'COMMIT',
'ROLLBACK')))
531 $SYSLOG_SQL_LIMIT = 10000;
532 dol_syslog(
'sql='.substr($query, 0, $SYSLOG_SQL_LIMIT), LOG_DEBUG);
535 $ret = @pg_query($this->
db, $query);
538 if (!preg_match(
"/^COMMIT/i", $query) && !preg_match(
"/^ROLLBACK/i", $query))
542 if ($this->
errno() !=
'DB_ERROR_25P02')
548 if ($conf->global->SYSLOG_LEVEL < LOG_DEBUG)
dol_syslog(get_class($this).
"::query SQL Error query: ".$query, LOG_ERR);
549 dol_syslog(get_class($this).
"::query SQL Error message: ".$this->
lasterror.
" (".$this->lasterrno.
")", LOG_ERR);
550 dol_syslog(get_class($this).
"::query SQL Error usesavepoint = ".$usesavepoint, LOG_ERR);
553 if ($usesavepoint && $this->transaction_opened)
555 @pg_query($this->
db,
'ROLLBACK TO SAVEPOINT mysavepoint');
559 $this->_results = $ret;
576 if (!is_resource($resultset)) { $resultset = $this->_results; }
577 return pg_fetch_object($resultset);
591 if (!is_resource($resultset)) { $resultset = $this->_results; }
592 return pg_fetch_array($resultset);
606 if (!is_resource($resultset)) { $resultset = $this->_results; }
607 return pg_fetch_row($resultset);
622 if (!is_resource($resultset)) { $resultset = $this->_results; }
623 return pg_num_rows($resultset);
638 if (!is_resource($resultset)) { $resultset = $this->_results; }
641 return pg_affected_rows($resultset);
651 public function free($resultset = null)
654 if (!is_resource($resultset)) { $resultset = $this->_results; }
656 if (is_resource($resultset)) pg_free_result($resultset);
667 public function plimit($limit = 0, $offset = 0)
670 if (empty($limit))
return "";
671 if ($limit < 0) $limit = $conf->liste_limit;
672 if ($offset > 0)
return " LIMIT ".$limit.
" OFFSET ".$offset.
" ";
673 else return " LIMIT $limit ";
685 return pg_escape_string($stringtoencode);
696 public function ifsql($test, $resok, $resko)
698 return '(CASE WHEN '.$test.
' THEN '.$resok.
' ELSE '.$resko.
' END)';
708 if (!$this->connected) {
710 return 'DB_ERROR_FAILED_TO_CONNECT';
713 $errorcode_map = array(
714 1004 =>
'DB_ERROR_CANNOT_CREATE',
715 1005 =>
'DB_ERROR_CANNOT_CREATE',
716 1006 =>
'DB_ERROR_CANNOT_CREATE',
717 1007 =>
'DB_ERROR_ALREADY_EXISTS',
718 1008 =>
'DB_ERROR_CANNOT_DROP',
719 1025 =>
'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
720 1044 =>
'DB_ERROR_ACCESSDENIED',
721 1046 =>
'DB_ERROR_NODBSELECTED',
722 1048 =>
'DB_ERROR_CONSTRAINT',
723 '42P07' =>
'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS',
724 '42703' =>
'DB_ERROR_NOSUCHFIELD',
725 1060 =>
'DB_ERROR_COLUMN_ALREADY_EXISTS',
726 42701=>
'DB_ERROR_COLUMN_ALREADY_EXISTS',
727 '42710' =>
'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
728 '23505' =>
'DB_ERROR_RECORD_ALREADY_EXISTS',
729 '42704' =>
'DB_ERROR_NO_INDEX_TO_DROP',
730 '42601' =>
'DB_ERROR_SYNTAX',
731 '42P16' =>
'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
732 1075 =>
'DB_ERROR_CANT_DROP_PRIMARY_KEY',
733 1091 =>
'DB_ERROR_NOSUCHFIELD',
734 1100 =>
'DB_ERROR_NOT_LOCKED',
735 1136 =>
'DB_ERROR_VALUE_COUNT_ON_ROW',
736 '42P01' =>
'DB_ERROR_NOSUCHTABLE',
737 '23503' =>
'DB_ERROR_NO_PARENT',
738 1217 =>
'DB_ERROR_CHILD_EXISTS',
739 1451 =>
'DB_ERROR_CHILD_EXISTS',
740 '42P04' =>
'DB_DATABASE_ALREADY_EXISTS'
743 $errorlabel = pg_last_error($this->
db);
745 if (preg_match(
'/: *([0-9P]+):/', $errorlabel, $reg))
747 $errorcode = $reg[1];
748 if (isset($errorcode_map[$errorcode]))
750 return $errorcode_map[$errorcode];
753 $errno = $errorcode ? $errorcode : $errorlabel;
754 return ($errno ?
'DB_ERROR_'.$errno :
'0');
773 return pg_last_error($this->
db);
788 $result = pg_query($this->
db,
"SELECT currval('".$tab.
"_".$fieldid.
"_seq')");
791 print pg_last_error($this->
db);
795 $row = pg_fetch_result($result, 0, 0);
807 public function encrypt($fieldorvalue, $withQuotes = 0)
812 $cryptType = ($conf->db->dolibarr_main_db_encryption ? $conf->db->dolibarr_main_db_encryption : 0);
815 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
817 $return = $fieldorvalue;
818 return ($withQuotes ?
"'" :
"").$this->escape($return).($withQuotes ?
"'" :
"");
833 $cryptType = ($conf->db->dolibarr_main_db_encryption ? $conf->db->dolibarr_main_db_encryption : 0);
836 $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey :
'');
869 public function DDLCreateDb($database, $charset =
'', $collation =
'', $owner =
'')
878 $sql =
'CREATE DATABASE "'.$database.
'" OWNER "'.$owner.
'" ENCODING \''.$charset.
'\'';
880 $ret = $this->
query($sql);
895 $listtables = array();
898 if ($table) $like =
" AND table_name LIKE '".$this->escape($table).
"'";
899 $result = pg_query($this->
db,
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'".$like.
" ORDER BY table_name");
904 $listtables[] = $row[0];
921 $infotables = array();
924 $sql .=
" infcol.column_name as \"Column\",";
925 $sql .=
" CASE WHEN infcol.character_maximum_length IS NOT NULL THEN infcol.udt_name || '('||infcol.character_maximum_length||')'";
926 $sql .=
" ELSE infcol.udt_name";
927 $sql .=
" END as \"Type\",";
928 $sql .=
" infcol.collation_name as \"Collation\",";
929 $sql .=
" infcol.is_nullable as \"Null\",";
930 $sql .=
" '' as \"Key\",";
931 $sql .=
" infcol.column_default as \"Default\",";
932 $sql .=
" '' as \"Extra\",";
933 $sql .=
" '' as \"Privileges\"";
934 $sql .=
" FROM information_schema.columns infcol";
935 $sql .=
" WHERE table_schema='public' ";
936 $sql .=
" AND table_name='".$this->escape($table).
"'";
937 $sql .=
" ORDER BY ordinal_position;";
940 $result = $this->
query($sql);
945 $infotables[] = $row;
965 public function DDLCreateTable($table, $fields, $primary_key,
$type, $unique_keys = null, $fulltext_keys = null, $keys = null)
972 $sql =
"create table ".$table.
"(";
974 foreach ($fields as $field_name => $field_desc)
976 $sqlfields[$i] = $field_name.
" ";
977 $sqlfields[$i] .= $field_desc[
'type'];
978 if (preg_match(
"/^[^\s]/i", $field_desc[
'value']))
979 $sqlfields[$i] .=
"(".$field_desc[
'value'].
")";
980 elseif (preg_match(
"/^[^\s]/i", $field_desc[
'attribute']))
981 $sqlfields[$i] .=
" ".$field_desc[
'attribute'];
982 elseif (preg_match(
"/^[^\s]/i", $field_desc[
'default']))
984 if (preg_match(
"/null/i", $field_desc[
'default']))
985 $sqlfields[$i] .=
" default ".$field_desc[
'default'];
986 else $sqlfields[$i] .=
" default '".$this->escape($field_desc[
'default']).
"'";
987 } elseif (preg_match(
"/^[^\s]/i", $field_desc[
'null']))
988 $sqlfields[$i] .=
" ".$field_desc[
'null'];
990 elseif (preg_match(
"/^[^\s]/i", $field_desc[
'extra']))
991 $sqlfields[$i] .=
" ".$field_desc[
'extra'];
994 if ($primary_key !=
"")
995 $pk =
"primary key(".$primary_key.
")";
997 if (is_array($unique_keys))
1000 foreach ($unique_keys as $key => $value)
1002 $sqluq[$i] =
"UNIQUE KEY '".$key.
"' ('".$this->
escape($value).
"')";
1006 if (is_array($keys))
1009 foreach ($keys as $key => $value)
1011 $sqlk[$i] =
"KEY ".$key.
" (".$value.
")";
1015 $sql .= implode(
',', $sqlfields);
1016 if ($primary_key !=
"")
1018 if (is_array($unique_keys))
1019 $sql .=
",".implode(
',', $sqluq);
1020 if (is_array($keys))
1021 $sql .=
",".implode(
',', $sqlk);
1022 $sql .=
") type=".$type;
1025 if (!$this->
query($sql))
1040 $sql =
"DROP TABLE ".$table;
1042 if (!$this->
query($sql))
1057 public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
1061 $sql =
"CREATE USER ".$this->escape($dolibarr_main_db_user).
" with password '".$this->
escape($dolibarr_main_db_pass).
"'";
1063 dol_syslog(get_class($this).
"::DDLCreateUser", LOG_DEBUG);
1084 $sql =
"SELECT attname FROM pg_attribute, pg_type WHERE typname = '".$this->escape($table).
"' AND attrelid = typrelid";
1085 $sql .=
" AND attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')";
1086 if ($field) $sql .=
" AND attname = '".$this->escape($field).
"'";
1089 $this->_results = $this->
query($sql);
1090 return $this->_results;
1103 public function DDLAddField($table, $field_name, $field_desc, $field_position =
"")
1108 $sql =
"ALTER TABLE ".$table.
" ADD ".$field_name.
" ";
1109 $sql .= $field_desc[
'type'];
1110 if (preg_match(
"/^[^\s]/i", $field_desc[
'value'])) {
1111 if (!in_array($field_desc[
'type'], array(
'int',
'date',
'datetime')) && $field_desc[
'value'])
1113 $sql .=
"(".$field_desc[
'value'].
")";
1116 if (preg_match(
"/^[^\s]/i", $field_desc[
'attribute']))
1117 $sql .=
" ".$field_desc[
'attribute'];
1118 if (preg_match(
"/^[^\s]/i", $field_desc[
'null']))
1119 $sql .=
" ".$field_desc[
'null'];
1120 if (preg_match(
"/^[^\s]/i", $field_desc[
'default'])) {
1121 if (preg_match(
"/null/i", $field_desc[
'default'])) {
1122 $sql .=
" default ".$field_desc[
'default'];
1124 $sql .=
" default '".$this->escape($field_desc[
'default']).
"'";
1127 if (preg_match(
"/^[^\s]/i", $field_desc[
'extra'])) {
1128 $sql .=
" ".$field_desc[
'extra'];
1130 $sql .=
" ".$field_position;
1133 if (!$this ->
query($sql))
1150 $sql =
"ALTER TABLE ".$table;
1151 $sql .=
" MODIFY COLUMN ".$field_name.
" ".$field_desc[
'type'];
1152 if (in_array($field_desc[
'type'], array(
'double',
'tinyint',
'int',
'varchar')) && $field_desc[
'value']) {
1153 $sql .=
"(".$field_desc[
'value'].
")";
1156 if ($field_desc[
'null'] ==
'not null' || $field_desc[
'null'] ==
'NOT NULL')
1159 if ($field_desc[
'type'] ==
'varchar' || $field_desc[
'type'] ==
'text')
1161 $sqlbis =
"UPDATE ".$table.
" SET ".$field_name.
" = '".$this->
escape($field_desc[
'default'] ? $field_desc[
'default'] :
'').
"' WHERE ".$field_name.
" IS NULL";
1162 $this->
query($sqlbis);
1163 } elseif ($field_desc[
'type'] ==
'tinyint' || $field_desc[
'type'] ==
'int')
1165 $sqlbis =
"UPDATE ".$table.
" SET ".$field_name.
" = ".((int) $this->
escape($field_desc[
'default'] ? $field_desc[
'default'] : 0)).
" WHERE ".$field_name.
" IS NULL";
1166 $this->query($sqlbis);
1170 if ($field_desc[
'default'] !=
'')
1172 if ($field_desc[
'type'] ==
'double' || $field_desc[
'type'] ==
'tinyint' || $field_desc[
'type'] ==
'int') $sql .=
" DEFAULT ".$this->escape($field_desc[
'default']);
1173 elseif ($field_desc[
'type'] !=
'text') $sql .=
" DEFAULT '".$this->escape($field_desc[
'default']).
"'";
1177 if (!$this->
query($sql))
1193 $sql =
"ALTER TABLE ".$table.
" DROP COLUMN ".$field_name;
1195 if (!$this->
query($sql))
1214 return $liste[
'server_encoding'];
1232 $liste[$i][
'charset'] = $obj->server_encoding;
1233 $liste[$i][
'description'] =
'Default database charset';
1254 return $liste[
'lc_collate'];
1272 $liste[$i][
'collation'] = $obj->lc_collate;
1289 $fullpathofdump =
'/pathtopgdump/pg_dump';
1291 if (file_exists(
'/usr/bin/pg_dump'))
1293 $fullpathofdump =
'/usr/bin/pg_dump';
1300 $basedir = $liste[
'data_directory'];
1301 $fullpathofdump = preg_replace(
'/data$/',
'bin', $basedir).
'/pg_dump';
1305 return $fullpathofdump;
1318 $fullpathofdump =
'/pathtopgrestore/'.$tool;
1320 if (file_exists(
'/usr/bin/'.$tool))
1322 $fullpathofdump =
'/usr/bin/'.$tool;
1329 $basedir = $liste[
'data_directory'];
1330 $fullpathofdump = preg_replace(
'/data$/',
'bin', $basedir).
'/'.$tool;
1334 return $fullpathofdump;
1347 $resql =
'select name,setting from pg_settings';
1348 if ($filter)
$resql .=
" WHERE name = '".$this->escape($filter).
"'";
affected_rows($resultset)
Return the number of lines in the result of a request INSERT, DELETE or UPDATE.
lastqueryerror()
Return last query in error.
DDLUpdateField($table, $field_name, $field_desc)
Update format of a field into a table.
DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys=null, $fulltext_keys=null, $keys=null)
Create a table into database.
static convertSQLFromMysql($line, $type= 'auto', $unescapeslashquot=false)
Convert a SQL request in Mysql syntax to native syntax.
getListOfCollation()
Return list of available collation that can be used for database.
DDLCreateDb($database, $charset= '', $collation= '', $owner= '')
Create a new database Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated We fo...
decrypt($value)
Decrypt sensitive data in database.
query($query, $usesavepoint=0, $type= 'auto')
Convert request to PostgreSQL syntax, execute it and return the resultset.
lastquery()
Return last request executed with query()
getPathOfDump()
Return full path of dump program.
Class to manage Dolibarr database access.
DDLDropTable($table)
Drop a table into database.
select_db($database)
Select a database Ici postgresql n'a aucune fonction equivalente de mysql_select_db On compare juste ...
Class to drive a Postgresql database for Dolibarr.
last_insert_id($tab, $fieldid= 'rowid')
Get last ID after an insert INSERT.
getVersion()
Return version of database server.
lasterror()
Return last error label.
$conf db
API class for accounts.
fetch_array($resultset)
Return datas as an array.
DDLInfoTable($table)
List information of columns into a table.
escape($stringtoencode)
Escape a string to insert data.
free($resultset=null)
Libere le dernier resultset utilise sur cette connexion.
fetch_row($resultset)
Return datas as an array.
DDLDescTable($table, $field="")
Return a pointer of line with description of a table or field.
DDLAddField($table, $field_name, $field_desc, $field_position="")
Create a new field into table.
close()
Close database connexion.
fetch_object($resultset)
Renvoie la ligne courante (comme un objet) pour le curseur resultset.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
Create a user to connect to database.
getDriverInfo()
Return version of database client driver.
DDLDropField($table, $field_name)
Drop a field from table.
connect($host, $login, $passwd, $name, $port=0)
Connexion to server.
getDefaultCollationDatabase()
Return collation used in database.
error()
Renvoie le texte de l'erreur pgsql de l'operation precedente.
DDLListTables($database, $table= '')
List tables into a database.
getServerParametersValues($filter= '')
Return value of server parameters.
print
Draft customers invoices.
getDefaultCharacterSetDatabase()
Return charset used to store data in database.
num_rows($resultset)
Return number of lines for result of a SELECT.
ifsql($test, $resok, $resko)
Format a SQL IF.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
plimit($limit=0, $offset=0)
Define limits and offset of request.
const VERSIONMIN
Version min database.
$forcecollate
Collate used to force collate when creating database.
getServerStatusValues($filter= '')
Return value of server status.
getListOfCharacterSet()
Return list of available charset that can be used to store data in database.
__construct($type, $host, $user, $pass, $name= '', $port=0)
Constructor.
lasterrno()
Return last error code.
DDLGetConnectId()
Return connexion ID.
getPathOfRestore()
Return full path of restore program.
const LABEL
Database label.
errno()
Renvoie le code erreur generique de l'operation precedente.
encrypt($fieldorvalue, $withQuotes=0)
Encrypt sensitive data in database Warning: This function includes the escape, so it must use direct ...