dolibarr  x.y.z
TraceableDB.php
1 <?php
2 
3 require_once DOL_DOCUMENT_ROOT.'/core/db/DoliDB.class.php';
4 
10 class TraceableDB extends DoliDB
11 {
15  public $db; // cannot be protected because of parent declaration
19  public $queries;
23  protected $startTime;
27  protected $startMemory;
31  public $type;
35  const LABEL = ''; // TODO: the right value should be $this->db::LABEL (but this is a constant? o_O)
39  const VERSIONMIN = ''; // TODO: the same thing here, $this->db::VERSIONMIN is the right value
40 
46  public function __construct($db)
47  {
48  $this->db = $db;
49  $this->type = $this->db->type;
50  $this->queries = array();
51  }
52 
61  public function ifsql($test, $resok, $resko)
62  {
63  return $this->db->ifsql($test, $resok, $resko);
64  }
65 
66  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
73  public function fetch_row($resultset)
74  {
75  // phpcs:enable
76  return $this->db->fetch_row($resultset);
77  }
78 
87  public function idate($param, $gm = 'tzserver')
88  {
89  return $this->db->idate($param, $gm);
90  }
91 
97  public function lasterrno()
98  {
99  return $this->db->lasterrno();
100  }
101 
108  public function begin($textinlog = '')
109  {
110  return $this->db->begin($textinlog);
111  }
112 
124  public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
125  {
126  return $this->db->DDLCreateDb($database, $charset, $collation, $owner);
127  }
128 
134  public function getVersionArray()
135  {
136  return $this->db->getVersionArray();
137  }
138 
146  public static function convertSQLFromMysql($line, $type = 'ddl')
147  {
148  return self::$db->convertSQLFromMysql($line);
149  }
150 
151  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
159  public function affected_rows($resultset)
160  {
161  // phpcs:enable
162  return $this->db->affected_rows($resultset);
163  }
164 
170  public function error()
171  {
172  return $this->db->error();
173  }
174 
182  public function DDLListTables($database, $table = '')
183  {
184  return $this->db->DDLListTables($database, $table);
185  }
186 
192  public function lastquery()
193  {
194  return $this->db->lastquery();
195  }
196 
204  public function order($sortfield = null, $sortorder = null)
205  {
206  return $this->db->order($sortfield, $sortorder);
207  }
208 
215  public function decrypt($value)
216  {
217  return $this->db->decrypt($value);
218  }
219 
220  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
227  public function fetch_array($resultset)
228  {
229  // phpcs:enable
230  return $this->db->fetch_array($resultset);
231  }
232 
238  public function lasterror()
239  {
240  return $this->db->lasterror();
241  }
242 
249  public function escape($stringtoencode)
250  {
251  return $this->db->escape($stringtoencode);
252  }
253 
260  public function escapeforlike($stringtoencode)
261  {
262  return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode);
263  }
264 
265  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
273  public function last_insert_id($tab, $fieldid = 'rowid')
274  {
275  // phpcs:enable
276  return $this->db->last_insert_id($tab, $fieldid);
277  }
278 
284  public function getPathOfRestore()
285  {
286  return $this->db->getPathOfRestore();
287  }
288 
295  public function rollback($log = '')
296  {
297  return $this->db->rollback($log);
298  }
299 
310  public function query($query, $usesavepoint = 0, $type = 'auto', $result_mode = 0)
311  {
312  $this->startTracing();
313 
314  $resql = $this->db->query($query, $usesavepoint, $type, $result_mode);
315 
316  $this->endTracing($query, $resql);
317 
318  return $resql;
319  }
320 
326  protected function startTracing()
327  {
328  $this->startTime = microtime(true);
329  $this->startMemory = memory_get_usage(true);
330  }
331 
339  protected function endTracing($sql, $resql)
340  {
341  $endTime = microtime(true);
342  $duration = $endTime - $this->startTime;
343  $endMemory = memory_get_usage(true);
344  $memoryDelta = $endMemory - $this->startMemory;
345 
346  $this->queries[] = array(
347  'sql' => $sql,
348  'duration' => $duration,
349  'memory_usage' => $memoryDelta,
350  'is_success' => $resql ? true : false,
351  'error_code' => $resql ? null : $this->db->lasterrno(),
352  'error_message' => $resql ? null : $this->db->lasterror()
353  );
354  }
355 
367  public function connect($host, $login, $passwd, $name, $port = 0)
368  {
369  return $this->db->connect($host, $login, $passwd, $name, $port);
370  }
371 
379  public function plimit($limit = 0, $offset = 0)
380  {
381  return $this->db->plimit($limit, $offset);
382  }
383 
390  public function getServerParametersValues($filter = '')
391  {
392  return $this->db->getServerParametersValues($filter);
393  }
394 
401  public function getServerStatusValues($filter = '')
402  {
403  return $this->db->getServerStatusValues($filter);
404  }
405 
411  public function getDefaultCollationDatabase()
412  {
413  return $this->db->getDefaultCollationDatabase();
414  }
415 
416  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
424  public function num_rows($resultset)
425  {
426  // phpcs:enable
427  return $this->db->num_rows($resultset);
428  }
429 
435  public function getPathOfDump()
436  {
437  return $this->db->getPathOfDump();
438  }
439 
445  public function getDriverInfo()
446  {
447  return $this->db->getDriverInfo();
448  }
449 
455  public function errno()
456  {
457  return $this->db->errno();
458  }
459 
472  public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
473  {
474  return $this->db->DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys, $fulltext_keys, $keys);
475  }
476 
483  public function DDLDropTable($table)
484  {
485  return $this->db->DDLDropTable($table);
486  }
487 
493  public function getListOfCharacterSet()
494  {
495  return $this->db->getListOfCharacterSet();
496  }
497 
507  public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
508  {
509  return $this->db->DDLAddField($table, $field_name, $field_desc, $field_position);
510  }
511 
519  public function DDLDropField($table, $field_name)
520  {
521  return $this->db->DDLDropField($table, $field_name);
522  }
523 
532  public function DDLUpdateField($table, $field_name, $field_desc)
533  {
534  return $this->db->DDLUpdateField($table, $field_name, $field_desc);
535  }
536 
542  public function getListOfCollation()
543  {
544  return $this->db->getListOfCollation();
545  }
546 
554  public function DDLDescTable($table, $field = "")
555  {
556  return $this->db->DDLDescTable($table, $field);
557  }
558 
564  public function getVersion()
565  {
566  return $this->db->getVersion();
567  }
568 
575  {
576  return $this->db->getDefaultCharacterSetDatabase();
577  }
578 
588  public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
589  {
590  return $this->db->DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name);
591  }
592 
593  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
603  public function jdate($string, $gm = false)
604  {
605  // phpcs:enable
606  return $this->db->jdate($string, $gm);
607  }
608 
617  public function encrypt($fieldorvalue, $withQuotes = 1)
618  {
619  return $this->db->encrypt($fieldorvalue, $withQuotes);
620  }
621 
628  public function commit($log = '')
629  {
630  return $this->db->commit($log);
631  }
632 
639  public function DDLInfoTable($table)
640  {
641  return $this->db->DDLInfoTable($table);
642  }
643 
650  public function free($resultset = null)
651  {
652  $this->db->free($resultset);
653  }
654 
661  public function close()
662  {
663  return $this->db->close();
664  }
665 
671  public function lastqueryerror()
672  {
673  return $this->db->lastqueryerror();
674  }
675 
681  public function DDLGetConnectId()
682  {
683  return $this->db->DDLGetConnectId();
684  }
685 
686  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
693  public function fetch_object($resultset)
694  {
695  // phpcs:enable
696  return $this->db->fetch_object($resultset);
697  }
698 
699  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
706  public function select_db($database)
707  {
708  // phpcs:enable
709  return $this->db->select_db($database);
710  }
711 }
Class to manage Dolibarr database access.
TraceableDB class.
Definition: TraceableDB.php:11
escape($stringtoencode)
Escape a string to insert data.
const VERSIONMIN
@const Version min database
Definition: TraceableDB.php:39
jdate($string, $gm=false)
Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -...
static convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
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:73
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.
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:46
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:61
getServerParametersValues($filter='')
Return value of server parameters.
DDLGetConnectId()
Return connexion 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)
Connexion 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.
Definition: TraceableDB.php:97
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...
Definition: TraceableDB.php:87
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 connexion.
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:35
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('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
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:119
$conf db
API class for accounts.
Definition: inc.php:41