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 - 2005 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 >
2015-12-17 21:10:14 +01:00
* Copyright ( C ) 2015 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . 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
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 / mysqli . class . php
2015-05-12 17:56:01 +02:00
* \brief Class file to manage Dolibarr database access for a MySQL database
2008-09-06 01:08:07 +02:00
*/
2009-07-22 15:35:09 +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
/**
2015-05-12 17:56:01 +02:00
* Class to manage Dolibarr database access for a MySQL database using the MySQLi extension
2008-09-06 01:08:07 +02:00
*/
2013-09-10 12:29:55 +02:00
class DoliDBMysqli extends DoliDB
2004-11-07 14:26:30 +01:00
{
2015-09-17 20:38:48 +02:00
/** @var mysqli Database object */
public $db ;
2011-06-19 20:09:41 +02:00
//! Database type
2011-12-21 19:34:16 +01:00
public $type = 'mysqli' ;
2011-06-19 20:09:41 +02:00
//! Database label
2015-07-12 11:44:37 +02:00
const LABEL = 'MySQL or MariaDB' ;
2011-06-19 20:09:41 +02:00
//! Version min database
2015-12-27 17:05:56 +01:00
const VERSIONMIN = '5.0.3' ;
2015-05-12 19:51:41 +02:00
/** @var mysqli_result Resultset of last query */
2012-02-15 13:55:00 +01:00
private $_results ;
2011-06-19 20:09:41 +02:00
/**
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
2011-06-19 20:09:41 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $type , $host , $user , $pass , $name = '' , $port = 0 )
2011-06-19 20:09:41 +02:00
{
global $conf , $langs ;
2012-04-28 18:04:55 +02:00
2014-02-24 09:52:48 +01:00
// Note that having "static" property for "$forcecharset" and "$forcecollate" will make error here in strict mode, so they are not static
if ( ! empty ( $conf -> db -> character_set )) $this -> forcecharset = $conf -> db -> character_set ;
if ( ! empty ( $conf -> db -> dolibarr_main_db_collation )) $this -> forcecollate = $conf -> db -> dolibarr_main_db_collation ;
2011-06-19 20:09:41 +02:00
$this -> database_user = $user ;
2014-10-26 19:41:06 +01:00
$this -> database_host = $host ;
$this -> database_port = $port ;
2011-06-19 20:09:41 +02:00
$this -> transaction_opened = 0 ;
//print "Name DB: $host,$user,$pass,$name<br>";
2015-12-03 22:29:44 +01:00
if ( ! class_exists ( 'mysqli' ))
2011-06-19 20:09:41 +02:00
{
2015-05-12 19:01:01 +02:00
$this -> connected = false ;
$this -> ok = false ;
2011-06-19 20:09:41 +02:00
$this -> error = " Mysqli PHP functions for using Mysqli 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 ) . " ::DoliDBMysqli : Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver. " , LOG_ERR );
2011-06-19 20:09:41 +02:00
}
if ( ! $host )
{
2015-05-12 19:01:01 +02:00
$this -> connected = false ;
$this -> ok = false ;
2011-06-19 20:09:41 +02:00
$this -> error = $langs -> trans ( " ErrorWrongHostParameter " );
2015-09-17 20:38:48 +02:00
dol_syslog ( get_class ( $this ) . " ::DoliDBMysqli : Connect error, wrong host parameters " , LOG_ERR );
2011-06-19 20:09:41 +02:00
}
2015-09-17 20:38:48 +02:00
// Try server connection
// We do not try to connect to database, only to server. Connect to database is done later in constrcutor
$this -> db = $this -> connect ( $host , $user , $pass , '' , $port );
if ( $this -> db -> connect_errno ) {
$this -> connected = false ;
$this -> ok = false ;
$this -> error = $this -> db -> connect_error ;
dol_syslog ( get_class ( $this ) . " ::DoliDBMysqli Connect error: " . $this -> error , LOG_ERR );
} else {
$this -> connected = true ;
$this -> ok = true ;
}
// If server connection is ok, we try to connect to the database
2011-06-19 20:09:41 +02:00
if ( $this -> connected && $name )
{
if ( $this -> select_db ( $name ))
{
2015-05-12 19:01:01 +02:00
$this -> database_selected = true ;
2011-06-19 20:09:41 +02:00
$this -> database_name = $name ;
2015-05-12 19:01:01 +02:00
$this -> ok = true ;
2011-06-19 20:09:41 +02:00
2017-11-27 21:53:22 +01:00
// If client is old latin, we force utf8
$clientmustbe = empty ( $conf -> db -> dolibarr_main_db_character_set ) ? 'utf8' : $conf -> db -> dolibarr_main_db_character_set ;
if ( preg_match ( '/latin1/' , $clientmustbe )) $clientmustbe = 'utf8' ;
2015-12-17 21:10:14 +01:00
if ( $this -> db -> character_set_name () != $clientmustbe ) {
2017-11-27 21:53:22 +01:00
$this -> db -> set_charset ( $clientmustbe ); // This set charset, but with a bad collation
$collation = $conf -> db -> dolibarr_main_db_collation ;
if ( preg_match ( '/latin1/' , $collation )) $collation = 'utf8_unicode_ci' ;
if ( ! preg_match ( '/general/' , $collation )) $this -> db -> query ( " SET collation_connection = " . $collation );
2015-12-17 21:10:14 +01:00
}
2011-06-19 20:09:41 +02:00
}
else
{
2015-05-12 19:01:01 +02:00
$this -> database_selected = false ;
2011-06-19 20:09:41 +02:00
$this -> database_name = '' ;
2015-05-12 19:01:01 +02:00
$this -> ok = false ;
2011-06-19 20:09:41 +02:00
$this -> error = $this -> error ();
2015-09-17 20:38:48 +02:00
dol_syslog ( get_class ( $this ) . " ::DoliDBMysqli : Select_db error " . $this -> error , LOG_ERR );
2011-06-19 20:09:41 +02:00
}
}
else
{
// Pas de selection de base demandee, ok ou ko
2015-05-12 19:01:01 +02:00
$this -> database_selected = false ;
2011-06-19 20:09:41 +02:00
if ( $this -> connected )
{
2017-11-27 21:53:22 +01:00
// If client is old latin, we force utf8
2017-12-11 14:17:26 +01:00
$clientmustbe = empty ( $conf -> db -> dolibarr_main_db_character_set ) ? 'utf8' : $conf -> db -> dolibarr_main_db_character_set ;
2017-11-27 21:53:22 +01:00
if ( preg_match ( '/latin1/' , $clientmustbe )) $clientmustbe = 'utf8' ;
2015-12-17 21:10:14 +01:00
if ( $this -> db -> character_set_name () != $clientmustbe ) {
2017-11-27 21:53:22 +01:00
$this -> db -> set_charset ( $clientmustbe ); // This set utf8_general_ci
$collation = $conf -> db -> dolibarr_main_db_collation ;
if ( preg_match ( '/latin1/' , $collation )) $collation = 'utf8_unicode_ci' ;
if ( ! preg_match ( '/general/' , $collation )) $this -> db -> query ( " SET collation_connection = " . $collation );
2015-12-17 21:10:14 +01:00
}
2017-11-27 21:53:22 +01:00
}
2011-06-19 20:09:41 +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' )
2011-06-19 20:09:41 +02:00
{
return $line ;
}
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
*/
2011-06-19 20:09:41 +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 );
2015-09-17 20:38:48 +02:00
return $this -> db -> select_db ( $database );
2011-06-19 20:09:41 +02:00
}
2015-09-17 20:38:48 +02:00
/**
* Connect to server
2011-09-03 01:09:39 +02:00
*
2015-09-17 20:38:48 +02:00
* @ 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 integer $port Port of database server
* @ return mysqli Database access object
* @ see close
*/
function connect ( $host , $login , $passwd , $name , $port = 0 )
{
dol_syslog ( get_class ( $this ) . " ::connect host= $host , port= $port , login= $login , passwd=--hidden--, name= $name " , LOG_DEBUG );
2011-06-19 20:09:41 +02:00
2017-09-12 11:17:23 +02:00
// Can also be
// mysqli::init(); mysql::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0'); mysqli::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
// return mysqli::real_connect($host, $user, $pass, $db, $port);
2015-09-17 20:38:48 +02:00
return new mysqli ( $host , $login , $passwd , $name , $port );
}
2011-06-19 20:09:41 +02:00
/**
2011-09-03 01:09:39 +02:00
* Return version of database server
*
* @ return string Version string
2011-06-19 20:09:41 +02:00
*/
function getVersion ()
{
2015-12-17 21:19:05 +01:00
return $this -> db -> server_info ;
2011-06-19 20:09:41 +02:00
}
2013-06-12 11:59:55 +02:00
/**
* Return version of database client driver
*
* @ return string Version string
*/
2015-09-17 20:38:48 +02:00
function getDriverInfo ()
{
2015-12-17 21:19:05 +01:00
return $this -> db -> client_info ;
2015-09-17 20:38:48 +02:00
}
2014-02-06 20:40:01 +01:00
2011-06-19 20:09:41 +02:00
/**
2011-09-03 01:09:39 +02:00
* Close database connexion
*
2015-05-12 17:56:01 +02:00
* @ return bool True if disconnect successfull , false otherwise
2011-09-03 01:09:39 +02:00
* @ see connect
2011-06-19 20:09:41 +02:00
*/
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 );
2015-05-12 19:01:01 +02:00
$this -> connected = false ;
2015-09-17 20:38:48 +02:00
return $this -> db -> close ();
2011-06-19 20:09:41 +02:00
}
return false ;
}
/**
* 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 ... )
2015-05-12 17:56:01 +02:00
* @ return bool | mysqli_result Resultset of answer
2011-06-19 20:09:41 +02:00
*/
function query ( $query , $usesavepoint = 0 , $type = 'auto' )
{
2015-07-22 23:33:48 +02:00
global $conf ;
2011-06-19 20:09:41 +02:00
$query = trim ( $query );
2014-07-27 23:23:29 +02:00
2014-08-15 19:29:19 +02:00
if ( ! in_array ( $query , array ( 'BEGIN' , 'COMMIT' , 'ROLLBACK' ))) dol_syslog ( 'sql=' . $query , LOG_DEBUG );
2014-07-27 23:23:29 +02:00
2011-06-19 20:09:41 +02:00
if ( ! $this -> database_name )
{
// Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE)
2015-09-17 20:38:48 +02:00
$ret = $this -> db -> query ( $query );
2011-06-19 20:09:41 +02:00
}
else
{
2015-09-17 20:38:48 +02:00
$ret = $this -> db -> query ( $query );
2011-06-19 20:09:41 +02:00
}
if ( ! preg_match ( " /^COMMIT/i " , $query ) && ! preg_match ( " /^ROLLBACK/i " , $query ))
{
// Si requete utilisateur, on la sauvegarde ainsi que son resultset
if ( ! $ret )
{
$this -> lastqueryerror = $query ;
$this -> lasterror = $this -> error ();
$this -> lasterrno = $this -> errno ();
2014-06-13 01:34:39 +02:00
2015-07-22 23:33:48 +02:00
if ( $conf -> global -> SYSLOG_LEVEL < LOG_DEBUG ) dol_syslog ( get_class ( $this ) . " ::query SQL Error query: " . $query , LOG_ERR ); // Log of request was not yet done previously
2014-08-17 19:34:12 +02:00
dol_syslog ( get_class ( $this ) . " ::query SQL Error message: " . $this -> lasterrno . " " . $this -> lasterror , LOG_ERR );
2011-06-19 20:09:41 +02:00
}
$this -> lastquery = $query ;
2012-02-15 13:55:00 +01:00
$this -> _results = $ret ;
2011-06-19 20:09:41 +02:00
}
return $ret ;
}
/**
2011-09-03 01:09:39 +02:00
* Renvoie la ligne courante ( comme un objet ) pour le curseur resultset
*
2015-05-12 17:56:01 +02:00
* @ param mysqli_result $resultset Curseur de la requete voulue
* @ return object | null Object result line or null if KO or end of cursor
2011-06-19 20:09:41 +02:00
*/
function fetch_object ( $resultset )
{
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
2012-02-15 13:55:00 +01:00
if ( ! is_object ( $resultset )) { $resultset = $this -> _results ; }
2015-09-17 20:38:48 +02:00
return $resultset -> fetch_object ();
2011-06-19 20:09:41 +02:00
}
/**
2012-01-04 14:35:41 +01:00
* Return datas as an array
2011-09-03 01:09:39 +02:00
*
2015-05-12 17:56:01 +02:00
* @ param mysqli_result $resultset Resultset of request
* @ return array | null Array or null if KO or end of cursor
2011-06-19 20:09:41 +02:00
*/
function fetch_array ( $resultset )
{
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_object ( $resultset )) { $resultset = $this -> _results ; }
2015-09-17 20:38:48 +02:00
return $resultset -> fetch_array ();
2011-06-19 20:09:41 +02:00
}
/**
2012-01-04 14:35:41 +01:00
* Return datas as an array
2011-09-03 01:09:39 +02:00
*
2015-05-12 17:56:01 +02:00
* @ param mysqli_result $resultset Resultset of request
* @ return array | null | 0 Array or null if KO or end of cursor or 0 if resultset is bool
2011-06-19 20:09:41 +02:00
*/
function fetch_row ( $resultset )
{
2010-11-15 20:08:35 +01:00
// If resultset not provided, we take the last used by connexion
2011-06-19 20:09:41 +02:00
if ( ! is_bool ( $resultset ))
{
2012-02-15 13:55:00 +01:00
if ( ! is_object ( $resultset )) { $resultset = $this -> _results ; }
2015-09-17 20:38:48 +02:00
return $resultset -> fetch_row ();
2011-06-19 20:09:41 +02:00
}
else
{
// si le curseur est un booleen on retourne la valeur 0
return 0 ;
}
}
/**
2012-01-04 14:35:41 +01:00
* Return number of lines for result of a SELECT
2011-09-03 01:09:39 +02:00
*
2015-05-12 17:56:01 +02:00
* @ param mysqli_result $resultset Resulset of requests
* @ return int Nb of lines
2012-01-04 14:35:41 +01:00
* @ see affected_rows
2011-06-19 20:09:41 +02:00
*/
function num_rows ( $resultset )
{
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_object ( $resultset )) { $resultset = $this -> _results ; }
2015-09-17 20:38:48 +02:00
return $resultset -> num_rows ;
2011-06-19 20:09:41 +02:00
}
/**
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
*
2015-05-12 17:56:01 +02:00
* @ param mysqli_result $resultset Curseur de la requete voulue
* @ return int Nombre de lignes
2012-01-28 17:22:02 +01:00
* @ see num_rows
2011-06-19 20:09:41 +02:00
*/
function affected_rows ( $resultset )
{
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_object ( $resultset )) { $resultset = $this -> _results ; }
2011-06-19 20:09:41 +02:00
// mysql necessite un link de base pour cette fonction contrairement
// a pqsql qui prend un resultset
2015-09-17 20:38:48 +02:00
return $this -> db -> affected_rows ;
2011-06-19 20:09:41 +02:00
}
/**
2011-09-03 01:09:39 +02:00
* Libere le dernier resultset utilise sur cette connexion
*
2015-05-12 17:56:01 +02:00
* @ param mysqli_result $resultset Curseur de la requete voulue
2012-01-28 17:22:02 +01:00
* @ return void
2011-06-19 20:09:41 +02:00
*/
2015-05-12 17:56:01 +02:00
function free ( $resultset = null )
2011-06-19 20:09:41 +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_object ( $resultset )) { $resultset = $this -> _results ; }
2011-06-19 20:09:41 +02:00
// Si resultset en est un, on libere la memoire
2015-09-17 20:38:48 +02:00
if ( is_object ( $resultset )) $resultset -> free_result ();
2011-06-19 20:09:41 +02:00
}
/**
2011-09-03 01:09:39 +02:00
* Escape a string to insert data
*
2012-01-28 17:22:02 +01:00
* @ param string $stringtoencode String to escape
* @ return string String escaped
2011-06-19 20:09:41 +02:00
*/
function escape ( $stringtoencode )
{
2015-12-17 21:53:09 +01:00
return $this -> db -> real_escape_string ( $stringtoencode );
2011-06-19 20:09:41 +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 ... )
2011-06-19 20:09:41 +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' ;
2015-05-12 17:56:01 +02:00
} else {
2011-06-19 20:09:41 +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' ,
2011-06-19 20:09:41 +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' ,
2016-06-22 09:53:02 +02:00
1215 => 'DB_ERROR_CANNOT_ADD_FOREIGN_KEY_CONSTRAINT' ,
2011-06-19 20:09:41 +02:00
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
2011-06-19 20:09:41 +02:00
1451 => 'DB_ERROR_CHILD_EXISTS'
);
2015-09-17 20:38:48 +02:00
if ( isset ( $errorcode_map [ $this -> db -> errno ])) {
return $errorcode_map [ $this -> db -> errno ];
2011-06-19 20:09:41 +02:00
}
2015-09-17 20:38:48 +02:00
$errno = $this -> db -> errno ;
2011-06-19 20:09:41 +02:00
return ( $errno ? 'DB_ERROR_' . $errno : '0' );
}
}
/**
2012-01-28 17:22:02 +01:00
* Return description of last error
*
* @ return string Error text
2011-06-19 20:09:41 +02:00
*/
function error ()
{
if ( ! $this -> connected ) {
// Si il y a eu echec de connexion, $this->db n'est pas valide pour mysqli_error.
return 'Not connected. Check setup parameters in conf/conf.php file and your mysql client and server versions' ;
}
else {
2015-09-17 20:38:48 +02:00
return $this -> db -> error ;
2011-06-19 20:09:41 +02:00
}
}
/**
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
2015-05-12 17:56:01 +02:00
* @ return int | string Id of row
2011-06-19 20:09:41 +02:00
*/
2011-12-19 23:32:24 +01:00
function last_insert_id ( $tab , $fieldid = 'rowid' )
2011-06-19 20:09:41 +02:00
{
2015-09-17 20:38:48 +02:00
return $this -> db -> insert_id ;
2011-06-19 20:09:41 +02:00
}
/**
* Encrypt sensitive data in database
* Warning : This function includes the escape , so it must use direct value
2011-09-12 19:08:02 +02:00
*
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'
2011-06-19 20:09:41 +02:00
*
*/
function encrypt ( $fieldorvalue , $withQuotes = 0 )
{
global $conf ;
// 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 );
2011-06-19 20:09:41 +02: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 : '' );
2011-06-19 20:09:41 +02:00
$return = ( $withQuotes ? " ' " : " " ) . $this -> escape ( $fieldorvalue ) . ( $withQuotes ? " ' " : " " );
if ( $cryptType && ! empty ( $cryptKey ))
{
if ( $cryptType == 2 )
{
$return = 'AES_ENCRYPT(' . $return . ',\'' . $cryptKey . '\')' ;
}
else if ( $cryptType == 1 )
{
$return = 'DES_ENCRYPT(' . $return . ',\'' . $cryptKey . '\')' ;
}
}
return $return ;
}
/**
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
2011-06-19 20:09:41 +02:00
*/
function decrypt ( $value )
{
global $conf ;
// 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 );
2011-06-19 20:09:41 +02:00
//Encryption key
$cryptKey = ( ! empty ( $conf -> db -> dolibarr_main_db_cryptkey ) ? $conf -> db -> dolibarr_main_db_cryptkey : '' );
$return = $value ;
if ( $cryptType && ! empty ( $cryptKey ))
{
if ( $cryptType == 2 )
{
$return = 'AES_DECRYPT(' . $value . ',\'' . $cryptKey . '\')' ;
}
else if ( $cryptType == 1 )
{
$return = 'DES_DECRYPT(' . $value . ',\'' . $cryptKey . '\')' ;
}
}
return $return ;
}
/**
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
2011-06-19 20:09:41 +02:00
*/
function DDLGetConnectId ()
{
$resql = $this -> query ( 'SELECT CONNECTION_ID()' );
2016-05-17 23:14:30 +02:00
if ( $resql )
{
$row = $this -> fetch_row ( $resql );
return $row [ 0 ];
}
else return '?' ;
2011-06-19 20:09:41 +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
2015-05-12 17:56:01 +02:00
* @ return bool | mysqli_result resource defined if OK , null if KO
2011-06-19 20:09:41 +02:00
*/
function DDLCreateDb ( $database , $charset = '' , $collation = '' , $owner = '' )
{
if ( empty ( $charset )) $charset = $this -> forcecharset ;
2012-04-30 09:33:43 +02:00
if ( empty ( $collation )) $collation = $this -> forcecollate ;
2011-06-19 20:09:41 +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 ) . " ` " ;
2011-06-19 20:09:41 +02:00
dol_syslog ( $sql , LOG_DEBUG );
$ret = $this -> query ( $sql );
if ( ! $ret )
{
// We try again for compatibility with Mysql < 4.1.1
2012-07-22 21:50:08 +02:00
$sql = " CREATE DATABASE ` " . $this -> escape ( $database ) . " ` " ;
2011-06-19 20:09:41 +02:00
dol_syslog ( $sql , LOG_DEBUG );
2012-07-22 16:57:15 +02:00
$ret = $this -> query ( $sql );
2011-06-19 20:09:41 +02:00
}
return $ret ;
}
/**
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%' )
2014-10-05 01:22:17 +02:00
* @ return array List of tables in an array
2011-06-19 20:09:41 +02:00
*/
function DDLListTables ( $database , $table = '' )
{
$listtables = array ();
$like = '' ;
if ( $table ) $like = " LIKE ' " . $table . " ' " ;
$sql = " SHOW TABLES FROM " . $database . " " . $like . " ; " ;
//print $sql;
$result = $this -> query ( $sql );
2016-07-23 10:25:31 +02:00
if ( $result )
2011-06-19 20:09:41 +02:00
{
2016-07-23 10:25:31 +02:00
while ( $row = $this -> fetch_row ( $result ))
{
$listtables [] = $row [ 0 ];
}
2011-06-19 20:09:41 +02:00
}
return $listtables ;
}
/**
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
2011-06-19 20:09:41 +02:00
*/
function DDLInfoTable ( $table )
{
$infotables = array ();
$sql = " SHOW FULL COLUMNS FROM " . $table . " ; " ;
dol_syslog ( $sql , LOG_DEBUG );
$result = $this -> query ( $sql );
2016-07-23 10:25:31 +02:00
if ( $result )
2011-06-19 20:09:41 +02:00
{
2016-07-23 10:25:31 +02:00
while ( $row = $this -> fetch_row ( $result ))
{
$infotables [] = $row ;
}
2011-06-19 20:09:41 +02:00
}
return $infotables ;
}
/**
2011-09-12 19:08:02 +02:00
* Create a table into database
*
2017-11-21 11:50:57 +01:00
* @ param string $table Name of table
2011-09-12 19:08:02 +02:00
* @ 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
2015-05-12 17:56:01 +02:00
* @ param array $keys Tableau des champs cles noms => valeur
2011-09-12 19:08:02 +02:00
* @ return int < 0 if KO , >= 0 if OK
2011-06-19 20:09:41 +02:00
*/
2015-05-12 17:56:01 +02:00
function DDLCreateTable ( $table , $fields , $primary_key , $type , $unique_keys = null , $fulltext_keys = null , $keys = null )
2011-06-19 20:09:41 +02:00
{
2015-05-12 17:56:01 +02:00
// FIXME: $fulltext_keys parameter is unused
2011-06-19 20:09:41 +02:00
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
// 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 . " ( " ;
2011-06-19 20:09:41 +02:00
$i = 0 ;
foreach ( $fields as $field_name => $field_desc )
{
2013-05-21 14:31:39 +02:00
$sqlfields [ $i ] = $field_name . " " ;
$sqlfields [ $i ] .= $field_desc [ 'type' ];
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'value' ])) {
$sqlfields [ $i ] .= " ( " . $field_desc [ 'value' ] . " ) " ;
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'attribute' ])) {
$sqlfields [ $i ] .= " " . $field_desc [ 'attribute' ];
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'default' ]))
{
if (( preg_match ( " /null/i " , $field_desc [ 'default' ])) || ( preg_match ( " /CURRENT_TIMESTAMP/i " , $field_desc [ 'default' ]))) {
$sqlfields [ $i ] .= " default " . $field_desc [ 'default' ];
}
else {
$sqlfields [ $i ] .= " default ' " . $field_desc [ 'default' ] . " ' " ;
}
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'null' ])) {
$sqlfields [ $i ] .= " " . $field_desc [ 'null' ];
}
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'extra' ])) {
$sqlfields [ $i ] .= " " . $field_desc [ 'extra' ];
}
2011-06-19 20:09:41 +02:00
$i ++ ;
}
if ( $primary_key != " " )
$pk = " primary key( " . $primary_key . " ) " ;
2015-05-12 17:56:01 +02:00
if ( is_array ( $unique_keys )) {
2011-06-19 20:09:41 +02:00
$i = 0 ;
foreach ( $unique_keys as $key => $value )
{
$sqluq [ $i ] = " UNIQUE KEY ' " . $key . " ' (' " . $value . " ') " ;
$i ++ ;
}
}
2015-05-12 17:56:01 +02:00
if ( is_array ( $keys ))
2011-06-19 20:09:41 +02:00
{
$i = 0 ;
foreach ( $keys as $key => $value )
{
$sqlk [ $i ] = " KEY " . $key . " ( " . $value . " ) " ;
$i ++ ;
}
}
$sql .= implode ( ',' , $sqlfields );
if ( $primary_key != " " )
$sql .= " , " . $pk ;
if ( $unique_keys != " " )
$sql .= " , " . implode ( ',' , $sqluq );
2015-05-12 17:56:01 +02:00
if ( is_array ( $keys ))
2011-06-19 20:09:41 +02:00
$sql .= " , " . implode ( ',' , $sqlk );
2013-03-26 17:45:07 +01:00
$sql .= " ) engine= " . $type ;
2011-06-19 20:09:41 +02:00
2017-11-21 11:50:57 +01:00
if ( ! $this -> query ( $sql ))
2011-06-19 20:09:41 +02:00
return - 1 ;
else
return 1 ;
}
2017-11-21 11:50:57 +01:00
/**
* Drop a table into database
*
* @ param string $table Name of table
* @ return int < 0 if KO , >= 0 if OK
*/
function DDLDropTable ( $table )
{
$sql = " DROP TABLE " . $table ;
if ( ! $this -> query ( $sql ))
return - 1 ;
else
return 1 ;
}
2011-06-19 20:09:41 +02: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
2015-05-12 17:56:01 +02:00
* @ return bool | mysqli_result Resultset x ( x -> Field , x -> Type , ... )
2011-06-19 20:09:41 +02:00
*/
function DDLDescTable ( $table , $field = " " )
{
$sql = " DESC " . $table . " " . $field ;
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 ;
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');
2017-07-04 11:18:48 +02:00
$sql = " ALTER TABLE " . $table . " ADD " . $field_name . " " ;
2011-06-19 20:09:41 +02:00
$sql .= $field_desc [ 'type' ];
2018-07-09 13:02:01 +02:00
if ( preg_match ( " /^[^ \ s]/i " , $field_desc [ 'value' ]))
{
2017-07-04 11:18:48 +02:00
if ( ! in_array ( $field_desc [ 'type' ], array ( 'date' , 'datetime' )))
{
$sql .= " ( " . $field_desc [ 'value' ] . " ) " ;
}
2018-07-09 13:02:01 +02:00
}
if ( isset ( $field_desc [ 'attribute' ]) && preg_match ( " /^[^ \ s]/i " , $field_desc [ 'attribute' ]))
{
$sql .= " " . $field_desc [ 'attribute' ];
}
if ( isset ( $field_desc [ 'null' ]) && preg_match ( " /^[^ \ s]/i " , $field_desc [ 'null' ]))
{
$sql .= " " . $field_desc [ 'null' ];
}
if ( isset ( $field_desc [ 'default' ]) && preg_match ( " /^[^ \ s]/i " , $field_desc [ 'default' ]))
2011-06-19 20:09:41 +02:00
{
if ( preg_match ( " /null/i " , $field_desc [ 'default' ]))
$sql .= " default " . $field_desc [ 'default' ];
else
$sql .= " default ' " . $field_desc [ 'default' ] . " ' " ;
}
2018-07-09 13:02:01 +02:00
if ( isset ( $field_desc [ 'extra' ]) && preg_match ( " /^[^ \ s]/i " , $field_desc [ 'extra' ]))
{
$sql .= " " . $field_desc [ 'extra' ];
}
2011-06-19 20:09:41 +02:00
$sql .= " " . $field_position ;
dol_syslog ( get_class ( $this ) . " ::DDLAddField " . $sql , LOG_DEBUG );
2018-07-09 13:02:01 +02:00
if ( $this -> query ( $sql )) {
2011-06-19 20:09:41 +02:00
return 1 ;
}
2015-05-12 17:56:01 +02:00
return - 1 ;
2011-06-19 20:09:41 +02:00
}
/**
2012-01-06 14:51:09 +01:00
* Update format of a field into a table
*
* @ 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-06-19 20:09:41 +02:00
*/
function DDLUpdateField ( $table , $field_name , $field_desc )
{
$sql = " ALTER TABLE " . $table ;
$sql .= " MODIFY COLUMN " . $field_name . " " . $field_desc [ 'type' ];
2017-09-03 14:25:54 +02:00
if ( $field_desc [ 'type' ] == 'double' || $field_desc [ 'type' ] == 'tinyint' || $field_desc [ 'type' ] == 'int' || $field_desc [ 'type' ] == 'varchar' ) {
2012-11-11 15:10:01 +01:00
$sql .= " ( " . $field_desc [ 'value' ] . " ) " ;
}
2017-09-02 15:12:21 +02:00
if ( $field_desc [ 'null' ] == 'not null' || $field_desc [ 'null' ] == 'NOT NULL' )
{
// We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL
if ( $field_desc [ 'type' ] == 'varchar' || $field_desc [ 'type' ] == 'text' )
{
$sqlbis = " UPDATE " . $table . " SET " . $field_name . " = ' " . $this -> escape ( $field_desc [ 'default' ] ? $field_desc [ 'default' ] : '' ) . " ' WHERE " . $field_name . " IS NULL " ;
$this -> query ( $sqlbis );
}
elseif ( $field_desc [ 'type' ] == 'tinyint' || $field_desc [ 'type' ] == 'int' )
{
$sqlbis = " UPDATE " . $table . " SET " . $field_name . " = " . (( int ) $this -> escape ( $field_desc [ 'default' ] ? $field_desc [ 'default' ] : 0 )) . " WHERE " . $field_name . " IS NULL " ;
$this -> query ( $sqlbis );
}
$sql .= " NOT NULL " ;
}
2017-09-03 14:25:54 +02:00
if ( $field_desc [ 'default' ] != '' )
2017-09-02 15:12:21 +02:00
{
2017-09-03 14:25:54 +02:00
if ( $field_desc [ 'type' ] == 'double' || $field_desc [ 'type' ] == 'tinyint' || $field_desc [ 'type' ] == 'int' ) $sql .= " DEFAULT " . $this -> escape ( $field_desc [ 'default' ]);
2017-09-02 15:12:21 +02:00
elseif ( $field_desc [ 'type' ] == 'text' ) $sql .= " DEFAULT ' " . $this -> escape ( $field_desc [ 'default' ]) . " ' " ; // Default not supported on text fields
}
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 );
if ( ! $this -> query ( $sql ))
return - 1 ;
else
return 1 ;
}
/**
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
2011-06-19 20:09:41 +02:00
*/
function DDLDropField ( $table , $field_name )
{
$sql = " ALTER TABLE " . $table . " DROP COLUMN ` " . $field_name . " ` " ;
dol_syslog ( get_class ( $this ) . " ::DDLDropField " . $sql , LOG_DEBUG );
2015-05-12 17:56:01 +02:00
if ( $this -> query ( $sql )) {
return 1 ;
2011-06-19 20:09:41 +02:00
}
2015-05-12 17:56:01 +02:00
$this -> error = $this -> lasterror ();
return - 1 ;
2011-06-19 20:09:41 +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 )
*
2016-12-23 00:23:57 +01:00
* @ param string $dolibarr_main_db_host Ip server or '%'
2012-01-06 14:51:09 +01:00
* @ 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
2011-06-19 20:09:41 +02:00
*/
function DDLCreateUser ( $dolibarr_main_db_host , $dolibarr_main_db_user , $dolibarr_main_db_pass , $dolibarr_main_db_name )
{
2012-01-28 16:04:48 +01:00
$sql = " CREATE USER ' " . $this -> escape ( $dolibarr_main_db_user ) . " ' " ;
2011-12-19 11:57:09 +01:00
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser " , LOG_DEBUG ); // No sql to avoid password in log
2011-06-19 20:09:41 +02:00
$resql = $this -> query ( $sql );
if ( ! $resql )
{
2014-07-27 23:23:29 +02:00
if ( $this -> lasterrno != 'DB_ERROR_USER_ALREADY_EXISTS' )
2014-05-23 16:13:00 +02:00
{
return - 1 ;
}
2014-08-17 12:43:24 +02:00
else
2014-08-17 19:34:12 +02:00
{
2014-05-23 16:13:00 +02:00
// If user already exists, we continue to set permissions
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser sql= " . $sql , LOG_WARNING );
}
2011-06-19 20:09:41 +02:00
}
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
2011-06-19 20:09:41 +02:00
$resql = $this -> query ( $sql );
if ( ! $resql )
{
return - 1 ;
}
$sql = " FLUSH Privileges " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::DDLCreateUser " , LOG_DEBUG );
2011-06-19 20:09:41 +02:00
$resql = $this -> query ( $sql );
if ( ! $resql )
{
return - 1 ;
}
return 1 ;
}
/**
2018-01-19 12:10:03 +01:00
* Return charset used to store data in current database
* Note : if we are connected to databasename , it is same result than using SELECT default_character_set_name FROM information_schema . SCHEMATA WHERE schema_name = " databasename " ;)
2012-01-06 14:51:09 +01:00
*
* @ return string Charset
2017-07-03 01:10:09 +02:00
* @ see getDefaultCollationDatabase
2011-06-19 20:09:41 +02:00
*/
function getDefaultCharacterSetDatabase ()
{
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'character_set_database\'' );
if ( ! $resql )
{
// version Mysql < 4.1.1
return $this -> forcecharset ;
}
$liste = $this -> fetch_array ( $resql );
2017-07-03 01:10:09 +02:00
$tmpval = $liste [ 'Value' ];
return $tmpval ;
2011-06-19 20:09:41 +02:00
}
/**
2012-01-06 14:51:09 +01:00
* Return list of available charset that can be used to store data in database
*
2015-05-12 17:56:01 +02:00
* @ return array | null List of Charset
2011-06-19 20:09:41 +02:00
*/
function getListOfCharacterSet ()
{
$resql = $this -> query ( 'SHOW CHARSET' );
$liste = array ();
if ( $resql )
{
$i = 0 ;
while ( $obj = $this -> fetch_object ( $resql ) )
{
$liste [ $i ][ 'charset' ] = $obj -> Charset ;
$liste [ $i ][ 'description' ] = $obj -> Description ;
$i ++ ;
}
$this -> free ( $resql );
} else {
// version Mysql < 4.1.1
return null ;
}
return $liste ;
}
/**
2016-11-07 03:18:53 +01:00
* Return collation used in current database
2012-01-06 14:51:09 +01:00
*
* @ return string Collation value
2017-07-03 01:10:09 +02:00
* @ see getDefaultCharacterSetDatabase
2011-06-19 20:09:41 +02:00
*/
function getDefaultCollationDatabase ()
{
$resql = $this -> query ( 'SHOW VARIABLES LIKE \'collation_database\'' );
if ( ! $resql )
{
// version Mysql < 4.1.1
return $this -> forcecollate ;
}
$liste = $this -> fetch_array ( $resql );
2017-07-03 01:10:09 +02:00
$tmpval = $liste [ 'Value' ];
return $tmpval ;
2011-06-19 20:09:41 +02:00
}
/**
2012-01-06 14:51:09 +01:00
* Return list of available collation that can be used for database
*
2015-05-12 17:56:01 +02:00
* @ return array | null Liste of Collation
2011-06-19 20:09:41 +02:00
*/
function getListOfCollation ()
{
$resql = $this -> query ( 'SHOW COLLATION' );
$liste = array ();
if ( $resql )
{
$i = 0 ;
while ( $obj = $this -> fetch_object ( $resql ) )
{
$liste [ $i ][ 'collation' ] = $obj -> Collation ;
$i ++ ;
}
$this -> free ( $resql );
} else {
// version Mysql < 4.1.1
return null ;
}
return $liste ;
}
/**
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
2011-06-19 20:09:41 +02: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' ;
2011-06-19 20:09:41 +02:00
}
return $fullpathofdump ;
}
/**
2011-08-04 15:58:14 +02:00
* Return full path of restore program
2012-01-06 14:51:09 +01:00
*
2011-08-04 15:58:14 +02:00
* @ return string Full path of restore program
2011-06-19 20:09:41 +02: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' ;
2011-06-19 20:09:41 +02:00
}
return $fullpathofimport ;
}
/**
2014-02-24 09:52:48 +01:00
* Return value of server parameters
2012-01-06 14:51:09 +01:00
*
2014-02-24 09:52:48 +01:00
* @ param string $filter Filter list on a particular value
* @ return array Array of key - values ( key => value )
2011-06-19 20:09:41 +02:00
*/
2011-12-01 17:09:01 +01:00
function getServerParametersValues ( $filter = '' )
2011-06-19 20:09:41 +02:00
{
$result = array ();
$sql = 'SHOW VARIABLES' ;
2014-02-25 18:25:17 +01:00
if ( $filter ) $sql .= " LIKE ' " . $this -> escape ( $filter ) . " ' " ;
2011-06-19 20:09:41 +02:00
$resql = $this -> query ( $sql );
if ( $resql )
{
2014-02-25 18:25:17 +01:00
while ( $obj = $this -> fetch_object ( $resql )) $result [ $obj -> Variable_name ] = $obj -> Value ;
2011-06-19 20:09:41 +02:00
}
return $result ;
}
/**
2014-02-24 09:52:48 +01:00
* Return value of server status ( current indicators on memory , cache ... )
2012-01-06 14:51:09 +01:00
*
2014-02-24 09:52:48 +01:00
* @ param string $filter Filter list on a particular value
* @ return array Array of key - values ( key => value )
2011-06-19 20:09:41 +02:00
*/
function getServerStatusValues ( $filter = '' )
{
$result = array ();
$sql = 'SHOW STATUS' ;
2014-02-24 09:52:48 +01:00
if ( $filter ) $sql .= " LIKE ' " . $this -> escape ( $filter ) . " ' " ;
2011-06-19 20:09:41 +02:00
$resql = $this -> query ( $sql );
if ( $resql )
{
2014-02-25 18:25:17 +01:00
while ( $obj = $this -> fetch_object ( $resql )) $result [ $obj -> Variable_name ] = $obj -> Value ;
2011-06-19 20:09:41 +02:00
}
return $result ;
}
2002-04-30 12:44:42 +02:00
}
2005-03-26 13:42:09 +01:00