2004-10-20 00:47:16 +02:00
< ? php
2012-09-02 22:48:52 +02:00
/* Copyright ( C ) 2001 Fabien Seisen < seisen @ linuxfr . org >
* Copyright ( C ) 2002 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2004 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2002-04-30 12:44:42 +02:00
* ( 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
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2002-04-30 12:44:42 +02:00
*/
2005-03-26 13:42:09 +01:00
/**
2011-10-24 18:40:28 +02:00
* \file htdocs / core / db / mysql . class . php
2008-10-09 19:29:32 +02:00
* \brief Class file to manage Dolibarr database access for a Mysql database
2008-09-06 01:08:07 +02:00
*/
2004-06-19 12:09:05 +02:00
2013-09-10 12:29:55 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/db/DoliDB.class.php' ;
2004-07-16 00:17:39 +02:00
2005-02-15 23:13:41 +01:00
/**
2012-12-02 15:20:45 +01:00
* Class to manage Dolibarr database access for a Mysql database
2008-09-06 01:08:07 +02:00
*/
2013-09-10 12:29:55 +02:00
class DoliDBMysql extends 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
2011-12-21 19:34:16 +01:00
public $type = 'mysql' ;
2009-10-26 02:29:00 +01:00
//! Database label
2011-12-19 21:07:55 +01:00
static $label = 'MySQL' ;
2008-10-09 19:29:32 +02:00
//! Charset used to force charset when creating database
2012-04-28 18:04:55 +02:00
var $forcecharset = 'utf8' ; // latin1, utf8. Can't be static as it may be forced with a dynamic value
2008-10-09 19:29:32 +02:00
//! Collate used to force collate when creating database
2012-04-28 18:04:55 +02:00
var $forcecollate = 'utf8_general_ci' ; // latin1_swedish_ci, utf8_general_ci. Can't be static as it may be forced with a dynamic value
2008-09-06 01:08:07 +02:00
//! Version min database
2012-01-28 16:04:48 +01:00
static $versionmin = array ( 4 , 1 , 0 );
2008-09-06 01:08:07 +02:00
//! Resultset of last request
2012-02-15 13:55:00 +01:00
private $_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 ;
2012-02-10 16:37:41 +01:00
//! >=1 if a transaction is opened, 0 otherwise
2008-09-06 01:08:07 +02:00
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 ;
/**
2011-09-03 01:09:39 +02:00
* Constructor .
* This create an opened connexion to a database server and eventually to a database
*
* @ param string $type Type of database ( mysql , pgsql ... )
* @ param string $host Address of database server
* @ param string $user Nom de l ' utilisateur autorise
* @ param string $pass Mot de passe
* @ param string $name Nom de la database
* @ param int $port Port of database server
* @ return int 1 if OK , 0 if not
2008-10-09 19:29:32 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $type , $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. " ;
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::DoliDBMysql : 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 " );
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::DoliDBMysql : 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 ();
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::DoliDBMysql : 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 ();
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::DoliDBMysql : 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
/**
2011-09-03 01:09:39 +02:00
* Convert a SQL request in Mysql syntax to native syntax
*
* @ param string $line SQL request line to convert
* @ param string $type Type of SQL order ( 'ddl' for insert , update , select , delete or 'dml' for create , alter ... )
* @ return string SQL request line converted
2010-11-15 20:08:35 +01:00
*/
2012-05-13 14:30:11 +02:00
static 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
/**
2011-09-03 01:09:39 +02:00
* Select a database
*
* @ param string $database Name of database
* @ return boolean true if OK , false if KO
2008-09-06 01:38:14 +02:00
*/
2008-04-21 15:17:36 +02:00
function select_db ( $database )
{
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::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
/**
2011-09-03 01:09:39 +02:00
* Connexion to server
*
* @ param string $host database server host
* @ param string $login login
* @ param string $passwd password
* @ param string $name name of database ( not used for mysql , used for pgsql )
* @ param string $port Port of database server
* @ return resource Database access handler
* @ see close
2008-09-25 15:02:32 +02:00
*/
2008-03-10 23:38:43 +01:00
function connect ( $host , $login , $passwd , $name , $port = 0 )
2007-09-15 21:36:18 +02:00
{
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::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
/**
2011-09-03 01:09:39 +02:00
* Return label of manager
*
* @ return string Label
2009-10-26 02:33:00 +01:00
*/
function getLabel ()
{
return $this -> label ;
}
/**
2011-09-03 01:09:39 +02:00
* Return version of database server
*
* @ return string Version string
2009-10-26 02:33:00 +01:00
*/
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
/**
2011-09-03 01:09:39 +02:00
* Return version of database server into an array
*
* @ return array Version array
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
}
2014-05-06 22:00:54 +02:00
2013-06-12 11:59:55 +02:00
/**
* Return version of database client driver
*
* @ return string Version string
*/
function getDriverInfo ()
{
return mysqli_get_client_info ();
}
2014-05-06 22:00:54 +02:00
2008-09-06 01:08:07 +02:00
2011-03-09 16:01:39 +01:00
/**
* Close database connexion
2011-09-03 01:09:39 +02:00
*
2011-03-09 16:01:39 +01:00
* @ return boolean True if disconnect successfull , false otherwise
* @ see connect
*/
function close ()
{
if ( $this -> db )
{
2012-02-10 16:37:41 +01:00
if ( $this -> transaction_opened > 0 ) dol_syslog ( get_class ( $this ) . " ::close Closing a connection with an opened transaction depth= " . $this -> transaction_opened , LOG_ERR );
2011-03-09 16:01:39 +01:00
$this -> connected = 0 ;
return mysql_close ( $this -> db );
}
return false ;
}
2008-09-06 01:08:07 +02:00
/**
2011-09-03 01:09:39 +02:00
* Start transaction
*
* @ return int 1 if transaction successfuly opened or already opened , 0 if error
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 );
2012-12-02 15:20:45 +01:00
dol_syslog ( '' , 0 , 1 );
2007-11-18 23:10:19 +01:00
}
return $ret ;
}
else
{
$this -> transaction_opened ++ ;
2012-12-02 15:20:45 +01:00
dol_syslog ( '' , 0 , 1 );
2007-11-18 23:10:19 +01:00
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
2011-09-03 01:09:39 +02:00
*
2012-01-04 14:35:41 +01:00
* @ param string $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
{
2012-12-02 15:20:45 +01:00
dol_syslog ( '' , 0 , - 1 );
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
/**
2011-09-03 01:09:39 +02:00
* Annulation d ' une transaction et retour aux anciennes valeurs
*
2012-01-04 14:35:41 +01:00
* @ param string $log Add more log to default log line
* @ return int 1 si annulation ok ou transaction non ouverte , 0 en cas d ' erreur
2008-11-11 20:03:26 +01:00
*/
function rollback ( $log = '' )
2008-09-06 01:08:07 +02:00
{
2012-12-02 15:20:45 +01:00
dol_syslog ( '' , 0 , - 1 );
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
2011-09-03 01:09:39 +02:00
*
2012-01-04 14:35:41 +01:00
* @ param string $query SQL query string
* @ param int $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 ) .
* 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 .
* @ param string $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 ();
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::query SQL error: " . $query . " " . $this -> lasterrno , LOG_WARNING );
2007-09-15 21:36:18 +02:00
}
$this -> lastquery = $query ;
2012-02-15 13:55:00 +01:00
$this -> _results = $ret ;
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 $ret ;
}
2006-06-24 22:58:22 +02:00
2008-09-06 01:08:07 +02:00
/**
2011-09-03 01:09:39 +02:00
* Renvoie la ligne courante ( comme un objet ) pour le curseur resultset
*
2012-01-04 14:35:41 +01:00
* @ param Resultset $resultset Curseur de la requete voulue
* @ 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
2012-02-15 13:55:00 +01:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> _results ; }
2008-09-06 01:08:07 +02:00
return mysql_fetch_object ( $resultset );
}
/**
2012-01-04 14:35:41 +01:00
* Return datas as an array
*
* @ param Resultset $resultset Resultset of request
* @ return array 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
2012-02-15 13:55:00 +01:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> _results ; }
2008-09-06 01:08:07 +02:00
return mysql_fetch_array ( $resultset );
}
/**
2012-01-04 14:35:41 +01:00
* Return datas as an array
*
* @ param Resultset $resultset Resultset of request
* @ return array 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
2012-02-15 13:55:00 +01:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> _results ; }
2008-09-06 01:08:07 +02:00
return @ mysql_fetch_row ( $resultset );
}
/**
2012-01-04 14:35:41 +01:00
* Return number of lines for result of a SELECT
*
* @ param Resultset $resultset Resulset of requests
* @ return int Nb of lines
* @ see affected_rows
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
2012-02-15 13:55:00 +01:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> _results ; }
2008-09-06 01:08:07 +02:00
return mysql_num_rows ( $resultset );
}
/**
2011-09-03 01:09:39 +02:00
* Renvoie le nombre de lignes dans le resultat d ' une requete INSERT , DELETE ou UPDATE
*
2012-01-28 17:22:02 +01:00
* @ param resultset $resultset Curseur de la requete voulue
* @ return int Nombre de lignes
* @ see num_rows
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
2012-02-15 13:55:00 +01:00
if ( ! is_resource ( $resultset )) { $resultset = $this -> _results ; }
2008-09-06 01:08:07 +02:00
// mysql necessite un link de base pour cette fonction contrairement
// a pqsql qui prend un resultset
return mysql_affected_rows ( $this -> db );
}
/**
2012-01-28 17:22:02 +01:00
* Free last resultset used .
2011-09-03 01:09:39 +02:00
*
2012-01-28 17:22:02 +01:00
* @ param resultset $resultset Curseur de la requete voulue
* @ return void
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
2012-02-15 13:55:00 +01: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 );
}
/**
2013-02-25 12:51:38 +01:00
* Define limits and offset of request
*
* @ param int $limit Maximum number of lines returned ( - 1 = conf -> liste_limit , 0 = no limit )
* @ param int $offset Numero of line from where starting fetch
* @ return string String with SQL syntax to add a limit and offset
2008-09-06 01:08:07 +02:00
*/
function plimit ( $limit = 0 , $offset = 0 )
{
global $conf ;
2013-03-05 13:28:31 +01:00
if ( empty ( $limit )) return " " ;
2013-02-25 12:51:38 +01:00
if ( $limit < 0 ) $limit = $conf -> liste_limit ;
2008-09-06 01:08:07 +02:00
if ( $offset > 0 ) return " LIMIT $offset , $limit " ;
else return " LIMIT $limit " ;
}
/**
2011-03-08 12:22:08 +01:00
* Define sort criteria of request
2011-09-03 01:09:39 +02:00
*
2012-01-28 17:22:02 +01:00
* @ param string $sortfield List of sort fields
* @ param string $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
2011-11-02 20:17:55 +01:00
$return .= preg_replace ( '/[^0-9a-z_\.]/i' , '' , $val );
if ( $sortorder ) $return .= ' ' . preg_replace ( '/[^0-9a-z]/i' , '' , $sortorder );
2007-05-03 00:53:57 +02:00
}
return $return ;
}
else
{
return '' ;
}
2008-09-06 01:08:07 +02:00
}
2008-04-21 15:17:36 +02:00
/**
2012-01-28 17:22:02 +01:00
* Escape a string to insert data
2011-09-03 01:09:39 +02:00
*
2012-01-28 17:22:02 +01:00
* @ param string $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
/**
2012-01-28 17:22:02 +01: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
2011-09-03 01:09:39 +02:00
*
2012-01-28 17:22:02 +01:00
* @ param string $param Date TMS to convert
* @ return string Date in a string YYYYMMDDHHMMSS
2009-01-07 11:03:00 +01:00
*/
function idate ( $param )
{
2011-12-13 00:23:48 +01:00
return dol_print_date ( $param , " %Y%m%d%H%M%S " );
2009-01-07 11:03:00 +01:00
}
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
2011-09-03 01:09:39 +02:00
*
2011-12-12 16:26:47 +01:00
* @ param string $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
/**
2012-02-24 11:23:52 +01:00
* Format a SQL IF
2011-09-03 01:09:39 +02:00
*
2012-02-24 11:23:52 +01:00
* @ param string $test Test string ( example : 'cd.statut=0' , 'field IS NULL' )
2012-01-28 17:22:02 +01:00
* @ param string $resok resultat si test egal
* @ param string $resko resultat si test non egal
* @ return string SQL string
2008-09-06 01:08:07 +02:00
*/
function ifsql ( $test , $resok , $resko )
{
return 'IF(' . $test . ',' . $resok . ',' . $resko . ')' ;
}
/**
2012-01-28 17:22:02 +01:00
* Return last request executed with query ()
*
* @ return string Last query
2008-09-06 01:08:07 +02:00
*/
function lastquery ()
{
return $this -> lastquery ;
}
/**
2012-01-28 17:22:02 +01:00
* Return last query in error
*
* @ 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
/**
2012-01-28 17:22:02 +01:00
* Return last error label
*
* @ 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
/**
2012-01-28 17:22:02 +01:00
* Return last error code
*
* @ 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
/**
2012-01-28 17:22:02 +01:00
* Return generic error code of last operation .
*
* @ return string Error code ( Exemples : DB_ERROR_TABLE_ALREADY_EXISTS , DB_ERROR_RECORD_ALREADY_EXISTS ... )
2008-09-06 01:08:07 +02:00
*/
function errno ()
{
2012-01-28 17:22:02 +01:00
if ( ! $this -> connected )
{
2008-09-06 01:08:07 +02:00
// Si il y a eu echec de connexion, $this->db n'est pas valide.
return 'DB_ERROR_FAILED_TO_CONNECT' ;
}
2012-01-28 17:22:02 +01:00
else
{
2008-09-06 01:08:07 +02:00
// 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' ,
2014-05-06 22:00:54 +02:00
1022 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS' ,
2008-09-06 01:08:07 +02:00
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' ,
2012-01-28 16:04:48 +01:00
1396 => 'DB_ERROR_USER_ALREADY_EXISTS' , // When creating user already existing
2008-09-06 01:08:07 +02:00
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' );
}
}
/**
2012-01-28 17:22:02 +01:00
* Return description of last error
*
* @ return string Error text
2008-09-06 01:08:07 +02:00
*/
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 );
}
}
/**
2011-12-19 23:32:24 +01:00
* Get last ID after an insert INSERT
*
* @ param string $tab Table name concerned by insert . Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
* @ param string $fieldid Field name
* @ return int Id of row
2008-09-06 01:08:07 +02:00
*/
2011-12-19 23:32:24 +01:00
function last_insert_id ( $tab , $fieldid = 'rowid' )
2008-09-06 01:08:07 +02:00
{
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
2012-01-28 16:04:48 +01:00
*
* @ param string $fieldorvalue Field name or value to encrypt
* @ param int $withQuotes Return string with quotes
* @ return string 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)
2012-09-03 10:17:58 +02:00
$cryptType = ( ! empty ( $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
2012-09-03 10:17:58 +02:00
$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
/**
2012-01-28 17:22:02 +01:00
* Decrypt sensitive data in database
*
* @ param string $value Value to decrypt
* @ return string 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)
2012-09-03 10:17:58 +02:00
$cryptType = ( ! empty ( $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
/**
2012-01-28 17:22:02 +01:00
* Return connexion ID
2012-01-28 16:04:48 +01:00
*
2012-01-28 17:22:02 +01:00
* @ return string Id connexion
2009-06-29 16:03:51 +02:00
*/
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
/**
2012-01-28 17:22:02 +01:00
* Create a new database
* 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
*
* @ param string $database Database name to create
* @ param string $charset Charset used to store data
* @ param string $collation Charset used to sort data
* @ param string $owner Username of database owner
* @ return resource resource defined if OK , null if KO
2008-10-09 19:29:32 +02:00
*/
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 ;
2012-04-30 09:33:43 +02:00
if ( empty ( $collation )) $collation = $this -> forcecollate ;
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
2012-07-22 21:50:08 +02:00
$sql = " CREATE DATABASE ` " . $this -> escape ( $database ) . " ` " ;
$sql .= " DEFAULT CHARACTER SET ` " . $this -> escape ( $charset ) . " ` DEFAULT COLLATE ` " . $this -> escape ( $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
2012-07-22 21:50:08 +02:00
$sql = " CREATE DATABASE ` " . $this -> escape ( $database ) . " ` " ;
2009-02-20 23:53:15 +01:00
dol_syslog ( $sql , LOG_DEBUG );
2012-05-06 15:28:51 +02:00
$ret = $this -> query ( $sql );
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
/**
2012-01-06 14:51:09 +01:00
* List tables into a database
*
* @ param string $database Name of database
* @ param string $table Nmae of table filter ( 'xxx%' )
* @ return resource Resource
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
/**
2012-01-06 14:51:09 +01:00
* List information of columns into a table .
*
* @ param string $table Name of table
* @ return array Tableau des informations des champs de la table
2009-07-03 10:22:06 +02:00
*/
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
/**
2011-09-12 19:08:02 +02:00
* Create a table into database
*
* @ param string $table Nom de la table
* @ param array $fields Tableau associatif [ nom champ ][ tableau des descriptions ]
* @ param string $primary_key Nom du champ qui sera la clef primaire
* @ param string $type Type de la table
* @ param array $unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur
* @ param array $fulltext_keys Tableau des Nom de champs qui seront indexes en fulltext
* @ param string $keys Tableau des champs cles noms => valeur
* @ return int < 0 if KO , >= 0 if 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');
2013-05-21 14:31:39 +02:00
$sql = " CREATE TABLE " . $table . " ( " ;
2006-08-06 01:55:10 +02:00
$i = 0 ;
foreach ( $fields as $field_name => $field_desc )
{
$sqlfields [ $i ] = $field_name . " " ;
$sqlfields [ $i ] .= $field_desc [ 'type' ];
2013-05-21 14:31:39 +02:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'value' ])) {
2013-03-29 02:09:19 +01:00
$sqlfields [ $i ] .= " ( " . $field_desc [ 'value' ] . " ) " ;
2013-05-21 14:31:39 +02:00
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'attribute' ])) {
2013-03-29 02:09:19 +01:00
$sqlfields [ $i ] .= " " . $field_desc [ 'attribute' ];
2013-05-21 14:31:39 +02:00
}
2013-03-29 02:09:19 +01:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'default' ]))
2006-08-06 01:55:10 +02:00
{
2013-05-21 14:31:39 +02:00
if (( preg_match ( " /null/i " , $field_desc [ 'default' ])) || ( preg_match ( " /CURRENT_TIMESTAMP/i " , $field_desc [ 'default' ]))) {
2013-03-29 02:09:19 +01:00
$sqlfields [ $i ] .= " default " . $field_desc [ 'default' ];
2013-05-21 14:31:39 +02:00
}
else {
2013-03-29 02:09:19 +01:00
$sqlfields [ $i ] .= " default ' " . $field_desc [ 'default' ] . " ' " ;
2013-05-21 14:31:39 +02:00
}
2006-08-06 01:55:10 +02:00
}
2013-05-21 14:31:39 +02:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'null' ])) {
2013-03-29 02:09:19 +01:00
$sqlfields [ $i ] .= " " . $field_desc [ 'null' ];
2013-05-21 14:31:39 +02:00
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'extra' ])) {
2013-03-29 02:09:19 +01:00
$sqlfields [ $i ] .= " " . $field_desc [ 'extra' ];
2013-05-21 14:31:39 +02:00
}
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 );
2013-03-26 16:49:02 +01:00
$sql .= " ) engine= " . $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
/**
2012-01-06 14:51:09 +01:00
* Return a pointer of line with description of a table or field
*
* @ param string $table Name of table
* @ param string $field Optionnel : Name of field if we want description of field
* @ return resource Resource
2009-06-29 16:03:51 +02:00
*/
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 ;
2011-06-19 20:09:41 +02:00
dol_syslog ( get_class ( $this ) . " ::DDLDescTable " . $sql , LOG_DEBUG );
2012-02-15 13:55:00 +01:00
$this -> _results = $this -> query ( $sql );
return $this -> _results ;
2008-09-06 01:08:07 +02:00
}
2006-11-15 02:04:38 +01:00
2011-06-19 20:09:41 +02:00
/**
2012-01-06 14:51:09 +01:00
* Create a new field into table
*
* @ param string $table Name of table
* @ param string $field_name Name of field to add
* @ param string $field_desc Tableau associatif de description du champ a inserer [ nom du parametre ][ valeur du parametre ]
* @ param string $field_position Optionnel ex .: " after champtruc "
* @ return int < 0 if KO , > 0 if OK
2011-06-19 20:09:41 +02:00
*/
function DDLAddField ( $table , $field_name , $field_desc , $field_position = " " )
{
// cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
// ex. : $field_desc = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
2014-03-10 23:58:16 +01:00
$sql = " ALTER TABLE " . $table . " ADD ` " . $field_name . " ` " ;
2011-06-19 20:09:41 +02:00
$sql .= $field_desc [ 'type' ];
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'value' ]))
if ( ! in_array ( $field_desc [ 'type' ], array ( 'date' , 'datetime' )))
{
$sql .= " ( " . $field_desc [ 'value' ] . " ) " ;
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'attribute' ]))
$sql .= " " . $field_desc [ 'attribute' ];
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'null' ]))
$sql .= " " . $field_desc [ 'null' ];
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'default' ]))
{
if ( preg_match ( " /null/i " , $field_desc [ 'default' ]))
$sql .= " default " . $field_desc [ 'default' ];
else
$sql .= " default ' " . $field_desc [ 'default' ] . " ' " ;
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'extra' ]))
$sql .= " " . $field_desc [ 'extra' ];
$sql .= " " . $field_position ;
2006-08-06 01:55:10 +02:00
2011-06-19 20:09:41 +02:00
dol_syslog ( get_class ( $this ) . " ::DDLAddField " . $sql , LOG_DEBUG );
if ( ! $this -> query ( $sql ))
{
return - 1 ;
}
else
{
return 1 ;
}
}
2008-09-06 01:08:07 +02:00
2011-03-08 11:33:43 +01:00
/**
* Update format of a field into a table
2012-01-06 14:51:09 +01:00
*
* @ param string $table Name of table
* @ param string $field_name Name of field to modify
* @ param string $field_desc Array with description of field format
* @ return int < 0 if KO , > 0 if OK
2011-03-08 11:33:43 +01:00
*/
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' ];
2012-11-11 15:10:01 +01:00
if ( $field_desc [ 'type' ] == 'tinyint' || $field_desc [ 'type' ] == 'int' || $field_desc [ 'type' ] == 'varchar' ) {
$sql .= " ( " . $field_desc [ 'value' ] . " ) " ;
}
2012-10-28 13:57:21 +01:00
if ( $field_desc [ 'null' ] == 'not null' || $field_desc [ 'null' ] == 'NOT NULL' ) $sql .= " NOT NULL " ;
2012-11-11 15:10:01 +01:00
2011-06-19 20:09:41 +02:00
dol_syslog ( get_class ( $this ) . " ::DDLUpdateField " . $sql , LOG_DEBUG );
2011-03-08 11:33:43 +01:00
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
/**
2012-01-06 14:51:09 +01:00
* Drop a field from table
*
* @ param string $table Name of table
* @ param string $field_name Name of field to drop
* @ return int < 0 if KO , > 0 if OK
2009-01-27 00:52:37 +01:00
*/
function DDLDropField ( $table , $field_name )
{
$sql = " ALTER TABLE " . $table . " DROP COLUMN ` " . $field_name . " ` " ;
2011-06-19 20:09:41 +02:00
dol_syslog ( get_class ( $this ) . " ::DDLDropField " . $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
/**
2012-01-06 14:51:09 +01:00
* Create a user and privileges to connect to database ( even if database does not exists yet )
*
* @ param string $dolibarr_main_db_host Ip serveur
* @ param string $dolibarr_main_db_user Nom user a creer
* @ param string $dolibarr_main_db_pass Mot de passe user a creer
* @ param string $dolibarr_main_db_name Database name where user must be granted
* @ 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
{
2012-01-28 16:04:48 +01:00
$sql = " CREATE USER ' " . $this -> escape ( $dolibarr_main_db_user ) . " ' " ;
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser " , LOG_DEBUG ); // No sql to avoid password in log
$resql = $this -> query ( $sql );
if ( ! $resql )
{
2014-05-23 16:13:00 +02:00
if ( $this -> lasterrno != 'DB_ERROR_USER_ALREADY_EXISTS' )
{
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser sql= " . $sql , LOG_ERR );
return - 1 ;
}
else
{
// If user already exists, we continue to set permissions
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser sql= " . $sql , LOG_WARNING );
}
2012-01-28 16:04:48 +01:00
}
$sql = " GRANT ALL PRIVILEGES ON " . $this -> escape ( $dolibarr_main_db_name ) . " .* TO ' " . $this -> escape ( $dolibarr_main_db_user ) . " '@' " . $this -> escape ( $dolibarr_main_db_host ) . " ' IDENTIFIED BY ' " . $this -> escape ( $dolibarr_main_db_pass ) . " ' " ;
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser " , LOG_DEBUG ); // No sql to avoid password in log
$resql = $this -> query ( $sql );
if ( ! $resql )
{
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser sql= " . $sql , LOG_ERR );
return - 1 ;
}
2007-05-08 23:54:11 +02:00
2012-01-28 16:04:48 +01:00
$sql = " FLUSH Privileges " ;
2007-05-08 23:54:11 +02:00
2012-01-28 16:04:48 +01:00
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser sql= " . $sql );
$resql = $this -> query ( $sql );
2010-01-20 00:53:13 +01:00
if ( ! $resql )
{
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::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
/**
2012-01-06 14:51:09 +01:00
* Return charset used to store data in database
*
* @ return string Charset
2008-09-06 01:08:07 +02:00
*/
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
/**
2012-01-06 14:51:09 +01:00
* Return list of available charset that can be used to store data in database
*
* @ return array List of Charset
2008-10-09 19:29:32 +02:00
*/
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 ;
}
/**
2012-01-06 14:51:09 +01:00
* Return collation used in database
*
* @ return string Collation value
2008-09-06 01:08:07 +02:00
*/
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
/**
2012-01-06 14:51:09 +01:00
* Return list of available collation that can be used for database
*
2012-01-28 17:22:02 +01:00
* @ return array List of Collation
2008-10-09 19:29:32 +02:00
*/
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
/**
2011-08-04 15:58:14 +02:00
* Return full path of dump program
2012-01-06 14:51:09 +01:00
*
2011-08-04 15:58:14 +02:00
* @ return string Full path of dump program
2009-11-29 20:32:16 +01:00
*/
function getPathOfDump ()
{
$fullpathofdump = '/pathtomysqldump/mysqldump' ;
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'basedir\'' );
if ( $resql )
{
$liste = $this -> fetch_array ( $resql );
$basedir = $liste [ 'Value' ];
2011-08-04 15:58:14 +02:00
$fullpathofdump = $basedir . ( preg_match ( '/\/$/' , $basedir ) ? '' : '/' ) . 'bin/mysqldump' ;
2009-11-29 20:32:16 +01:00
}
return $fullpathofdump ;
}
/**
2012-01-06 14:51:09 +01:00
* Return full path of restore program
*
* @ return string Full path of restore program
2009-11-29 20:32:16 +01:00
*/
function getPathOfRestore ()
{
$fullpathofimport = '/pathtomysql/mysql' ;
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'basedir\'' );
if ( $resql )
{
$liste = $this -> fetch_array ( $resql );
$basedir = $liste [ 'Value' ];
2011-08-04 15:58:14 +02:00
$fullpathofimport = $basedir . ( preg_match ( '/\/$/' , $basedir ) ? '' : '/' ) . 'bin/mysql' ;
2009-11-29 20:32:16 +01:00
}
return $fullpathofimport ;
}
2010-05-09 15:45:27 +02:00
/**
2012-01-06 14:51:09 +01:00
* Return value of server parameters
*
* @ param string $filter Filter list on a particular value
* @ return string Value for parameter
2010-05-09 15:45:27 +02:00
*/
function getServerParametersValues ( $filter = '' )
{
$result = array ();
$sql = 'SHOW VARIABLES' ;
2011-12-01 17:09:01 +01:00
if ( $filter ) $sql .= " LIKE ' " . addslashes ( $filter ) . " ' " ;
2010-05-09 15:45:27 +02:00
$resql = $this -> query ( $sql );
if ( $resql )
{
$obj = $this -> fetch_object ( $resql );
$result [ $obj -> Variable_name ] = $obj -> Value ;
}
return $result ;
}
/**
2012-01-06 14:51:09 +01:00
* Return value of server status
*
* @ param string $filter Filter list on a particular value
* @ return string Value for parameter
2010-05-09 15:45:27 +02:00
*/
2011-12-01 17:09:01 +01:00
function getServerStatusValues ( $filter = '' )
2010-05-09 15:45:27 +02:00
{
$result = array ();
$sql = 'SHOW STATUS' ;
2011-12-01 17:09:01 +01:00
if ( $filter ) $sql .= " LIKE ' " . addslashes ( $filter ) . " ' " ;
2010-05-09 15:45:27 +02:00
$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
?>