mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2026-02-07 16:41:48 +01:00
code syntax core directory
This commit is contained in:
@@ -73,8 +73,12 @@ class DoliDBPgsql extends DoliDB
|
||||
global $conf, $langs;
|
||||
|
||||
// Note that having "static" property for "$forcecharset" and "$forcecollate" will make error here in strict mode, so they are not static
|
||||
if (!empty($conf->db->character_set)) $this->forcecharset = $conf->db->character_set;
|
||||
if (!empty($conf->db->dolibarr_main_db_collation)) $this->forcecollate = $conf->db->dolibarr_main_db_collation;
|
||||
if (!empty($conf->db->character_set)) {
|
||||
$this->forcecharset = $conf->db->character_set;
|
||||
}
|
||||
if (!empty($conf->db->dolibarr_main_db_collation)) {
|
||||
$this->forcecollate = $conf->db->dolibarr_main_db_collation;
|
||||
}
|
||||
|
||||
$this->database_user = $user;
|
||||
$this->database_host = $host;
|
||||
@@ -84,8 +88,7 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
//print "Name DB: $host,$user,$pass,$name<br>";
|
||||
|
||||
if (!function_exists("pg_connect"))
|
||||
{
|
||||
if (!function_exists("pg_connect")) {
|
||||
$this->connected = false;
|
||||
$this->ok = false;
|
||||
$this->error = "Pgsql PHP functions are not available in this version of PHP";
|
||||
@@ -93,8 +96,7 @@ class DoliDBPgsql extends DoliDB
|
||||
return $this->ok;
|
||||
}
|
||||
|
||||
if (!$host)
|
||||
{
|
||||
if (!$host) {
|
||||
$this->connected = false;
|
||||
$this->ok = false;
|
||||
$this->error = $langs->trans("ErrorWrongHostParameter");
|
||||
@@ -106,8 +108,7 @@ class DoliDBPgsql extends DoliDB
|
||||
//print "$host, $user, $pass, $name, $port";
|
||||
$this->db = $this->connect($host, $user, $pass, $name, $port);
|
||||
|
||||
if ($this->db)
|
||||
{
|
||||
if ($this->db) {
|
||||
$this->connected = true;
|
||||
$this->ok = true;
|
||||
} else {
|
||||
@@ -119,10 +120,8 @@ class DoliDBPgsql extends DoliDB
|
||||
}
|
||||
|
||||
// Si connexion serveur ok et si connexion base demandee, on essaie connexion base
|
||||
if ($this->connected && $name)
|
||||
{
|
||||
if ($this->select_db($name))
|
||||
{
|
||||
if ($this->connected && $name) {
|
||||
if ($this->select_db($name)) {
|
||||
$this->database_selected = true;
|
||||
$this->database_name = $name;
|
||||
$this->ok = true;
|
||||
@@ -159,12 +158,10 @@ class DoliDBPgsql extends DoliDB
|
||||
return '';
|
||||
}
|
||||
// Return line if this is a comment
|
||||
if (preg_match('/^#/i', $line) || preg_match('/^$/i', $line) || preg_match('/^--/i', $line))
|
||||
{
|
||||
if (preg_match('/^#/i', $line) || preg_match('/^$/i', $line) || preg_match('/^--/i', $line)) {
|
||||
return $line;
|
||||
}
|
||||
if ($line != "")
|
||||
{
|
||||
if ($line != "") {
|
||||
// group_concat support (PgSQL >= 9.0)
|
||||
// Replace group_concat(x) or group_concat(x SEPARATOR ',') with string_agg(x, ',')
|
||||
$line = preg_replace('/GROUP_CONCAT/i', 'STRING_AGG', $line);
|
||||
@@ -172,17 +169,19 @@ class DoliDBPgsql extends DoliDB
|
||||
$line = preg_replace('/STRING_AGG\(([^,\)]+)\)/i', 'STRING_AGG(\\1, \',\')', $line);
|
||||
//print $line."\n";
|
||||
|
||||
if ($type == 'auto')
|
||||
{
|
||||
if (preg_match('/ALTER TABLE/i', $line)) $type = 'dml';
|
||||
elseif (preg_match('/CREATE TABLE/i', $line)) $type = 'dml';
|
||||
elseif (preg_match('/DROP TABLE/i', $line)) $type = 'dml';
|
||||
if ($type == 'auto') {
|
||||
if (preg_match('/ALTER TABLE/i', $line)) {
|
||||
$type = 'dml';
|
||||
} elseif (preg_match('/CREATE TABLE/i', $line)) {
|
||||
$type = 'dml';
|
||||
} elseif (preg_match('/DROP TABLE/i', $line)) {
|
||||
$type = 'dml';
|
||||
}
|
||||
}
|
||||
|
||||
$line = preg_replace('/ as signed\)/i', ' as integer)', $line);
|
||||
|
||||
if ($type == 'dml')
|
||||
{
|
||||
if ($type == 'dml') {
|
||||
$reg = array();
|
||||
|
||||
$line = preg_replace('/\s/', ' ', $line); // Replace tabulation with space
|
||||
@@ -247,8 +246,7 @@ class DoliDBPgsql extends DoliDB
|
||||
$line = preg_replace('/(\s*)ON(\s*)UPDATE(\s*)CURRENT_TIMESTAMP/i', '\\1', $line);
|
||||
|
||||
// unique index(field1,field2)
|
||||
if (preg_match('/unique index\s*\((\w+\s*,\s*\w+)\)/i', $line))
|
||||
{
|
||||
if (preg_match('/unique index\s*\((\w+\s*,\s*\w+)\)/i', $line)) {
|
||||
$line = preg_replace('/unique index\s*\((\w+\s*,\s*\w+)\)/i', 'UNIQUE\(\\1\)', $line);
|
||||
}
|
||||
|
||||
@@ -259,15 +257,13 @@ class DoliDBPgsql extends DoliDB
|
||||
$line = preg_replace('/ALTER TABLE [a-z0-9_]+\s+DROP INDEX/i', 'DROP INDEX', $line);
|
||||
|
||||
// Translate order to rename fields
|
||||
if (preg_match('/ALTER TABLE ([a-z0-9_]+)\s+CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER TABLE ([a-z0-9_]+)\s+CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i', $line, $reg)) {
|
||||
$line = "-- ".$line." replaced by --\n";
|
||||
$line .= "ALTER TABLE ".$reg[1]." RENAME COLUMN ".$reg[2]." TO ".$reg[3];
|
||||
}
|
||||
|
||||
// Translate order to modify field format
|
||||
if (preg_match('/ALTER TABLE ([a-z0-9_]+)\s+MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER TABLE ([a-z0-9_]+)\s+MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i', $line, $reg)) {
|
||||
$line = "-- ".$line." replaced by --\n";
|
||||
$newreg3 = $reg[3];
|
||||
$newreg3 = preg_replace('/ DEFAULT NULL/i', '', $newreg3);
|
||||
@@ -281,40 +277,35 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
// alter table add primary key (field1, field2 ...) -> We remove the primary key name not accepted by PostGreSQL
|
||||
// ALTER TABLE llx_dolibarr_modules ADD PRIMARY KEY pk_dolibarr_modules (numero, entity)
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i', $line, $reg)) {
|
||||
$line = "-- ".$line." replaced by --\n";
|
||||
$line .= "ALTER TABLE ".$reg[1]." ADD PRIMARY KEY (".$reg[3];
|
||||
}
|
||||
|
||||
// Translate order to drop primary keys
|
||||
// ALTER TABLE llx_dolibarr_modules DROP PRIMARY KEY pk_xxx
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+PRIMARY\s+KEY\s*([^;]+)$/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+PRIMARY\s+KEY\s*([^;]+)$/i', $line, $reg)) {
|
||||
$line = "-- ".$line." replaced by --\n";
|
||||
$line .= "ALTER TABLE ".$reg[1]." DROP CONSTRAINT ".$reg[2];
|
||||
}
|
||||
|
||||
// Translate order to drop foreign keys
|
||||
// ALTER TABLE llx_dolibarr_modules DROP FOREIGN KEY fk_xxx
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i', $line, $reg)) {
|
||||
$line = "-- ".$line." replaced by --\n";
|
||||
$line .= "ALTER TABLE ".$reg[1]." DROP CONSTRAINT ".$reg[2];
|
||||
}
|
||||
|
||||
// Translate order to add foreign keys
|
||||
// ALTER TABLE llx_tablechild ADD CONSTRAINT fk_tablechild_fk_fieldparent FOREIGN KEY (fk_fieldparent) REFERENCES llx_tableparent (rowid)
|
||||
if (preg_match('/ALTER\s+TABLE\s+(.*)\s*ADD CONSTRAINT\s+(.*)\s*FOREIGN\s+KEY\s*(.*)$/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER\s+TABLE\s+(.*)\s*ADD CONSTRAINT\s+(.*)\s*FOREIGN\s+KEY\s*(.*)$/i', $line, $reg)) {
|
||||
$line = preg_replace('/;$/', '', $line);
|
||||
$line .= " DEFERRABLE INITIALLY IMMEDIATE;";
|
||||
}
|
||||
|
||||
// alter table add [unique] [index] (field1, field2 ...)
|
||||
// ALTER TABLE llx_accountingaccount ADD INDEX idx_accountingaccount_fk_pcg_version (fk_pcg_version)
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i', $line, $reg))
|
||||
{
|
||||
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i', $line, $reg)) {
|
||||
$fieldlist = $reg[4];
|
||||
$idxname = $reg[3];
|
||||
$tablename = $reg[1];
|
||||
@@ -326,8 +317,7 @@ class DoliDBPgsql extends DoliDB
|
||||
// To have postgresql case sensitive
|
||||
$count_like = 0;
|
||||
$line = str_replace(' LIKE \'', ' ILIKE \'', $line, $count_like);
|
||||
if (!empty($conf->global->PSQL_USE_UNACCENT) && $count_like > 0)
|
||||
{
|
||||
if (!empty($conf->global->PSQL_USE_UNACCENT) && $count_like > 0) {
|
||||
// @see https://docs.postgresql.fr/11/unaccent.html : 'unaccent()' function must be installed before
|
||||
$line = preg_replace('/\s+(\(+\s*)([a-zA-Z0-9\-\_\.]+) ILIKE /', ' \1unaccent(\2) ILIKE ', $line);
|
||||
}
|
||||
@@ -339,10 +329,8 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
// Delete using criteria on other table must not declare twice the deleted table
|
||||
// DELETE FROM tabletodelete USING tabletodelete, othertable -> DELETE FROM tabletodelete USING othertable
|
||||
if (preg_match('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i', $line, $reg))
|
||||
{
|
||||
if ($reg[1] == $reg[2]) // If same table, we remove second one
|
||||
{
|
||||
if (preg_match('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i', $line, $reg)) {
|
||||
if ($reg[1] == $reg[2]) { // If same table, we remove second one
|
||||
$line = preg_replace('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i', 'DELETE FROM \\1 USING \\3', $line);
|
||||
}
|
||||
}
|
||||
@@ -371,7 +359,9 @@ class DoliDBPgsql extends DoliDB
|
||||
// By default we do not (should be already done by db->escape function if required
|
||||
// except for sql insert in data file that are mysql escaped so we removed them to
|
||||
// be compatible with standard_conforming_strings=on that considers \ as ordinary character).
|
||||
if ($unescapeslashquot) $line = preg_replace("/\\\'/", "''", $line);
|
||||
if ($unescapeslashquot) {
|
||||
$line = preg_replace("/\\\'/", "''", $line);
|
||||
}
|
||||
|
||||
//print "type=".$type." newline=".$line."<br>\n";
|
||||
}
|
||||
@@ -422,28 +412,31 @@ class DoliDBPgsql extends DoliDB
|
||||
$name = str_replace(array("\\", "'"), array("\\\\", "\\'"), $name);
|
||||
$port = str_replace(array("\\", "'"), array("\\\\", "\\'"), $port);
|
||||
|
||||
if (!$name) $name = "postgres"; // When try to connect using admin user
|
||||
if (!$name) {
|
||||
$name = "postgres"; // When try to connect using admin user
|
||||
}
|
||||
|
||||
// try first Unix domain socket (local)
|
||||
if ((!empty($host) && $host == "socket") && !defined('NOLOCALSOCKETPGCONNECT'))
|
||||
{
|
||||
if ((!empty($host) && $host == "socket") && !defined('NOLOCALSOCKETPGCONNECT')) {
|
||||
$con_string = "dbname='".$name."' user='".$login."' password='".$passwd."'"; // $name may be empty
|
||||
$this->db = @pg_connect($con_string);
|
||||
}
|
||||
|
||||
// if local connection failed or not requested, use TCP/IP
|
||||
if (!$this->db)
|
||||
{
|
||||
if (!$host) $host = "localhost";
|
||||
if (!$port) $port = 5432;
|
||||
if (!$this->db) {
|
||||
if (!$host) {
|
||||
$host = "localhost";
|
||||
}
|
||||
if (!$port) {
|
||||
$port = 5432;
|
||||
}
|
||||
|
||||
$con_string = "host='".$host."' port='".$port."' dbname='".$name."' user='".$login."' password='".$passwd."'";
|
||||
$this->db = @pg_connect($con_string);
|
||||
}
|
||||
|
||||
// now we test if at least one connect method was a success
|
||||
if ($this->db)
|
||||
{
|
||||
if ($this->db) {
|
||||
$this->database_name = $name;
|
||||
pg_set_error_verbosity($this->db, PGSQL_ERRORS_VERBOSE); // Set verbosity to max
|
||||
pg_query($this->db, "set datestyle = 'ISO, YMD';");
|
||||
@@ -460,8 +453,7 @@ class DoliDBPgsql extends DoliDB
|
||||
public function getVersion()
|
||||
{
|
||||
$resql = $this->query('SHOW server_version');
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$liste = $this->fetch_array($resql);
|
||||
return $liste['server_version'];
|
||||
}
|
||||
@@ -486,9 +478,10 @@ class DoliDBPgsql extends DoliDB
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if ($this->db)
|
||||
{
|
||||
if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
|
||||
if ($this->db) {
|
||||
if ($this->transaction_opened > 0) {
|
||||
dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened, LOG_ERR);
|
||||
}
|
||||
$this->connected = false;
|
||||
return pg_close($this->db);
|
||||
}
|
||||
@@ -513,27 +506,24 @@ class DoliDBPgsql extends DoliDB
|
||||
$query = $this->convertSQLFromMysql($query, $type, ($this->unescapeslashquot && $this->standard_conforming_strings));
|
||||
//print "After convertSQLFromMysql:\n".$query."<br>\n";
|
||||
|
||||
if (!empty($conf->global->MAIN_DB_AUTOFIX_BAD_SQL_REQUEST))
|
||||
{
|
||||
if (!empty($conf->global->MAIN_DB_AUTOFIX_BAD_SQL_REQUEST)) {
|
||||
// Fix bad formed requests. If request contains a date without quotes, we fix this but this should not occurs.
|
||||
$loop = true;
|
||||
while ($loop)
|
||||
{
|
||||
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))
|
||||
{
|
||||
while ($loop) {
|
||||
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)) {
|
||||
$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);
|
||||
dol_syslog("Warning: Bad formed request converted into ".$query, LOG_WARNING);
|
||||
} else $loop = false;
|
||||
} else {
|
||||
$loop = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($usesavepoint && $this->transaction_opened)
|
||||
{
|
||||
if ($usesavepoint && $this->transaction_opened) {
|
||||
@pg_query($this->db, 'SAVEPOINT mysavepoint');
|
||||
}
|
||||
|
||||
if (!in_array($query, array('BEGIN', 'COMMIT', 'ROLLBACK')))
|
||||
{
|
||||
if (!in_array($query, array('BEGIN', 'COMMIT', 'ROLLBACK'))) {
|
||||
$SYSLOG_SQL_LIMIT = 10000; // limit log to 10kb per line to limit DOS attacks
|
||||
dol_syslog('sql='.substr($query, 0, $SYSLOG_SQL_LIMIT), LOG_DEBUG);
|
||||
}
|
||||
@@ -541,23 +531,21 @@ class DoliDBPgsql extends DoliDB
|
||||
$ret = @pg_query($this->db, $query);
|
||||
|
||||
//print $query;
|
||||
if (!preg_match("/^COMMIT/i", $query) && !preg_match("/^ROLLBACK/i", $query)) // Si requete utilisateur, on la sauvegarde ainsi que son resultset
|
||||
{
|
||||
if (!$ret)
|
||||
{
|
||||
if ($this->errno() != 'DB_ERROR_25P02') // Do not overwrite errors if this is a consecutive error
|
||||
{
|
||||
if (!preg_match("/^COMMIT/i", $query) && !preg_match("/^ROLLBACK/i", $query)) { // Si requete utilisateur, on la sauvegarde ainsi que son resultset
|
||||
if (!$ret) {
|
||||
if ($this->errno() != 'DB_ERROR_25P02') { // Do not overwrite errors if this is a consecutive error
|
||||
$this->lastqueryerror = $query;
|
||||
$this->lasterror = $this->error();
|
||||
$this->lasterrno = $this->errno();
|
||||
|
||||
if ($conf->global->SYSLOG_LEVEL < LOG_DEBUG) dol_syslog(get_class($this)."::query SQL Error query: ".$query, LOG_ERR); // Log of request was not yet done previously
|
||||
if ($conf->global->SYSLOG_LEVEL < LOG_DEBUG) {
|
||||
dol_syslog(get_class($this)."::query SQL Error query: ".$query, LOG_ERR); // Log of request was not yet done previously
|
||||
}
|
||||
dol_syslog(get_class($this)."::query SQL Error message: ".$this->lasterror." (".$this->lasterrno.")", LOG_ERR);
|
||||
dol_syslog(get_class($this)."::query SQL Error usesavepoint = ".$usesavepoint, LOG_ERR);
|
||||
}
|
||||
|
||||
if ($usesavepoint && $this->transaction_opened) // Warning, after that errno will be erased
|
||||
{
|
||||
if ($usesavepoint && $this->transaction_opened) { // Warning, after that errno will be erased
|
||||
@pg_query($this->db, 'ROLLBACK TO SAVEPOINT mysavepoint');
|
||||
}
|
||||
}
|
||||
@@ -579,7 +567,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
// phpcs:enable
|
||||
// If resultset not provided, we take the last used by connexion
|
||||
if (!is_resource($resultset)) { $resultset = $this->_results; }
|
||||
if (!is_resource($resultset)) {
|
||||
$resultset = $this->_results;
|
||||
}
|
||||
return pg_fetch_object($resultset);
|
||||
}
|
||||
|
||||
@@ -594,7 +584,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
// phpcs:enable
|
||||
// If resultset not provided, we take the last used by connexion
|
||||
if (!is_resource($resultset)) { $resultset = $this->_results; }
|
||||
if (!is_resource($resultset)) {
|
||||
$resultset = $this->_results;
|
||||
}
|
||||
return pg_fetch_array($resultset);
|
||||
}
|
||||
|
||||
@@ -609,7 +601,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
// phpcs:enable
|
||||
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
||||
if (!is_resource($resultset)) { $resultset = $this->_results; }
|
||||
if (!is_resource($resultset)) {
|
||||
$resultset = $this->_results;
|
||||
}
|
||||
return pg_fetch_row($resultset);
|
||||
}
|
||||
|
||||
@@ -625,7 +619,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
// phpcs:enable
|
||||
// If resultset not provided, we take the last used by connexion
|
||||
if (!is_resource($resultset)) { $resultset = $this->_results; }
|
||||
if (!is_resource($resultset)) {
|
||||
$resultset = $this->_results;
|
||||
}
|
||||
return pg_num_rows($resultset);
|
||||
}
|
||||
|
||||
@@ -641,7 +637,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
// phpcs:enable
|
||||
// If resultset not provided, we take the last used by connexion
|
||||
if (!is_resource($resultset)) { $resultset = $this->_results; }
|
||||
if (!is_resource($resultset)) {
|
||||
$resultset = $this->_results;
|
||||
}
|
||||
// pgsql necessite un resultset pour cette fonction contrairement
|
||||
// a mysql qui prend un link de base
|
||||
return pg_affected_rows($resultset);
|
||||
@@ -657,9 +655,13 @@ class DoliDBPgsql extends DoliDB
|
||||
public function free($resultset = null)
|
||||
{
|
||||
// If resultset not provided, we take the last used by connexion
|
||||
if (!is_resource($resultset)) { $resultset = $this->_results; }
|
||||
if (!is_resource($resultset)) {
|
||||
$resultset = $this->_results;
|
||||
}
|
||||
// Si resultset en est un, on libere la memoire
|
||||
if (is_resource($resultset)) pg_free_result($resultset);
|
||||
if (is_resource($resultset)) {
|
||||
pg_free_result($resultset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -673,10 +675,17 @@ class DoliDBPgsql extends DoliDB
|
||||
public function plimit($limit = 0, $offset = 0)
|
||||
{
|
||||
global $conf;
|
||||
if (empty($limit)) return "";
|
||||
if ($limit < 0) $limit = $conf->liste_limit;
|
||||
if ($offset > 0) return " LIMIT ".$limit." OFFSET ".$offset." ";
|
||||
else return " LIMIT $limit ";
|
||||
if (empty($limit)) {
|
||||
return "";
|
||||
}
|
||||
if ($limit < 0) {
|
||||
$limit = $conf->liste_limit;
|
||||
}
|
||||
if ($offset > 0) {
|
||||
return " LIMIT ".$limit." OFFSET ".$offset." ";
|
||||
} else {
|
||||
return " LIMIT $limit ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -748,11 +757,9 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
$errorlabel = pg_last_error($this->db);
|
||||
$errorcode = '';
|
||||
if (preg_match('/: *([0-9P]+):/', $errorlabel, $reg))
|
||||
{
|
||||
if (preg_match('/: *([0-9P]+):/', $errorlabel, $reg)) {
|
||||
$errorcode = $reg[1];
|
||||
if (isset($errorcode_map[$errorcode]))
|
||||
{
|
||||
if (isset($errorcode_map[$errorcode])) {
|
||||
return $errorcode_map[$errorcode];
|
||||
}
|
||||
}
|
||||
@@ -792,8 +799,7 @@ class DoliDBPgsql extends DoliDB
|
||||
// phpcs:enable
|
||||
//$result = pg_query($this->db,"SELECT MAX(".$fieldid.") FROM ".$tab);
|
||||
$result = pg_query($this->db, "SELECT currval('".$tab."_".$fieldid."_seq')");
|
||||
if (!$result)
|
||||
{
|
||||
if (!$result) {
|
||||
print pg_last_error($this->db);
|
||||
exit;
|
||||
}
|
||||
@@ -875,8 +881,12 @@ class DoliDBPgsql extends DoliDB
|
||||
public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
|
||||
{
|
||||
// phpcs:enable
|
||||
if (empty($charset)) $charset = $this->forcecharset;
|
||||
if (empty($collation)) $collation = $this->forcecollate;
|
||||
if (empty($charset)) {
|
||||
$charset = $this->forcecharset;
|
||||
}
|
||||
if (empty($collation)) {
|
||||
$collation = $this->forcecollate;
|
||||
}
|
||||
|
||||
// Test charset match LC_TYPE (pgsql error otherwise)
|
||||
//print $charset.' '.setlocale(LC_CTYPE,'0'); exit;
|
||||
@@ -901,12 +911,12 @@ class DoliDBPgsql extends DoliDB
|
||||
$listtables = array();
|
||||
|
||||
$like = '';
|
||||
if ($table) $like = " AND table_name LIKE '".$this->escape($table)."'";
|
||||
if ($table) {
|
||||
$like = " AND table_name LIKE '".$this->escape($table)."'";
|
||||
}
|
||||
$result = pg_query($this->db, "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'".$like." ORDER BY table_name");
|
||||
if ($result)
|
||||
{
|
||||
while ($row = $this->fetch_row($result))
|
||||
{
|
||||
if ($result) {
|
||||
while ($row = $this->fetch_row($result)) {
|
||||
$listtables[] = $row[0];
|
||||
}
|
||||
}
|
||||
@@ -944,10 +954,8 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
dol_syslog($sql, LOG_DEBUG);
|
||||
$result = $this->query($sql);
|
||||
if ($result)
|
||||
{
|
||||
while ($row = $this->fetch_row($result))
|
||||
{
|
||||
if ($result) {
|
||||
while ($row = $this->fetch_row($result)) {
|
||||
$infotables[] = $row;
|
||||
}
|
||||
}
|
||||
@@ -977,60 +985,62 @@ class DoliDBPgsql extends DoliDB
|
||||
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
|
||||
$sql = "create table ".$table."(";
|
||||
$i = 0;
|
||||
foreach ($fields as $field_name => $field_desc)
|
||||
{
|
||||
foreach ($fields as $field_name => $field_desc) {
|
||||
$sqlfields[$i] = $field_name." ";
|
||||
$sqlfields[$i] .= $field_desc['type'];
|
||||
if (preg_match("/^[^\s]/i", $field_desc['value']))
|
||||
if (preg_match("/^[^\s]/i", $field_desc['value'])) {
|
||||
$sqlfields[$i] .= "(".$field_desc['value'].")";
|
||||
elseif (preg_match("/^[^\s]/i", $field_desc['attribute']))
|
||||
} elseif (preg_match("/^[^\s]/i", $field_desc['attribute'])) {
|
||||
$sqlfields[$i] .= " ".$field_desc['attribute'];
|
||||
elseif (preg_match("/^[^\s]/i", $field_desc['default']))
|
||||
{
|
||||
if (preg_match("/null/i", $field_desc['default']))
|
||||
} elseif (preg_match("/^[^\s]/i", $field_desc['default'])) {
|
||||
if (preg_match("/null/i", $field_desc['default'])) {
|
||||
$sqlfields[$i] .= " default ".$field_desc['default'];
|
||||
else $sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'";
|
||||
} elseif (preg_match("/^[^\s]/i", $field_desc['null']))
|
||||
} else {
|
||||
$sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'";
|
||||
}
|
||||
} elseif (preg_match("/^[^\s]/i", $field_desc['null'])) {
|
||||
$sqlfields[$i] .= " ".$field_desc['null'];
|
||||
|
||||
elseif (preg_match("/^[^\s]/i", $field_desc['extra']))
|
||||
} elseif (preg_match("/^[^\s]/i", $field_desc['extra'])) {
|
||||
$sqlfields[$i] .= " ".$field_desc['extra'];
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
if ($primary_key != "")
|
||||
$pk = "primary key(".$primary_key.")";
|
||||
if ($primary_key != "") {
|
||||
$pk = "primary key(".$primary_key.")";
|
||||
}
|
||||
|
||||
if (is_array($unique_keys))
|
||||
{
|
||||
if (is_array($unique_keys)) {
|
||||
$i = 0;
|
||||
foreach ($unique_keys as $key => $value)
|
||||
{
|
||||
foreach ($unique_keys as $key => $value) {
|
||||
$sqluq[$i] = "UNIQUE KEY '".$key."' ('".$this->escape($value)."')";
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
if (is_array($keys))
|
||||
{
|
||||
if (is_array($keys)) {
|
||||
$i = 0;
|
||||
foreach ($keys as $key => $value)
|
||||
{
|
||||
foreach ($keys as $key => $value) {
|
||||
$sqlk[$i] = "KEY ".$key." (".$value.")";
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$sql .= implode(',', $sqlfields);
|
||||
if ($primary_key != "")
|
||||
$sql .= ",".$pk;
|
||||
if (is_array($unique_keys))
|
||||
$sql .= ",".implode(',', $sqluq);
|
||||
if (is_array($keys))
|
||||
$sql .= ",".implode(',', $sqlk);
|
||||
if ($primary_key != "") {
|
||||
$sql .= ",".$pk;
|
||||
}
|
||||
if (is_array($unique_keys)) {
|
||||
$sql .= ",".implode(',', $sqluq);
|
||||
}
|
||||
if (is_array($keys)) {
|
||||
$sql .= ",".implode(',', $sqlk);
|
||||
}
|
||||
$sql .= ") type=".$type;
|
||||
|
||||
dol_syslog($sql, LOG_DEBUG);
|
||||
if (!$this->query($sql))
|
||||
return -1;
|
||||
else return 1;
|
||||
if (!$this->query($sql)) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
|
||||
@@ -1045,9 +1055,11 @@ class DoliDBPgsql extends DoliDB
|
||||
// phpcs:enable
|
||||
$sql = "DROP TABLE ".$table;
|
||||
|
||||
if (!$this->query($sql))
|
||||
if (!$this->query($sql)) {
|
||||
return -1;
|
||||
else return 1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
|
||||
@@ -1068,8 +1080,7 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
|
||||
$resql = $this->query($sql);
|
||||
if (!$resql)
|
||||
{
|
||||
if (!$resql) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1089,7 +1100,9 @@ class DoliDBPgsql extends DoliDB
|
||||
// phpcs:enable
|
||||
$sql = "SELECT attname FROM pg_attribute, pg_type WHERE typname = '".$this->escape($table)."' AND attrelid = typrelid";
|
||||
$sql .= " AND attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')";
|
||||
if ($field) $sql .= " AND attname = '".$this->escape($field)."'";
|
||||
if ($field) {
|
||||
$sql .= " AND attname = '".$this->escape($field)."'";
|
||||
}
|
||||
|
||||
dol_syslog($sql, LOG_DEBUG);
|
||||
$this->_results = $this->query($sql);
|
||||
@@ -1114,15 +1127,16 @@ class DoliDBPgsql extends DoliDB
|
||||
$sql = "ALTER TABLE ".$table." ADD ".$field_name." ";
|
||||
$sql .= $field_desc['type'];
|
||||
if (preg_match("/^[^\s]/i", $field_desc['value'])) {
|
||||
if (!in_array($field_desc['type'], array('int', 'date', 'datetime')) && $field_desc['value'])
|
||||
{
|
||||
if (!in_array($field_desc['type'], array('int', 'date', 'datetime')) && $field_desc['value']) {
|
||||
$sql .= "(".$field_desc['value'].")";
|
||||
}
|
||||
}
|
||||
if (preg_match("/^[^\s]/i", $field_desc['attribute']))
|
||||
if (preg_match("/^[^\s]/i", $field_desc['attribute'])) {
|
||||
$sql .= " ".$field_desc['attribute'];
|
||||
if (preg_match("/^[^\s]/i", $field_desc['null']))
|
||||
}
|
||||
if (preg_match("/^[^\s]/i", $field_desc['null'])) {
|
||||
$sql .= " ".$field_desc['null'];
|
||||
}
|
||||
if (preg_match("/^[^\s]/i", $field_desc['default'])) {
|
||||
if (preg_match("/null/i", $field_desc['default'])) {
|
||||
$sql .= " default ".$field_desc['default'];
|
||||
@@ -1136,8 +1150,9 @@ class DoliDBPgsql extends DoliDB
|
||||
$sql .= " ".$field_position;
|
||||
|
||||
dol_syslog($sql, LOG_DEBUG);
|
||||
if (!$this -> query($sql))
|
||||
if (!$this -> query($sql)) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1159,29 +1174,29 @@ class DoliDBPgsql extends DoliDB
|
||||
$sql .= "(".$field_desc['value'].")";
|
||||
}
|
||||
|
||||
if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL')
|
||||
{
|
||||
if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') {
|
||||
// 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
|
||||
if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text')
|
||||
{
|
||||
if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text') {
|
||||
$sqlbis = "UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL";
|
||||
$this->query($sqlbis);
|
||||
} elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int')
|
||||
{
|
||||
} elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') {
|
||||
$sqlbis = "UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL";
|
||||
$this->query($sqlbis);
|
||||
}
|
||||
}
|
||||
|
||||
if ($field_desc['default'] != '')
|
||||
{
|
||||
if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql .= " DEFAULT ".$this->escape($field_desc['default']);
|
||||
elseif ($field_desc['type'] != 'text') $sql .= " DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
|
||||
if ($field_desc['default'] != '') {
|
||||
if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') {
|
||||
$sql .= " DEFAULT ".$this->escape($field_desc['default']);
|
||||
} elseif ($field_desc['type'] != 'text') {
|
||||
$sql .= " DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
|
||||
}
|
||||
}
|
||||
|
||||
dol_syslog($sql, LOG_DEBUG);
|
||||
if (!$this->query($sql))
|
||||
if (!$this->query($sql)) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1198,8 +1213,7 @@ class DoliDBPgsql extends DoliDB
|
||||
// phpcs:enable
|
||||
$sql = "ALTER TABLE ".$table." DROP COLUMN ".$field_name;
|
||||
dol_syslog($sql, LOG_DEBUG);
|
||||
if (!$this->query($sql))
|
||||
{
|
||||
if (!$this->query($sql)) {
|
||||
$this->error = $this->lasterror();
|
||||
return -1;
|
||||
}
|
||||
@@ -1214,11 +1228,12 @@ class DoliDBPgsql extends DoliDB
|
||||
public function getDefaultCharacterSetDatabase()
|
||||
{
|
||||
$resql = $this->query('SHOW SERVER_ENCODING');
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$liste = $this->fetch_array($resql);
|
||||
return $liste['server_encoding'];
|
||||
} else return '';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1230,11 +1245,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
$resql = $this->query('SHOW SERVER_ENCODING');
|
||||
$liste = array();
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$i = 0;
|
||||
while ($obj = $this->fetch_object($resql))
|
||||
{
|
||||
while ($obj = $this->fetch_object($resql)) {
|
||||
$liste[$i]['charset'] = $obj->server_encoding;
|
||||
$liste[$i]['description'] = 'Default database charset';
|
||||
$i++;
|
||||
@@ -1254,11 +1267,12 @@ class DoliDBPgsql extends DoliDB
|
||||
public function getDefaultCollationDatabase()
|
||||
{
|
||||
$resql = $this->query('SHOW LC_COLLATE');
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$liste = $this->fetch_array($resql);
|
||||
return $liste['lc_collate'];
|
||||
} else return '';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1270,11 +1284,9 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
$resql = $this->query('SHOW LC_COLLATE');
|
||||
$liste = array();
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$i = 0;
|
||||
while ($obj = $this->fetch_object($resql))
|
||||
{
|
||||
while ($obj = $this->fetch_object($resql)) {
|
||||
$liste[$i]['collation'] = $obj->lc_collate;
|
||||
$i++;
|
||||
}
|
||||
@@ -1294,14 +1306,12 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
$fullpathofdump = '/pathtopgdump/pg_dump';
|
||||
|
||||
if (file_exists('/usr/bin/pg_dump'))
|
||||
{
|
||||
if (file_exists('/usr/bin/pg_dump')) {
|
||||
$fullpathofdump = '/usr/bin/pg_dump';
|
||||
} else {
|
||||
// TODO L'utilisateur de la base doit etre un superadmin pour lancer cette commande
|
||||
$resql = $this->query('SHOW data_directory');
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$liste = $this->fetch_array($resql);
|
||||
$basedir = $liste['data_directory'];
|
||||
$fullpathofdump = preg_replace('/data$/', 'bin', $basedir).'/pg_dump';
|
||||
@@ -1323,14 +1333,12 @@ class DoliDBPgsql extends DoliDB
|
||||
|
||||
$fullpathofdump = '/pathtopgrestore/'.$tool;
|
||||
|
||||
if (file_exists('/usr/bin/'.$tool))
|
||||
{
|
||||
if (file_exists('/usr/bin/'.$tool)) {
|
||||
$fullpathofdump = '/usr/bin/'.$tool;
|
||||
} else {
|
||||
// TODO L'utilisateur de la base doit etre un superadmin pour lancer cette commande
|
||||
$resql = $this->query('SHOW data_directory');
|
||||
if ($resql)
|
||||
{
|
||||
if ($resql) {
|
||||
$liste = $this->fetch_array($resql);
|
||||
$basedir = $liste['data_directory'];
|
||||
$fullpathofdump = preg_replace('/data$/', 'bin', $basedir).'/'.$tool;
|
||||
@@ -1351,11 +1359,14 @@ class DoliDBPgsql extends DoliDB
|
||||
$result = array();
|
||||
|
||||
$resql = 'select name,setting from pg_settings';
|
||||
if ($filter) $resql .= " WHERE name = '".$this->escape($filter)."'";
|
||||
if ($filter) {
|
||||
$resql .= " WHERE name = '".$this->escape($filter)."'";
|
||||
}
|
||||
$resql = $this->query($resql);
|
||||
if ($resql)
|
||||
{
|
||||
while ($obj = $this->fetch_object($resql)) $result[$obj->name] = $obj->setting;
|
||||
if ($resql) {
|
||||
while ($obj = $this->fetch_object($resql)) {
|
||||
$result[$obj->name] = $obj->setting;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -1371,13 +1382,13 @@ class DoliDBPgsql extends DoliDB
|
||||
{
|
||||
/* This is to return current running requests.
|
||||
$sql='SELECT datname,procpid,current_query FROM pg_stat_activity ORDER BY procpid';
|
||||
if ($filter) $sql.=" LIKE '".$this->escape($filter)."'";
|
||||
$resql=$this->query($sql);
|
||||
if ($resql)
|
||||
{
|
||||
$obj=$this->fetch_object($resql);
|
||||
$result[$obj->Variable_name]=$obj->Value;
|
||||
}
|
||||
if ($filter) $sql.=" LIKE '".$this->escape($filter)."'";
|
||||
$resql=$this->query($sql);
|
||||
if ($resql)
|
||||
{
|
||||
$obj=$this->fetch_object($resql);
|
||||
$result[$obj->Variable_name]=$obj->Value;
|
||||
}
|
||||
*/
|
||||
|
||||
return array();
|
||||
|
||||
Reference in New Issue
Block a user