dolibarr 20.0.2
DoliDBPgsql Class Reference

Class to drive a PostgreSQL database for Dolibarr. More...

Inheritance diagram for DoliDBPgsql:
Collaboration diagram for DoliDBPgsql:

Public Member Functions

 __construct ($type, $host, $user, $pass, $name='', $port=0)
 Constructor.
 
 convertSQLFromMysql ($line, $type='auto', $unescapeslashquot=false)
 Convert a SQL request in Mysql syntax to native syntax.
 
 select_db ($database)
 Select a database PostgreSQL does not have an equivalent for mysql_select_db Only compare if the chosen DB is the one active on the connection.
 
 connect ($host, $login, $passwd, $name, $port=0)
 Connection to server.
 
 getVersion ()
 Return version of database server.
 
 getDriverInfo ()
 Return version of database client driver.
 
 close ()
 Close database connection.
 
 query ($query, $usesavepoint=0, $type='auto', $result_mode=0)
 Convert request to PostgreSQL syntax, execute it and return the resultset.
 
 fetch_object ($resultset)
 Returns the current line (as an object) for the resultset cursor.
 
 fetch_array ($resultset)
 Return datas as an array.
 
 fetch_row ($resultset)
 Return datas as an array.
 
 num_rows ($resultset)
 Return number of lines for result of a SELECT.
 
 affected_rows ($resultset)
 Return the number of lines in the result of a request INSERT, DELETE or UPDATE.
 
 free ($resultset=null)
 Libere le dernier resultset utilise sur cette connection.
 
 plimit ($limit=0, $offset=0)
 Define limits and offset of request.
 
 escape ($stringtoencode)
 Escape a string to insert data.
 
 escapeforlike ($stringtoencode)
 Escape a string to insert data into a like.
 
 ifsql ($test, $resok, $resko)
 Format a SQL IF.
 
 regexpsql ($subject, $pattern, $sqlstring=0)
 Format a SQL REGEXP.
 
 errno ()
 Renvoie le code erreur generique de l'operation precedente.
 
 error ()
 Renvoie le texte de l'erreur pgsql de l'operation precedente.
 
 last_insert_id ($tab, $fieldid='rowid')
 Get last ID after an insert INSERT.
 
 encrypt ($fieldorvalue, $withQuotes=1)
 Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple quotes on strings.
 
 decrypt ($value)
 Decrypt sensitive data in database.
 
 DDLGetConnectId ()
 Return connection ID.
 
 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.
 
 DDLListTables ($database, $table='')
 List tables into a database.
 
 DDLListTablesFull ($database, $table='')
 List tables into a database.
 
 DDLInfoTable ($table)
 List information of columns into a table.
 
 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.
 
 DDLDescTable ($table, $field="")
 Return a pointer of line with description of a table or field.
 
 DDLAddField ($table, $field_name, $field_desc, $field_position="")
 Create a new field into table.
 
 DDLUpdateField ($table, $field_name, $field_desc)
 Update format of a field into a table.
 
 DDLDropField ($table, $field_name)
 Drop a field from table.
 
 DDLCreateUser ($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
 Create a user to connect to database.
 
 getDefaultCharacterSetDatabase ()
 Return charset used to store data in database.
 
 getListOfCharacterSet ()
 Return list of available charset that can be used to store data in database.
 
 getDefaultCollationDatabase ()
 Return collation used in database.
 
 getListOfCollation ()
 Return list of available collation that can be used for database.
 
 getPathOfDump ()
 Return full path of dump program.
 
 getPathOfRestore ()
 Return full path of restore program.
 
 getServerParametersValues ($filter='')
 Return value of server parameters.
 
 getServerStatusValues ($filter='')
 Return value of server status.
 
- Public Member Functions inherited from DoliDB
 prefix ()
 Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=...).
 
 stddevpop ($nameoffield)
 Return SQL string to aggregate using the Standard Deviation of population.
 
 hintindex ($nameofindex)
 Return SQL string to force an index.
 
 idate ($param, $gm='tzserver')
 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.
 
 sanitize ($stringtosanitize, $allowsimplequote=0, $allowsequals=0, $allowsspace=0, $allowschars=1)
 Sanitize a string for SQL forging.
 
 begin ($textinlog='')
 Start transaction.
 
 commit ($log='')
 Validate a database transaction.
 
 rollback ($log='')
 Cancel a transaction and go back to initial data values.
 
 getVersionArray ()
 Return version of database server into an array.
 
 lastquery ()
 Return last request executed with query()
 
 order ($sortfield='', $sortorder='')
 Define sort criteria of request.
 
 lasterror ()
 Return last error label.
 
 jdate ($string, $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 whaterver is server TZ if $gm='gmt'.
 
 lastqueryerror ()
 Return last query in error.
 
 getRow ($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.
 
 getRows ($sql)
 Return all results from query as an array of objects.
 
- Public Member Functions inherited from Database
 idate ($param)
 Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
 
 convertSQLFromMysql ($line, $type='ddl')
 Convert a SQL request in Mysql syntax to native syntax.
 
 sanitize ($stringtosanitize)
 Sanitize a string for SQL forging.
 

Public Attributes

 $type = 'pgsql'
 Database type.
 
const LABEL = 'PostgreSQL'
 Database label.
 
 $forcecharset = 'UTF8'
 Charset.
 
 $forcecollate = ''
 Collate used to force collate when creating database.
 
const VERSIONMIN = '9.0.0'
 Version min database.
 
- Public Attributes inherited from DoliDB
const VERSIONMIN = self::VERSIONMIN
 Force subclass to implement VERSIONMIN - required DB version.
 
const LABEL = self::LABEL
 Force subclass to implement LABEL - description of DB type.
 

Detailed Description

Class to drive a PostgreSQL database for Dolibarr.

Definition at line 37 of file pgsql.class.php.

Constructor & Destructor Documentation

◆ __construct()

DoliDBPgsql::__construct ( $type,
$host,
$user,
$pass,
$name = '',
$port = 0 )

Constructor.

This create an opened connection to a database server and eventually to a database

Parameters
string$typeType of database (mysql, pgsql...). Not used.
string$hostAddress of database server
string$userNom de l'utilisateur autorise
string$passPassword
string$nameNom de la database
int$portPort of database server

Definition at line 80 of file pgsql.class.php.

References connect(), dol_syslog(), error(), and select_db().

Member Function Documentation

◆ affected_rows()

DoliDBPgsql::affected_rows ( $resultset)

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

Parameters
resource$resultsetResult set of request
Returns
int Nb of lines
See also
num_rows()

Implements Database.

Definition at line 664 of file pgsql.class.php.

◆ close()

DoliDBPgsql::close ( )

Close database connection.

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

Implements Database.

Definition at line 494 of file pgsql.class.php.

References dol_syslog().

◆ connect()

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

Connection to server.

Parameters
string$hostDatabase server host
string$loginLogin
string$passwdPassword
string$nameName of database (not used for mysql, used for pgsql)
integer$portPort of database server
Returns
false|resource Database access handler
See also
close()

Implements Database.

Definition at line 409 of file pgsql.class.php.

Referenced by __construct().

◆ convertSQLFromMysql()

DoliDBPgsql::convertSQLFromMysql ( $line,
$type = 'auto',
$unescapeslashquot = false )

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...)
bool$unescapeslashquotUnescape "slash quote" with "quote quote"
Returns
string SQL request line converted

Definition at line 159 of file pgsql.class.php.

References $type, and getDolGlobalString().

Referenced by query().

◆ DDLAddField()

DoliDBPgsql::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
array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}$field_desc Associative array of description of the field to insert [parameter name][parameter value]
string$field_positionOptionnel ex.: "after champtruc"
Returns
int Return integer <0 if KO, >0 if OK

