dolibarr/htdocs/core/db/Database.interface.php

461 lines
14 KiB
PHP
Raw Permalink Normal View History

2014-02-21 12:38:43 +01:00
<?php
/* 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>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
2015-05-12 19:01:01 +02:00
* Copyright (C) 2014-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
2014-02-21 12:38:43 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Class to manage Dolibarr database access for an SQL database
2014-02-21 12:38:43 +01:00
*/
interface Database
{
/**
2015-04-06 11:28:06 +02:00
* Format a SQL IF
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $test Test string (example: 'cd.statut=0', 'field IS NULL')
* @param string $resok resultat si test egal
* @param string $resko resultat si test non egal
* @return string SQL string
2014-02-21 12:38:43 +01:00
*/
function ifsql($test, $resok, $resko);
/**
2015-04-06 11:28:06 +02:00
* Return datas as an array
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param resource $resultset Resultset of request
* @return array Array
2014-02-21 12:38:43 +01:00
*/
function fetch_row($resultset);
/**
2015-04-06 11:28:06 +02:00
* Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
* Function to use to build INSERT, UPDATE or WHERE predica
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param int $param Date TMS to convert
* @return string Date in a string YYYYMMDDHHMMSS
2014-02-21 12:38:43 +01:00
*/
function idate($param);
/**
2015-04-06 11:28:06 +02:00
* Return last error code
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string lasterrno
2014-02-21 12:38:43 +01:00
*/
function lasterrno();
/**
* Start transaction
*
2015-04-06 11:28:06 +02:00
* @return int 1 if transaction successfuly opened or already opened, 0 if error
2014-02-21 12:38:43 +01:00
*/
function begin();
/**
2015-04-06 11:28:06 +02: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
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @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
2014-02-21 12:38:43 +01:00
*/
function DDLCreateDb($database, $charset = '', $collation = '', $owner = '');
/**
2015-04-06 11:28:06 +02:00
* Return version of database server into an array
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return array Version array
2014-02-21 12:38:43 +01:00
*/
function getVersionArray();
/**
* Convert a SQL request in Mysql syntax to native syntax
*
2015-04-06 11:28:06 +02:00
* @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
2014-02-21 12:38:43 +01:00
*/
static function convertSQLFromMysql($line, $type = 'ddl');
/**
2015-04-06 11:28:06 +02:00
* Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param resource $resultset Curseur de la requete voulue
* @return int Nombre de lignes
* @see num_rows
2014-02-21 12:38:43 +01:00
*/
function affected_rows($resultset);
/**
2015-04-06 11:28:06 +02:00
* Return description of last error
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string Error text
2014-02-21 12:38:43 +01:00
*/
function error();
/**
* List tables into a database
*
2014-11-15 15:19:37 +01:00
* @param string $database Name of database
* @param string $table Nmae of table filter ('xxx%')
* @return array List of tables in an array
2014-02-21 12:38:43 +01:00
*/
function DDLListTables($database, $table = '');
/**
2015-04-06 11:28:06 +02:00
* Return last request executed with query()
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string Last query
2014-02-21 12:38:43 +01:00
*/
function lastquery();
/**
* Define sort criteria of request
*
2015-04-06 11:28:06 +02:00
* @param string $sortfield List of sort fields
* @param string $sortorder Sort order
* @return string String to provide syntax of a sort sql string
2014-02-21 12:38:43 +01:00
*/
2015-05-12 19:01:01 +02:00
function order($sortfield = null, $sortorder = null);
2014-02-21 12:38:43 +01:00
/**
2015-04-06 11:28:06 +02:00
* Decrypt sensitive data in database
2014-02-21 12:38:43 +01:00
*
* @param string $value Value to decrypt
2015-04-06 11:28:06 +02:00
* @return string Decrypted value if used
2014-02-21 12:38:43 +01:00
*/
function decrypt($value);
/**
* Return datas as an array
*
2015-04-06 11:28:06 +02:00
* @param resource $resultset Resultset of request
* @return array Array
2014-02-21 12:38:43 +01:00
*/
function fetch_array($resultset);
/**
2015-04-06 11:28:06 +02:00
* Return last error label
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string lasterror
2014-02-21 12:38:43 +01:00
*/
function lasterror();
/**
2015-04-06 11:28:06 +02:00
* Escape a string to insert data
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $stringtoencode String to escape
* @return string String escaped
2014-02-21 12:38:43 +01:00
*/
function escape($stringtoencode);
/**
* Get last ID after an insert INSERT
*
2015-04-06 11:28:06 +02:00
* @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
2014-02-21 12:38:43 +01:00
*/
function last_insert_id($tab, $fieldid = 'rowid');
/**
* Return full path of restore program
*
* @return string Full path of restore program
*/
function getPathOfRestore();
/**
* Annulation d'une transaction et retour aux anciennes valeurs
*
2015-04-06 11:28:06 +02: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
2014-02-21 12:38:43 +01:00
*/
function rollback($log = '');
/**
* Execute a SQL request and return the resultset
*
2015-04-06 11:28:06 +02:00
* @param string $query SQL query string
2017-03-10 15:19:15 +01:00
* @param int $usesavepoint 0=Default mode, 1=Run a savepoint before and a rollback to savepoint if error (this allow to have some request with errors inside global transactions).
2014-02-21 12:38:43 +01:00
* Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints.
* @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
2015-04-06 11:28:06 +02:00
* @return resource Resultset of answer
2014-02-21 12:38:43 +01:00
*/
function query($query, $usesavepoint = 0, $type = 'auto');
/**
* Connexion to server
*
2015-04-06 11:28:06 +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 int $port Port of database server
2015-04-06 11:28:06 +02:00
* @return resource Database access handler
* @see close
2014-02-21 12:38:43 +01:00
*/
function connect($host, $login, $passwd, $name, $port = 0);
/**
* Define limits and offset of request
*
2015-04-06 11:28:06 +02:00
* @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
2014-02-21 12:38:43 +01:00
*/
function plimit($limit = 0, $offset = 0);
/**
* Return value of server parameters
*
2015-04-06 11:28:06 +02:00
* @param string $filter Filter list on a particular value
* @return array Array of key-values (key=>value)
*/
function getServerParametersValues($filter = '');
2014-02-21 12:38:43 +01:00
/**
2015-04-06 11:28:06 +02:00
* Return value of server status
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $filter Filter list on a particular value
* @return array Array of key-values (key=>value)
2014-02-21 12:38:43 +01:00
*/
function getServerStatusValues($filter = '');
/**
2015-04-06 11:28:06 +02:00
* Return collation used in database
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string Collation value
2014-02-21 12:38:43 +01:00
*/
function getDefaultCollationDatabase();
/**
2015-04-06 11:28:06 +02:00
* Return number of lines for result of a SELECT
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param resource $resultset Resulset of requests
* @return int Nb of lines
* @see affected_rows
2014-02-21 12:38:43 +01:00
*/
function num_rows($resultset);
/**
2015-04-06 11:28:06 +02:00
* Return full path of dump program
2014-02-21 12:38:43 +01:00
*
* @return string Full path of dump program
*/
function getPathOfDump();
/**
2015-04-06 11:28:06 +02:00
* Return version of database client driver
2014-02-21 12:38:43 +01:00
*
* @return string Version string
*/
function getDriverInfo();
/**
2015-04-06 11:28:06 +02:00
* Return generic error code of last operation.
2014-02-21 12:38:43 +01:00
*
* @return string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)
*/
function errno();
/**
2015-04-06 11:28:06 +02:00
* Create a table into database
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @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 array $keys Tableau des champs cles noms => valeur
2015-04-06 11:28:06 +02:00
* @return int <0 if KO, >=0 if OK
2014-02-21 12:38:43 +01:00
*/
function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null);
2014-02-21 12:38:43 +01:00
/**
2015-04-06 11:28:06 +02:00
* Return list of available charset that can be used to store data in database
2014-02-21 12:38:43 +01:00
*
* @return array List of Charset
*/
function getListOfCharacterSet();
/**
2015-04-06 11:28:06 +02:00
* Create a new field into table
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @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
2014-02-21 12:38:43 +01:00
*/
function DDLAddField($table, $field_name, $field_desc, $field_position = "");
/**
2015-04-06 11:28:06 +02:00
* Drop a field from table
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $table Name of table
* @param string $field_name Name of field to drop
* @return int <0 if KO, >0 if OK
2014-02-21 12:38:43 +01:00
*/
function DDLDropField($table, $field_name);
/**
2015-04-06 11:28:06 +02:00
* Update format of a field into a table
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02: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
2014-02-21 12:38:43 +01:00
*/
function DDLUpdateField($table, $field_name, $field_desc);
/**
2015-04-06 11:28:06 +02:00
* Return list of available collation that can be used for database
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return array List of Collation
2014-02-21 12:38:43 +01:00
*/
function getListOfCollation();
/**
2015-04-06 11:28:06 +02:00
* Return a pointer of line with description of a table or field
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $table Name of table
* @param string $field Optionnel : Name of field if we want description of field
* @return resource Resource
2014-02-21 12:38:43 +01:00
*/
function DDLDescTable($table, $field = "");
/**
2015-04-06 11:28:06 +02:00
* Return version of database server
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string Version string
2014-02-21 12:38:43 +01:00
*/
function getVersion();
/**
2015-04-06 11:28:06 +02:00
* Return charset used to store data in database
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string Charset
2014-02-21 12:38:43 +01:00
*/
function getDefaultCharacterSetDatabase();
/**
2015-04-06 11:28:06 +02:00
* Create a user and privileges to connect to database (even if database does not exists yet)
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @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
2014-02-21 12:38:43 +01:00
*/
function DDLCreateUser(
$dolibarr_main_db_host,
$dolibarr_main_db_user,
$dolibarr_main_db_pass,
$dolibarr_main_db_name
);
/**
2015-05-12 19:01:01 +02:00
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
2015-04-06 11:28:06 +02:00
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
2015-05-12 19:01:01 +02:00
* @param bool $gm 1=Input informations are GMT values, otherwise local to server TZ
2015-04-06 11:28:06 +02:00
* @return int|string Date TMS or ''
2015-05-12 19:01:01 +02:00
*/
function jdate($string, $gm=false);
2014-02-21 12:38:43 +01:00
/**
2015-04-06 11:28:06 +02:00
* Encrypt sensitive data in database
* Warning: This function includes the escape, so it must use direct value
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02: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'
2014-02-21 12:38:43 +01:00
*/
function encrypt($fieldorvalue, $withQuotes = 0);
/**
* Validate a database transaction
*
2015-04-06 11:28:06 +02: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
2014-02-21 12:38:43 +01:00
*/
function commit($log = '');
/**
2015-04-06 11:28:06 +02:00
* List information of columns into a table.
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $table Name of table
* @return array Array with inforation on table
2014-02-21 12:38:43 +01:00
*/
function DDLInfoTable($table);
/**
2015-04-06 11:28:06 +02:00
* Free last resultset used.
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param resource $resultset Fre cursor
* @return void
2014-02-21 12:38:43 +01:00
*/
function free($resultset = null);
2014-02-21 12:38:43 +01:00
/**
2015-04-06 11:28:06 +02:00
* Close database connexion
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return boolean True if disconnect successfull, false otherwise
* @see connect
2014-02-21 12:38:43 +01:00
*/
function close();
/**
2015-04-06 11:28:06 +02:00
* Return last query in error
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @return string lastqueryerror
2014-02-21 12:38:43 +01:00
*/
function lastqueryerror();
/**
* Return connexion ID
*
2015-04-06 11:28:06 +02:00
* @return string Id connexion
2014-02-21 12:38:43 +01:00
*/
function DDLGetConnectId();
/**
2015-04-06 11:28:06 +02:00
* Renvoie la ligne courante (comme un objet) pour le curseur resultset
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param resource $resultset Curseur de la requete voulue
* @return Object Object result line or false if KO or end of cursor
2014-02-21 12:38:43 +01:00
*/
function fetch_object($resultset);
/**
2015-04-06 11:28:06 +02:00
* Select a database
2014-02-21 12:38:43 +01:00
*
2015-04-06 11:28:06 +02:00
* @param string $database Name of database
* @return boolean true if OK, false if KO
2014-02-21 12:38:43 +01:00
*/
function select_db($database);
}