dolibarr  19.0.0-dev
TraceableDB.php
1 <?php
2 /* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT.'/core/db/DoliDB.class.php';
25 
31 class TraceableDB extends DoliDB
32 {
36  public $db; // cannot be protected because of parent declaration
40  public $queries;
44  protected $startTime;
48  protected $startMemory;
52  public $type;
56  const LABEL = ''; // TODO: the right value should be $this->db::LABEL (but this is a constant? o_O)
60  const VERSIONMIN = ''; // TODO: the same thing here, $this->db::VERSIONMIN is the right value
61 
67  public function __construct($db)
68  {
69  $this->db = $db;
70  $this->type = $this->db->type;
71  $this->queries = array();
72  }
73 
82  public function ifsql($test, $resok, $resko)
83  {
84  return $this->db->ifsql($test, $resok, $resko);
85  }
86 
87  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
94  public function fetch_row($resultset)
95  {
96  // phpcs:enable
97  return $this->db->fetch_row($resultset);
98  }
99 
108  public function idate($param, $gm = 'tzserver')
109  {
110  return $this->db->idate($param, $gm);
111  }
112 
118  public function lasterrno()
119  {
120  return $this->db->lasterrno();
121  }
122 
129  public function begin($textinlog = '')
130  {
131  return $this->db->begin($textinlog);
132  }
133 
145  public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
146  {
147  return $this->db->DDLCreateDb($database, $charset, $collation, $owner);
148  }
149 
155  public function getVersionArray()
156  {
157  return $this->db->getVersionArray();
158  }
159 
167  public function convertSQLFromMysql($line, $type = 'ddl')
168  {
169  return $this->db->convertSQLFromMysql($line);
170  }
171 
172  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
180  public function affected_rows($resultset)
181  {
182  // phpcs:enable
183  return $this->db->affected_rows($resultset);
184  }
185 
191  public function error()
192  {
193  return $this->db->error();
194  }
195 
203  public function DDLListTables($database, $table = '')
204  {
205  return $this->db->DDLListTables($database, $table);
206  }
207 
215  public function DDLListTablesFull($database, $table = '')
216  {
217  return $this->db->DDLListTablesFull($database, $table);
218  }
219 
225  public function lastquery()
226  {
227  return $this->db->lastquery();
228  }
229 
237  public function order($sortfield = null, $sortorder = null)
238  {
239  return $this->db->order($sortfield, $sortorder);
240  }
241 
248  public function decrypt($value)
249  {
250  return $this->db->decrypt($value);
251  }
252 
253  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
260  public function fetch_array($resultset)
261  {
262  // phpcs:enable
263  return $this->db->fetch_array($resultset);
264  }
265 
271  public function lasterror()
272  {
273  return $this->db->lasterror();
274  }
275 
282  public function escape($stringtoencode)
283  {
284  return $this->db->escape($stringtoencode);
285  }
286 
293  public function escapeforlike($stringtoencode)
294  {
295  return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode);
296  }
297 
298  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
306  public function last_insert_id($tab, $fieldid = 'rowid')
307  {
308  // phpcs:enable
309  return $this->db->last_insert_id($tab, $fieldid);
310  }
311 
317  public function getPathOfRestore()
318  {
319  return $this->db->getPathOfRestore();
320  }
321 
328  public function rollback($log = '')
329  {
330  return $this->db->rollback($log);
331  }
332 
343  public function query($query, $usesavepoint = 0, $type = 'auto', $result_mode = 0)
344  {
345  $this->startTracing();
346 
347  $resql = $this->db->query($query, $usesavepoint, $type, $result_mode);
348 
349  $this->endTracing($query, $resql);
350 
351  return $resql;
352  }
353 
359  protected function startTracing()
360  {
361  $this->startTime = microtime(true);
362  $this->startMemory = memory_get_usage(true);
363  }
364 
372  protected function endTracing($sql, $resql)
373  {
374  $endTime = microtime(true);
375  $duration = $endTime - $this->startTime;
376  $endMemory = memory_get_usage(true);
377  $memoryDelta = $endMemory - $this->startMemory;
378 
379  $this->queries[] = array(
380  'sql' => $sql,
381  'duration' => $duration,
382  'memory_usage' => $memoryDelta,
383  'is_success' => $resql ? true : false,
384  'error_code' => $resql ? null : $this->db->lasterrno(),
385  'error_message' => $resql ? null : $this->db->lasterror()
386  );
387  }
388 
400  public function connect($host, $login, $passwd, $name, $port = 0)
401  {
402  return $this->db->connect($host, $login, $passwd, $name, $port);
403  }
404 
412  public function plimit($limit = 0, $offset = 0)
413  {
414  return $this->db->plimit($limit, $offset);
415  }
416 
423  public function getServerParametersValues($filter = '')
424  {
425  return $this->db->getServerParametersValues($filter);
426  }
427 
434  public function getServerStatusValues($filter = '')
435  {
436  return $this->db->getServerStatusValues($filter);
437  }
438 
444  public function getDefaultCollationDatabase()
445  {
446  return $this->db->getDefaultCollationDatabase();
447  }
448 
449  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
457  public function num_rows($resultset)
458  {
459  // phpcs:enable
460  return $this->db->num_rows($resultset);
461  }
462 
468  public function getPathOfDump()
469  {
470  return $this->db->getPathOfDump();
471  }
472 
478  public function getDriverInfo()
479  {
480  return $this->db->getDriverInfo();
481  }
482 
488  public function errno()
489  {
490  return $this->db->errno();
491  }
492 
505  public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
506  {
507  return $this->db->DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys, $fulltext_keys, $keys);
508  }
509 
516  public function DDLDropTable($table)
517  {
518  return $this->db->DDLDropTable($table);
519  }
520 
526  public function getListOfCharacterSet()
527  {
528  return $this->db->getListOfCharacterSet();
529  }
530 
540  public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
541  {
542  return $this->db->DDLAddField($table, $field_name, $field_desc, $field_position);
543  }
544 
552  public function DDLDropField($table, $field_name)
553  {
554  return $this->db->DDLDropField($table, $field_name);
555  }
556 
565  public function DDLUpdateField($table, $field_name, $field_desc)
566  {
567  return $this->db->DDLUpdateField($table, $field_name, $field_desc);
568  }
569 
575  public function getListOfCollation()
576  {
577  return $this->db->getListOfCollation();
578  }
579 
587  public function DDLDescTable($table, $field = "")
588  {
589  return $this->db->DDLDescTable($table, $field);
590  }
591 
597  public function getVersion()
598  {
599  return $this->db->getVersion();
600  }
601 
608  {
609  return $this->db->getDefaultCharacterSetDatabase();
610  }
611 
621  public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
622  {
623  return $this->db->DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name);
624  }
625 
626  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
636  public function jdate($string, $gm = false)
637  {
638  // phpcs:enable
639  return $this->db->jdate($string, $gm);
640  }
641 
650  public function encrypt($fieldorvalue, $withQuotes = 1)
651  {
652  return $this->db->encrypt($fieldorvalue, $withQuotes);
653  }
654 
661  public function commit($log = '')
662  {
663  return $this->db->commit($log);
664  }
665 
672  public function DDLInfoTable($table)
673  {
674  return $this->db->DDLInfoTable($table);
675  }
676 
683  public function free($resultset = null)
684  {
685  $this->db->free($resultset);
686  }
687 
694  public function close()
695  {
696  return $this->db->close();
697  }
698 
704  public function lastqueryerror()
705  {
706  return $this->db->lastqueryerror();
707  }
708 
714  public function DDLGetConnectId()
715  {
716  return $this->db->DDLGetConnectId();
717  }
718 
719  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
726  public function fetch_object($resultset)
727  {
728  // phpcs:enable
729  return $this->db->fetch_object($resultset);
730  }
731 
732  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
739  public function select_db($database)
740  {
741  // phpcs:enable
742  return $this->db->select_db($database);
743  }
744 }
TraceableDB\connect
connect($host, $login, $passwd, $name, $port=0)
Connexion to server.
Definition: TraceableDB.php:400
TraceableDB\select_db
select_db($database)
Select a database.
Definition: TraceableDB.php:739
TraceableDB\LABEL
const LABEL
@const Database label
Definition: TraceableDB.php:56
TraceableDB\fetch_object
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
Definition: TraceableDB.php:726
TraceableDB\DDLCreateDb
DDLCreateDb($database, $charset='', $collation='', $owner='')
Create a new database Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated We fo...
Definition: TraceableDB.php:145
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
TraceableDB\errno
errno()
Return generic error code of last operation.
Definition: TraceableDB.php:488
TraceableDB\getServerParametersValues
getServerParametersValues($filter='')
Return value of server parameters.
Definition: TraceableDB.php:423
TraceableDB\getDefaultCollationDatabase
getDefaultCollationDatabase()
Return collation used in database.
Definition: TraceableDB.php:444
TraceableDB\getPathOfDump
getPathOfDump()
Return full path of dump program.
Definition: TraceableDB.php:468
TraceableDB\getVersionArray
getVersionArray()
Return version of database server into an array.
Definition: TraceableDB.php:155
TraceableDB\lasterrno
lasterrno()
Return last error code.
Definition: TraceableDB.php:118
TraceableDB\escapeforlike
escapeforlike($stringtoencode)
Escape a string to insert data into a like.
Definition: TraceableDB.php:293
TraceableDB\DDLDropTable
DDLDropTable($table)
Drop a table into database.
Definition: TraceableDB.php:516
TraceableDB\plimit
plimit($limit=0, $offset=0)
Define limits and offset of request.
Definition: TraceableDB.php:412
TraceableDB\getListOfCharacterSet
getListOfCharacterSet()
Return list of available charset that can be used to store data in database.
Definition: TraceableDB.php:526
TraceableDB\close
close()
Close database connexion.
Definition: TraceableDB.php:694
TraceableDB\getVersion
getVersion()
Return version of database server.
Definition: TraceableDB.php:597
TraceableDB\free
free($resultset=null)
Free last resultset used.
Definition: TraceableDB.php:683
TraceableDB\error
error()
Return description of last error.
Definition: TraceableDB.php:191
TraceableDB\VERSIONMIN
const VERSIONMIN
@const Version min database
Definition: TraceableDB.php:60
TraceableDB\DDLUpdateField
DDLUpdateField($table, $field_name, $field_desc)
Update format of a field into a table.
Definition: TraceableDB.php:565
TraceableDB\endTracing
endTracing($sql, $resql)
End query tracing.
Definition: TraceableDB.php:372
TraceableDB\commit
commit($log='')
Validate a database transaction.
Definition: TraceableDB.php:661
TraceableDB\convertSQLFromMysql
convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
Definition: TraceableDB.php:167
TraceableDB\jdate
jdate($string, $gm=false)
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
Definition: TraceableDB.php:636
TraceableDB\fetch_array
fetch_array($resultset)
Return datas as an array.
Definition: TraceableDB.php:260
TraceableDB\encrypt
encrypt($fieldorvalue, $withQuotes=1)
Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple ...
Definition: TraceableDB.php:650
TraceableDB\DDLListTablesFull
DDLListTablesFull($database, $table='')
List tables into a database with table info.
Definition: TraceableDB.php:215
TraceableDB\decrypt
decrypt($value)
Decrypt sensitive data in database.
Definition: TraceableDB.php:248
TraceableDB\escape
escape($stringtoencode)
Escape a string to insert data.
Definition: TraceableDB.php:282
TraceableDB\num_rows
num_rows($resultset)
Return number of lines for result of a SELECT.
Definition: TraceableDB.php:457
TraceableDB\last_insert_id
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.
Definition: TraceableDB.php:306
TraceableDB\begin
begin($textinlog='')
Start transaction.
Definition: TraceableDB.php:129
TraceableDB\DDLInfoTable
DDLInfoTable($table)
List information of columns into a table.
Definition: TraceableDB.php:672
TraceableDB\getServerStatusValues
getServerStatusValues($filter='')
Return value of server status.
Definition: TraceableDB.php:434
TraceableDB\getDefaultCharacterSetDatabase
getDefaultCharacterSetDatabase()
Return charset used to store data in database.
Definition: TraceableDB.php:607
TraceableDB\DDLCreateTable
DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys=null, $fulltext_keys=null, $keys=null)
Create a table into database.
Definition: TraceableDB.php:505
TraceableDB\query
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
Definition: TraceableDB.php:343
TraceableDB\getPathOfRestore
getPathOfRestore()
Return full path of restore program.
Definition: TraceableDB.php:317
TraceableDB\DDLDescTable
DDLDescTable($table, $field="")
Return a pointer of line with description of a table or field.
Definition: TraceableDB.php:587
TraceableDB\DDLGetConnectId
DDLGetConnectId()
Return connexion ID.
Definition: TraceableDB.php:714
TraceableDB\fetch_row
fetch_row($resultset)
Return datas as an array.
Definition: TraceableDB.php:94
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
TraceableDB\order
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
Definition: TraceableDB.php:237
TraceableDB\DDLDropField
DDLDropField($table, $field_name)
Drop a field from table.
Definition: TraceableDB.php:552
TraceableDB
TraceableDB class.
Definition: TraceableDB.php:31
TraceableDB\DDLListTables
DDLListTables($database, $table='')
List tables into a database.
Definition: TraceableDB.php:203
TraceableDB\lasterror
lasterror()
Return last error label.
Definition: TraceableDB.php:271
TraceableDB\__construct
__construct($db)
Constructor.
Definition: TraceableDB.php:67
TraceableDB\ifsql
ifsql($test, $resok, $resko)
Format a SQL IF.
Definition: TraceableDB.php:82
TraceableDB\affected_rows
affected_rows($resultset)
Return the number o flines into the result of a request INSERT, DELETE or UPDATE.
Definition: TraceableDB.php:180
TraceableDB\startTracing
startTracing()
Start query tracing.
Definition: TraceableDB.php:359
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120
TraceableDB\getListOfCollation
getListOfCollation()
Return list of available collation that can be used for database.
Definition: TraceableDB.php:575
TraceableDB\lastquery
lastquery()
Return last request executed with query()
Definition: TraceableDB.php:225
TraceableDB\lastqueryerror
lastqueryerror()
Return last query in error.
Definition: TraceableDB.php:704
TraceableDB\rollback
rollback($log='')
Cancel a transaction and go back to initial data values.
Definition: TraceableDB.php:328
TraceableDB\idate
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...
Definition: TraceableDB.php:108
TraceableDB\DDLAddField
DDLAddField($table, $field_name, $field_desc, $field_position="")
Create a new field into table.
Definition: TraceableDB.php:540
TraceableDB\DDLCreateUser
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)
Definition: TraceableDB.php:621
TraceableDB\getDriverInfo
getDriverInfo()
Return version of database client driver.
Definition: TraceableDB.php:478