Implements Database.

Definition at line 1200 of file pgsql.class.php.

References dol_syslog(), query(), and DoliDB\sanitize().

◆ DDLCreateDb()

DoliDBPgsql::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
false|resource Resource defined if OK, null if KO

Implements Database.

Definition at line 939 of file pgsql.class.php.

References $forcecharset, $forcecollate, dol_syslog(), escape(), and query().

◆ DDLCreateTable()

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

Create a table into database.

Parameters
string$tableNom de la table
array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}>$fields Tableau associatif [nom champ][tableau des descriptions]
string$primary_keyNom du champ qui sera la clef primaire
string$typeType de la table
array$unique_keysTableau associatifs Nom de champs qui seront clef unique => valeur
array$fulltext_keysTableau des Nom de champs qui seront indexes en fulltext
array$keysTableau des champs cles noms => valeur
Returns
int Return integer <0 if KO, >=0 if OK

Implements Database.

Definition at line 1068 of file pgsql.class.php.

References escape(), query(), and DoliDB\sanitize().

◆ DDLCreateUser()

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

Create a user to connect to database.

Parameters
string$dolibarr_main_db_hostIp server
string$dolibarr_main_db_userName of user to create
string$dolibarr_main_db_passPassword of user to create
string$dolibarr_main_db_nameDatabase name where user must be granted
Returns
int Return integer <0 if KO, >=0 if OK

Implements Database.

Definition at line 1316 of file pgsql.class.php.

References dol_syslog(), escape(), and query().

◆ DDLDescTable()

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

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

Parameters
string$tableName of table
string$fieldOptionnel : Name of field if we want description of field
Returns
false|resource Resultset x (x->attname)

