dolibarr 19.0.4
Database Interface Reference

Class to manage Dolibarr database access for an SQL database. More...

Inheritance diagram for Database:

Public Member Functions

 ifsql ($test, $resok, $resko)
 Format a SQL IF.
 
 fetch_row ($resultset)
 Return datas as an array.
 
 idate ($param)
 Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
 
 lasterrno ()
 Return last error code.
 
 begin ($textinlog='')
 Start transaction.
 
 DDLCreateDb ($database, $charset='', $collation='', $owner='')
 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.
 
 getVersionArray ()
 Return version of database server into an array.
 
 convertSQLFromMysql ($line, $type='ddl')
 Convert a SQL request in Mysql syntax to native syntax.
 
 affected_rows ($resultset)
 Return the number of lines in the result of a request INSERT, DELETE or UPDATE.
 
 error ()
 Return description of last error.
 
 DDLListTables ($database, $table='')
 List tables into a database.
 
 DDLListTablesFull ($database, $table='')
 List tables into a database with table type.
 
 lastquery ()
 Return last request executed with query()
 
 order ($sortfield='', $sortorder='')
 Define sort criteria of request.
 
 decrypt ($value)
 Decrypt sensitive data in database.
 
 fetch_array ($resultset)
 Return datas as an array.
 
 lasterror ()
 Return last error label.
 
 escape ($stringtoencode)
 Escape a string to insert data.
 
 escapeforlike ($stringtoencode)
 Escape a string to insert data into a like.
 
 sanitize ($stringtosanitize)
 Sanitize a string for SQL forging.
 
 last_insert_id ($tab, $fieldid='rowid')
 Get last ID after an insert INSERT.
 
 getPathOfRestore ()
 Return full path of restore program.
 
 rollback ($log='')
 Canceling a transaction and returning to old values.
 
 query ($query, $usesavepoint=0, $type='auto', $result_mode=0)
 Execute a SQL request and return the resultset.
 
 connect ($host, $login, $passwd, $name, $port=0)
 Connexion to server.
 
 plimit ($limit=0, $offset=0)
 Define limits and offset of request.
 
 getServerParametersValues ($filter='')
 Return value of server parameters.
 
 getServerStatusValues ($filter='')
 Return value of server status.
 
 getDefaultCollationDatabase ()
 Return collation used in database.
 
 num_rows ($resultset)
 Return number of lines for result of a SELECT.
 
 getPathOfDump ()
 Return full path of dump program.
 
 getDriverInfo ()
 Return version of database client driver.
 
 errno ()
 Return generic error code of last operation.
 
 DDLCreateTable ($table, $fields, $primary_key, $type, $unique_keys=null, $fulltext_keys=null, $keys=null)
 Create a table into database.
 
 DDLDropTable ($table)
 Drop a table into database.
 
 getListOfCharacterSet ()
 Return list of available charset that can be used to store data in database.
 
 DDLAddField ($table, $field_name, $field_desc, $field_position="")
 Create a new field into table.
 
 DDLDropField ($table, $field_name)
 Drop a field from table.
 
 DDLUpdateField ($table, $field_name, $field_desc)
 Update format of a field into a table.
 
 getListOfCollation ()
 Return list of available collation that can be used for database.
 
 DDLDescTable ($table, $field="")
 Return a pointer of line with description of a table or field.
 
 getVersion ()
 Return version of database server.
 
 getDefaultCharacterSetDatabase ()
 Return charset used to store data in database.
 
 DDLCreateUser ( $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
 Create a user and privileges to connect to database (even if database does not exists yet)
 
 jdate ($string, $gm=false)
 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.
 
 encrypt ($fieldorvalue, $withQuotes=1)
 Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple quotes on strings.
 
 commit ($log='')
 Validate a database transaction.
 
 DDLInfoTable ($table)
 List information of columns into a table.
 
 free ($resultset=null)
 Free last resultset used.
 
 close ()
 Close database connexion.
 
 lastqueryerror ()
 Return last query in error.
 
 DDLGetConnectId ()
 Return connexion ID.
 
 fetch_object ($resultset)
 Returns the current line (as an object) for the resultset cursor.
 
 select_db ($database)
 Select a database.
 

Detailed Description

Class to manage Dolibarr database access for an SQL database.

Definition at line 26 of file Database.interface.php.

Member Function Documentation

◆ affected_rows()

Database::affected_rows ( $resultset)

Return the number of lines in the result of a request INSERT, DELETE or UPDATE.

Parameters
resource$resultsetCursor of the desired request
Returns
int Number of lines
See also
num_rows()

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ begin()

Database::begin ( $textinlog = '')

Start transaction.

Parameters
string$textinlogAdd a small text into log. '' by default.
Returns
int 1 if transaction successfuly opened or already opened, 0 if error

Implemented in DoliDB, and TraceableDB.

◆ close()

Database::close ( )

Close database connexion.

Returns
boolean True if disconnect successfull, false otherwise
See also
connect()

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ commit()

Database::commit ( $log = '')

Validate a database transaction.

Parameters
string$logAdd more log to default log line
Returns
int 1 if validation is OK or transaction level no started, 0 if ERROR

Implemented in DoliDB, and TraceableDB.

◆ connect()

Database::connect ( $host,
$login,
$passwd,
$name,
$port = 0 )

Connexion to server.

Parameters
string$hostdatabase server host
string$loginlogin
string$passwdpassword
string$namename of database (not used for mysql, used for pgsql)
int$portPort of database server
Returns
resource Database access handler
See also
close()

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ convertSQLFromMysql()

Database::convertSQLFromMysql ( $line,
$type = 'ddl' )

Convert a SQL request in Mysql syntax to native syntax.

Parameters
string$lineSQL request line to convert
string$typeType of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
Returns
string SQL request line converted

Implemented in DoliDBMysqli, DoliDBSqlite3, and TraceableDB.

◆ DDLAddField()

Database::DDLAddField ( $table,
$field_name,
$field_desc,
$field_position = "" )

Create a new field into table.

Parameters
string$tableName of table
string$field_nameName of field to add
string$field_descAssociative array of description of the field to insert [parameter name][parameter value]
string$field_positionOptional ex .: "after field stuff"
Returns
int Return integer <0 if KO, >0 if OK

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLCreateDb()

Database::DDLCreateDb ( $database,
$charset = '',
$collation = '',
$owner = '' )

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.

Parameters
string$databaseDatabase name to create
string$charsetCharset used to store data
string$collationCharset used to sort data
string$ownerUsername of database owner
Returns
resource resource defined if OK, null if KO

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLCreateTable()

Database::DDLCreateTable ( $table,
$fields,
$primary_key,
$type,
$unique_keys = null,
$fulltext_keys = null,
$keys = null )

Create a table into database.

Parameters
string$tableName of table
array$fieldsAssociative table [field name][table of descriptions]
string$primary_keyName of the field that will be the primary key
string$typeType of the table
array$unique_keysAssociative array Name of fields that will be unique key => value
array$fulltext_keysField name table that will be indexed in fulltext
array$keysTable of key fields names => value
Returns
int Return integer <0 if KO, >=0 if OK

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLCreateUser()

Database::DDLCreateUser ( $dolibarr_main_db_host,
$dolibarr_main_db_user,
$dolibarr_main_db_pass,
$dolibarr_main_db_name )

Create a user and privileges to connect to database (even if database does not exists yet)

Parameters
string$dolibarr_main_db_hostServer IP
string$dolibarr_main_db_userUsername to create
string$dolibarr_main_db_passUser password to create
string$dolibarr_main_db_nameDatabase name where user must be granted
Returns
int Return integer <0 if KO, >=0 if OK

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLDescTable()

Database::DDLDescTable ( $table,
$field = "" )

Return a pointer of line with description of a table or field.

Parameters
string$tableName of table
string$fieldOptional : Name of field if we want description of field
Returns
resource Resource

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLDropField()

Database::DDLDropField ( $table,
$field_name )

Drop a field from table.

Parameters
string$tableName of table
string$field_nameName of field to drop
Returns
int Return integer <0 if KO, >0 if OK

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLDropTable()

Database::DDLDropTable ( $table)

Drop a table into database.

Parameters
string$tableName of table
Returns
int Return integer <0 if KO, >=0 if OK

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLGetConnectId()

Database::DDLGetConnectId ( )

Return connexion ID.

Returns
string Id connexion

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLInfoTable()

Database::DDLInfoTable ( $table)

List information of columns into a table.

Parameters
string$tableName of table
Returns
array Array with information on table

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLListTables()

Database::DDLListTables ( $database,
$table = '' )

List tables into a database.

Parameters
string$databaseName of database
string$tableName of table filter ('xxx')
Returns
array List of tables in an array

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLListTablesFull()

Database::DDLListTablesFull ( $database,
$table = '' )

List tables into a database with table type.

Parameters
string$databaseName of database
string$tableName of table filter ('xxx')
Returns
array List of tables in an array

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ DDLUpdateField()

Database::DDLUpdateField ( $table,
$field_name,
$field_desc )

Update format of a field into a table.

Parameters
string$tableName of table
string$field_nameName of field to modify
string$field_descArray with description of field format
Returns
int Return integer <0 if KO, >0 if OK

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ decrypt()

Database::decrypt ( $value)

Decrypt sensitive data in database.

Parameters
string$valueValue to decrypt
Returns
string Decrypted value if used

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ encrypt()

Database::encrypt ( $fieldorvalue,
$withQuotes = 1 )

Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple quotes on strings.

Parameters
string$fieldorvalueField name or value to encrypt
int$withQuotesReturn string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
Returns
string XXX(field) or XXX('value') or field or 'value'

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ errno()

Database::errno ( )

Return generic error code of last operation.

Returns
string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ error()

Database::error ( )

Return description of last error.

Returns
string Error text

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ escape()

Database::escape ( $stringtoencode)

Escape a string to insert data.

Parameters
string$stringtoencodeString to escape
Returns
string String escaped

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ escapeforlike()

Database::escapeforlike ( $stringtoencode)

Escape a string to insert data into a like.

Can be used this way: LIKE '%".dbhandler->escape(dbhandler->escapeforlike(...))."'

Parameters
string$stringtoencodeString to escape
Returns
string String escaped

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ fetch_array()

Database::fetch_array ( $resultset)

Return datas as an array.

Parameters
resource$resultsetResultset of request
Returns
array Array

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ fetch_object()

Database::fetch_object ( $resultset)

Returns the current line (as an object) for the resultset cursor.

Parameters
resource | PgSql\Connection$resultsetHandler of the desired request
Returns
Object|false Object result line or false if KO or end of cursor

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

Referenced by DoliDB\getRow(), DoliDB\getRows(), and Societe\replaceThirdparty().

◆ fetch_row()

Database::fetch_row ( $resultset)

Return datas as an array.

Parameters
resource$resultsetResultset of request
Returns
array Array

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ free()

Database::free ( $resultset = null)

Free last resultset used.

Parameters
resource$resultsetFree cursor
Returns
void

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getDefaultCharacterSetDatabase()

Database::getDefaultCharacterSetDatabase ( )

Return charset used to store data in database.

Returns
string Charset

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getDefaultCollationDatabase()

Database::getDefaultCollationDatabase ( )

Return collation used in database.

Returns
string Collation value

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getDriverInfo()

Database::getDriverInfo ( )

Return version of database client driver.

Returns
string Version string

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getListOfCharacterSet()

Database::getListOfCharacterSet ( )

Return list of available charset that can be used to store data in database.

Returns
array List of Charset

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getListOfCollation()

Database::getListOfCollation ( )

Return list of available collation that can be used for database.

Returns
array List of Collation

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getPathOfDump()

Database::getPathOfDump ( )

Return full path of dump program.

Returns
string Full path of dump program

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getPathOfRestore()

Database::getPathOfRestore ( )

Return full path of restore program.

Returns
string Full path of restore program

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getServerParametersValues()

Database::getServerParametersValues ( $filter = '')

Return value of server parameters.

Parameters
string$filterFilter list on a particular value
Returns
array Array of key-values (key=>value)

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getServerStatusValues()

Database::getServerStatusValues ( $filter = '')

Return value of server status.

Parameters
string$filterFilter list on a particular value
Returns
array Array of key-values (key=>value)

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ getVersion()

Database::getVersion ( )

Return version of database server.

Returns
string Version string

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

Referenced by DoliDB\getVersionArray().

◆ getVersionArray()

Database::getVersionArray ( )

Return version of database server into an array.

Returns
array Version array

Implemented in DoliDB, and TraceableDB.

◆ idate()

Database::idate ( $param)

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

Parameters
int$paramDate TMS to convert
Returns
string Date in a string YYYYMMDDHHMMSS

◆ ifsql()

Database::ifsql ( $test,
$resok,
$resko )

Format a SQL IF.

Parameters
string$testTest string (example: 'cd.statut=0', 'field IS NULL')
string$resokresult if test is equal
string$reskoresult if test is not equal
Returns
string SQL string

Implemented in DoliDB, DoliDBPgsql, and TraceableDB.

◆ jdate()

Database::jdate ( $string,
$gm = false )

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.

Parameters
string$stringDate in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
bool$gm1=Input informations are GMT values, otherwise local to server TZ
Returns
int|string Date TMS or ''

Implemented in DoliDB, and TraceableDB.

◆ last_insert_id()

Database::last_insert_id ( $tab,
$fieldid = 'rowid' )

Get last ID after an insert INSERT.

Parameters
string$tabTable name concerned by insert. Not used under MySql but required for compatibility with Postgresql
string$fieldidField name
Returns
int Id of row

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

◆ lasterrno()

Database::lasterrno ( )

Return last error code.

Returns
string lasterrno

Implemented in DoliDB, and TraceableDB.

◆ lasterror()

Database::lasterror ( )

Return last error label.

Returns
string lasterror

Implemented in DoliDB, and TraceableDB.

◆ lastquery()

Database::lastquery ( )

Return last request executed with query()

Returns
string Last query

Implemented in DoliDB, and TraceableDB.

◆ lastqueryerror()

Database::lastqueryerror ( )

Return last query in error.

Returns
string lastqueryerror

Implemented in DoliDB, and TraceableDB.

◆ num_rows()

Database::num_rows ( $resultset)

Return number of lines for result of a SELECT.

Parameters
resource$resultsetResulset of requests
Returns
int Nb of lines
See also
affected_rows()

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

Referenced by DoliDB\getRows().

◆ order()

Database::order ( $sortfield = '',
$sortorder = '' )

Define sort criteria of request.

Parameters
string$sortfieldList of sort fields
string$sortorderSort order
Returns
string String to provide syntax of a sort sql string

Implemented in DoliDB, and TraceableDB.

◆ plimit()

Database::plimit ( $limit = 0,
$offset = 0 )

Define limits and offset of request.

Parameters
int$limitMaximum number of lines returned (-1=conf->liste_limit, 0=no limit)
int$offsetNumero of line from where starting fetch
Returns
string String with SQL syntax to add a limit and offset

Implemented in DoliDB, DoliDBPgsql, and TraceableDB.

◆ query()

Database::query ( $query,
$usesavepoint = 0,
$type = 'auto',
$result_mode = 0 )

Execute a SQL request and return the resultset.

Parameters
string$querySQL query string
int$usesavepoint0=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). 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.
string$typeType of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
int$result_modeResult mode
Returns
bool|resource Resultset of answer or false

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.

Referenced by DoliDB\begin(), DoliDB\commit(), CommonObject\commonReplaceProduct(), CommonObject\commonReplaceThirdparty(), DoliDB\getRow(), DoliDB\getRows(), ActionComm\replaceProduct(), Societe\replaceThirdparty(), and DoliDB\rollback().

◆ rollback()

Database::rollback ( $log = '')

Canceling a transaction and returning to old values.

Parameters
string$logAdd more log to default log line
Returns
int 1 if cancelation ok or transaction not open, 0 if error

Implemented in DoliDB, and TraceableDB.

◆ sanitize()

Database::sanitize ( $stringtosanitize)

Sanitize a string for SQL forging.

Parameters
string$stringtosanitizeString to escape
Returns
string String escaped

◆ select_db()

Database::select_db ( $database)

Select a database.

Parameters
string$databaseName of database
Returns
boolean true if OK, false if KO

Implemented in DoliDBMysqli, DoliDBPgsql, DoliDBSqlite3, and TraceableDB.


The documentation for this interface was generated from the following file: