dolibarr  9.0.0
pgsql.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
3  * Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4  * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
6  * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
7  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
8  * Copyright (C) 2012 Yann Droneaud <yann@droneaud.fr>
9  * Copyright (C) 2012 Florian Henry <florian.henry@open-concept.pro>
10  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
31 require_once DOL_DOCUMENT_ROOT .'/core/db/DoliDB.class.php';
32 
36 class DoliDBPgsql extends DoliDB
37 {
39  public $type='pgsql'; // Name of manager
41  const LABEL='PostgreSQL'; // Label of manager
43  var $forcecharset='UTF8'; // Can't be static as it may be forced with a dynamic value
45  var $forcecollate=''; // Can't be static as it may be forced with a dynamic value
47  const VERSIONMIN='9.0.0'; // Version min database
49  private $_results;
50 
51  public $unescapeslashquot;
52  public $standard_conforming_strings;
53 
65  function __construct($type, $host, $user, $pass, $name='', $port=0)
66  {
67  global $conf,$langs;
68 
69  // Note that having "static" property for "$forcecharset" and "$forcecollate" will make error here in strict mode, so they are not static
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;
72 
73  $this->database_user=$user;
74  $this->database_host=$host;
75  $this->database_port=$port;
76 
77  $this->transaction_opened=0;
78 
79  //print "Name DB: $host,$user,$pass,$name<br>";
80 
81  if (! function_exists("pg_connect"))
82  {
83  $this->connected = false;
84  $this->ok = 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);
87  return $this->ok;
88  }
89 
90  if (! $host)
91  {
92  $this->connected = false;
93  $this->ok = false;
94  $this->error=$langs->trans("ErrorWrongHostParameter");
95  dol_syslog(get_class($this)."::DoliDBPgsql : Erreur Connect, wrong host parameters",LOG_ERR);
96  return $this->ok;
97  }
98 
99  // Essai connexion serveur
100  //print "$host, $user, $pass, $name, $port";
101  $this->db = $this->connect($host, $user, $pass, $name, $port);
102 
103  if ($this->db)
104  {
105  $this->connected = true;
106  $this->ok = true;
107  }
108  else
109  {
110  // host, login ou password incorrect
111  $this->connected = false;
112  $this->ok = false;
113  $this->error='Host, login or password incorrect';
114  dol_syslog(get_class($this)."::DoliDBPgsql : Erreur Connect ".$this->error,LOG_ERR);
115  }
116 
117  // Si connexion serveur ok et si connexion base demandee, on essaie connexion base
118  if ($this->connected && $name)
119  {
120  if ($this->select_db($name))
121  {
122  $this->database_selected = true;
123  $this->database_name = $name;
124  $this->ok = true;
125  }
126  else
127  {
128  $this->database_selected = false;
129  $this->database_name = '';
130  $this->ok = false;
131  $this->error=$this->error();
132  dol_syslog(get_class($this)."::DoliDBPgsql : Erreur Select_db ".$this->error,LOG_ERR);
133  }
134  }
135  else
136  {
137  // Pas de selection de base demandee, ok ou ko
138  $this->database_selected = false;
139  }
140 
141  return $this->ok;
142  }
143 
144 
153  static function convertSQLFromMysql($line,$type='auto',$unescapeslashquot=false)
154  {
155  // Removed empty line if this is a comment line for SVN tagging
156  if (preg_match('/^--\s\$Id/i',$line)) {
157  return '';
158  }
159  // Return line if this is a comment
160  if (preg_match('/^#/i',$line) || preg_match('/^$/i',$line) || preg_match('/^--/i',$line))
161  {
162  return $line;
163  }
164  if ($line != "")
165  {
166  // group_concat support (PgSQL >= 9.0)
167  // Replace group_concat(x) or group_concat(x SEPARATOR ',') with string_agg(x, ',')
168  $line = preg_replace('/GROUP_CONCAT/i', 'STRING_AGG', $line);
169  $line = preg_replace('/ SEPARATOR/i', ',', $line);
170  $line = preg_replace('/STRING_AGG\(([^,\)]+)\)/i', 'STRING_AGG(\\1, \',\')', $line);
171  //print $line."\n";
172 
173  if ($type == 'auto')
174  {
175  if (preg_match('/ALTER TABLE/i',$line)) $type='dml';
176  else if (preg_match('/CREATE TABLE/i',$line)) $type='dml';
177  else if (preg_match('/DROP TABLE/i',$line)) $type='dml';
178  }
179 
180  $line=preg_replace('/ as signed\)/i',' as integer)',$line);
181 
182  if ($type == 'dml')
183  {
184  $line=preg_replace('/\s/',' ',$line); // Replace tabulation with space
185 
186  // we are inside create table statement so lets process datatypes
187  if (preg_match('/(ISAM|innodb)/i',$line)) { // end of create table sequence
188  $line=preg_replace('/\)[\s\t]*type[\s\t]*=[\s\t]*(MyISAM|innodb).*;/i',');',$line);
189  $line=preg_replace('/\)[\s\t]*engine[\s\t]*=[\s\t]*(MyISAM|innodb).*;/i',');',$line);
190  $line=preg_replace('/,$/','',$line);
191  }
192 
193  // Process case: "CREATE TABLE llx_mytable(rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY,code..."
194  if (preg_match('/[\s\t\(]*(\w*)[\s\t]+int.*auto_increment/i',$line,$reg)) {
195  $newline=preg_replace('/([\s\t\(]*)([a-zA-Z_0-9]*)[\s\t]+int.*auto_increment[^,]*/i','\\1 \\2 SERIAL PRIMARY KEY',$line);
196  //$line = "-- ".$line." replaced by --\n".$newline;
197  $line=$newline;
198  }
199 
200  // tinyint type conversion
201  $line=preg_replace('/tinyint\(?[0-9]*\)?/','smallint',$line);
202  $line=preg_replace('/tinyint/i','smallint',$line);
203 
204  // nuke unsigned
205  $line=preg_replace('/(int\w+|smallint)\s+unsigned/i','\\1',$line);
206 
207  // blob -> text
208  $line=preg_replace('/\w*blob/i','text',$line);
209 
210  // tinytext/mediumtext -> text
211  $line=preg_replace('/tinytext/i','text',$line);
212  $line=preg_replace('/mediumtext/i','text',$line);
213  $line=preg_replace('/longtext/i','text',$line);
214 
215  $line=preg_replace('/text\([0-9]+\)/i','text',$line);
216 
217  // change not null datetime field to null valid ones
218  // (to support remapping of "zero time" to null
219  $line=preg_replace('/datetime not null/i','datetime',$line);
220  $line=preg_replace('/datetime/i','timestamp',$line);
221 
222  // double -> numeric
223  $line=preg_replace('/^double/i','numeric',$line);
224  $line=preg_replace('/(\s*)double/i','\\1numeric',$line);
225  // float -> numeric
226  $line=preg_replace('/^float/i','numeric',$line);
227  $line=preg_replace('/(\s*)float/i','\\1numeric',$line);
228 
229  //Check tms timestamp field case (in Mysql this field is defautled to now and
230  // on update defaulted by now
231  $line=preg_replace('/(\s*)tms(\s*)timestamp/i','\\1tms timestamp without time zone DEFAULT now() NOT NULL',$line);
232 
233  // nuke ON UPDATE CURRENT_TIMESTAMP
234  $line=preg_replace('/(\s*)on(\s*)update(\s*)CURRENT_TIMESTAMP/i','\\1',$line);
235 
236  // unique index(field1,field2)
237  if (preg_match('/unique index\s*\((\w+\s*,\s*\w+)\)/i',$line))
238  {
239  $line=preg_replace('/unique index\s*\((\w+\s*,\s*\w+)\)/i','UNIQUE\(\\1\)',$line);
240  }
241 
242  // We remove end of requests "AFTER fieldxxx"
243  $line=preg_replace('/\sAFTER [a-z0-9_]+/i','',$line);
244 
245  // We remove start of requests "ALTER TABLE tablexxx" if this is a DROP INDEX
246  $line=preg_replace('/ALTER TABLE [a-z0-9_]+\s+DROP INDEX/i','DROP INDEX',$line);
247 
248  // Translate order to rename fields
249  if (preg_match('/ALTER TABLE ([a-z0-9_]+)\s+CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i',$line,$reg))
250  {
251  $line = "-- ".$line." replaced by --\n";
252  $line.= "ALTER TABLE ".$reg[1]." RENAME COLUMN ".$reg[2]." TO ".$reg[3];
253  }
254 
255  // Translate order to modify field format
256  if (preg_match('/ALTER TABLE ([a-z0-9_]+)\s+MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i',$line,$reg))
257  {
258  $line = "-- ".$line." replaced by --\n";
259  $newreg3=$reg[3];
260  $newreg3=preg_replace('/ DEFAULT NULL/i','',$newreg3);
261  $newreg3=preg_replace('/ NOT NULL/i','',$newreg3);
262  $newreg3=preg_replace('/ NULL/i','',$newreg3);
263  $newreg3=preg_replace('/ DEFAULT 0/i','',$newreg3);
264  $newreg3=preg_replace('/ DEFAULT \'?[0-9a-zA-Z_@]*\'?/i','',$newreg3);
265  $line.= "ALTER TABLE ".$reg[1]." ALTER COLUMN ".$reg[2]." TYPE ".$newreg3;
266  // TODO Add alter to set default value or null/not null if there is this in $reg[3]
267  }
268 
269  // alter table add primary key (field1, field2 ...) -> We remove the primary key name not accepted by PostGreSQL
270  // ALTER TABLE llx_dolibarr_modules ADD PRIMARY KEY pk_dolibarr_modules (numero, entity)
271  if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i',$line,$reg))
272  {
273  $line = "-- ".$line." replaced by --\n";
274  $line.= "ALTER TABLE ".$reg[1]." ADD PRIMARY KEY (".$reg[3];
275  }
276 
277  // Translate order to drop primary keys
278  // ALTER TABLE llx_dolibarr_modules DROP PRIMARY KEY pk_xxx
279  if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+PRIMARY\s+KEY\s*([^;]+)$/i',$line,$reg))
280  {
281  $line = "-- ".$line." replaced by --\n";
282  $line.= "ALTER TABLE ".$reg[1]." DROP CONSTRAINT ".$reg[2];
283  }
284 
285  // Translate order to drop foreign keys
286  // ALTER TABLE llx_dolibarr_modules DROP FOREIGN KEY fk_xxx
287  if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i',$line,$reg))
288  {
289  $line = "-- ".$line." replaced by --\n";
290  $line.= "ALTER TABLE ".$reg[1]." DROP CONSTRAINT ".$reg[2];
291  }
292 
293  // Translate order to add foreign keys
294  // ALTER TABLE llx_tablechild ADD CONSTRAINT fk_tablechild_fk_fieldparent FOREIGN KEY (fk_fieldparent) REFERENCES llx_tableparent (rowid)
295  if (preg_match('/ALTER\s+TABLE\s+(.*)\s*ADD CONSTRAINT\s+(.*)\s*FOREIGN\s+KEY\s*(.*)$/i',$line,$reg))
296  {
297  $line=preg_replace('/;$/','',$line);
298  $line.=" DEFERRABLE INITIALLY IMMEDIATE;";
299  }
300 
301  // alter table add [unique] [index] (field1, field2 ...)
302  // ALTER TABLE llx_accountingaccount ADD INDEX idx_accountingaccount_fk_pcg_version (fk_pcg_version)
303  if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i',$line,$reg))
304  {
305  $fieldlist=$reg[4];
306  $idxname=$reg[3];
307  $tablename=$reg[1];
308  $line = "-- ".$line." replaced by --\n";
309  $line.= "CREATE ".(preg_match('/UNIQUE/',$reg[2])?'UNIQUE ':'')."INDEX ".$idxname." ON ".$tablename." (".$fieldlist.")";
310  }
311  }
312 
313  // To have postgresql case sensitive
314  $line=str_replace(' LIKE \'',' ILIKE \'',$line);
315  $line=str_replace(' LIKE BINARY \'',' LIKE \'',$line);
316 
317  // Replace INSERT IGNORE into INSERT
318  $line=preg_replace('/^INSERT IGNORE/','INSERT',$line);
319 
320  // Delete using criteria on other table must not declare twice the deleted table
321  // DELETE FROM tabletodelete USING tabletodelete, othertable -> DELETE FROM tabletodelete USING othertable
322  if (preg_match('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i',$line,$reg))
323  {
324  if ($reg[1] == $reg[2]) // If same table, we remove second one
325  {
326  $line=preg_replace('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i','DELETE FROM \\1 USING \\3', $line);
327  }
328  }
329 
330  // Remove () in the tables in FROM if 1 table
331  $line=preg_replace('/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i','FROM \\1',$line);
332  //print $line."\n";
333 
334  // Remove () in the tables in FROM if 2 table
335  $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);
336  //print $line."\n";
337 
338  // Remove () in the tables in FROM if 3 table
339  $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);
340  //print $line."\n";
341 
342  // Remove () in the tables in FROM if 4 table
343  $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);
344  //print $line."\n";
345 
346  // Remove () in the tables in FROM if 5 table
347  $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);
348  //print $line."\n";
349 
350  // Replace espacing \' by ''.
351  // By default we do not (should be already done by db->escape function if required
352  // except for sql insert in data file that are mysql escaped so we removed them to
353  // be compatible with standard_conforming_strings=on that considers \ as ordinary character).
354  if ($unescapeslashquot) $line=preg_replace("/\\\'/","''",$line);
355 
356  //print "type=".$type." newline=".$line."<br>\n";
357  }
358 
359  return $line;
360  }
361 
362  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
371  function select_db($database)
372  {
373  // phpcs:enable
374  if ($database == $this->database_name) {
375  return true;
376  } else {
377  return false;
378  }
379  }
380 
392  function connect($host, $login, $passwd, $name, $port=0)
393  {
394  // use pg_pconnect() instead of pg_connect() if you want to use persistent connection costing 1ms, instead of 30ms for non persistent
395 
396  $this->db = false;
397 
398  // connections parameters must be protected (only \ and ' according to pg_connect() manual)
399  $host = str_replace(array("\\", "'"), array("\\\\", "\\'"), $host);
400  $login = str_replace(array("\\", "'"), array("\\\\", "\\'"), $login);
401  $passwd = str_replace(array("\\", "'"), array("\\\\", "\\'"), $passwd);
402  $name = str_replace(array("\\", "'"), array("\\\\", "\\'"), $name);
403  $port = str_replace(array("\\", "'"), array("\\\\", "\\'"), $port);
404 
405  if (! $name) $name="postgres"; // When try to connect using admin user
406 
407  // try first Unix domain socket (local)
408  if ((! empty($host) && $host == "socket") && ! defined('NOLOCALSOCKETPGCONNECT'))
409  {
410  $con_string = "dbname='".$name."' user='".$login."' password='".$passwd."'"; // $name may be empty
411  $this->db = @pg_connect($con_string);
412  }
413 
414  // if local connection failed or not requested, use TCP/IP
415  if (! $this->db)
416  {
417  if (! $host) $host = "localhost";
418  if (! $port) $port = 5432;
419 
420  $con_string = "host='".$host."' port='".$port."' dbname='".$name."' user='".$login."' password='".$passwd."'";
421  $this->db = @pg_connect($con_string);
422  }
423 
424  // now we test if at least one connect method was a success
425  if ($this->db)
426  {
427  $this->database_name = $name;
428  pg_set_error_verbosity($this->db, PGSQL_ERRORS_VERBOSE); // Set verbosity to max
429  pg_query($this->db, "set datestyle = 'ISO, YMD';");
430  }
431 
432  return $this->db;
433  }
434 
440  function getVersion()
441  {
442  $resql=$this->query('SHOW server_version');
443  if ($resql)
444  {
445  $liste=$this->fetch_array($resql);
446  return $liste['server_version'];
447  }
448  return '';
449  }
450 
456  function getDriverInfo()
457  {
458  return 'pgsql php driver';
459  }
460 
467  function close()
468  {
469  if ($this->db)
470  {
471  if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened,LOG_ERR);
472  $this->connected=false;
473  return pg_close($this->db);
474  }
475  return false;
476  }
477 
486  function query($query,$usesavepoint=0,$type='auto')
487  {
488  global $conf;
489 
490  $query = trim($query);
491 
492  // Convert MySQL syntax to PostgresSQL syntax
493  $query=$this->convertSQLFromMysql($query,$type,($this->unescapeslashquot && $this->standard_conforming_strings));
494  //print "After convertSQLFromMysql:\n".$query."<br>\n";
495 
496  if (! empty($conf->global->MAIN_DB_AUTOFIX_BAD_SQL_REQUEST))
497  {
498  // Fix bad formed requests. If request contains a date without quotes, we fix this but this should not occurs.
499  $loop=true;
500  while ($loop)
501  {
502  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))
503  {
504  $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);
505  dol_syslog("Warning: Bad formed request converted into ".$query,LOG_WARNING);
506  }
507  else $loop=false;
508  }
509  }
510 
511  if ($usesavepoint && $this->transaction_opened)
512  {
513  @pg_query($this->db, 'SAVEPOINT mysavepoint');
514  }
515 
516  if (! in_array($query,array('BEGIN','COMMIT','ROLLBACK'))) dol_syslog('sql='.$query, LOG_DEBUG);
517 
518  $ret = @pg_query($this->db, $query);
519 
520  //print $query;
521  if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query)) // Si requete utilisateur, on la sauvegarde ainsi que son resultset
522  {
523  if (! $ret)
524  {
525  if ($this->errno() != 'DB_ERROR_25P02') // Do not overwrite errors if this is a consecutive error
526  {
527  $this->lastqueryerror = $query;
528  $this->lasterror = $this->error();
529  $this->lasterrno = $this->errno();
530 
531  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
532  dol_syslog(get_class($this)."::query SQL Error message: ".$this->lasterror." (".$this->lasterrno.")", LOG_ERR);
533  dol_syslog(get_class($this)."::query SQL Error usesavepoint = ".$usesavepoint, LOG_ERR);
534  }
535 
536  if ($usesavepoint && $this->transaction_opened) // Warning, after that errno will be erased
537  {
538  @pg_query($this->db, 'ROLLBACK TO SAVEPOINT mysavepoint');
539  }
540  }
541  $this->lastquery=$query;
542  $this->_results = $ret;
543  }
544 
545  return $ret;
546  }
547 
548  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
555  function fetch_object($resultset)
556  {
557  // phpcs:enable
558  // If resultset not provided, we take the last used by connexion
559  if (! is_resource($resultset)) { $resultset=$this->_results; }
560  return pg_fetch_object($resultset);
561  }
562 
563  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
570  function fetch_array($resultset)
571  {
572  // phpcs:enable
573  // If resultset not provided, we take the last used by connexion
574  if (! is_resource($resultset)) { $resultset=$this->_results; }
575  return pg_fetch_array($resultset);
576  }
577 
578  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
585  function fetch_row($resultset)
586  {
587  // phpcs:enable
588  // Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
589  if (! is_resource($resultset)) { $resultset=$this->_results; }
590  return pg_fetch_row($resultset);
591  }
592 
593  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
601  function num_rows($resultset)
602  {
603  // phpcs:enable
604  // If resultset not provided, we take the last used by connexion
605  if (! is_resource($resultset)) { $resultset=$this->_results; }
606  return pg_num_rows($resultset);
607  }
608 
609  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
617  function affected_rows($resultset)
618  {
619  // phpcs:enable
620  // If resultset not provided, we take the last used by connexion
621  if (! is_resource($resultset)) { $resultset=$this->_results; }
622  // pgsql necessite un resultset pour cette fonction contrairement
623  // a mysql qui prend un link de base
624  return pg_affected_rows($resultset);
625  }
626 
627 
634  function free($resultset=null)
635  {
636  // If resultset not provided, we take the last used by connexion
637  if (! is_resource($resultset)) { $resultset=$this->_results; }
638  // Si resultset en est un, on libere la memoire
639  if (is_resource($resultset)) pg_free_result($resultset);
640  }
641 
642 
650  function plimit($limit=0,$offset=0)
651  {
652  global $conf;
653  if (empty($limit)) return "";
654  if ($limit < 0) $limit=$conf->liste_limit;
655  if ($offset > 0) return " LIMIT ".$limit." OFFSET ".$offset." ";
656  else return " LIMIT $limit ";
657  }
658 
659 
666  function escape($stringtoencode)
667  {
668  return pg_escape_string($stringtoencode);
669  }
670 
678  function idate($param)
679  {
680  return dol_print_date($param,"%Y-%m-%d %H:%M:%S");
681  }
682 
691  function ifsql($test,$resok,$resko)
692  {
693  return '(CASE WHEN '.$test.' THEN '.$resok.' ELSE '.$resko.' END)';
694  }
695 
701  function errno()
702  {
703  if (! $this->connected) {
704  // Si il y a eu echec de connexion, $this->db n'est pas valide.
705  return 'DB_ERROR_FAILED_TO_CONNECT';
706  }
707  else {
708  // Constants to convert error code to a generic Dolibarr error code
709  $errorcode_map = array(
710  1004 => 'DB_ERROR_CANNOT_CREATE',
711  1005 => 'DB_ERROR_CANNOT_CREATE',
712  1006 => 'DB_ERROR_CANNOT_CREATE',
713  1007 => 'DB_ERROR_ALREADY_EXISTS',
714  1008 => 'DB_ERROR_CANNOT_DROP',
715  1025 => 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
716  1044 => 'DB_ERROR_ACCESSDENIED',
717  1046 => 'DB_ERROR_NODBSELECTED',
718  1048 => 'DB_ERROR_CONSTRAINT',
719  '42P07' => 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS',
720  '42703' => 'DB_ERROR_NOSUCHFIELD',
721  1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS',
722  42701=> 'DB_ERROR_COLUMN_ALREADY_EXISTS',
723  '42710' => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
724  '23505' => 'DB_ERROR_RECORD_ALREADY_EXISTS',
725  '42704' => 'DB_ERROR_NO_INDEX_TO_DROP', // May also be Type xxx does not exists
726  '42601' => 'DB_ERROR_SYNTAX',
727  '42P16' => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
728  1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY',
729  1091 => 'DB_ERROR_NOSUCHFIELD',
730  1100 => 'DB_ERROR_NOT_LOCKED',
731  1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW',
732  '42P01' => 'DB_ERROR_NOSUCHTABLE',
733  '23503' => 'DB_ERROR_NO_PARENT',
734  1217 => 'DB_ERROR_CHILD_EXISTS',
735  1451 => 'DB_ERROR_CHILD_EXISTS',
736  '42P04' => 'DB_DATABASE_ALREADY_EXISTS'
737  );
738 
739  $errorlabel=pg_last_error($this->db);
740  $errorcode='';
741  if (preg_match('/: *([0-9P]+):/',$errorlabel,$reg))
742  {
743  $errorcode=$reg[1];
744  if (isset($errorcode_map[$errorcode]))
745  {
746  return $errorcode_map[$errorcode];
747  }
748  }
749  $errno=$errorcode?$errorcode:$errorlabel;
750  return ($errno?'DB_ERROR_'.$errno:'0');
751  }
752  // '/(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$/' => 'DB_ERROR_NOSUCHTABLE',
753  // '/table [\"\'].*[\"\'] does not exist/' => 'DB_ERROR_NOSUCHTABLE',
754  // '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*/' => 'DB_ERROR_RECORD_ALREADY_EXISTS',
755  // '/divide by zero$/' => 'DB_ERROR_DIVZERO',
756  // '/pg_atoi: error in .*: can\'t parse /' => 'DB_ERROR_INVALID_NUMBER',
757  // '/ttribute [\"\'].*[\"\'] not found$|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']/' => 'DB_ERROR_NOSUCHFIELD',
758  // '/parser: parse error at or near \"/' => 'DB_ERROR_SYNTAX',
759  // '/referential integrity violation/' => 'DB_ERROR_CONSTRAINT'
760  }
761 
767  function error()
768  {
769  return pg_last_error($this->db);
770  }
771 
772  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
780  function last_insert_id($tab,$fieldid='rowid')
781  {
782  // phpcs:enable
783  //$result = pg_query($this->db,"SELECT MAX(".$fieldid.") FROM ".$tab);
784  $result = pg_query($this->db,"SELECT currval('".$tab."_".$fieldid."_seq')");
785  if (! $result)
786  {
787  print pg_last_error($this->db);
788  exit;
789  }
790  //$nbre = pg_num_rows($result);
791  $row = pg_fetch_result($result,0,0);
792  return $row;
793  }
794 
803  function encrypt($fieldorvalue, $withQuotes=0)
804  {
805  global $conf;
806 
807  // Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
808  $cryptType = ($conf->db->dolibarr_main_db_encryption?$conf->db->dolibarr_main_db_encryption:0);
809 
810  //Encryption key
811  $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
812 
813  $return = $fieldorvalue;
814  return ($withQuotes?"'":"").$this->escape($return).($withQuotes?"'":"");
815  }
816 
817 
824  function decrypt($value)
825  {
826  global $conf;
827 
828  // Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
829  $cryptType = ($conf->db->dolibarr_main_db_encryption?$conf->db->dolibarr_main_db_encryption:0);
830 
831  //Encryption key
832  $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
833 
834  $return = $value;
835  return $return;
836  }
837 
838 
839  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
845  function DDLGetConnectId()
846  {
847  // phpcs:enable
848  return '?';
849  }
850 
851 
852 
853  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
865  function DDLCreateDb($database,$charset='',$collation='',$owner='')
866  {
867  // phpcs:enable
868  if (empty($charset)) $charset=$this->forcecharset;
869  if (empty($collation)) $collation=$this->forcecollate;
870 
871  // Test charset match LC_TYPE (pgsql error otherwise)
872  //print $charset.' '.setlocale(LC_CTYPE,'0'); exit;
873 
874  $sql='CREATE DATABASE "'.$database.'" OWNER "'.$owner.'" ENCODING \''.$charset.'\'';
875  dol_syslog($sql,LOG_DEBUG);
876  $ret=$this->query($sql);
877  return $ret;
878  }
879 
880  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
888  function DDLListTables($database, $table='')
889  {
890  // phpcs:enable
891  $listtables=array();
892 
893  $like = '';
894  if ($table) $like = " AND table_name LIKE '".$table."'";
895  $result = pg_query($this->db, "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'".$like." ORDER BY table_name");
896  if ($result)
897  {
898  while($row = $this->fetch_row($result))
899  {
900  $listtables[] = $row[0];
901  }
902  }
903  return $listtables;
904  }
905 
906  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
914  function DDLInfoTable($table)
915  {
916  // phpcs:enable
917  $infotables=array();
918 
919  $sql="SELECT ";
920  $sql.=" infcol.column_name as \"Column\",";
921  $sql.=" CASE WHEN infcol.character_maximum_length IS NOT NULL THEN infcol.udt_name || '('||infcol.character_maximum_length||')'";
922  $sql.=" ELSE infcol.udt_name";
923  $sql.=" END as \"Type\",";
924  $sql.=" infcol.collation_name as \"Collation\",";
925  $sql.=" infcol.is_nullable as \"Null\",";
926  $sql.=" '' as \"Key\",";
927  $sql.=" infcol.column_default as \"Default\",";
928  $sql.=" '' as \"Extra\",";
929  $sql.=" '' as \"Privileges\"";
930  $sql.=" FROM information_schema.columns infcol";
931  $sql.=" WHERE table_schema='public' ";
932  $sql.=" AND table_name='".$table."'";
933  $sql.=" ORDER BY ordinal_position;";
934 
935  dol_syslog($sql,LOG_DEBUG);
936  $result = $this->query($sql);
937  if ($result)
938  {
939  while($row = $this->fetch_row($result))
940  {
941  $infotables[] = $row;
942  }
943  }
944  return $infotables;
945  }
946 
947 
948  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
961  function DDLCreateTable($table,$fields,$primary_key,$type,$unique_keys=null,$fulltext_keys=null,$keys=null)
962  {
963  // phpcs:enable
964  // FIXME: $fulltext_keys parameter is unused
965 
966  // cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
967  // ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
968  $sql = "create table ".$table."(";
969  $i=0;
970  foreach($fields as $field_name => $field_desc)
971  {
972  $sqlfields[$i] = $field_name." ";
973  $sqlfields[$i] .= $field_desc['type'];
974  if( preg_match("/^[^\s]/i",$field_desc['value']))
975  $sqlfields[$i] .= "(".$field_desc['value'].")";
976  else if( preg_match("/^[^\s]/i",$field_desc['attribute']))
977  $sqlfields[$i] .= " ".$field_desc['attribute'];
978  else if( preg_match("/^[^\s]/i",$field_desc['default']))
979  {
980  if(preg_match("/null/i",$field_desc['default']))
981  $sqlfields[$i] .= " default ".$field_desc['default'];
982  else
983  $sqlfields[$i] .= " default '".$field_desc['default']."'";
984  }
985  else if( preg_match("/^[^\s]/i",$field_desc['null']))
986  $sqlfields[$i] .= " ".$field_desc['null'];
987 
988  else if( preg_match("/^[^\s]/i",$field_desc['extra']))
989  $sqlfields[$i] .= " ".$field_desc['extra'];
990  $i++;
991  }
992  if($primary_key != "")
993  $pk = "primary key(".$primary_key.")";
994 
995  if(is_array($unique_keys))
996  {
997  $i = 0;
998  foreach($unique_keys as $key => $value)
999  {
1000  $sqluq[$i] = "UNIQUE KEY '".$key."' ('".$value."')";
1001  $i++;
1002  }
1003  }
1004  if(is_array($keys))
1005  {
1006  $i = 0;
1007  foreach($keys as $key => $value)
1008  {
1009  $sqlk[$i] = "KEY ".$key." (".$value.")";
1010  $i++;
1011  }
1012  }
1013  $sql .= implode(',',$sqlfields);
1014  if($primary_key != "")
1015  $sql .= ",".$pk;
1016  if(is_array($unique_keys))
1017  $sql .= ",".implode(',',$sqluq);
1018  if(is_array($keys))
1019  $sql .= ",".implode(',',$sqlk);
1020  $sql .=") type=".$type;
1021 
1022  dol_syslog($sql,LOG_DEBUG);
1023  if(! $this->query($sql))
1024  return -1;
1025  else
1026  return 1;
1027  }
1028 
1029  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1036  function DDLDropTable($table)
1037  {
1038  // phpcs:enable
1039  $sql = "DROP TABLE ".$table;
1040 
1041  if (! $this->query($sql))
1042  return -1;
1043  else
1044  return 1;
1045  }
1046 
1047  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1057  function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name)
1058  {
1059  // phpcs:enable
1060  // Note: using ' on user does not works with pgsql
1061  $sql = "CREATE USER ".$this->escape($dolibarr_main_db_user)." with password '".$this->escape($dolibarr_main_db_pass)."'";
1062 
1063  dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
1064  $resql=$this->query($sql);
1065  if (! $resql)
1066  {
1067  return -1;
1068  }
1069 
1070  return 1;
1071  }
1072 
1073  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1081  function DDLDescTable($table,$field="")
1082  {
1083  // phpcs:enable
1084  $sql ="SELECT attname FROM pg_attribute, pg_type WHERE typname = '".$table."' AND attrelid = typrelid";
1085  $sql.=" AND attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')";
1086  if ($field) $sql.= " AND attname = '".$field."'";
1087 
1088  dol_syslog($sql,LOG_DEBUG);
1089  $this->_results = $this->query($sql);
1090  return $this->_results;
1091  }
1092 
1093  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1103  function DDLAddField($table,$field_name,$field_desc,$field_position="")
1104  {
1105  // phpcs:enable
1106  // cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
1107  // ex. : $field_desc = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
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')))
1112  {
1113  $sql.= "(".$field_desc['value'].")";
1114  }
1115  if (preg_match("/^[^\s]/i",$field_desc['attribute']))
1116  $sql .= " ".$field_desc['attribute'];
1117  if (preg_match("/^[^\s]/i",$field_desc['null']))
1118  $sql .= " ".$field_desc['null'];
1119  if (preg_match("/^[^\s]/i",$field_desc['default']))
1120  if (preg_match("/null/i",$field_desc['default']))
1121  $sql .= " default ".$field_desc['default'];
1122  else
1123  $sql .= " default '".$field_desc['default']."'";
1124  if (preg_match("/^[^\s]/i",$field_desc['extra']))
1125  $sql .= " ".$field_desc['extra'];
1126  $sql .= " ".$field_position;
1127 
1128  dol_syslog($sql,LOG_DEBUG);
1129  if (! $this -> query($sql))
1130  return -1;
1131  return 1;
1132  }
1133 
1134  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1143  function DDLUpdateField($table,$field_name,$field_desc)
1144  {
1145  // phpcs:enable
1146  $sql = "ALTER TABLE ".$table;
1147  $sql .= " MODIFY COLUMN ".$field_name." ".$field_desc['type'];
1148  if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar') {
1149  $sql.="(".$field_desc['value'].")";
1150  }
1151 
1152  if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL')
1153  {
1154  // 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
1155  if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text')
1156  {
1157  $sqlbis="UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL";
1158  $this->query($sqlbis);
1159  }
1160  elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int')
1161  {
1162  $sqlbis="UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL";
1163  $this->query($sqlbis);
1164  }
1165  }
1166 
1167  if ($field_desc['default'] != '')
1168  {
1169  if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']);
1170  elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
1171  }
1172 
1173  dol_syslog($sql,LOG_DEBUG);
1174  if (! $this->query($sql))
1175  return -1;
1176  return 1;
1177  }
1178 
1179  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1187  function DDLDropField($table,$field_name)
1188  {
1189  // phpcs:enable
1190  $sql= "ALTER TABLE ".$table." DROP COLUMN ".$field_name;
1191  dol_syslog($sql,LOG_DEBUG);
1192  if (! $this->query($sql))
1193  {
1194  $this->error=$this->lasterror();
1195  return -1;
1196  }
1197  return 1;
1198  }
1199 
1206  {
1207  $resql=$this->query('SHOW SERVER_ENCODING');
1208  if ($resql)
1209  {
1210  $liste=$this->fetch_array($resql);
1211  return $liste['server_encoding'];
1212  }
1213  else return '';
1214  }
1215 
1222  {
1223  $resql=$this->query('SHOW SERVER_ENCODING');
1224  $liste = array();
1225  if ($resql)
1226  {
1227  $i = 0;
1228  while ($obj = $this->fetch_object($resql))
1229  {
1230  $liste[$i]['charset'] = $obj->server_encoding;
1231  $liste[$i]['description'] = 'Default database charset';
1232  $i++;
1233  }
1234  $this->free($resql);
1235  } else {
1236  return null;
1237  }
1238  return $liste;
1239  }
1240 
1247  {
1248  $resql=$this->query('SHOW LC_COLLATE');
1249  if ($resql)
1250  {
1251  $liste=$this->fetch_array($resql);
1252  return $liste['lc_collate'];
1253  }
1254  else return '';
1255  }
1256 
1263  {
1264  $resql=$this->query('SHOW LC_COLLATE');
1265  $liste = array();
1266  if ($resql)
1267  {
1268  $i = 0;
1269  while ($obj = $this->fetch_object($resql) )
1270  {
1271  $liste[$i]['collation'] = $obj->lc_collate;
1272  $i++;
1273  }
1274  $this->free($resql);
1275  } else {
1276  return null;
1277  }
1278  return $liste;
1279  }
1280 
1286  function getPathOfDump()
1287  {
1288  $fullpathofdump='/pathtopgdump/pg_dump';
1289 
1290  if (file_exists('/usr/bin/pg_dump'))
1291  {
1292  $fullpathofdump='/usr/bin/pg_dump';
1293  }
1294  else
1295  {
1296  // TODO L'utilisateur de la base doit etre un superadmin pour lancer cette commande
1297  $resql=$this->query('SHOW data_directory');
1298  if ($resql)
1299  {
1300  $liste=$this->fetch_array($resql);
1301  $basedir=$liste['data_directory'];
1302  $fullpathofdump=preg_replace('/data$/','bin',$basedir).'/pg_dump';
1303  }
1304  }
1305 
1306  return $fullpathofdump;
1307  }
1308 
1314  function getPathOfRestore()
1315  {
1316  //$tool='pg_restore';
1317  $tool='psql';
1318 
1319  $fullpathofdump='/pathtopgrestore/'.$tool;
1320 
1321  if (file_exists('/usr/bin/'.$tool))
1322  {
1323  $fullpathofdump='/usr/bin/'.$tool;
1324  }
1325  else
1326  {
1327  // TODO L'utilisateur de la base doit etre un superadmin pour lancer cette commande
1328  $resql=$this->query('SHOW data_directory');
1329  if ($resql)
1330  {
1331  $liste=$this->fetch_array($resql);
1332  $basedir=$liste['data_directory'];
1333  $fullpathofdump=preg_replace('/data$/','bin',$basedir).'/'.$tool;
1334  }
1335  }
1336 
1337  return $fullpathofdump;
1338  }
1339 
1346  function getServerParametersValues($filter='')
1347  {
1348  $result=array();
1349 
1350  $resql='select name,setting from pg_settings';
1351  if ($filter) $resql.=" WHERE name = '".$this->escape($filter)."'";
1352  $resql=$this->query($resql);
1353  if ($resql)
1354  {
1355  while ($obj=$this->fetch_object($resql)) $result[$obj->name]=$obj->setting;
1356  }
1357 
1358  return $result;
1359  }
1360 
1367  function getServerStatusValues($filter='')
1368  {
1369  /* This is to return current running requests.
1370  $sql='SELECT datname,procpid,current_query FROM pg_stat_activity ORDER BY procpid';
1371  if ($filter) $sql.=" LIKE '".$this->escape($filter)."'";
1372  $resql=$this->query($sql);
1373  if ($resql)
1374  {
1375  $obj=$this->fetch_object($resql);
1376  $result[$obj->Variable_name]=$obj->Value;
1377  }
1378  */
1379 
1380  return array();
1381  }
1382 }
affected_rows($resultset)
Renvoie le nombre de lignes dans le resultat d&#39;une requete INSERT, DELETE ou 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.
print
Draft customers invoices.
Definition: index.php:91
getServerStatusValues($filter='')
Return value of server status.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
idate($param)
Convert (by PHP) a GM Timestamp date into a GM string date to insert into a date field.
getListOfCollation()
Return list of available collation that can be used for database.
DDLListTables($database, $table='')
List tables into a database.
decrypt($value)
Decrypt sensitive data in database.
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&#39;a aucune fonction equivalente de mysql_select_db On compare juste ...
Class to drive a Postgresql database for Dolibarr.
Definition: pgsql.class.php:36
static convertSQLFromMysql($line, $type='auto', $unescapeslashquot=false)
Convert a SQL request in Mysql syntax to native syntax.
getVersion()
Return version of database server.
lasterror()
Return last error label.
$type
Database type.
Definition: pgsql.class.php:39
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
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.
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.
getServerParametersValues($filter='')
Return value of server parameters.
getDefaultCollationDatabase()
Return collation used in database.
error()
Renvoie le texte de l&#39;erreur pgsql de l&#39;operation precedente.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
DDLCreateDb($database, $charset='', $collation='', $owner='')
Create a new database Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated We fo...
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.
plimit($limit=0, $offset=0)
Define limits and offset of request.
const VERSIONMIN
Version min database.
Definition: pgsql.class.php:47
query($query, $usesavepoint=0, $type='auto')
Convert request to PostgreSQL syntax, execute it and return the resultset.
__construct($type, $host, $user, $pass, $name='', $port=0)
Constructor.
Definition: pgsql.class.php:65
$forcecollate
Collate used to force collate when creating database.
Definition: pgsql.class.php:45
getListOfCharacterSet()
Return list of available charset that can be used to store data in database.
lasterrno()
Return last error code.
DDLGetConnectId()
Return connexion ID.
getPathOfRestore()
Return full path of restore program.
const LABEL
Database label.
Definition: pgsql.class.php:41
errno()
Renvoie le code erreur generique de l&#39;operation precedente.
encrypt($fieldorvalue, $withQuotes=0)
Encrypt sensitive data in database Warning: This function includes the escape, so it must use direct ...
$forcecharset
Charset.
Definition: pgsql.class.php:43
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.