abstract class DoliDB implements Database (View source)

Class to manage Dolibarr database access

Constants

VERSIONMIN

Force subclass to implement VERSIONMIN - required DB version

LABEL

Force subclass to implement LABEL - description of DB type

Properties

false|resource|mysqli|mysqliDoli|SQLite3|Connection|DoliDB $db
string $type
string $forcecharset
string $forcecollate
bool $connected
bool $database_selected
string $database_name
string $database_user
string $database_host
int $database_port
int $transaction_opened
string $lastquery
string $lastqueryerror
string $lasterror
string $lasterrno
string $prefix_db
bool $ok
string $error

Methods

string
prefix()

Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=...).

string
ifsql(string $test, string $resok, string $resko)

Format a SQL IF

string
stddevpop(string $nameoffield)

Return SQL string to aggregate using the Standard Deviation of population

string
hintindex(string $nameofindex, int $mode = 1)

Return SQL string to force an index

string
regexpsql(string $subject, string $pattern, int $sqlstring = 0)

Format a SQL REGEXP

string
idate(int $param, "gmt"|"tzserver" $gm = 'tzserver')

Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.

string
lasterrno()

Return last error code

string
sanitize(string $stringtosanitize, int $allowsimplequote = 0, int $allowsequals = 0, int $allowsspace = 0, int $allowschars = 1)

Sanitize a string for SQL forging

int
begin(string $textinlog = '')

Start transaction

int
commit(string $log = '')

Validate a database transaction

int
rollback(string $log = '')

Cancel a transaction and go back to initial data values

string
plimit(int $limit = 0, int $offset = 0)

Define limits and offset of request

string[]
getVersionArray()

Return version of database server into an array

string
lastquery()

Return last request executed with query()

string
order(string $sortfield = '', string $sortorder = '')

Define sort criteria of request

string
lasterror()

Return last error label

int|""
jdate(string $string, bool $gm = 'tzserver')

Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -> 3600 with server TZ = +1 and $gm='tzserver' 19700101020000 -> 7200 whatever is server TZ if $gm='gmt'

string
lastqueryerror()

Return last query in error

bool|int|object
getRow(string $sql)

Return first result from query as object Note : This method executes a given SQL query and retrieves the first row of results as an object. It should only be used with SELECT queries Don't add LIMIT to your query, it will be added by this method

false|object[]
getRows(string $sql)

Return all results from query as an array of objects. Using this is a bad practice and is discouraged.

int
getNextAutoIncrementId(string $table)

Get the last ID of an auto-increment field of a table

mixed
prepare(string $sql)

Prepare a SQL statement for execution

Details

string prefix()

Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=...).

Otherwise return MAIN_DB_PREFIX (common use).

Return Value

string

The DB prefix

string ifsql(string $test, string $resok, string $resko)

Format a SQL IF

Parameters

string $test

Test string (example: 'cd.statut=0', 'field IS NULL')

string $resok

result if test is equal

string $resko

result if test is not equal

Return Value

string

SQL string

string stddevpop(string $nameoffield)

Return SQL string to aggregate using the Standard Deviation of population

Parameters

string $nameoffield

Name of field

Return Value

string

SQL string

string hintindex(string $nameofindex, int $mode = 1)

Return SQL string to force an index

Parameters

string $nameofindex

Name of index

int $mode

0=Use, 1=Force

Return Value

string

SQL string

string regexpsql(string $subject, string $pattern, int $sqlstring = 0)

Format a SQL REGEXP

Parameters

string $subject

Field name to test

string $pattern

SQL pattern to match

int $sqlstring

0=the string being tested is a hard coded string, 1=the string is a field

Return Value

string

SQL string

string idate(int $param, "gmt"|"tzserver" $gm = 'tzserver')

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 $param

Date TMS to convert

"gmt"|"tzserver" $gm

'gmt'=Input information are GMT values, 'tzserver'=Local to server TZ

Return Value

string

Date in a string YYYYMMDDHHMMSS

string lasterrno()

Return last error code

Return Value

string lasterrno

string sanitize(string $stringtosanitize, int $allowsimplequote = 0, int $allowsequals = 0, int $allowsspace = 0, int $allowschars = 1)

Sanitize a string for SQL forging

Parameters

string $stringtosanitize

String to escape

int $allowsimplequote

1=Allow simple quotes in string around val separated by "," but only when string is used as a list of SQL string "'aa', 'bb', 'cc', ..."). Can be used for IN ... 2=Allow all simple quotes. If you use this value, the return MUST be escaped to forge SQL strings.

int $allowsequals

1=Allow equals sign

int $allowsspace

1=Allow space char

int $allowschars

1=Allow a-z chars

Return Value

string

String escaped

int begin(string $textinlog = '')

Start transaction

Parameters

string $textinlog

Add a small text into log. '' by default.

Return Value

int

1 if transaction successfully opened or already opened, 0 if error

int commit(string $log = '')

Validate a database transaction

Parameters

string $log

Add more log to default log line

Return Value

int

1 if validation is OK or transaction level no started, 0 if ERROR

int rollback(string $log = '')

Cancel a transaction and go back to initial data values

@param  string          $log        Add more log to default log line

Parameters

string $log

Add more log to default log line

Return Value

int

1 if cancellation ok or transaction not open, 0 if error

string plimit(int $limit = 0, int $offset = 0)

Define limits and offset of request

Parameters

int $limit

Maximum number of lines returned (-1=conf->liste_limit, 0=no limit)

int $offset

Numero of line from where starting fetch

Return Value

string

String with SQL syntax to add a limit and offset

string[] getVersionArray()

Return version of database server into an array

Return Value

string[]

Version array

string lastquery()

Return last request executed with query()

Return Value

string

Last query

string order(string $sortfield = '', string $sortorder = '')

Define sort criteria of request

Parameters

string $sortfield

List of sort fields

string $sortorder

Sort order

Return Value

string

String to provide syntax of a sort sql string

string lasterror()

Return last error label

Return Value

string lasterror

int|"" jdate(string $string, bool $gm = 'tzserver')

Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -> 3600 with server TZ = +1 and $gm='tzserver' 19700101020000 -> 7200 whatever is server TZ if $gm='gmt'

@param  string              $string     Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)

Parameters

string $string

Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)

bool $gm

1=Input information are GMT values, otherwise local to server TZ

Return Value

int|""

Date TMS or ''

string lastqueryerror()

Return last query in error

Return Value

string lastqueryerror

bool|int|object getRow(string $sql)

Return first result from query as object Note : This method executes a given SQL query and retrieves the first row of results as an object. It should only be used with SELECT queries Don't add LIMIT to your query, it will be added by this method

Parameters

string $sql

The sql query string

Return Value

bool|int|object

False on failure, 0 on empty, object on success

false|object[] getRows(string $sql)

Return all results from query as an array of objects. Using this is a bad practice and is discouraged.

Note : It should only be used with SELECT queries and with a limit. If you are not able to defined/know what can be the limit, it just means this function is not what you need. Do not use it.

Parameters

string $sql

The sql query string. Must end with "... LIMIT x"

Return Value

false|object[] Result

int getNextAutoIncrementId(string $table)

Get the last ID of an auto-increment field of a table

Parameters

string $table

Name of table

Return Value

int

Next ID or -1 if error

mixed prepare(string $sql)

Prepare a SQL statement for execution

This method must be implemented by subclasses.

Parameters

string $sql

SQL query to prepare

Return Value

mixed

Driver-specific prepared statement object or false on failure