2004-10-20 00:47:16 +02:00
< ? php
2004-02-05 00:09:03 +01:00
/* Copyright ( C ) 2001 Fabien Seisen < seisen @ linuxfr . org >
2007-01-29 16:24:14 +01:00
* Copyright ( C ) 2002 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2008-01-25 18:40:07 +01:00
* Copyright ( C ) 2004 - 2008 Laurent Destailleur < eldy @ users . sourceforge . net >
2006-02-03 19:17:00 +01:00
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
2009-06-27 09:08:35 +02:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis @ dolibarr . fr >
2002-04-30 12:44:42 +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-26 13:42:09 +01:00
/**
2008-09-25 15:02:32 +02:00
* \file htdocs / lib / databases / mysql . lib . php
2008-10-09 19:29:32 +02:00
* \brief Class file to manage Dolibarr database access for a Mysql database
2008-09-25 15:02:32 +02:00
* \version $Id $
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-06-19 12:09:05 +02:00
2004-07-16 00:17:39 +02:00
2005-02-15 23:13:41 +01:00
/**
2008-09-25 15:02:32 +02:00
* \class DoliDb
2008-10-09 19:29:32 +02:00
* \brief Class to manage Dolibarr database access for a Mysql database
2008-09-06 01:08:07 +02:00
*/
2005-02-14 15:31:43 +01:00
class DoliDb
2004-11-07 14:26:30 +01:00
{
2008-10-09 19:29:32 +02:00
//! Database handler
2008-09-06 01:08:07 +02:00
var $db ;
2008-10-09 19:29:32 +02:00
//! Database type
2008-09-06 01:08:07 +02:00
var $type = 'mysql' ;
2009-10-26 02:29:00 +01:00
//! Database label
var $label = 'MySQL' ;
2008-10-09 19:29:32 +02:00
//! Charset used to force charset when creating database
2009-08-26 20:59:13 +02:00
var $forcecharset = 'utf8' ; // latin1, utf8
2008-10-09 19:29:32 +02:00
//! Collate used to force collate when creating database
2009-08-26 20:59:13 +02:00
var $forcecollate = 'utf8_general_ci' ; // latin1_swedish_ci, utf8_general_ci
2008-09-06 01:08:07 +02:00
//! Version min database
var $versionmin = array ( 3 , 1 , 0 );
//! Resultset of last request
var $results ;
2008-10-09 19:29:32 +02:00
//! 1 if connected, 0 else
2008-09-06 01:08:07 +02:00
var $connected ;
2008-09-25 15:02:32 +02:00
//! 1 if database selected, 0 else
2008-09-06 01:08:07 +02:00
var $database_selected ;
2008-09-25 15:02:32 +02:00
//! Database name selected
2008-09-06 01:08:07 +02:00
var $database_name ;
//! Nom user base
var $database_user ;
//! 1 si une transaction est en cours, 0 sinon
var $transaction_opened ;
2008-09-25 15:02:32 +02:00
//! Last executed request
2008-09-06 01:08:07 +02:00
var $lastquery ;
2008-09-25 15:02:32 +02:00
//! Last failed executed request
2008-09-06 01:08:07 +02:00
var $lastqueryerror ;
//! Message erreur mysql
var $lasterror ;
//! Message erreur mysql
var $lasterrno ;
var $ok ;
var $error ;
/**
2010-11-15 20:08:35 +01:00
* Ouverture d ' une connexion vers le serveur et eventuellement 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
2008-10-09 19:29:32 +02:00
*/
2008-03-10 23:38:43 +01:00
function DoliDb ( $type = 'mysql' , $host , $user , $pass , $name = '' , $port = 0 )
2007-09-15 21:36:18 +02:00
{
2007-07-13 14:57:07 +02:00
global $conf , $langs ;
2009-01-12 20:36:40 +01:00
if ( ! empty ( $conf -> db -> character_set )) $this -> forcecharset = $conf -> db -> character_set ;
if ( ! empty ( $conf -> db -> dolibarr_main_db_collation )) $this -> forcecollate = $conf -> db -> dolibarr_main_db_collation ;
2008-01-02 23:11:50 +01:00
$this -> database_user = $user ;
2007-07-13 14:57:07 +02:00
$this -> transaction_opened = 0 ;
2008-09-06 01:08:07 +02:00
2008-10-09 19:53:37 +02:00
//print "Name DB: $host,$user,$pass,$name<br>";
2007-09-15 21:36:18 +02:00
if ( ! function_exists ( " mysql_connect " ))
{
$this -> connected = 0 ;
$this -> ok = 0 ;
2008-10-09 19:53:37 +02:00
$this -> error = " Mysql PHP functions for using MySql driver are not available in this version of PHP. Try to use another driver. " ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " DoliDB::DoliDB : Mysql PHP functions for using Mysql driver are not available in this version of PHP. Try to use another driver. " , LOG_ERR );
2007-09-15 21:36:18 +02:00
return $this -> ok ;
}
2008-09-06 01:08:07 +02:00
2007-09-15 21:36:18 +02:00
if ( ! $host )
{
$this -> connected = 0 ;
$this -> ok = 0 ;
$this -> error = $langs -> trans ( " ErrorWrongHostParameter " );
2009-02-20 23:53:15 +01:00
dol_syslog ( " DoliDB::DoliDB : Erreur Connect, wrong host parameters " , LOG_ERR );
2007-09-15 21:36:18 +02:00
return $this -> ok ;
}
// Essai connexion serveur
2008-03-10 23:38:43 +01:00
$this -> db = $this -> connect ( $host , $user , $pass , $name , $port );
2007-09-15 21:36:18 +02:00
if ( $this -> db )
{
$this -> connected = 1 ;
$this -> ok = 1 ;
}
else
{
// host, login ou password incorrect
$this -> connected = 0 ;
$this -> ok = 0 ;
$this -> error = mysql_error ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " DoliDB::DoliDB : Erreur Connect mysql_error= " . $this -> error , LOG_ERR );
2007-09-15 21:36:18 +02:00
}
2008-10-09 19:29:32 +02:00
// Si connexion serveur ok et si connexion base demandee, on essaie connexion base
2007-09-15 21:36:18 +02:00
if ( $this -> connected && $name )
{
if ( $this -> select_db ( $name ))
{
$this -> database_selected = 1 ;
$this -> database_name = $name ;
$this -> ok = 1 ;
2007-12-21 20:27:57 +01:00
2008-10-09 19:29:32 +02:00
// If client connected with different charset than Dolibarr HTML output
$clientmustbe = '' ;
2009-10-23 12:44:19 +02:00
if ( preg_match ( '/UTF-8/i' , $conf -> file -> character_set_client )) $clientmustbe = 'utf8' ;
if ( preg_match ( '/ISO-8859-1/i' , $conf -> file -> character_set_client )) $clientmustbe = 'latin1' ;
2008-10-09 19:29:32 +02:00
if ( mysql_client_encoding ( $this -> db ) != $clientmustbe )
2007-12-21 20:27:57 +01:00
{
2008-10-09 19:29:32 +02:00
$this -> query ( " SET NAMES ' " . $clientmustbe . " ' " , $this -> db );
//$this->query("SET CHARACTER SET ". $this->forcecharset);
}
2007-09-15 21:36:18 +02:00
}
else
{
$this -> database_selected = 0 ;
$this -> database_name = '' ;
$this -> ok = 0 ;
$this -> error = $this -> error ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " DoliDB::DoliDB : Erreur Select_db " . $this -> error , LOG_ERR );
2007-09-15 21:36:18 +02:00
}
}
else
{
2008-09-06 01:08:07 +02:00
// Pas de selection de base demandee, ok ou ko
2007-09-15 21:36:18 +02:00
$this -> database_selected = 0 ;
2009-01-21 16:06:34 +01:00
2008-10-09 19:29:32 +02:00
if ( $this -> connected )
{
// If client connected with different charset than Dolibarr HTML output
$clientmustbe = '' ;
2009-10-23 12:44:19 +02:00
if ( preg_match ( '/UTF-8/i' , $conf -> file -> character_set_client )) $clientmustbe = 'utf8' ;
if ( preg_match ( '/ISO-8859-1/i' , $conf -> file -> character_set_client )) $clientmustbe = 'latin1' ;
2008-10-09 19:29:32 +02:00
if ( mysql_client_encoding ( $this -> db ) != $clientmustbe )
{
$this -> query ( " SET NAMES ' " . $clientmustbe . " ' " , $this -> db );
//$this->query("SET CHARACTER SET ". $this->forcecharset);
}
}
2007-09-15 21:36:18 +02:00
}
2008-09-06 01:08:07 +02:00
2007-09-15 21:36:18 +02:00
return $this -> ok ;
2007-05-25 22:02:23 +02:00
}
2008-09-06 01:08:07 +02:00
2010-11-15 20:08:35 +01:00
/**
* Convert a SQL request in Mysql syntax to PostgreSQL syntax
* @ param line SQL request line to convert
* @ param type Type of SQL order ( 'ddl' for insert , update , select , delete or 'dml' for create , alter ... )
* @ return string SQL request line converted
*/
function convertSQLFromMysql ( $line , $type = 'ddl' )
2008-09-06 01:08:07 +02:00
{
2008-09-06 01:38:14 +02:00
return $line ;
2008-09-06 01:08:07 +02:00
}
2007-01-29 16:24:14 +01:00
2008-04-21 15:17:36 +02:00
/**
2008-09-06 01:38:14 +02:00
* \brief Selectionne une database .
* \param database Nom de la database
* \return boolean true si ok , false si ko
*/
2008-04-21 15:17:36 +02:00
function select_db ( $database )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " DoliDB::select_db database= " . $database , LOG_DEBUG );
2008-04-21 15:17:36 +02:00
return mysql_select_db ( $database , $this -> db );
}
2007-01-29 16:24:14 +01:00
2007-09-15 21:36:18 +02:00
/**
2008-10-09 19:29:32 +02:00
* \brief Connexion to server
2008-09-25 15:02:32 +02:00
* \param host database server host
* \param login login
* \param passwd password
* \param name nom de la database ( ne sert pas sous mysql , sert sous pgsql )
* \param port Port of database server
* \return resource Database access handler
* \seealso close
*/
2008-03-10 23:38:43 +01:00
function connect ( $host , $login , $passwd , $name , $port = 0 )
2007-09-15 21:36:18 +02:00
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " DoliDB::connect host= $host , port= $port , login= $login , passwd=--hidden--, name= $name " , LOG_DEBUG );
2008-03-17 02:10:31 +01:00
2008-03-10 23:38:43 +01:00
$newhost = $host ;
2008-09-06 01:08:07 +02:00
2008-08-07 19:11:05 +02:00
// With mysql, port must be in hostname
2008-03-10 23:38:43 +01:00
if ( $port ) $newhost .= ':' . $port ;
2008-03-17 02:10:31 +01:00
2008-03-10 23:38:43 +01:00
$this -> db = @ mysql_connect ( $newhost , $login , $passwd );
2008-10-09 19:29:32 +02:00
2007-09-15 21:36:18 +02:00
//print "Resultat fonction connect: ".$this->db;
return $this -> db ;
}
2008-09-06 01:08:07 +02:00
2008-04-21 15:17:36 +02:00
/**
2009-10-26 02:33:00 +01:00
* \brief Return label of manager
* \return string Label
*/
function getLabel ()
{
return $this -> label ;
}
/**
* \brief Renvoie la version du serveur
* \return string Chaine version
*/
2008-04-21 15:17:36 +02:00
function getVersion ()
{
return mysql_get_server_info ( $this -> db );
}
2008-09-06 01:08:07 +02:00
/**
2009-10-26 02:33:00 +01:00
* \brief Renvoie la version du serveur sous forme de nombre
* \return string Chaine version
2008-09-06 01:08:07 +02:00
*/
function getIntVersion ()
{
$version = $this -> getVersion ();
2009-10-20 15:14:44 +02:00
$vlist = preg_split ( '/[.-]/' , $version );
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $vlist [ 1 ]) == 1 ){
2008-09-06 01:08:07 +02:00
$vlist [ 1 ] = " 0 " . $vlist [ 1 ];
}
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $vlist [ 2 ]) == 1 ){
2008-09-06 01:08:07 +02:00
$vlist [ 2 ] = " 0 " . $vlist [ 2 ];
}
return $vlist [ 0 ] . $vlist [ 1 ] . $vlist [ 2 ];
}
/**
2009-11-27 19:26:03 +01:00
* \brief Renvoie la version du serveur dans un tableau
* \return array Tableau de chaque niveau de version
2008-10-09 19:29:32 +02:00
*/
2008-09-06 01:08:07 +02:00
function getVersionArray ()
{
2009-10-20 15:14:44 +02:00
return explode ( '.' , $this -> getVersion ());
2008-09-06 01:08:07 +02:00
}
2011-03-09 16:01:39 +01:00
/**
* Close database connexion
* @ return boolean True if disconnect successfull , false otherwise
* @ see connect
*/
function close ()
{
if ( $this -> db )
{
//dol_syslog("DoliDB::disconnect",LOG_DEBUG);
$this -> connected = 0 ;
return mysql_close ( $this -> db );
}
return false ;
}
2008-09-06 01:08:07 +02:00
/**
2009-11-27 19:26:03 +01:00
* \brief Debut d ' une transaction .
* \return int 1 si ouverture transaction ok ou deja ouverte , 0 en cas d ' erreur
2008-10-09 19:29:32 +02:00
*/
2007-11-18 23:10:19 +01:00
function begin ()
{
if ( ! $this -> transaction_opened )
{
$ret = $this -> query ( " BEGIN " );
2008-09-06 01:08:07 +02:00
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
}
return $ret ;
}
else
{
$this -> transaction_opened ++ ;
return 1 ;
}
}
2007-01-29 16:24:14 +01:00
2008-03-31 23:33:07 +02:00
/**
2010-11-15 20:08:35 +01:00
* Validate a database transaction
* @ param log Add more log to default log line
* @ return int 1 if validation is OK or transaction level no started , 0 if ERROR
2008-11-11 20:03:26 +01:00
*/
function commit ( $log = '' )
2008-03-31 23:33:07 +02:00
{
if ( $this -> transaction_opened <= 1 )
{
$ret = $this -> query ( " COMMIT " );
2008-09-06 01:08:07 +02: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 ? ' ' . $log : '' ), LOG_DEBUG );
2007-11-18 23:10:19 +01:00
}
2008-03-31 23:33:07 +02:00
return $ret ;
}
else
{
$this -> transaction_opened -- ;
return 1 ;
}
}
2008-09-06 01:08:07 +02:00
2008-03-31 23:33:07 +02:00
/**
2008-11-11 20:03:26 +01:00
* \brief Annulation d ' une transaction et retour aux anciennes valeurs
* \param log Add more log to default log line
* \return int 1 si annulation ok ou transaction non ouverte , 0 en cas d ' erreur
*/
function rollback ( $log = '' )
2008-09-06 01:08:07 +02:00
{
2008-03-31 23:33:07 +02:00
if ( $this -> transaction_opened <= 1 )
{
$ret = $this -> query ( " ROLLBACK " );
$this -> transaction_opened = 0 ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " ROLLBACK Transaction " . ( $log ? ' ' . $log : '' ), LOG_DEBUG );
2008-03-31 23:33:07 +02:00
return $ret ;
}
else
{
$this -> transaction_opened -- ;
return 1 ;
}
}
2008-09-06 01:08:07 +02:00
2008-03-31 23:33:07 +02:00
/**
2010-11-15 20:08:35 +01:00
* Execute a SQL request and return the resultset
* @ param query SQL query string
* @ param usesavepoint 0 = Default mode , 1 = Run a savepoint before and a rollbock to savepoint if error ( this allow to have some request with errors inside global transactions ) .
2010-04-30 19:31:46 +02:00
* Note that with Mysql , this parameter is not used as Myssql can already commit a transaction even if one request is in error , without using savepoints .
2010-11-15 20:08:35 +01:00
* @ param type Type of SQL order ( 'ddl' for insert , update , select , delete or 'dml' for create , alter ... )
* @ return resource Resultset of answer
2009-11-27 19:26:03 +01:00
*/
2010-12-01 22:16:28 +01:00
function query ( $query , $usesavepoint = 0 , $type = 'auto' )
2007-09-15 21:36:18 +02:00
{
$query = trim ( $query );
2008-09-06 01:08:07 +02:00
2007-09-15 21:36:18 +02:00
if ( ! $this -> database_name )
{
2008-10-09 19:53:37 +02:00
// Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE)
2007-09-15 21:36:18 +02:00
$ret = mysql_query ( $query , $this -> db );
}
else
{
2010-01-24 00:18:43 +01:00
mysql_select_db ( $this -> database_name );
$ret = mysql_query ( $query , $this -> db );
2007-09-15 21:36:18 +02:00
}
2008-09-06 01:08:07 +02:00
2009-10-23 12:44:19 +02:00
if ( ! preg_match ( " /^COMMIT/i " , $query ) && ! preg_match ( " /^ROLLBACK/i " , $query ))
2007-09-15 21:36:18 +02:00
{
// Si requete utilisateur, on la sauvegarde ainsi que son resultset
if ( ! $ret )
{
$this -> lastqueryerror = $query ;
$this -> lasterror = $this -> error ();
$this -> lasterrno = $this -> errno ();
2010-11-16 21:14:29 +01:00
dol_syslog ( " Mysql.lib::query SQL error: " . $query . " " . $this -> lasterrno , LOG_WARNING );
2007-09-15 21:36:18 +02:00
}
$this -> lastquery = $query ;
$this -> results = $ret ;
}
2008-09-06 01:08:07 +02:00
2007-09-15 21:36:18 +02:00
return $ret ;
}
2006-06-24 22:58:22 +02:00
2008-09-06 01:08:07 +02:00
/**
2009-11-27 19:26:03 +01:00
* \brief Renvoie la ligne courante ( comme un objet ) pour le curseur resultset .
* \param resultset Curseur de la requete voulue
2010-04-21 20:23:32 +02:00
* \return object Object result line or false if KO or end of cursor
2008-09-06 01:08:07 +02:00
*/
2010-08-09 17:59:55 +02:00
function fetch_object ( $resultset )
2008-09-06 01:08:07 +02:00
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2008-09-06 01:08:07 +02:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> results ; }
return mysql_fetch_object ( $resultset );
}
/**
2009-11-27 19:26:03 +01:00
* \brief Renvoie les donnees dans un tableau .
* \param resultset Curseur de la requete voulue
* \return array
2008-09-06 01:08:07 +02:00
*/
2010-08-09 17:59:55 +02:00
function fetch_array ( $resultset )
2008-09-06 01:08:07 +02:00
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2008-09-06 01:08:07 +02:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> results ; }
return mysql_fetch_array ( $resultset );
}
/**
2009-11-27 19:26:03 +01:00
* \brief Renvoie les donnees comme un tableau .
* \param resultset Curseur de la requete voulue
* \return array
2008-09-06 01:08:07 +02:00
*/
2010-08-09 18:07:23 +02:00
function fetch_row ( $resultset )
2008-09-06 01:08:07 +02:00
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2008-09-06 01:08:07 +02:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> results ; }
return @ mysql_fetch_row ( $resultset );
}
/**
2009-11-27 19:26:03 +01:00
* \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
2008-09-06 01:08:07 +02:00
*/
2010-08-09 18:07:23 +02:00
function num_rows ( $resultset )
2008-09-06 01:08:07 +02:00
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2008-09-06 01:08:07 +02:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> results ; }
return mysql_num_rows ( $resultset );
}
/**
2009-11-27 19:26:03 +01:00
* \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
2008-09-06 01:08:07 +02:00
*/
2010-08-09 18:07:24 +02:00
function affected_rows ( $resultset )
2008-09-06 01:08:07 +02:00
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2008-09-06 01:08:07 +02:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> results ; }
// mysql necessite un link de base pour cette fonction contrairement
// a pqsql qui prend un resultset
return mysql_affected_rows ( $this -> db );
}
/**
2009-11-27 19:26:03 +01:00
* \brief Libere le dernier resultset utilise sur cette connexion .
* \param resultset Curseur de la requete voulue
2008-09-06 01:08:07 +02:00
*/
function free ( $resultset = 0 )
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2008-09-06 01:08:07 +02:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> results ; }
2009-06-08 12:56:54 +02:00
// Si resultset en est un, on libere la memoire
2008-09-06 01:08:07 +02:00
if ( is_resource ( $resultset )) mysql_free_result ( $resultset );
}
/**
2009-01-21 16:06:34 +01:00
* \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 ligne
* \return string chaine exprimant la syntax sql de la limite
2008-09-06 01:08:07 +02:00
*/
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 " ;
}
/**
2011-03-08 12:22:08 +01:00
* Define sort criteria of request
* @ param sortfield List of sort fields
* @ param sortorder Sort order
* @ return string String to provide syntax of a sort sql string
* TODO Mutualized this into a mother class
2009-01-21 16:06:34 +01:00
*/
2008-09-06 01:08:07 +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 .= ',' ;
2008-09-06 01:08:07 +02:00
2007-05-03 00:53:57 +02:00
$return .= $val ;
if ( $sortorder ) $return .= ' ' . $sortorder ;
}
return $return ;
}
else
{
return '' ;
}
2008-09-06 01:08:07 +02:00
}
2008-04-21 15:17:36 +02:00
/**
2011-04-17 15:26:12 +02:00
* Escape a string to insert data .
* @ param stringtoencode String to escape
* @ return string String escaped
2008-09-06 01:08:07 +02:00
*/
function escape ( $stringtoencode )
2008-04-21 15:17:36 +02:00
{
return addslashes ( $stringtoencode );
}
2008-09-06 01:08:07 +02:00
/**
2011-04-17 15:26:12 +02:00
* Convert ( by PHP ) a GM Timestamp date into a string date with PHP server TZ 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
2009-01-07 11:03:00 +01:00
*/
function idate ( $param )
{
return adodb_strftime ( " %Y%m%d%H%M%S " , $param );
}
2009-01-21 16:06:34 +01:00
2009-01-04 23:09:02 +01:00
/**
2011-04-17 15:26:12 +02:00
* Convert ( by PHP ) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ + 1
* @ param string Date in a string ( YYYYMMDDHHMMSS , YYYYMMDD , YYYY - MM - DD HH : MM : SS )
* @ return date Date TMS
2009-01-04 23:09:02 +01:00
*/
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-21 16:06:34 +01:00
2008-09-06 01:08:07 +02:00
/**
2011-04-17 15:26:12 +02:00
* Formate a SQL IF
* @ param test chaine test
* @ param resok resultat si test egal
* @ param resko resultat si test non egal
* @ return string chaine formate SQL
2008-09-06 01:08:07 +02:00
*/
function ifsql ( $test , $resok , $resko )
{
return 'IF(' . $test . ',' . $resok . ',' . $resko . ')' ;
}
/**
2008-12-11 22:40:21 +01:00
* \brief Renvoie la derniere requete soumise par la methode query ()
* \return lastquery
2008-09-06 01:08:07 +02:00
*/
function lastquery ()
{
return $this -> lastquery ;
}
/**
2008-12-11 22:40:21 +01:00
* \brief Renvoie la derniere requete en erreur
* \return string lastqueryerror
2008-09-06 01:08:07 +02:00
*/
2006-03-14 21:53:51 +01:00
function lastqueryerror ()
{
return $this -> lastqueryerror ;
}
2008-09-06 01:08:07 +02:00
/**
2008-12-11 22:40:21 +01:00
* \brief Renvoie le libelle derniere erreur
* \return string lasterror
2008-09-06 01:08:07 +02:00
*/
2006-04-02 04:18:06 +02:00
function lasterror ()
{
return $this -> lasterror ;
}
2008-09-06 01:08:07 +02:00
/**
2008-12-11 22:40:21 +01:00
* \brief Renvoie le code derniere erreur
* \return string lasterrno
2008-09-06 01:08:07 +02:00
*/
2006-04-02 04:18:06 +02:00
function lasterrno ()
{
return $this -> lasterrno ;
}
2008-09-06 01:08:07 +02:00
/**
2008-12-11 22:40:21 +01: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 ... )
2008-09-06 01:08:07 +02:00
*/
function errno ()
{
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' ,
1050 => 'DB_ERROR_TABLE_ALREADY_EXISTS' ,
1051 => 'DB_ERROR_NOSUCHTABLE' ,
1054 => 'DB_ERROR_NOSUCHFIELD' ,
1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS' ,
1061 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS' ,
1062 => 'DB_ERROR_RECORD_ALREADY_EXISTS' ,
1064 => 'DB_ERROR_SYNTAX' ,
1068 => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS' ,
1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY' ,
1091 => 'DB_ERROR_NOSUCHFIELD' ,
1100 => 'DB_ERROR_NOT_LOCKED' ,
1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW' ,
1146 => 'DB_ERROR_NOSUCHTABLE' ,
1216 => 'DB_ERROR_NO_PARENT' ,
1217 => 'DB_ERROR_CHILD_EXISTS' ,
1451 => 'DB_ERROR_CHILD_EXISTS'
2008-10-09 19:29:32 +02:00
);
2009-01-21 16:06:34 +01:00
2008-09-06 01:49:40 +02:00
if ( isset ( $errorcode_map [ mysql_errno ( $this -> db )]))
2008-09-06 01:08:07 +02:00
{
2008-09-06 01:49:40 +02:00
return $errorcode_map [ mysql_errno ( $this -> db )];
2008-09-06 01:08:07 +02:00
}
$errno = mysql_errno ( $this -> db );
return ( $errno ? 'DB_ERROR_' . $errno : '0' );
}
}
/**
\brief Renvoie le texte de l 'erreur mysql de l' operation precedente .
\return error_text
*/
function error ()
{
if ( ! $this -> connected ) {
// Si il y a eu echec de connexion, $this->db n'est pas valide pour mysql_error.
return 'Not connected. Check setup parameters in conf/conf.php file and your mysql client and server versions' ;
}
else {
return mysql_error ( $this -> db );
}
}
/**
2009-06-27 09:08:35 +02:00
\brief Recupere l ' id genere par le dernier INSERT .
\param tab Nom de la table concernee par l ' insert . Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
2008-09-06 01:08:07 +02:00
\return int id
*/
function last_insert_id ( $tab )
{
return mysql_insert_id ( $this -> db );
}
2006-08-06 01:55:10 +02:00
2009-06-29 16:03:51 +02:00
// Next functions are not required. Only minor features use them.
//---------------------------------------------------------------
2009-06-27 09:08:35 +02:00
/**
2010-09-02 00:45:10 +02:00
* Encrypt sensitive data in database
* Warning : This function includes the escape , so it must use direct value
* @ param fieldorvalue Field name or value to encrypt
* @ param withQuotes Return string with quotes
* @ return return XXX ( field ) or XXX ( 'value' ) or field or 'value'
2009-06-27 09:08:35 +02:00
*/
2009-12-23 02:12:47 +01:00
function encrypt ( $fieldorvalue , $withQuotes = 0 )
2009-06-27 09:08:35 +02:00
{
2009-12-23 02:12:47 +01:00
global $conf ;
2010-01-20 00:53:13 +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-01-20 00:53:13 +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-01-20 00:53:13 +01:00
2010-09-02 00:45:10 +02:00
$return = ( $withQuotes ? " ' " : " " ) . $this -> escape ( $fieldorvalue ) . ( $withQuotes ? " ' " : " " );
2009-06-29 16:03:51 +02:00
2009-06-27 09:08:35 +02:00
if ( $cryptType && ! empty ( $cryptKey ))
{
if ( $cryptType == 2 )
{
2009-09-16 10:31:33 +02:00
$return = 'AES_ENCRYPT(' . $return . ',\'' . $cryptKey . '\')' ;
2009-06-27 09:08:35 +02:00
}
else if ( $cryptType == 1 )
{
2009-09-16 10:31:33 +02:00
$return = 'DES_ENCRYPT(' . $return . ',\'' . $cryptKey . '\')' ;
2009-06-27 09:08:35 +02:00
}
}
2007-01-29 16:24:14 +01:00
2009-09-15 22:56:35 +02:00
return $return ;
2009-06-27 09:08:35 +02:00
}
2009-06-29 16:03:51 +02:00
2009-06-27 09:08:35 +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-06-27 09:08:35 +02:00
*/
2009-12-23 02:12:47 +01:00
function decrypt ( $value )
2009-06-27 09:08:35 +02:00
{
2009-12-23 02:12:47 +01:00
global $conf ;
2010-01-20 00:53:13 +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-01-20 00:53:13 +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-01-20 00:53:13 +01:00
2009-12-23 02:12:47 +01:00
$return = $value ;
2009-06-29 16:03:51 +02:00
2009-06-27 09:08:35 +02:00
if ( $cryptType && ! empty ( $cryptKey ))
{
if ( $cryptType == 2 )
{
2009-12-23 02:12:47 +01:00
$return = 'AES_DECRYPT(' . $value . ',\'' . $cryptKey . '\')' ;
2009-06-27 09:08:35 +02:00
}
else if ( $cryptType == 1 )
{
2009-12-23 02:12:47 +01:00
$return = 'DES_DECRYPT(' . $value . ',\'' . $cryptKey . '\')' ;
2009-06-27 09:08:35 +02:00
}
}
2009-06-29 16:03:51 +02:00
2009-06-27 09:08:35 +02:00
return $return ;
}
2007-01-29 16:24:14 +01:00
2009-06-29 16:03:51 +02:00
2008-04-21 15:17:36 +02:00
/**
2009-06-29 16:03:51 +02:00
* \brief Renvoie l ' id de la connexion
* \return string Id connexion
*/
2008-04-21 15:17:36 +02:00
function DDLGetConnectId ()
{
$resql = $this -> query ( 'SELECT CONNECTION_ID()' );
$row = $this -> fetch_row ( $resql );
return $row [ 0 ];
}
2007-01-29 16:24:14 +01:00
2008-04-21 15:17:36 +02:00
/**
2008-10-09 19:29:32 +02:00
* \brief Create a new database
* \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
* \return resource resource defined if OK , null if KO
2008-10-09 19:29:32 +02:00
* \remarks Do not use function xxx_create_db ( xxx = mysql , ... ) as they are deprecated
* We force to create database with charset this -> forcecharset and collate this -> forcecollate
*/
2009-10-22 19:15:09 +02:00
function DDLCreateDb ( $database , $charset = '' , $collation = '' , $owner = '' )
2008-04-21 15:17:36 +02:00
{
2008-12-21 22:19:49 +01:00
if ( empty ( $charset )) $charset = $this -> forcecharset ;
if ( empty ( $collation )) $collation = $this -> collation ;
2009-01-21 16:06:34 +01:00
2008-04-21 15:17:36 +02:00
// ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
2007-05-23 23:10:11 +02:00
$sql = 'CREATE DATABASE ' . $database ;
2008-12-21 22:19:49 +01:00
$sql .= ' DEFAULT CHARACTER SET ' . $charset . ' DEFAULT COLLATE ' . $collation ;
2008-10-09 19:29:32 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( $sql , LOG_DEBUG );
2007-05-23 23:10:11 +02:00
$ret = $this -> query ( $sql );
2008-04-21 15:17:36 +02:00
if ( ! $ret )
{
2008-10-09 19:29:32 +02:00
// We try again for compatibility with Mysql < 4.1.1
2008-04-21 15:17:36 +02:00
$sql = 'CREATE DATABASE ' . $database ;
$ret = $this -> query ( $sql );
2009-02-20 23:53:15 +01:00
dol_syslog ( $sql , LOG_DEBUG );
2007-05-23 23:10:11 +02:00
}
2008-04-21 15:17:36 +02:00
return $ret ;
}
2008-09-06 01:08:07 +02:00
2007-06-15 00:09:06 +02:00
/**
2010-04-30 19:31:46 +02:00
* \brief List tables into a database .
* \param database Name of database
* \param table Filter on some tables
* \return array Array list of tables
2009-06-29 16:03:51 +02:00
*/
2008-09-06 01:08:07 +02:00
function DDLListTables ( $database , $table = '' )
{
2007-06-15 00:28:53 +02:00
$listtables = array ();
2008-09-06 01:08:07 +02:00
2007-06-15 00:09:06 +02:00
$like = '' ;
2007-06-15 00:28:53 +02:00
if ( $table ) $like = " LIKE ' " . $table . " ' " ;
$sql = " SHOW TABLES FROM " . $database . " " . $like . " ; " ;
//print $sql;
2007-06-15 00:49:55 +02:00
$result = $this -> query ( $sql );
while ( $row = $this -> fetch_row ( $result ))
2007-06-15 00:09:06 +02:00
{
2007-06-15 00:28:53 +02:00
$listtables [] = $row [ 0 ];
2007-06-15 00:09:06 +02:00
}
2007-06-15 00:28:53 +02:00
return $listtables ;
2008-09-06 01:08:07 +02:00
}
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
*/
function DDLInfoTable ( $table )
{
$infotables = array ();
$sql = " SHOW FULL COLUMNS FROM " . $table . " ; " ;
2009-07-30 00:59:18 +02:00
2009-07-03 10:22:06 +02:00
dol_syslog ( $sql , LOG_DEBUG );
$result = $this -> query ( $sql );
while ( $row = $this -> fetch_row ( $result ))
{
$infotables [] = $row ;
}
return $infotables ;
}
2008-09-06 01:08:07 +02:00
/**
2009-06-29 16:03:51 +02:00
* \brief Cree une table
* \param table Nom de la table
* \param fields Tableau associatif [ nom champ ][ tableau des descriptions ]
* \param primary_key Nom du champ qui sera la clef primaire
* \param unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur
* \param fulltext Tableau des Nom de champs qui seront indexes en fulltext
* \param key Tableau des champs cles noms => valeur
* \param type Type de la table
* \return int < 0 si KO , >= 0 si OK
2008-09-06 01:08:07 +02:00
*/
2006-11-15 02:04:38 +01:00
function DDLCreateTable ( $table , $fields , $primary_key , $type , $unique_keys = " " , $fulltext_keys = " " , $keys = " " )
2006-08-06 01:55:10 +02:00
{
2009-06-27 09:08:35 +02:00
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
2006-08-06 01:55:10 +02:00
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
$sql = " create table " . $table . " ( " ;
$i = 0 ;
foreach ( $fields as $field_name => $field_desc )
{
$sqlfields [ $i ] = $field_name . " " ;
$sqlfields [ $i ] .= $field_desc [ 'type' ];
2009-10-23 12:44:19 +02:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'value' ]))
2006-11-30 07:41:48 +01:00
$sqlfields [ $i ] .= " ( " . $field_desc [ 'value' ] . " ) " ;
2009-10-23 12:44:19 +02:00
else if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'attribute' ]))
2006-11-30 07:41:48 +01:00
$sqlfields [ $i ] .= " " . $field_desc [ 'attribute' ];
2009-10-23 12:44:19 +02:00
else if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'default' ]))
2006-08-06 01:55:10 +02:00
{
2009-10-23 12:44:19 +02:00
if ( preg_match ( " /null/i " , $field_desc [ 'default' ]))
2006-11-30 07:41:48 +01:00
$sqlfields [ $i ] .= " default " . $field_desc [ 'default' ];
2006-08-06 01:55:10 +02:00
else
2006-11-30 07:41:48 +01:00
$sqlfields [ $i ] .= " default ' " . $field_desc [ 'default' ] . " ' " ;
2006-08-06 01:55:10 +02:00
}
2009-10-23 12:44:19 +02:00
else if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'null' ]))
2006-11-30 07:41:48 +01:00
$sqlfields [ $i ] .= " " . $field_desc [ 'null' ];
2008-09-06 01:08:07 +02:00
2009-10-23 12:44:19 +02:00
else if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'extra' ]))
2006-11-30 07:41:48 +01:00
$sqlfields [ $i ] .= " " . $field_desc [ 'extra' ];
2006-08-06 01:55:10 +02:00
$i ++ ;
}
if ( $primary_key != " " )
2006-11-30 07:41:48 +01:00
$pk = " primary key( " . $primary_key . " ) " ;
2008-09-06 01:08:07 +02:00
2006-08-06 01:55:10 +02:00
if ( $unique_keys != " " )
{
$i = 0 ;
foreach ( $unique_keys as $key => $value )
{
$sqluq [ $i ] = " UNIQUE KEY ' " . $key . " ' (' " . $value . " ') " ;
$i ++ ;
}
}
if ( $keys != " " )
{
$i = 0 ;
foreach ( $keys as $key => $value )
{
$sqlk [ $i ] = " KEY " . $key . " ( " . $value . " ) " ;
$i ++ ;
}
}
$sql .= implode ( ',' , $sqlfields );
if ( $primary_key != " " )
2006-11-30 07:41:48 +01:00
$sql .= " , " . $pk ;
2006-08-06 01:55:10 +02:00
if ( $unique_keys != " " )
2006-11-30 07:41:48 +01:00
$sql .= " , " . implode ( ',' , $sqluq );
2006-08-06 01:55:10 +02:00
if ( $keys != " " )
2006-11-30 07:41:48 +01:00
$sql .= " , " . implode ( ',' , $sqlk );
2006-08-06 01:55:10 +02:00
$sql .= " ) type= " . $type ;
2008-09-06 01:08:07 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( $sql , LOG_DEBUG );
2006-08-06 01:55:10 +02:00
if ( ! $this -> query ( $sql ))
2008-09-06 01:08:07 +02:00
return - 1 ;
2006-08-06 01:55:10 +02:00
else
2008-09-06 01:08:07 +02:00
return 1 ;
2006-08-06 01:55:10 +02:00
}
2006-11-15 02:04:38 +01:00
/**
2009-06-29 16:03:51 +02:00
* \brief decrit une table dans une database .
* \param table Nom de la table
* \param field Optionnel : Nom du champ si l 'on veut la desc d' un champ
* \return resource
*/
2006-11-15 02:04:38 +01:00
function DDLDescTable ( $table , $field = " " )
2008-09-06 01:08:07 +02:00
{
2006-11-30 07:41:48 +01:00
$sql = " DESC " . $table . " " . $field ;
2009-02-20 23:53:15 +01:00
dol_syslog ( $sql , LOG_DEBUG );
2006-11-30 07:41:48 +01:00
$this -> results = $this -> query ( $sql );
2006-11-15 02:04:38 +01:00
return $this -> results ;
2008-09-06 01:08:07 +02:00
}
2006-11-15 02:04:38 +01:00
2006-08-06 01:55:10 +02: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-06-27 09:08:35 +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
*/
2006-11-15 02:04:38 +01:00
function DDLAddField ( $table , $field_name , $field_desc , $field_position = " " )
2006-08-06 01:55:10 +02:00
{
2009-06-27 09:08:35 +02:00
// cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
2006-08-06 01:55:10 +02: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' ]))
2008-09-06 01:08:07 +02:00
$sql .= " ( " . $field_desc [ 'value' ] . " ) " ;
2009-10-23 12:44:19 +02:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'attribute' ]))
2008-09-06 01:08:07 +02:00
$sql .= " " . $field_desc [ 'attribute' ];
2009-10-23 12:44:19 +02:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'null' ]))
2008-09-06 01:08:07 +02: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' ]))
2008-09-06 01:08:07 +02: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' ]))
2008-09-06 01:08:07 +02:00
$sql .= " " . $field_desc [ 'extra' ];
2006-08-06 01:55:10 +02:00
$sql .= " " . $field_position ;
2009-02-20 23:53:15 +01:00
dol_syslog ( $sql , LOG_DEBUG );
2006-08-06 01:55:10 +02:00
if ( ! $this -> query ( $sql ))
2008-09-06 01:08:07 +02:00
return - 1 ;
2006-08-06 01:55:10 +02:00
else
2008-09-06 01:08:07 +02:00
return 1 ;
2006-08-06 01:55:10 +02:00
}
2008-09-06 01:08:07 +02:00
2011-03-08 11:33:43 +01:00
/**
* Update format of a field into a table
* @ param table Name of table
* @ param field_name Name of field to modify
* @ param field_desc Array with description of field format
* @ return int < 0 if KO , > 0 if OK
*/
function DDLUpdateField ( $table , $field_name , $field_desc )
{
2011-03-08 11:48:53 +01:00
$sql = " ALTER TABLE " . $table ;
2011-03-08 11:33:43 +01:00
$sql .= " MODIFY COLUMN " . $field_name . " " . $field_desc [ 'type' ];
if ( $field_desc [ 'type' ] == 'int' || $field_desc [ 'type' ] == 'varchar' ) $sql .= " ( " . $field_desc [ 'value' ] . " ) " ;
dol_syslog ( $sql , LOG_DEBUG );
if ( ! $this -> query ( $sql ))
return - 1 ;
else
return 1 ;
}
2011-03-09 16:01:39 +01:00
2009-01-27 00:52:37 +01:00
/**
* \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 ;
}
2007-05-08 23:54:11 +02:00
/**
2009-10-22 19:15:09 +02:00
* \brief Create a user and privileges to connect to database ( even if database does not exists yet )
* \param dolibarr_main_db_host Ip server
* \param dolibarr_main_db_user Username to create
* \param dolibarr_main_db_pass Password
2009-06-29 16:03:51 +02:00
* \param dolibarr_main_db_name Database name where user must be granted
2009-10-22 19:15:09 +02:00
* \return int < 0 if KO , >= 0 if OK
2008-09-06 01:08:07 +02:00
*/
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
{
$sql = " INSERT INTO user " ;
2009-11-27 19:26:03 +01:00
$sql .= " (Host,User,password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv) " ;
2010-02-22 20:16:03 +01:00
$sql .= " VALUES (' " . addslashes ( $dolibarr_main_db_host ) . " ',' " . addslashes ( $dolibarr_main_db_user ) . " ',password(' " . addslashes ( $dolibarr_main_db_pass ) . " ') " ;
2009-11-27 19:26:03 +01:00
$sql .= " ,'Y','Y','Y','Y','Y','Y','Y','Y','Y') " ;
2008-09-06 01:08:07 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " mysql.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 )
{
2010-01-20 00:55:29 +01:00
dol_syslog ( " mysqli.lib::DDLCreateUser sql= " . $sql , LOG_ERR );
2007-05-08 23:54:11 +02:00
return - 1 ;
}
2008-09-06 01:08:07 +02:00
2007-05-08 23:54:11 +02:00
$sql = " INSERT INTO db " ;
2009-11-27 19:26:03 +01:00
$sql .= " (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv) " ;
2010-02-22 20:16:03 +01:00
$sql .= " VALUES (' " . addslashes ( $dolibarr_main_db_host ) . " ',' " . addslashes ( $dolibarr_main_db_name ) . " ',' " . addslashes ( $dolibarr_main_db_user ) . " ' " ;
2009-11-27 19:26:03 +01:00
$sql .= " ,'Y','Y','Y','Y','Y','Y','Y','Y','Y') " ;
2008-09-06 01:08:07 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " mysql.lib::DDLCreateUser sql= " . $sql , LOG_DEBUG );
2007-05-08 23:54:11 +02:00
$resql = $this -> query ( $sql );
if ( ! $resql )
{
2010-01-20 00:55:29 +01:00
dol_syslog ( " mysqli.lib::DDLCreateUser sql= " . $sql , LOG_ERR );
2007-05-08 23:54:11 +02:00
return - 1 ;
}
$sql = " FLUSH Privileges " ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " mysql.lib::DDLCreateUser sql= " . $sql , LOG_DEBUG );
2007-05-08 23:54:11 +02:00
$resql = $this -> query ( $sql );
2010-01-20 00:53:13 +01:00
if ( ! $resql )
{
2010-01-20 00:55:29 +01:00
dol_syslog ( " mysqli.lib::DDLCreateUser sql= " . $sql , LOG_ERR );
2010-01-20 00:53:13 +01:00
return - 1 ;
}
2008-09-06 01:08:07 +02:00
2007-05-08 23:54:11 +02:00
return 1 ;
}
2008-09-06 01:08:07 +02:00
2007-12-21 20:14:11 +01:00
/**
2008-09-06 01:08:07 +02:00
* \brief Return charset used to store data in database
* \return string Charset
*/
2008-03-31 23:33:07 +02:00
function getDefaultCharacterSetDatabase ()
{
2008-09-06 01:08:07 +02:00
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'character_set_database\'' );
if ( ! $resql )
{
2007-05-23 23:10:11 +02:00
// version Mysql < 4.1.1
return $this -> forcecharset ;
2008-09-06 01:08:07 +02:00
}
$liste = $this -> fetch_array ( $resql );
return $liste [ 'Value' ];
2007-05-23 23:10:11 +02:00
}
2008-09-06 01:08:07 +02:00
2008-10-09 19:29:32 +02:00
/**
* \brief Return list of available charset that can be used to store data in database
* \return array List of Charset
*/
2008-03-31 23:33:07 +02:00
function getListOfCharacterSet ()
{
$resql = $this -> query ( 'SHOW CHARSET' );
2007-05-23 23:10:11 +02:00
$liste = array ();
2008-09-06 01:08:07 +02:00
if ( $resql )
{
2007-05-23 23:10:11 +02:00
$i = 0 ;
while ( $obj = $this -> fetch_object ( $resql ) )
{
$liste [ $i ][ 'charset' ] = $obj -> Charset ;
$liste [ $i ][ 'description' ] = $obj -> Description ;
2008-09-06 01:08:07 +02:00
$i ++ ;
}
$this -> free ( $resql );
} else {
// version Mysql < 4.1.1
return null ;
}
return $liste ;
}
/**
* \brief Return collation used in database
* \return string Collation value
*/
2008-03-31 23:33:07 +02:00
function getDefaultCollationDatabase ()
{
2007-07-14 22:36:31 +02:00
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'collation_database\'' );
2008-09-06 01:08:07 +02:00
if ( ! $resql )
{
2007-05-23 23:10:11 +02:00
// version Mysql < 4.1.1
return $this -> forcecollate ;
2008-09-06 01:08:07 +02:00
}
$liste = $this -> fetch_array ( $resql );
return $liste [ 'Value' ];
2007-05-23 23:10:11 +02:00
}
2008-09-06 01:08:07 +02:00
2008-10-09 19:29:32 +02:00
/**
* \brief Return list of available collation that can be used for database
* \return array Liste of Collation
*/
function getListOfCollation ()
{
2008-09-06 01:08:07 +02:00
$resql = $this -> query ( 'SHOW COLLATION' );
2007-05-23 23:10:11 +02:00
$liste = array ();
2008-09-06 01:08:07 +02:00
if ( $resql )
{
2007-05-23 23:10:11 +02:00
$i = 0 ;
while ( $obj = $this -> fetch_object ( $resql ) )
{
$liste [ $i ][ 'collation' ] = $obj -> Collation ;
2008-09-06 01:08:07 +02:00
$i ++ ;
}
$this -> free ( $resql );
} else {
// version Mysql < 4.1.1
return null ;
}
return $liste ;
2007-05-23 23:10:11 +02: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 = '/pathtomysqldump/mysqldump' ;
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'basedir\'' );
if ( $resql )
{
$liste = $this -> fetch_array ( $resql );
$basedir = $liste [ 'Value' ];
$fullpathofdump = $basedir . 'bin/mysqldump' ;
}
return $fullpathofdump ;
}
/**
* \brief Return full path of restore program
* \return string Full path of restore program
*/
function getPathOfRestore ()
{
$fullpathofimport = '/pathtomysql/mysql' ;
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'basedir\'' );
if ( $resql )
{
$liste = $this -> fetch_array ( $resql );
$basedir = $liste [ 'Value' ];
$fullpathofimport = $basedir . 'bin/mysql' ;
}
return $fullpathofimport ;
}
2010-05-09 15:45:27 +02:00
/**
* \brief Return value of server parameters
* \param filter Filter list on a particular value
* \return string Value for parameter
*/
function getServerParametersValues ( $filter = '' )
{
$result = array ();
$sql = 'SHOW VARIABLES' ;
if ( $filter ) $sql .= " LIKE ' " . addslashes ( $key ) . " ' " ;
$resql = $this -> query ( $sql );
if ( $resql )
{
$obj = $this -> fetch_object ( $resql );
$result [ $obj -> Variable_name ] = $obj -> Value ;
}
return $result ;
}
/**
* \brief Return value of server status
* \param filter Filter list on a particular value
* \return string Value for parameter
*/
function getServerStatusValues ( $key , $filter = '' )
{
$result = array ();
$sql = 'SHOW STATUS' ;
if ( $filter ) $sql .= " LIKE ' " . addslashes ( $key ) . " ' " ;
$resql = $this -> query ( $sql );
if ( $resql )
{
$obj = $this -> fetch_object ( $resql );
$result [ $obj -> Variable_name ] = $obj -> Value ;
}
return $result ;
}
2002-04-30 12:44:42 +02:00
}
2005-03-26 13:42:09 +01:00
2002-04-30 12:44:42 +02:00
?>