Implements Database.

Definition at line 1176 of file pgsql.class.php.

References dol_syslog(), and query().

◆ DDLDropField()

DoliDBPgsql::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

Implements Database.

Definition at line 1293 of file pgsql.class.php.

References error(), DoliDB\lasterror(), query(), and DoliDB\sanitize().

◆ DDLDropTable()

DoliDBPgsql::DDLDropTable ( $table)

Drop a table into database.

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

Implements Database.

Definition at line 1154 of file pgsql.class.php.

References query().

◆ DDLGetConnectId()

DoliDBPgsql::DDLGetConnectId ( )

Return connection ID.

Returns
string Id connection

Implements Database.

Definition at line 919 of file pgsql.class.php.

◆ DDLInfoTable()

DoliDBPgsql::DDLInfoTable ( $table)

List information of columns into a table.

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

Implements Database.

Definition at line 1024 of file pgsql.class.php.

References fetch_row(), and query().

◆ DDLListTables()

DoliDBPgsql::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

Implements Database.

Definition at line 969 of file pgsql.class.php.

References fetch_row().

◆ DDLListTablesFull()

DoliDBPgsql::DDLListTablesFull ( $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

Implements Database.

Definition at line 997 of file pgsql.class.php.

References fetch_row().

◆ DDLUpdateField()

DoliDBPgsql::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
array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}$field_desc Array with description of field format
Returns
int Return integer <0 if KO, >0 if OK

Implements Database.

Definition at line 1248 of file pgsql.class.php.

References dol_syslog(), escape(), query(), and DoliDB\sanitize().

◆ decrypt()

DoliDBPgsql::decrypt ( $value)

Decrypt sensitive data in database.

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

Implements Database.

Definition at line 898 of file pgsql.class.php.

◆ encrypt()

DoliDBPgsql::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'

Implements Database.

Definition at line 877 of file pgsql.class.php.

References escape().

◆ errno()

DoliDBPgsql::errno ( )

Renvoie le code erreur generique de l'operation precedente.

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

Implements Database.

Definition at line 778 of file pgsql.class.php.

Referenced by query().

◆ error()

DoliDBPgsql::error ( )

Renvoie le texte de l'erreur pgsql de l'operation precedente.

Returns
string Error text

Implements Database.

Definition at line 842 of file pgsql.class.php.

Referenced by __construct(), DDLDropField(), and query().

◆ escape()

DoliDBPgsql::escape ( $stringtoencode)

Escape a string to insert data.

Parameters
string$stringtoencodeString to escape
Returns
string String escaped

Implements Database.

Definition at line 726 of file pgsql.class.php.

Referenced by DDLCreateDb(), DDLCreateTable(), DDLCreateUser(), DDLUpdateField(), encrypt(), and regexpsql().

◆ escapeforlike()

DoliDBPgsql::escapeforlike ( $stringtoencode)

Escape a string to insert data into a like.

Parameters
string$stringtoencodeString to escape
Returns
string String escaped

Implements Database.

Definition at line 737 of file pgsql.class.php.

◆ fetch_array()

DoliDBPgsql::fetch_array ( $resultset)

Return datas as an array.

Parameters
resource$resultsetResultset of request
Returns
false|array Array

Implements Database.

Definition at line 611 of file pgsql.class.php.

Referenced by getDefaultCharacterSetDatabase(), getDefaultCollationDatabase(), getPathOfDump(), getPathOfRestore(), and getVersion().

◆ fetch_object()

DoliDBPgsql::fetch_object ( $resultset)

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

Parameters
resource$resultsetCurseur de la requete voulue
Returns
false|object Object result line or false if KO or end of cursor

Implements Database.

Definition at line 594 of file pgsql.class.php.

Referenced by getListOfCharacterSet(), getListOfCollation(), and getServerParametersValues().

◆ fetch_row()

DoliDBPgsql::fetch_row ( $resultset)

Return datas as an array.

Parameters
resource$resultsetResultset of request
Returns
false|array Array

Implements Database.

Definition at line 628 of file pgsql.class.php.

Referenced by DDLInfoTable(), DDLListTables(), and DDLListTablesFull().

◆ free()

DoliDBPgsql::free ( $resultset = null)

Libere le dernier resultset utilise sur cette connection.

Parameters
resource$resultsetResult set of request
Returns
void

Implements Database.

Definition at line 683 of file pgsql.class.php.

Referenced by getListOfCharacterSet(), and getListOfCollation().

◆ getDefaultCharacterSetDatabase()

DoliDBPgsql::getDefaultCharacterSetDatabase ( )

Return charset used to store data in database.

Returns
string Charset

Implements Database.

Definition at line 1336 of file pgsql.class.php.

References fetch_array(), and query().

