2004-10-20 00:47:16 +02:00
|
|
|
|
<?php
|
2004-08-24 09:02:32 +02:00
|
|
|
|
/* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
|
2005-03-15 21:23:00 +01:00
|
|
|
|
* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
|
2009-10-25 19:53:09 +01:00
|
|
|
|
* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
|
2004-09-02 23:14:20 +02:00
|
|
|
|
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
|
2009-07-03 10:22:06 +02:00
|
|
|
|
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
|
|
|
|
|
|
* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
|
2004-08-24 09:02:32 +02:00
|
|
|
|
*
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2005-03-20 03:26:59 +01:00
|
|
|
|
/**
|
2009-10-25 19:53:09 +01:00
|
|
|
|
* \file htdocs/lib/databases/pgsql.lib.php
|
|
|
|
|
|
* \brief Fichier de la classe permettant de gerer une base pgsql
|
|
|
|
|
|
* \version $Id$
|
2009-10-22 19:15:09 +02:00
|
|
|
|
*/
|
2008-09-06 01:08:07 +02:00
|
|
|
|
// For compatibility during upgrade
|
2008-03-05 19:33:31 +01:00
|
|
|
|
if (! defined('DOL_DOCUMENT_ROOT')) define('DOL_DOCUMENT_ROOT', '../..');
|
|
|
|
|
|
if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/adodbtime/adodb-time.inc.php");
|
2004-08-24 09:02:32 +02:00
|
|
|
|
|
2005-09-04 03:48:09 +02:00
|
|
|
|
|
2005-03-20 03:26:59 +01:00
|
|
|
|
/**
|
2009-10-25 19:53:09 +01:00
|
|
|
|
* \class DoliDb
|
|
|
|
|
|
* \brief Classe permettant de gerer la database de dolibarr
|
2009-10-22 19:15:09 +02:00
|
|
|
|
*/
|
2006-06-24 22:58:22 +02:00
|
|
|
|
class DoliDb
|
2004-09-02 22:28:56 +02:00
|
|
|
|
{
|
2009-10-26 02:29:00 +01:00
|
|
|
|
var $db; // Database handler
|
|
|
|
|
|
var $type='pgsql'; // Name of manager
|
|
|
|
|
|
var $label='PostgreSQL'; // Label of manager
|
2009-10-22 19:15:09 +02:00
|
|
|
|
//! Charset
|
|
|
|
|
|
var $forcecharset='latin1';
|
2006-11-15 02:04:38 +01:00
|
|
|
|
var $versionmin=array(8,1,0); // Version min database
|
2005-03-20 03:26:59 +01:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
var $results; // Resultset de la derniere requete
|
2006-06-24 22:58:22 +02:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
var $connected; // 1 si connecte, 0 sinon
|
|
|
|
|
|
var $database_selected; // 1 si base selectionne, 0 sinon
|
|
|
|
|
|
var $database_name; // Nom base selectionnee
|
|
|
|
|
|
var $database_user; //! Nom user base
|
|
|
|
|
|
var $transaction_opened; // 1 si une transaction est en cours, 0 sinon
|
|
|
|
|
|
var $lastquery;
|
2006-03-14 21:53:51 +01:00
|
|
|
|
var $lastqueryerror; // Ajout d'une variable en cas d'erreur
|
2006-06-24 22:58:22 +02:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
var $ok;
|
|
|
|
|
|
var $error;
|
|
|
|
|
|
var $lasterror;
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2006-06-24 22:58:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Ouverture d'une connexion vers le serveur et une database.
|
|
|
|
|
|
* \param type type de base de donnees (mysql ou pgsql)
|
|
|
|
|
|
* \param host addresse de la base de donnees
|
|
|
|
|
|
* \param user nom de l'utilisateur autorise
|
|
|
|
|
|
* \param pass mot de passe
|
|
|
|
|
|
* \param name nom de la database
|
|
|
|
|
|
* \param port Port of database server
|
|
|
|
|
|
* \return int 1 en cas de succes, 0 sinon
|
|
|
|
|
|
*/
|
|
|
|
|
|
function DoliDb($type='pgsql', $host, $user, $pass, $name='', $port=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
global $conf,$langs;
|
2007-07-13 14:57:07 +02:00
|
|
|
|
|
2009-05-08 03:11:04 +02:00
|
|
|
|
$this->forcecharset=$conf->file->character_set_client;
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$this->forcecollate=$conf->db->dolibarr_main_db_collation;
|
|
|
|
|
|
$this->database_user=$user;
|
|
|
|
|
|
|
|
|
|
|
|
$this->transaction_opened=0;
|
|
|
|
|
|
|
|
|
|
|
|
//print "Name DB: $host,$user,$pass,$name<br>";
|
|
|
|
|
|
|
|
|
|
|
|
if (! function_exists("pg_connect"))
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->connected = 0;
|
|
|
|
|
|
$this->ok = 0;
|
|
|
|
|
|
$this->error="Pgsql PHP functions are not available in this version of PHP";
|
|
|
|
|
|
dol_syslog("DoliDB::DoliDB : Pgsql PHP functions are not available in this version of PHP",LOG_ERR);
|
|
|
|
|
|
return $this->ok;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (! $host)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->connected = 0;
|
|
|
|
|
|
$this->ok = 0;
|
|
|
|
|
|
$this->error=$langs->trans("ErrorWrongHostParameter");
|
|
|
|
|
|
dol_syslog("DoliDB::DoliDB : Erreur Connect, wrong host parameters",LOG_ERR);
|
|
|
|
|
|
return $this->ok;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Essai connexion serveur
|
|
|
|
|
|
//print "$host, $user, $pass, $name, $port";
|
|
|
|
|
|
$this->db = $this->connect($host, $user, $pass, $name, $port);
|
|
|
|
|
|
if ($this->db)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->connected = 1;
|
|
|
|
|
|
$this->ok = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// host, login ou password incorrect
|
|
|
|
|
|
$this->connected = 0;
|
|
|
|
|
|
$this->ok = 0;
|
2008-03-17 02:10:31 +01:00
|
|
|
|
$this->error='Host, login or password incorrect';
|
2009-10-22 19:15:09 +02:00
|
|
|
|
dol_syslog("DoliDB::DoliDB : Erreur Connect ".$this->error,LOG_ERR);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Si connexion serveur ok et si connexion base demandee, on essaie connexion base
|
|
|
|
|
|
if ($this->connected && $name)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($this->select_db($name))
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->database_selected = 1;
|
|
|
|
|
|
$this->database_name = $name;
|
|
|
|
|
|
$this->ok = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->database_selected = 0;
|
|
|
|
|
|
$this->database_name = '';
|
|
|
|
|
|
$this->ok = 0;
|
|
|
|
|
|
$this->error=$this->error();
|
|
|
|
|
|
dol_syslog("DoliDB::DoliDB : Erreur Select_db ".$this->error,LOG_ERR);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Pas de selection de base demandee, ok ou ko
|
|
|
|
|
|
$this->database_selected = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->ok;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-10-22 20:21:06 +02:00
|
|
|
|
* \brief Convert a SQL request in Mysql syntax to PostgreSQL syntax
|
2008-09-06 01:38:14 +02:00
|
|
|
|
* \param line SQL request line to convert
|
|
|
|
|
|
* \return string SQL request line converted
|
2008-09-06 01:08:07 +02:00
|
|
|
|
*/
|
2008-09-06 01:38:14 +02:00
|
|
|
|
function convertSQLFromMysql($line)
|
2008-09-06 01:08:07 +02:00
|
|
|
|
{
|
2008-09-06 01:38:14 +02:00
|
|
|
|
# comments or empty lines
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if (preg_match('/^--\s\$Id/i',$line)) {
|
2009-10-22 19:15:09 +02:00
|
|
|
|
return '';
|
2008-09-06 01:38:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
# comments or empty lines
|
2009-10-22 19:15:09 +02:00
|
|
|
|
if (preg_match('/^#/i',$line) || preg_match('/^$/i',$line) || preg_match('/^--/i',$line))
|
|
|
|
|
|
{
|
|
|
|
|
|
return $line;
|
|
|
|
|
|
}
|
2009-10-22 20:21:06 +02:00
|
|
|
|
if ($line != "")
|
2009-10-25 17:30:26 +01:00
|
|
|
|
{
|
|
|
|
|
|
# we are inside create table statement so lets process datatypes
|
2009-10-22 19:15:09 +02:00
|
|
|
|
if (preg_match('/(ISAM|innodb)/i',$line)) { # end of create table sequence
|
2009-10-23 12:44:19 +02:00
|
|
|
|
$line=preg_replace('/\)[\s\t]*type=(MyISAM|innodb);/i',');',$line);
|
|
|
|
|
|
$line=preg_replace('/\)[\s\t]*engine=(MyISAM|innodb);/i',');',$line);
|
2009-10-22 20:21:06 +02:00
|
|
|
|
$line=preg_replace('/,$/','',$line);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-22 20:21:06 +02:00
|
|
|
|
if (preg_match('/[\s\t]*(\w*)\s*.*int.*auto_increment/i',$line,$reg)) {
|
2009-10-23 12:44:19 +02:00
|
|
|
|
$line=preg_replace('/[\s\t]*([a-zA-Z_0-9]*)[\s\t]*.*int.*auto_increment[^,]*/i','\\1 SERIAL PRIMARY KEY',$line);
|
2009-10-22 20:21:06 +02:00
|
|
|
|
}
|
2009-10-22 19:15:09 +02:00
|
|
|
|
|
2009-10-25 17:30:26 +01:00
|
|
|
|
# tinyint type conversion
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$line=str_replace('tinyint','smallint',$line);
|
|
|
|
|
|
|
|
|
|
|
|
# nuke unsigned
|
2009-10-22 20:21:06 +02:00
|
|
|
|
$line=preg_replace('/(int\w+|smallint)\s+unsigned/i','\\1',$line);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
|
|
|
|
|
|
# blob -> text
|
2009-10-22 20:21:06 +02:00
|
|
|
|
$line=preg_replace('/\w*blob/i','text',$line);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
|
|
|
|
|
|
# tinytext/mediumtext -> text
|
2009-10-22 20:21:06 +02:00
|
|
|
|
$line=preg_replace('/tinytext/i','text',$line);
|
|
|
|
|
|
$line=preg_replace('/mediumtext/i','text',$line);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
|
|
|
|
|
|
# char -> varchar
|
|
|
|
|
|
# PostgreSQL would otherwise pad with spaces as opposed
|
|
|
|
|
|
# to MySQL! Your user interface may depend on this!
|
|
|
|
|
|
// s/(\s+)char/${1}varchar/gi;
|
|
|
|
|
|
|
|
|
|
|
|
# nuke date representation (not supported in PostgreSQL)
|
|
|
|
|
|
// s/datetime default '[^']+'/datetime/i;
|
|
|
|
|
|
// s/date default '[^']+'/datetime/i;
|
|
|
|
|
|
// s/time default '[^']+'/datetime/i;
|
|
|
|
|
|
|
|
|
|
|
|
# change not null datetime field to null valid ones
|
|
|
|
|
|
# (to support remapping of "zero time" to null
|
2009-10-22 20:21:06 +02:00
|
|
|
|
$line=preg_replace('/datetime not null/i','datetime',$line);
|
|
|
|
|
|
$line=preg_replace('/datetime/i','timestamp',$line);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
|
2009-10-24 01:01:03 +02:00
|
|
|
|
# double -> numeric
|
2009-10-25 19:53:09 +01:00
|
|
|
|
$line=preg_replace('/^double/i','numeric',$line);
|
|
|
|
|
|
$line=preg_replace('/(\s*)double/i','\\1numeric',$line);
|
2009-10-24 01:01:03 +02:00
|
|
|
|
# float -> numeric
|
2009-10-25 19:53:09 +01:00
|
|
|
|
$line=preg_replace('/^float/i','numeric',$line);
|
|
|
|
|
|
$line=preg_replace('/(\s*)float/i','\\1numeric',$line);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
|
2009-10-25 19:53:09 +01:00
|
|
|
|
# unique index(field1,field2)
|
2009-10-24 01:01:03 +02:00
|
|
|
|
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);
|
2009-10-22 20:21:06 +02:00
|
|
|
|
}
|
2009-10-25 17:30:26 +01:00
|
|
|
|
|
2009-10-25 18:57:23 +01:00
|
|
|
|
# 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))
|
|
|
|
|
|
{
|
|
|
|
|
|
$line = "-- ".$line." replaced by --\n";
|
|
|
|
|
|
$line.= "ALTER TABLE ".$reg[1]." ADD PRIMARY KEY (".$reg[3];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# alter table add [unique] [index] (field1, field2 ...)
|
2009-10-25 17:30:26 +01:00
|
|
|
|
# ALTER TABLE llx_accountingaccount ADD INDEX idx_accountingaccount_fk_pcg_version (fk_pcg_version)
|
2009-10-25 18:57:23 +01:00
|
|
|
|
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i',$line,$reg))
|
2009-10-25 17:30:26 +01:00
|
|
|
|
{
|
2009-10-25 18:57:23 +01:00
|
|
|
|
$fieldlist=$reg[4];
|
|
|
|
|
|
$idxname=$reg[3];
|
2009-10-25 17:30:26 +01:00
|
|
|
|
$tablename=$reg[1];
|
|
|
|
|
|
$line = "-- ".$line." replaced by --\n";
|
2009-10-25 18:57:23 +01:00
|
|
|
|
$line.= "CREATE ".(preg_match('/UNIQUE/',$reg[2])?'UNIQUE ':'')."INDEX ".$idxname." ON ".$tablename." (".$fieldlist.")";
|
2009-10-22 20:21:06 +02:00
|
|
|
|
}
|
2009-10-29 01:08:14 +01:00
|
|
|
|
|
2009-10-29 02:12:53 +01:00
|
|
|
|
// 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
|
|
|
|
|
|
{
|
|
|
|
|
|
$line=preg_replace('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i','DELETE FROM \\1 USING \\3', $line);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-29 02:26:38 +01:00
|
|
|
|
// Remove () in the tables in FROM if one table
|
|
|
|
|
|
$line=preg_replace('/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i','FROM \\1',$line);
|
|
|
|
|
|
//print $line;
|
|
|
|
|
|
|
|
|
|
|
|
// Remove () in the tables in FROM if two table
|
2010-03-24 14:26:27 +01:00
|
|
|
|
$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);
|
2009-10-29 01:08:14 +01:00
|
|
|
|
//print $line;
|
2009-10-29 02:12:53 +01:00
|
|
|
|
|
2010-03-24 14:26:27 +01:00
|
|
|
|
// Remove () in the tables in FROM if two table
|
|
|
|
|
|
$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);
|
|
|
|
|
|
//print $line;
|
2009-10-22 19:15:09 +02:00
|
|
|
|
} # END of if ($create_sql ne "") i.e. were inside create table statement so processed datatypes
|
|
|
|
|
|
else { # not inside create table
|
|
|
|
|
|
#---- fix data in inserted data: (from MS world)
|
|
|
|
|
|
# FIX: disabled for now
|
|
|
|
|
|
/* if (00 && /insert into/i) {
|
2009-10-22 20:21:06 +02:00
|
|
|
|
s!\x96!-!g; # --
|
|
|
|
|
|
s!\x93!"!g; # ``
|
|
|
|
|
|
s!\x94!"!g; # ''
|
|
|
|
|
|
s!\x85!... !g; # \ldots
|
|
|
|
|
|
s!\x92!`!g;
|
|
|
|
|
|
}
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
# fix dates '0000-00-00 00:00:00' (should be null)
|
|
|
|
|
|
/* s/'0000-00-00 00:00:00'/null/gi;
|
2009-10-22 20:21:06 +02:00
|
|
|
|
s/'0000-00-00'/null/gi;
|
|
|
|
|
|
s/'00:00:00'/null/gi;
|
|
|
|
|
|
s/([12]\d\d\d)([01]\d)([0-3]\d)([0-2]\d)([0-6]\d)([0-6]\d)/'$1-$2-$3 $4:$5:$6'/;
|
|
|
|
|
|
|
|
|
|
|
|
if (/create\s+table\s+(\w+)/i) {
|
|
|
|
|
|
$create_sql = $_;
|
|
|
|
|
|
/create\s*table\s*(\w+)/i;
|
|
|
|
|
|
$table=$1 if (defined($1));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
print OUT $_;
|
|
|
|
|
|
}
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
} # end of if inside create_table
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
2008-09-06 01:38:14 +02:00
|
|
|
|
return $line;
|
2008-09-06 01:08:07 +02:00
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Selectionne une database.
|
|
|
|
|
|
* \param database nom de la database
|
|
|
|
|
|
* \return boolean true si ok, false si ko
|
|
|
|
|
|
* \remarks Ici postgresql n'a aucune fonction equivalente de mysql_select_db
|
|
|
|
|
|
* \remarks On compare juste manuellement si la database choisie est bien celle activee par la connexion
|
|
|
|
|
|
*/
|
|
|
|
|
|
function select_db($database)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($database == $this->database_name)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
else
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Connection vers le serveur
|
|
|
|
|
|
* \param host addresse de la base de donnees
|
|
|
|
|
|
* \param login nom de l'utilisateur autorise
|
|
|
|
|
|
* \param passwd mot de passe
|
|
|
|
|
|
* \param name nom de la database (ne sert pas sous mysql, sert sous pgsql)
|
|
|
|
|
|
* \param port Port of database server
|
|
|
|
|
|
* \return resource handler d'acces a la base
|
|
|
|
|
|
*/
|
|
|
|
|
|
function connect($host, $login, $passwd, $name, $port=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!$name){
|
|
|
|
|
|
$name="postgres";
|
|
|
|
|
|
}
|
|
|
|
|
|
$con_string = "host=$host port=$port dbname=$name user=$login password=$passwd";
|
|
|
|
|
|
$this->db = pg_connect($con_string);
|
|
|
|
|
|
if ($this->db)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->database_name = $name;
|
2009-10-22 20:21:06 +02:00
|
|
|
|
pg_set_error_verbosity($this->db, PGSQL_ERRORS_VERBOSE); // Set verbosity to max
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
return $this->db;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-26 02:33:00 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return label of manager
|
|
|
|
|
|
* \return string Label
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getLabel()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->label;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie la version du serveur
|
|
|
|
|
|
* \return string Chaine version
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getVersion()
|
|
|
|
|
|
{
|
|
|
|
|
|
$resql=$this->query('SHOW server_version');
|
|
|
|
|
|
$liste=$this->fetch_array($resql);
|
|
|
|
|
|
return $liste['server_version'];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie la version du serveur sous forme de nombre
|
|
|
|
|
|
* \return string Chaine version
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getIntVersion()
|
2009-07-03 10:22:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
$version = $this->getVersion();
|
2009-10-20 15:14:44 +02:00
|
|
|
|
$vlist = preg_split('/[.-]/',$version);
|
2009-07-03 10:22:06 +02:00
|
|
|
|
if (strlen($vlist[1])==1){
|
|
|
|
|
|
$vlist[1]="0".$vlist[1];
|
|
|
|
|
|
}
|
|
|
|
|
|
if (strlen($vlist[2])==1){
|
|
|
|
|
|
$vlist[2]="0".$vlist[2];
|
|
|
|
|
|
}
|
|
|
|
|
|
return $vlist[0].$vlist[1].$vlist[2];
|
|
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie la version du serveur dans un tableau
|
|
|
|
|
|
* \return array Tableau de chaque niveau de version
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getVersionArray()
|
|
|
|
|
|
{
|
|
|
|
|
|
return explode('.',$this->getVersion());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Fermeture d'une connexion vers une database.
|
|
|
|
|
|
* \return resource
|
|
|
|
|
|
*/
|
|
|
|
|
|
function close()
|
|
|
|
|
|
{
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog("DoliDB::disconnect",LOG_DEBUG);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
return pg_close($this->db);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Debut d'une transaction.
|
|
|
|
|
|
* \return int 1 si ouverture transaction ok ou deja ouverte, 0 en cas d'erreur
|
|
|
|
|
|
*/
|
|
|
|
|
|
function begin()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (! $this->transaction_opened)
|
|
|
|
|
|
{
|
|
|
|
|
|
$ret=$this->query("BEGIN;");
|
|
|
|
|
|
if ($ret)
|
2007-11-18 23:10:19 +01:00
|
|
|
|
{
|
|
|
|
|
|
$this->transaction_opened++;
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog("BEGIN Transaction",LOG_DEBUG);
|
2007-11-18 23:10:19 +01:00
|
|
|
|
}
|
2009-10-22 19:15:09 +02:00
|
|
|
|
return $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->transaction_opened++;
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Validation d'une transaction
|
|
|
|
|
|
* \return int 1 si validation ok ou niveau de transaction non ouverte, 0 en cas d'erreur
|
|
|
|
|
|
*/
|
|
|
|
|
|
function commit()
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($this->transaction_opened<=1)
|
|
|
|
|
|
{
|
|
|
|
|
|
$ret=$this->query("COMMIT;");
|
2009-01-25 18:56:42 +01:00
|
|
|
|
if ($ret)
|
2007-11-18 23:10:19 +01:00
|
|
|
|
{
|
|
|
|
|
|
$this->transaction_opened=0;
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog("COMMIT Transaction",LOG_DEBUG);
|
2007-11-18 23:10:19 +01:00
|
|
|
|
}
|
2009-10-22 19:15:09 +02:00
|
|
|
|
return $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->transaction_opened--;
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Annulation d'une transaction et retour aux anciennes valeurs
|
|
|
|
|
|
* \return int 1 si annulation ok ou transaction non ouverte, 0 en cas d'erreur
|
|
|
|
|
|
*/
|
|
|
|
|
|
function rollback()
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($this->transaction_opened<=1)
|
|
|
|
|
|
{
|
|
|
|
|
|
$ret=$this->query("ROLLBACK;");
|
2007-11-18 23:10:19 +01:00
|
|
|
|
$this->transaction_opened=0;
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog("ROLLBACK Transaction",LOG_DEBUG);
|
2009-10-22 19:15:09 +02:00
|
|
|
|
return $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->transaction_opened--;
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-10-25 19:53:09 +01:00
|
|
|
|
* \brief Convert request to PostgreSQL syntax, execute it and return the resultset
|
|
|
|
|
|
* \param query query string
|
|
|
|
|
|
* \return resource Resultset of answer
|
2009-10-22 19:15:09 +02:00
|
|
|
|
*/
|
|
|
|
|
|
function query($query)
|
|
|
|
|
|
{
|
|
|
|
|
|
$query = trim($query);
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-10-22 20:21:06 +02:00
|
|
|
|
// Convert MySQL syntax to PostgresSQL syntax
|
|
|
|
|
|
$query=$this->convertSQLFromMysql($query);
|
2009-10-25 19:53:09 +01:00
|
|
|
|
//print "FF\n".$query."<br>\n";
|
2009-10-28 18:37:40 +01:00
|
|
|
|
|
|
|
|
|
|
// Fix bad formed requests. If request contains a date without quotes, we fix this but this should not occurs.
|
|
|
|
|
|
$loop=true;
|
|
|
|
|
|
while ($loop)
|
|
|
|
|
|
{
|
2009-10-29 01:08:14 +01:00
|
|
|
|
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))
|
2009-10-28 18:37:40 +01:00
|
|
|
|
{
|
2009-10-29 01:08:14 +01:00
|
|
|
|
$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);
|
2009-10-28 18:37:40 +01:00
|
|
|
|
dol_syslog("Warning: Bad formed request converted into ".$query,LOG_WARNING);
|
|
|
|
|
|
}
|
|
|
|
|
|
else $loop=false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-24 01:01:03 +02:00
|
|
|
|
$ret = @pg_query($this->db, $query);
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query))
|
2009-10-22 19:15:09 +02:00
|
|
|
|
{
|
|
|
|
|
|
// Si requete utilisateur, on la sauvegarde ainsi que son resultset
|
2006-11-15 02:04:38 +01:00
|
|
|
|
if (! $ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->lastqueryerror = $query;
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$this->lasterror = $this->error();
|
|
|
|
|
|
$this->lasterrno = $this->errno();
|
2009-10-24 01:01:03 +02:00
|
|
|
|
//print "\n>> ".$query."<br>\n";
|
2009-10-25 17:30:26 +01:00
|
|
|
|
//print '>> '.$this->lasterrno.' - '.$this->lasterror.' - '.$this->lastqueryerror."<br>\n";
|
2009-10-22 19:15:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
$this->lastquery=$query;
|
|
|
|
|
|
$this->results = $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie la ligne courante (comme un objet) pour le curseur resultset.
|
|
|
|
|
|
* \param resultset Curseur de la requete voulue
|
|
|
|
|
|
* \return resource
|
|
|
|
|
|
*/
|
|
|
|
|
|
function fetch_object($resultset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
|
|
|
|
|
if (! is_resource($resultset)) { $resultset=$this->results; }
|
|
|
|
|
|
return pg_fetch_object($resultset);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie les donnees dans un tableau.
|
|
|
|
|
|
* \param resultset Curseur de la requete voulue
|
|
|
|
|
|
* \return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function fetch_array($resultset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
|
|
|
|
|
if (! is_resource($resultset)) { $resultset=$this->results; }
|
|
|
|
|
|
return pg_fetch_array($resultset);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie les donnees comme un tableau.
|
|
|
|
|
|
* \param resultset Curseur de la requete voulue
|
|
|
|
|
|
* \return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function fetch_row($resultset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
|
|
|
|
|
if (! is_resource($resultset)) { $resultset=$this->results; }
|
|
|
|
|
|
return pg_fetch_row($resultset);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie le nombre de lignes dans le resultat d'une requete SELECT
|
|
|
|
|
|
* \see affected_rows
|
|
|
|
|
|
* \param resultset Curseur de la requete voulue
|
|
|
|
|
|
* \return int Nombre de lignes
|
|
|
|
|
|
*/
|
|
|
|
|
|
function num_rows($resultset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
|
|
|
|
|
if (! is_resource($resultset)) { $resultset=$this->results; }
|
|
|
|
|
|
return pg_num_rows($resultset);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE
|
|
|
|
|
|
* \see num_rows
|
|
|
|
|
|
* \param resultset Curseur de la requete voulue
|
|
|
|
|
|
* \return int Nombre de lignes
|
|
|
|
|
|
*/
|
|
|
|
|
|
function affected_rows($resultset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Libere le dernier resultset utilise sur cette connexion.
|
|
|
|
|
|
* \param resultset Curseur de la requete voulue
|
|
|
|
|
|
*/
|
|
|
|
|
|
function free($resultset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
|
|
|
|
|
|
if (! is_resource($resultset)) { $resultset=$this->results; }
|
|
|
|
|
|
// Si resultset en est un, on libere la m<>moire
|
|
|
|
|
|
if (is_resource($resultset)) pg_free_result($resultset);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Defini les limites de la requete.
|
|
|
|
|
|
* \param limit nombre maximum de lignes retournees
|
|
|
|
|
|
* \param offset numero de la ligne a partir de laquelle recuperer les lignes
|
|
|
|
|
|
* \return string chaine exprimant la syntax sql de la limite
|
|
|
|
|
|
*/
|
|
|
|
|
|
function plimit($limit=0,$offset=0)
|
|
|
|
|
|
{
|
|
|
|
|
|
global $conf;
|
|
|
|
|
|
if (! $limit) $limit=$conf->liste_limit;
|
|
|
|
|
|
if ($offset > 0) return " LIMIT $offset,$limit ";
|
|
|
|
|
|
else return " LIMIT $limit ";
|
|
|
|
|
|
}
|
2006-06-24 22:58:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
2007-05-03 00:53:57 +02:00
|
|
|
|
/**
|
2009-07-03 10:22:06 +02:00
|
|
|
|
* \brief Defini le tri de la requete.
|
|
|
|
|
|
* \param sortfield liste des champ de tri
|
|
|
|
|
|
* \param sortorder ordre du tri
|
|
|
|
|
|
* \return string chaine exprimant la syntax sql de l'ordre de tri
|
|
|
|
|
|
* \TODO A mutualiser dans classe mere
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
function order($sortfield=0,$sortorder=0)
|
|
|
|
|
|
{
|
2007-05-03 00:53:57 +02:00
|
|
|
|
if ($sortfield)
|
|
|
|
|
|
{
|
|
|
|
|
|
$return='';
|
2009-10-20 15:14:44 +02:00
|
|
|
|
$fields=explode(',',$sortfield);
|
2007-05-03 00:53:57 +02:00
|
|
|
|
foreach($fields as $val)
|
|
|
|
|
|
{
|
2008-04-21 13:52:30 +02:00
|
|
|
|
if (! $return) $return.=' ORDER BY ';
|
2007-05-03 00:53:57 +02:00
|
|
|
|
else $return.=',';
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2007-05-03 00:53:57 +02:00
|
|
|
|
$return.=$val;
|
|
|
|
|
|
if ($sortorder) $return.=' '.$sortorder;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
2009-10-22 19:15:09 +02:00
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
2008-04-21 15:17:36 +02:00
|
|
|
|
/**
|
2009-07-03 10:22:06 +02:00
|
|
|
|
* \brief Escape a string to insert data.
|
|
|
|
|
|
* \param stringtoencode String to escape
|
|
|
|
|
|
* \return string String escaped
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
function escape($stringtoencode)
|
2008-04-21 15:17:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
return addslashes($stringtoencode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Formatage (par la base de donnees) d'un champ de la base au format tms ou Date (YYYY-MM-DD HH:MM:SS)
|
|
|
|
|
|
* afin de retourner une donnee toujours au format universel date tms unix.
|
|
|
|
|
|
* Fonction a utiliser pour generer les SELECT.
|
|
|
|
|
|
* \param param Date au format text a convertir
|
|
|
|
|
|
* \return date Date au format tms.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function pdate($param)
|
|
|
|
|
|
{
|
|
|
|
|
|
return "unix_timestamp(".$param.")";
|
|
|
|
|
|
}
|
2005-09-04 03:48:09 +02:00
|
|
|
|
|
2008-12-11 22:40:21 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Convert (by PHP) a GM Timestamp date into a GM string date to insert into a date field.
|
|
|
|
|
|
* Function to use to build INSERT, UPDATE or WHERE predica
|
|
|
|
|
|
* \param param Date TMS to convert
|
|
|
|
|
|
* \return string Date in a string YYYYMMDDHHMMSS
|
|
|
|
|
|
*/
|
|
|
|
|
|
function idate($param)
|
|
|
|
|
|
{
|
2009-10-28 18:37:40 +01:00
|
|
|
|
return adodb_strftime("%Y-%m-%d %H:%M:%S",$param);
|
2008-12-11 22:40:21 +01:00
|
|
|
|
}
|
2005-07-03 13:41:12 +02:00
|
|
|
|
|
2009-01-04 23:09:02 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
|
2009-07-30 00:59:18 +02:00
|
|
|
|
* 19700101020000 -> 3600 with TZ+1
|
|
|
|
|
|
* \param string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
|
2009-01-04 23:09:02 +01:00
|
|
|
|
* \return date Date TMS
|
|
|
|
|
|
*/
|
|
|
|
|
|
function jdate($string)
|
|
|
|
|
|
{
|
2009-10-21 18:50:15 +02:00
|
|
|
|
$string=preg_replace('/([^0-9])/i','',$string);
|
2009-01-04 23:09:02 +01:00
|
|
|
|
$tmp=$string.'000000';
|
2009-02-20 23:53:15 +01:00
|
|
|
|
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4));
|
2009-01-04 23:09:02 +01:00
|
|
|
|
return $date;
|
|
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Formatage d'un if SQL
|
|
|
|
|
|
* \param test chaine test
|
|
|
|
|
|
* \param resok resultat si test egal
|
|
|
|
|
|
* \param resko resultat si test non egal
|
|
|
|
|
|
* \return string chaine formate SQL
|
|
|
|
|
|
*/
|
|
|
|
|
|
function ifsql($test,$resok,$resko)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 'IF('.$test.','.$resok.','.$resko.')';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie la derniere requete soumise par la methode query()
|
|
|
|
|
|
* \return lastquery
|
|
|
|
|
|
*/
|
|
|
|
|
|
function lastquery()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->lastquery;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie la derniere requete en erreur
|
|
|
|
|
|
* \return string lastqueryerror
|
|
|
|
|
|
*/
|
2006-03-14 21:53:51 +01:00
|
|
|
|
function lastqueryerror()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->lastqueryerror;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie le libelle derniere erreur
|
|
|
|
|
|
* \return string lasterror
|
|
|
|
|
|
*/
|
2006-11-15 02:04:38 +01:00
|
|
|
|
function lasterror()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->lasterror;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie le code derniere erreur
|
|
|
|
|
|
* \return string lasterrno
|
|
|
|
|
|
*/
|
2006-11-15 02:04:38 +01:00
|
|
|
|
function lasterrno()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->lasterrno;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie le code erreur generique de l'operation precedente.
|
|
|
|
|
|
* \return error_num (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)
|
|
|
|
|
|
*/
|
|
|
|
|
|
function errno()
|
|
|
|
|
|
{
|
2009-10-22 20:21:06 +02:00
|
|
|
|
if (! $this->connected) {
|
|
|
|
|
|
// Si il y a eu echec de connexion, $this->db n'est pas valide.
|
|
|
|
|
|
return 'DB_ERROR_FAILED_TO_CONNECT';
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// Constants to convert a MySql error code to a generic Dolibarr error code
|
|
|
|
|
|
$errorcode_map = array(
|
|
|
|
|
|
1004 => 'DB_ERROR_CANNOT_CREATE',
|
|
|
|
|
|
1005 => 'DB_ERROR_CANNOT_CREATE',
|
|
|
|
|
|
1006 => 'DB_ERROR_CANNOT_CREATE',
|
|
|
|
|
|
1007 => 'DB_ERROR_ALREADY_EXISTS',
|
|
|
|
|
|
1008 => 'DB_ERROR_CANNOT_DROP',
|
|
|
|
|
|
1025 => 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
|
|
|
|
|
|
1044 => 'DB_ERROR_ACCESSDENIED',
|
|
|
|
|
|
1046 => 'DB_ERROR_NODBSELECTED',
|
|
|
|
|
|
1048 => 'DB_ERROR_CONSTRAINT',
|
2009-10-25 18:57:23 +01:00
|
|
|
|
'42P07' => 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS',
|
2009-10-25 19:53:09 +01:00
|
|
|
|
'42703' => 'DB_ERROR_NOSUCHFIELD',
|
2009-10-22 20:21:06 +02:00
|
|
|
|
1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS',
|
2009-10-25 17:30:26 +01:00
|
|
|
|
'42710' => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
|
2009-10-25 19:53:09 +01:00
|
|
|
|
'23505' => 'DB_ERROR_RECORD_ALREADY_EXISTS',
|
2009-10-24 01:01:03 +02:00
|
|
|
|
'42704' => 'DB_ERROR_SYNTAX',
|
|
|
|
|
|
'42601' => 'DB_ERROR_SYNTAX',
|
2009-10-25 18:57:23 +01:00
|
|
|
|
'42P16' => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
|
2009-10-22 20:21:06 +02:00
|
|
|
|
1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY',
|
|
|
|
|
|
1091 => 'DB_ERROR_NOSUCHFIELD',
|
|
|
|
|
|
1100 => 'DB_ERROR_NOT_LOCKED',
|
|
|
|
|
|
1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW',
|
2009-10-25 19:53:09 +01:00
|
|
|
|
'42P01' => 'DB_ERROR_NOSUCHTABLE',
|
|
|
|
|
|
'23503' => 'DB_ERROR_NO_PARENT',
|
2009-10-22 20:21:06 +02:00
|
|
|
|
1217 => 'DB_ERROR_CHILD_EXISTS',
|
|
|
|
|
|
1451 => 'DB_ERROR_CHILD_EXISTS'
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$errorlabel=pg_last_error($this->db);
|
|
|
|
|
|
$errorcode='';
|
|
|
|
|
|
if (preg_match('/: *([0-9P]+):/',$errorlabel,$reg))
|
|
|
|
|
|
{
|
|
|
|
|
|
$errorcode=$reg[1];
|
2009-10-24 01:01:03 +02:00
|
|
|
|
if (isset($errorcode_map[$errorcode]))
|
|
|
|
|
|
{
|
|
|
|
|
|
return $errorcode_map[$errorcode];
|
|
|
|
|
|
}
|
2009-10-22 20:21:06 +02:00
|
|
|
|
}
|
2009-10-24 01:01:03 +02:00
|
|
|
|
$errno=$errorcode?$errorcode:$errorlabel;
|
2009-10-22 20:21:06 +02:00
|
|
|
|
return ($errno?'DB_ERROR_'.$errno:'0');
|
|
|
|
|
|
}
|
2009-10-25 19:53:09 +01:00
|
|
|
|
// '/(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$/' => 'DB_ERROR_NOSUCHTABLE',
|
|
|
|
|
|
// '/table [\"\'].*[\"\'] does not exist/' => 'DB_ERROR_NOSUCHTABLE',
|
|
|
|
|
|
// '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*/' => 'DB_ERROR_RECORD_ALREADY_EXISTS',
|
|
|
|
|
|
// '/divide by zero$/' => 'DB_ERROR_DIVZERO',
|
|
|
|
|
|
// '/pg_atoi: error in .*: can\'t parse /' => 'DB_ERROR_INVALID_NUMBER',
|
|
|
|
|
|
// '/ttribute [\"\'].*[\"\'] not found$|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']/' => 'DB_ERROR_NOSUCHFIELD',
|
|
|
|
|
|
// '/parser: parse error at or near \"/' => 'DB_ERROR_SYNTAX',
|
|
|
|
|
|
// '/referential integrity violation/' => 'DB_ERROR_CONSTRAINT'
|
2009-10-22 19:15:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoie le texte de l'erreur pgsql de l'operation precedente.
|
|
|
|
|
|
* \return error_text
|
|
|
|
|
|
*/
|
|
|
|
|
|
function error()
|
|
|
|
|
|
{
|
|
|
|
|
|
return pg_last_error($this->db);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-10-28 18:37:40 +01:00
|
|
|
|
* \brief Get last ID after an insert INSERT.
|
|
|
|
|
|
* \param tab Table name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
|
|
|
|
|
|
* \return int id
|
2009-10-22 19:15:09 +02:00
|
|
|
|
*/
|
2009-10-28 18:37:40 +01:00
|
|
|
|
function last_insert_id($tab,$fieldid='rowid')
|
2009-10-22 19:15:09 +02:00
|
|
|
|
{
|
2009-10-28 18:37:40 +01:00
|
|
|
|
$result = pg_query($this->db,"SELECT MAX(".$fieldid.") FROM ".$tab." ;");
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$nbre = pg_num_rows($result);
|
|
|
|
|
|
$row = pg_fetch_result($result,0,0);
|
|
|
|
|
|
return $row;
|
|
|
|
|
|
}
|
2006-11-15 02:04:38 +01:00
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Encrypt sensitive data in database
|
2009-09-07 16:03:19 +02:00
|
|
|
|
* \param fieldorvalue Field name or value to encrypt
|
2009-09-07 21:33:21 +02:00
|
|
|
|
* \param withQuotes Return string with quotes
|
2009-09-07 16:03:19 +02:00
|
|
|
|
* \return return XXX(field) or XXX('value') or field or 'value'
|
2009-07-03 10:22:06 +02:00
|
|
|
|
*/
|
2009-12-23 02:12:47 +01:00
|
|
|
|
function encrypt($fieldorvalue, $withQuotes=0)
|
2009-07-03 10:22:06 +02:00
|
|
|
|
{
|
2009-12-23 02:12:47 +01:00
|
|
|
|
global $conf;
|
2010-02-03 03:22:15 +01:00
|
|
|
|
|
2009-12-23 02:12:47 +01:00
|
|
|
|
// Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
|
|
|
|
|
|
$cryptType = ($conf->db->dolibarr_main_db_encryption?$conf->db->dolibarr_main_db_encryption:0);
|
2010-02-03 03:22:15 +01:00
|
|
|
|
|
2009-12-23 02:12:47 +01:00
|
|
|
|
//Encryption key
|
|
|
|
|
|
$cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
|
2010-02-03 03:22:15 +01:00
|
|
|
|
|
2009-09-07 16:03:19 +02:00
|
|
|
|
$return = $fieldorvalue;
|
2009-09-07 21:33:21 +02:00
|
|
|
|
return ($withQuotes?"'":"").$return.($withQuotes?"'":"");
|
2009-07-03 10:22:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2009-09-07 16:03:19 +02:00
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Decrypt sensitive data in database
|
2009-12-23 02:12:47 +01:00
|
|
|
|
* \param value Value to decrypt
|
|
|
|
|
|
* \return return Decrypted value if used
|
2009-07-03 10:22:06 +02:00
|
|
|
|
*/
|
2009-12-23 02:12:47 +01:00
|
|
|
|
function decrypt($value)
|
2009-07-03 10:22:06 +02:00
|
|
|
|
{
|
2009-12-23 02:12:47 +01:00
|
|
|
|
global $conf;
|
2010-02-03 03:22:15 +01:00
|
|
|
|
|
2009-12-23 02:12:47 +01:00
|
|
|
|
// Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
|
|
|
|
|
|
$cryptType = ($conf->db->dolibarr_main_db_encryption?$conf->db->dolibarr_main_db_encryption:0);
|
2010-02-03 03:22:15 +01:00
|
|
|
|
|
2009-12-23 02:12:47 +01:00
|
|
|
|
//Encryption key
|
|
|
|
|
|
$cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
|
2010-02-03 03:22:15 +01:00
|
|
|
|
|
2009-12-23 02:12:47 +01:00
|
|
|
|
$return = $value;
|
2009-07-03 10:22:06 +02:00
|
|
|
|
return $return;
|
|
|
|
|
|
}
|
2006-11-15 02:04:38 +01:00
|
|
|
|
|
2009-09-07 16:03:19 +02:00
|
|
|
|
|
|
|
|
|
|
// Next function are not required. Only minor features use them.
|
|
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-10-22 19:15:09 +02:00
|
|
|
|
* \brief Renvoie l'id de la connexion
|
|
|
|
|
|
* \return string Id connexion
|
|
|
|
|
|
*/
|
|
|
|
|
|
function DDLGetConnectId()
|
|
|
|
|
|
{
|
|
|
|
|
|
return '?';
|
|
|
|
|
|
}
|
2006-11-15 02:04:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
2009-01-27 00:52:37 +01:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
/**
|
2008-12-21 22:19:49 +01:00
|
|
|
|
* \brief Create a new database
|
2009-10-22 19:15:09 +02:00
|
|
|
|
* \param database Database name to create
|
2008-12-21 22:19:49 +01:00
|
|
|
|
* \param charset Charset used to store data
|
|
|
|
|
|
* \param collation Charset used to sort data
|
2009-10-22 19:15:09 +02:00
|
|
|
|
* \param owner Username of database owner
|
2008-12-21 22:19:49 +01:00
|
|
|
|
* \return resource resource defined if OK, null if KO
|
|
|
|
|
|
* \remarks Ne pas utiliser les fonctions xxx_create_db (xxx=mysql, ...) car elles sont deprecated
|
2009-10-22 19:15:09 +02:00
|
|
|
|
*/
|
|
|
|
|
|
function DDLCreateDb($database,$charset='',$collation='',$owner='')
|
|
|
|
|
|
{
|
2008-12-21 22:19:49 +01:00
|
|
|
|
if (empty($charset)) $charset=$this->forcecharset;
|
|
|
|
|
|
if (empty($collation)) $collation=$this->collation;
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$ret=$this->query('CREATE DATABASE '.$database.' OWNER '.$owner.' ENCODING \''.$charset.'\'');
|
|
|
|
|
|
return $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Liste des tables dans une database.
|
|
|
|
|
|
* \param database Nom de la database
|
|
|
|
|
|
* \return resource
|
|
|
|
|
|
*/
|
|
|
|
|
|
function DDLListTables($database)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->results = pg_query($this->db, "SHOW TABLES");
|
|
|
|
|
|
return $this->results;
|
|
|
|
|
|
}
|
2009-07-30 00:59:18 +02:00
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Liste les informations des champs d'une table.
|
|
|
|
|
|
* \param table Nom de la table
|
|
|
|
|
|
* \return array Tableau des informations des champs de la table
|
|
|
|
|
|
* TODO modifier pour postgresql
|
|
|
|
|
|
*/
|
|
|
|
|
|
function DDLInfoTable($table)
|
|
|
|
|
|
{
|
|
|
|
|
|
/*
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$infotables=array();
|
2009-07-03 10:22:06 +02:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$sql="SHOW FULL COLUMNS FROM ".$table.";";
|
2009-07-30 00:59:18 +02:00
|
|
|
|
|
2009-10-22 19:15:09 +02:00
|
|
|
|
dol_syslog($sql,LOG_DEBUG);
|
|
|
|
|
|
$result = $this->pg_query($this->db,$sql);
|
|
|
|
|
|
while($row = $this->fetch_row($result))
|
|
|
|
|
|
{
|
2009-07-03 10:22:06 +02:00
|
|
|
|
$infotables[] = $row;
|
2009-10-22 19:15:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
return $infotables;
|
|
|
|
|
|
*/
|
2009-07-03 10:22:06 +02:00
|
|
|
|
}
|
2005-09-04 03:48:09 +02:00
|
|
|
|
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2007-05-08 23:54:11 +02:00
|
|
|
|
/**
|
2009-07-03 10:22:06 +02:00
|
|
|
|
* \brief Create a user
|
|
|
|
|
|
* \param dolibarr_main_db_host Ip serveur
|
|
|
|
|
|
* \param dolibarr_main_db_user Nom user a creer
|
|
|
|
|
|
* \param dolibarr_main_db_pass Mot de passe user a creer
|
|
|
|
|
|
* \param dolibarr_main_db_name Database name where user must be granted
|
|
|
|
|
|
* \return int <0 si KO, >=0 si OK
|
|
|
|
|
|
*/
|
2008-05-16 20:56:19 +02:00
|
|
|
|
function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name)
|
2007-05-08 23:54:11 +02:00
|
|
|
|
{
|
2010-02-22 20:16:03 +01:00
|
|
|
|
$sql = "create user \"".addslashes($dolibarr_main_db_user)."\" with password '".addslashes($dolibarr_main_db_pass)."'";
|
2007-05-08 23:54:11 +02:00
|
|
|
|
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog("pgsql.lib::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
|
2007-05-08 23:54:11 +02:00
|
|
|
|
$resql=$this->query($sql);
|
|
|
|
|
|
if (! $resql)
|
|
|
|
|
|
{
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2007-05-08 23:54:11 +02:00
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-01-27 00:52:37 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Insert a new field in table
|
|
|
|
|
|
* \param table Nom de la table
|
|
|
|
|
|
* \param field_name Nom du champ a inserer
|
2009-07-03 10:22:06 +02:00
|
|
|
|
* \param field_desc Tableau associatif de description du champ a inserer[nom du parametre][valeur du parametre]
|
2009-01-27 00:52:37 +01:00
|
|
|
|
* \param field_position Optionnel ex.: "after champtruc"
|
|
|
|
|
|
* \return int <0 si KO, >0 si OK
|
|
|
|
|
|
*/
|
|
|
|
|
|
function DDLAddField($table,$field_name,$field_desc,$field_position="")
|
|
|
|
|
|
{
|
2009-07-03 10:22:06 +02:00
|
|
|
|
// cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
|
2009-01-27 00:52:37 +01:00
|
|
|
|
// ex. : $field_desc = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
|
|
|
|
|
|
$sql= "ALTER TABLE ".$table." ADD ".$field_name." ";
|
|
|
|
|
|
$sql .= $field_desc['type'];
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['value']))
|
2009-01-27 00:52:37 +01:00
|
|
|
|
$sql .= "(".$field_desc['value'].")";
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['attribute']))
|
2009-01-27 00:52:37 +01:00
|
|
|
|
$sql .= " ".$field_desc['attribute'];
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['null']))
|
2009-01-27 00:52:37 +01:00
|
|
|
|
$sql .= " ".$field_desc['null'];
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['default']))
|
|
|
|
|
|
if(preg_match("/null/i",$field_desc['default']))
|
2009-01-27 00:52:37 +01:00
|
|
|
|
$sql .= " default ".$field_desc['default'];
|
|
|
|
|
|
else
|
|
|
|
|
|
$sql .= " default '".$field_desc['default']."'";
|
2009-10-23 12:44:19 +02:00
|
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['extra']))
|
2009-01-27 00:52:37 +01:00
|
|
|
|
$sql .= " ".$field_desc['extra'];
|
|
|
|
|
|
$sql .= " ".$field_position;
|
|
|
|
|
|
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog($sql,LOG_DEBUG);
|
2009-01-27 00:52:37 +01:00
|
|
|
|
if(! $this -> query($sql))
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
else
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Drop a field in table
|
|
|
|
|
|
* \param table Nom de la table
|
|
|
|
|
|
* \param field_name Nom du champ a inserer
|
|
|
|
|
|
* \return int <0 si KO, >0 si OK
|
|
|
|
|
|
*/
|
|
|
|
|
|
function DDLDropField($table,$field_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql= "ALTER TABLE ".$table." DROP COLUMN `".$field_name."`";
|
2009-02-20 23:53:15 +01:00
|
|
|
|
dol_syslog($sql,LOG_DEBUG);
|
2009-01-27 00:52:37 +01:00
|
|
|
|
if (! $this->query($sql))
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->error=$this->lasterror();
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return charset used to store data in database
|
|
|
|
|
|
* \return string Charset
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
function getDefaultCharacterSetDatabase()
|
|
|
|
|
|
{
|
|
|
|
|
|
$resql=$this->query('SHOW SERVER_ENCODING');
|
|
|
|
|
|
$liste=$this->fetch_array($resql);
|
|
|
|
|
|
return $liste['server_encoding'];
|
2007-05-23 23:10:11 +02:00
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return list of available charset that can be used to store data in database
|
|
|
|
|
|
* \return array List of Charset
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
function getListOfCharacterSet()
|
|
|
|
|
|
{
|
|
|
|
|
|
$resql=$this->query('SHOW SERVER_ENCODING');
|
2007-08-20 15:53:28 +02:00
|
|
|
|
$liste = array();
|
2009-01-25 18:56:42 +01:00
|
|
|
|
if ($resql)
|
2009-10-22 19:15:09 +02:00
|
|
|
|
{
|
2007-08-20 15:53:28 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
|
while ($obj = $this->fetch_object($resql) )
|
|
|
|
|
|
{
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$liste[$i]['charset'] = $obj->server_encoding;
|
|
|
|
|
|
$liste[$i]['description'] = 'Default database charset';
|
2009-01-25 18:56:42 +01:00
|
|
|
|
$i++;
|
|
|
|
|
|
}
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$this->free($resql);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $liste;
|
2007-08-20 15:53:28 +02:00
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return collation used in database
|
|
|
|
|
|
* \return string Collation value
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
function getDefaultCollationDatabase()
|
|
|
|
|
|
{
|
2009-10-26 02:22:40 +01:00
|
|
|
|
$resql=$this->query('SHOW LC_COLLATE');
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$liste=$this->fetch_array($resql);
|
|
|
|
|
|
return $liste['lc_collate'];
|
2007-08-20 15:53:28 +02:00
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-07-03 10:22:06 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return list of available collation that can be used for database
|
|
|
|
|
|
* \return array Liste of Collation
|
|
|
|
|
|
*/
|
2009-10-22 19:15:09 +02:00
|
|
|
|
function getListOfCollation()
|
|
|
|
|
|
{
|
|
|
|
|
|
$resql=$this->query('SHOW LC_COLLATE');
|
2007-08-20 15:53:28 +02:00
|
|
|
|
$liste = array();
|
2009-01-25 18:56:42 +01:00
|
|
|
|
if ($resql)
|
2009-10-22 19:15:09 +02:00
|
|
|
|
{
|
2007-08-20 15:53:28 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
|
while ($obj = $this->fetch_object($resql) )
|
|
|
|
|
|
{
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$liste[$i]['collation'] = $obj->lc_collate;
|
2009-01-25 18:56:42 +01:00
|
|
|
|
$i++;
|
|
|
|
|
|
}
|
2009-10-22 19:15:09 +02:00
|
|
|
|
$this->free($resql);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $liste;
|
2007-08-20 15:53:28 +02:00
|
|
|
|
}
|
2009-01-25 18:56:42 +01:00
|
|
|
|
|
2009-11-29 20:32:16 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return full path of dump program
|
|
|
|
|
|
* \return string Full path of dump program
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getPathOfDump()
|
|
|
|
|
|
{
|
|
|
|
|
|
$fullpathofdump='/pathtopgdump/pg_dump';
|
|
|
|
|
|
|
|
|
|
|
|
$resql=$db->query('SHOW data_directory');
|
|
|
|
|
|
if ($resql)
|
|
|
|
|
|
{
|
|
|
|
|
|
$liste=$db->fetch_array($resql);
|
|
|
|
|
|
$basedir=$liste['data_directory'];
|
|
|
|
|
|
$fullpathofdump=preg_replace('/data$/','bin',$basedir).'/pg_dump';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $fullpathofdump;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return full path of restore program
|
|
|
|
|
|
* \return string Full path of restore program
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getPathOfRestore()
|
|
|
|
|
|
{
|
|
|
|
|
|
$fullpathofmysqldump='/pathtomysql/mysql';
|
|
|
|
|
|
|
|
|
|
|
|
$resql=$this->query('SHOW VARIABLES LIKE \'basedir\'');
|
|
|
|
|
|
if ($resql)
|
|
|
|
|
|
{
|
|
|
|
|
|
$liste=$this->fetch_array($resql);
|
|
|
|
|
|
$basedir=$liste['Value'];
|
|
|
|
|
|
$fullpathofmysqldump=$basedir.'bin/mysql';
|
|
|
|
|
|
}
|
|
|
|
|
|
return $fullpathofmysqldump;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2004-08-24 09:02:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
?>
|