dolibarr  20.0.0-beta
TraceableDB.php
1 <?php
2 /* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/db/DoliDB.class.php';
27 
33 class TraceableDB extends DoliDB
34 {
38  public $db; // cannot be protected because of parent declaration
42  public $queries;
46  protected $startTime;
50  protected $startMemory;
54  public $type;
58  const LABEL = ''; // TODO: the right value should be $this->db::LABEL (but this is a constant? o_O)
62  const VERSIONMIN = ''; // TODO: the same thing here, $this->db::VERSIONMIN is the right value
63 
69  public function __construct($db)
70  {
71  $this->db = $db;
72  $this->type = $this->db->type;
73  $this->queries = array();
74  }
75 
84  public function ifsql($test, $resok, $resko)
85  {
86  return $this->db->ifsql($test, $resok, $resko);
87  }
88 
89  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
96  public function fetch_row($resultset)
97  {
98  // phpcs:enable
99  return $this->db->fetch_row($resultset);
100  }
101 
110  public function idate($param, $gm = 'tzserver')
111  {
112  return $this->db->idate($param, $gm);
113  }
114 
120  public function lasterrno()
121  {
122  return $this->db->lasterrno();
123  }
124 
131  public function begin($textinlog = '')
132  {
133  return $this->db->begin($textinlog);
134  }
135 
147  public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
148  {
149  return $this->db->DDLCreateDb($database, $charset, $collation, $owner);
150  }
151 
157  public function getVersionArray()
158  {
159  return $this->db->getVersionArray();
160  }
161 
169  public function convertSQLFromMysql($line, $type = 'ddl')
170  {
171  return $this->db->convertSQLFromMysql($line);
172  }
173 
174  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
182  public function affected_rows($resultset)
183  {
184  // phpcs:enable
185  return $this->db->affected_rows($resultset);
186  }
187 
193  public function error()
194  {
195  return $this->db->error();
196  }
197 
205  public function DDLListTables($database, $table = '')
206  {
207  return $this->db->DDLListTables($database, $table);
208  }
209 
217  public function DDLListTablesFull($database, $table = '')
218  {
219  return $this->db->DDLListTablesFull($database, $table);
220  }
221 
227  public function lastquery()
228  {
229  return $this->db->lastquery();
230  }
231 
239  public function order($sortfield = null, $sortorder = null)
240  {
241  return $this->db->order($sortfield, $sortorder);
242  }
243 
250  public function decrypt($value)
251  {
252  return $this->db->decrypt($value);
253  }
254 
255  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
262  public function fetch_array($resultset)
263  {
264  // phpcs:enable
265  return $this->db->fetch_array($resultset);
266  }
267 
273  public function lasterror()
274  {
275  return $this->db->lasterror();
276  }
277 
284  public function escape($stringtoencode)
285  {
286  return $this->db->escape($stringtoencode);
287  }
288 
295  public function escapeforlike($stringtoencode)
296  {
297  return $this->db->escapeforlike($stringtoencode);
298  }
299 
300  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
308  public function last_insert_id($tab, $fieldid = 'rowid')
309  {
310  // phpcs:enable
311  return $this->db->last_insert_id($tab, $fieldid);
312  }
313 
319  public function getPathOfRestore()
320  {
321  return $this->db->getPathOfRestore();
322  }
323 
330  public function rollback($log = '')
331  {
332  return $this->db->rollback($log);
333  }
334 
345  public function query($query, $usesavepoint = 0, $type = 'auto', $result_mode = 0)
346  {
347  $this->startTracing();
348 
349  $resql = $this->db->query($query, $usesavepoint, $type, $result_mode);
350 
351  $this->endTracing($query, $resql);
352 
353  return $resql;
354  }
355 
361  protected function startTracing()
362  {
363  $this->startTime = microtime(true);
364  $this->startMemory = memory_get_usage(true);
365  }
366 
374  protected function endTracing($sql, $resql)
375  {
376  $endTime = microtime(true);
377  $duration = $endTime - $this->startTime;
378  $endMemory = memory_get_usage(true);
379  $memoryDelta = $endMemory - $this->startMemory;
380 
381  $this->queries[] = array(
382  'sql' => $sql,
383  'duration' => $duration,
384  'memory_usage' => $memoryDelta,
385  'is_success' => $resql ? true : false,
386  'error_code' => $resql ? null : $this->db->lasterrno(),
387  'error_message' => $resql ? null : $this->db->lasterror()
388  );
389  }
390 
402  public function connect($host, $login, $passwd, $name, $port = 0)
403  {
404  return $this->db->connect($host, $login, $passwd, $name, $port);
405  }
406 
414  public function plimit($limit = 0, $offset = 0)
415  {
416  return $this->db->plimit($limit, $offset);
417  }
418 
425  public function getServerParametersValues($filter = '')
426  {
427  return $this->db->getServerParametersValues($filter);
428  }
429 
436  public function getServerStatusValues($filter = '')
437  {
438  return $this->db->getServerStatusValues($filter);
439  }
440 
446  public function getDefaultCollationDatabase()
447  {
448  return $this->db->getDefaultCollationDatabase();
449  }
450 
451  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
459  public function num_rows($resultset)
460  {
461  // phpcs:enable
462  return $this->db->num_rows($resultset);
463  }
464 
470  public function getPathOfDump()
471  {
472  return $this->db->getPathOfDump();
473  }
474 
480  public function getDriverInfo()
481  {
482  return $this->db->getDriverInfo();
483  }
484 
490  public function errno()
491  {
492  return $this->db->errno();
493  }
494 
507  public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
508  {
509  return $this->db->DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys, $fulltext_keys, $keys);
510  }
511 
518  public function DDLDropTable($table)
519  {
520  return $this->db->DDLDropTable($table);
521  }
522 
528  public function getListOfCharacterSet()
529  {
530  return $this->db->getListOfCharacterSet();
531  }
532 
542  public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
543  {
544  return $this->db->DDLAddField($table, $field_name, $field_desc, $field_position);
545  }
546 
554  public function DDLDropField($table, $field_name)
555  {
556  return $this->db->DDLDropField($table, $field_name);
557  }
558 
567  public function DDLUpdateField($table, $field_name, $field_desc)
568  {
569  return $this->db->DDLUpdateField($table, $field_name, $field_desc);
570  }
571 
577  public function getListOfCollation()
578  {
579  return $this->db->getListOfCollation();
580  }
581 
589  public function DDLDescTable($table, $field = "")
590  {
591  return $this->db->DDLDescTable($table, $field);
592  }
593 
599  public function getVersion()
600  {
601  return $this->db->getVersion();
602  }
603 
610  {
611  return $this->db->getDefaultCharacterSetDatabase();
612  }
613 
623  public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
624  {
625  return $this->db->DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name);
626  }
627 
628  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
638  public function jdate($string, $gm = false)
639  {
640  // phpcs:enable
641  return $this->db->jdate($string, $gm);
642  }
643 
652  public function encrypt($fieldorvalue, $withQuotes = 1)
653  {
654  return $this->db->encrypt($fieldorvalue, $withQuotes);
655  }
656 
663  public function commit($log = '')
664  {
665  return $this->db->commit($log);
666  }
667 
674  public function DDLInfoTable($table)
675  {
676  return $this->db->DDLInfoTable($table);
677  }
678 
685  public function free($resultset = null)
686  {
687  $this->db->free($resultset);
688  }
689 
696  public function close()
697  {
698  return $this->db->close();
699  }
700 
706  public function lastqueryerror()
707  {
708  return $this->db->lastqueryerror();
709  }
710 
716  public function DDLGetConnectId()
717  {
718  return $this->db->DDLGetConnectId();
719  }
720 
721  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
728  public function fetch_object($resultset)
729  {
730  // phpcs:enable
731  return $this->db->fetch_object($resultset);
732  }
733 
734  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
741  public function select_db($database)
742  {
743  // phpcs:enable
744  return $this->db->select_db($database);
745  }
746 }
Class to manage Dolibarr database access.
TraceableDB class.
Definition: TraceableDB.php:34
escape($stringtoencode)
Escape a string to insert data.
convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
const VERSIONMIN
@const Version min database
Definition: TraceableDB.php:62
jdate($string, $gm=false)
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
begin($textinlog='')
Start transaction.
getDriverInfo()
Return version of database client driver.
free($resultset=null)
Free last resultset used.
getPathOfDump()
Return full path of dump program.
getPathOfRestore()
Return full path of restore program.
select_db($database)
Select a database.
rollback($log='')
Cancel a transaction and go back to initial data values.
encrypt($fieldorvalue, $withQuotes=1)
Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple ...
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
fetch_array($resultset)
Return datas as an array.
getVersionArray()
Return version of database server into an array.
num_rows($resultset)
Return number of lines for result of a SELECT.
escapeforlike($stringtoencode)
Escape a string to insert data into a like.
commit($log='')
Validate a database transaction.
fetch_row($resultset)
Return datas as an array.
Definition: TraceableDB.php:96
DDLCreateDb($database, $charset='', $collation='', $owner='')
Create a new database Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated We fo...
DDLAddField($table, $field_name, $field_desc, $field_position="")
Create a new field into table.
affected_rows($resultset)
Return the number o flines into the result of a request INSERT, DELETE or UPDATE.
DDLListTablesFull($database, $table='')
List tables into a database with table info.
decrypt($value)
Decrypt sensitive data in database.
DDLInfoTable($table)
List information of columns into a table.
lasterror()
Return last error label.
__construct($db)
Constructor.
Definition: TraceableDB.php:69
getDefaultCollationDatabase()
Return collation used in database.
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.
ifsql($test, $resok, $resko)
Format a SQL IF.
Definition: TraceableDB.php:84
getServerParametersValues($filter='')
Return value of server parameters.
DDLGetConnectId()
Return connection ID.
lastqueryerror()
Return last query in error.
getDefaultCharacterSetDatabase()
Return charset used to store data in database.
getListOfCharacterSet()
Return list of available charset that can be used to store data in database.
endTracing($sql, $resql)
End query tracing.
getListOfCollation()
Return list of available collation that can be used for database.
connect($host, $login, $passwd, $name, $port=0)
Connection to server.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
DDLDropField($table, $field_name)
Drop a field from table.
lastquery()
Return last request executed with query()
errno()
Return generic error code of last operation.
DDLDescTable($table, $field="")
Return a pointer of line with description of a table or field.
lasterrno()
Return last error code.
DDLListTables($database, $table='')
List tables into a database.
plimit($limit=0, $offset=0)
Define limits and offset of request.
idate($param, $gm='tzserver')
Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date fiel...
DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys=null, $fulltext_keys=null, $keys=null)
Create a table into database.
getServerStatusValues($filter='')
Return value of server status.
startTracing()
Start query tracing.
DDLUpdateField($table, $field_name, $field_desc)
Update format of a field into a table.
close()
Close database connection.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
getVersion()
Return version of database server.
error()
Return description of last error.
const LABEL
@const Database label
Definition: TraceableDB.php:58
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)
DDLDropTable($table)
Drop a table into database.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123