◆ getDefaultCollationDatabase()

DoliDBPgsql::getDefaultCollationDatabase ( )

Return collation used in database.

Returns
string Collation value

Implements Database.

Definition at line 1375 of file pgsql.class.php.

References fetch_array(), and query().

◆ getDriverInfo()

DoliDBPgsql::getDriverInfo ( )

Return version of database client driver.

Returns
string Version string

Implements Database.

Definition at line 483 of file pgsql.class.php.

◆ getListOfCharacterSet()

DoliDBPgsql::getListOfCharacterSet ( )

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

Returns
array|null List of Charset

Implements Database.

Definition at line 1352 of file pgsql.class.php.

References fetch_object(), free(), and query().

◆ getListOfCollation()

DoliDBPgsql::getListOfCollation ( )

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

Returns
array|null Liste of Collation

Implements Database.

Definition at line 1391 of file pgsql.class.php.

References fetch_object(), free(), and query().

◆ getPathOfDump()

DoliDBPgsql::getPathOfDump ( )

Return full path of dump program.

Returns
string Full path of dump program

Implements Database.

Definition at line 1413 of file pgsql.class.php.

References fetch_array(), and query().

◆ getPathOfRestore()

DoliDBPgsql::getPathOfRestore ( )

Return full path of restore program.

Returns
string Full path of restore program

Implements Database.

Definition at line 1437 of file pgsql.class.php.

References fetch_array(), and query().

◆ getServerParametersValues()

DoliDBPgsql::getServerParametersValues ( $filter = '')

Return value of server parameters.

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

Implements Database.

Definition at line 1465 of file pgsql.class.php.

References fetch_object(), and query().

◆ getServerStatusValues()

DoliDBPgsql::getServerStatusValues ( $filter = '')

Return value of server status.

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

Implements Database.

Definition at line 1489 of file pgsql.class.php.

◆ getVersion()

DoliDBPgsql::getVersion ( )

Return version of database server.

Returns
string Version string

Implements Database.

Definition at line 468 of file pgsql.class.php.

References fetch_array(), and query().

◆ ifsql()

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

Format a SQL IF.

Parameters
string$testTest expression (example: 'cd.statut=0', 'field IS NULL')
string$resokResult to generate when test is True
string$reskoResult to generate when test is False
Returns
string chaine format SQL

Reimplemented from DoliDB.

Definition at line 750 of file pgsql.class.php.

◆ last_insert_id()

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

Get last ID after an insert INSERT.

Parameters
string$tabTable name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec PostgreSQL
string$fieldidField name
Returns
int Id of row

Implements Database.

Definition at line 855 of file pgsql.class.php.

◆ num_rows()

DoliDBPgsql::num_rows ( $resultset)

Return number of lines for result of a SELECT.

Parameters
resource$resultsetResulset of requests
Returns
int Nb of lines, -1 on error
See also
affected_rows()

Implements Database.

Definition at line 646 of file pgsql.class.php.

◆ plimit()

DoliDBPgsql::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

Reimplemented from DoliDB.

Definition at line 703 of file pgsql.class.php.

◆ query()

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

Convert request to PostgreSQL syntax, execute it 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).
string$typeType of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
int$result_modeResult mode (not used with pgsql)
Returns
bool|resource Resultset of answer

Implements Database.

Definition at line 515 of file pgsql.class.php.

References $type, convertSQLFromMysql(), dol_syslog(), errno(), error(), getDolGlobalInt(), getDolGlobalString(), DoliDB\lasterrno(), DoliDB\lasterror(), DoliDB\lastquery(), and DoliDB\lastqueryerror().

Referenced by DDLAddField(), DDLCreateDb(), DDLCreateTable(), DDLCreateUser(), DDLDescTable(), DDLDropField(), DDLDropTable(), DDLInfoTable(), DDLUpdateField(), getDefaultCharacterSetDatabase(), getDefaultCollationDatabase(), getListOfCharacterSet(), getListOfCollation(), getPathOfDump(), getPathOfRestore(), getServerParametersValues(), and getVersion().

◆ regexpsql()

DoliDBPgsql::regexpsql ( $subject,
$pattern,
$sqlstring = 0 )

Format a SQL REGEXP.

Parameters
string$subjectField name to test
string$patternSQL pattern to match
int$sqlstring0=the string being tested is a hard coded string, 1=the string is a field
Returns
string SQL string

Reimplemented from DoliDB.

Definition at line 763 of file pgsql.class.php.

References escape().

◆ select_db()

DoliDBPgsql::select_db ( $database)

Select a database PostgreSQL does not have an equivalent for mysql_select_db Only compare if the chosen DB is the one active on the connection.

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

Implements Database.

Definition at line 388 of file pgsql.class.php.

Referenced by __construct().


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