dolibarr  16.0.5
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 
107  public function begin()
108  {
109  return $this->db->begin();
110  }
111 
123  public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
124  {
125  return $this->db->DDLCreateDb($database, $charset, $collation, $owner);
126  }
127 
133  public function getVersionArray()
134  {
135  return $this->db->getVersionArray();
136  }
137 
145  public static function convertSQLFromMysql($line, $type = 'ddl')
146  {
147  return self::$db->convertSQLFromMysql($line);
148  }
149 
150  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
158  public function affected_rows($resultset)
159  {
160  // phpcs:enable
161  return $this->db->affected_rows($resultset);
162  }
163 
169  public function error()
170  {
171  return $this->db->error();
172  }
173 
181  public function DDLListTables($database, $table = '')
182  {
183  return $this->db->DDLListTables($database, $table);
184  }
185 
191  public function lastquery()
192  {
193  return $this->db->lastquery();
194  }
195 
203  public function order($sortfield = null, $sortorder = null)
204  {
205  return $this->db->order($sortfield, $sortorder);
206  }
207 
214  public function decrypt($value)
215  {
216  return $this->db->decrypt($value);
217  }
218 
219  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
226  public function fetch_array($resultset)
227  {
228  // phpcs:enable
229  return $this->db->fetch_array($resultset);
230  }
231 
237  public function lasterror()
238  {
239  return $this->db->lasterror();
240  }
241 
248  public function escape($stringtoencode)
249  {
250  return $this->db->escape($stringtoencode);
251  }
252 
260  public function escapeunderscore($stringtoencode)
261  {
262  return $this->db->escapeunderscore($stringtoencode);
263  }
264 
271  public function escapeforlike($stringtoencode)
272  {
273  return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode);
274  }
275 
276  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
284  public function last_insert_id($tab, $fieldid = 'rowid')
285  {
286  // phpcs:enable
287  return $this->db->last_insert_id($tab, $fieldid);
288  }
289 
295  public function getPathOfRestore()
296  {
297  return $this->db->getPathOfRestore();
298  }
299 
306  public function rollback($log = '')
307  {
308  return $this->db->rollback($log);
309  }
310 
321  public function query($query, $usesavepoint = 0, $type = 'auto', $result_mode = 0)
322  {
323  $this->startTracing();
324 
325  $resql = $this->db->query($query, $usesavepoint, $type, $result_mode);
326 
327  $this->endTracing($query, $resql);
328 
329  return $resql;
330  }
331 
337  protected function startTracing()
338  {
339  $this->startTime = microtime(true);
340  $this->startMemory = memory_get_usage(true);
341  }
342 
350  protected function endTracing($sql, $resql)
351  {
352  $endTime = microtime(true);
353  $duration = $endTime - $this->startTime;
354  $endMemory = memory_get_usage(true);
355  $memoryDelta = $endMemory - $this->startMemory;
356 
357  $this->queries[] = array(
358  'sql' => $sql,
359  'duration' => $duration,
360  'memory_usage' => $memoryDelta,
361  'is_success' => $resql ? true : false,
362  'error_code' => $resql ? null : $this->db->lasterrno(),
363  'error_message' => $resql ? null : $this->db->lasterror()
364  );
365  }
366 
378  public function connect($host, $login, $passwd, $name, $port = 0)
379  {
380  return $this->db->connect($host, $login, $passwd, $name, $port);
381  }
382 
390  public function plimit($limit = 0, $offset = 0)
391  {
392  return $this->db->plimit($limit, $offset);
393  }
394 
401  public function getServerParametersValues($filter = '')
402  {
403  return $this->db->getServerParametersValues($filter);
404  }
405 
412  public function getServerStatusValues($filter = '')
413  {
414  return $this->db->getServerStatusValues($filter);
415  }
416 
422  public function getDefaultCollationDatabase()
423  {
424  return $this->db->getDefaultCollationDatabase();
425  }
426 
427  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
435  public function num_rows($resultset)
436  {
437  // phpcs:enable
438  return $this->db->num_rows($resultset);
439  }
440 
446  public function getPathOfDump()
447  {
448  return $this->db->getPathOfDump();
449  }
450 
456  public function getDriverInfo()
457  {
458  return $this->db->getDriverInfo();
459  }
460 
466  public function errno()
467  {
468  return $this->db->errno();
469  }
470 
483  public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
484  {
485  return $this->db->DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys, $fulltext_keys, $keys);
486  }
487 
494  public function DDLDropTable($table)
495  {
496  return $this->db->DDLDropTable($table);
497  }
498 
504  public function getListOfCharacterSet()
505  {
506  return $this->db->getListOfCharacterSet();
507  }
508 
518  public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
519  {
520  return $this->db->DDLAddField($table, $field_name, $field_desc, $field_position);
521  }
522 
530  public function DDLDropField($table, $field_name)
531  {
532  return $this->db->DDLDropField($table, $field_name);
533  }
534 
543  public function DDLUpdateField($table, $field_name, $field_desc)
544  {
545  return $this->db->DDLUpdateField($table, $field_name, $field_desc);
546  }
547 
553  public function getListOfCollation()
554  {
555  return $this->db->getListOfCollation();
556  }
557 
565  public function DDLDescTable($table, $field = "")
566  {
567  return $this->db->DDLDescTable($table, $field);
568  }
569 
575  public function getVersion()
576  {
577  return $this->db->getVersion();
578  }
579 
586  {
587  return $this->db->getDefaultCharacterSetDatabase();
588  }
589 
599  public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
600  {
601  return $this->db->DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name);
602  }
603 
604  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
614  public function jdate($string, $gm = false)
615  {
616  // phpcs:enable
617  return $this->db->jdate($string, $gm);
618  }
619 
628  public function encrypt($fieldorvalue, $withQuotes = 1)
629  {
630  return $this->db->encrypt($fieldorvalue, $withQuotes);
631  }
632 
639  public function commit($log = '')
640  {
641  return $this->db->commit($log);
642  }
643 
650  public function DDLInfoTable($table)
651  {
652  return $this->db->DDLInfoTable($table);
653  }
654 
661  public function free($resultset = null)
662  {
663  return $this->db->free($resultset);
664  }
665 
672  public function close()
673  {
674  return $this->db->close();
675  }
676 
682  public function lastqueryerror()
683  {
684  return $this->db->lastqueryerror();
685  }
686 
692  public function DDLGetConnectId()
693  {
694  return $this->db->DDLGetConnectId();
695  }
696 
697  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
704  public function fetch_object($resultset)
705  {
706  // phpcs:enable
707  return $this->db->fetch_object($resultset);
708  }
709 
710  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
717  public function select_db($database)
718  {
719  // phpcs:enable
720  return $this->db->select_db($database);
721  }
722 }
TraceableDB\connect
connect($host, $login, $passwd, $name, $port=0)
Connexion to server.
Definition: TraceableDB.php:378
TraceableDB\select_db
select_db($database)
Select a database.
Definition: TraceableDB.php:717
db
$conf db
API class for accounts.
Definition: inc.php:41
TraceableDB\LABEL
const LABEL
@const Database label
Definition: TraceableDB.php:35
TraceableDB\begin
begin()
Start transaction.
Definition: TraceableDB.php:107
TraceableDB\fetch_object
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
Definition: TraceableDB.php:704
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:123
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:466
TraceableDB\getServerParametersValues
getServerParametersValues($filter='')
Return value of server parameters.
Definition: TraceableDB.php:401
TraceableDB\getDefaultCollationDatabase
getDefaultCollationDatabase()
Return collation used in database.
Definition: TraceableDB.php:422
TraceableDB\getPathOfDump
getPathOfDump()
Return full path of dump program.
Definition: TraceableDB.php:446
TraceableDB\getVersionArray
getVersionArray()
Return version of database server into an array.
Definition: TraceableDB.php:133
TraceableDB\lasterrno
lasterrno()
Return last error code.
Definition: TraceableDB.php:97
TraceableDB\escapeforlike
escapeforlike($stringtoencode)
Escape a string to insert data into a like.
Definition: TraceableDB.php:271
TraceableDB\DDLDropTable
DDLDropTable($table)
Drop a table into database.
Definition: TraceableDB.php:494
TraceableDB\plimit
plimit($limit=0, $offset=0)
Define limits and offset of request.
Definition: TraceableDB.php:390
TraceableDB\getListOfCharacterSet
getListOfCharacterSet()
Return list of available charset that can be used to store data in database.
Definition: TraceableDB.php:504
TraceableDB\close
close()
Close database connexion.
Definition: TraceableDB.php:672
TraceableDB\getVersion
getVersion()
Return version of database server.
Definition: TraceableDB.php:575
TraceableDB\free
free($resultset=null)
Free last resultset used.
Definition: TraceableDB.php:661
TraceableDB\error
error()
Return description of last error.
Definition: TraceableDB.php:169
TraceableDB\VERSIONMIN
const VERSIONMIN
@const Version min database
Definition: TraceableDB.php:39
TraceableDB\DDLUpdateField
DDLUpdateField($table, $field_name, $field_desc)
Update format of a field into a table.
Definition: TraceableDB.php:543
TraceableDB\endTracing
endTracing($sql, $resql)
End query tracing.
Definition: TraceableDB.php:350
TraceableDB\commit
commit($log='')
Validate a database transaction.
Definition: TraceableDB.php:639
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:614
TraceableDB\fetch_array
fetch_array($resultset)
Return datas as an array.
Definition: TraceableDB.php:226
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:628
TraceableDB\escapeunderscore
escapeunderscore($stringtoencode)
Escape a string to insert data.
Definition: TraceableDB.php:260
TraceableDB\decrypt
decrypt($value)
Decrypt sensitive data in database.
Definition: TraceableDB.php:214
TraceableDB\escape
escape($stringtoencode)
Escape a string to insert data.
Definition: TraceableDB.php:248
TraceableDB\num_rows
num_rows($resultset)
Return number of lines for result of a SELECT.
Definition: TraceableDB.php:435
TraceableDB\last_insert_id
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.
Definition: TraceableDB.php:284
TraceableDB\DDLInfoTable
DDLInfoTable($table)
List information of columns into a table.
Definition: TraceableDB.php:650
TraceableDB\getServerStatusValues
getServerStatusValues($filter='')
Return value of server status.
Definition: TraceableDB.php:412
TraceableDB\getDefaultCharacterSetDatabase
getDefaultCharacterSetDatabase()
Return charset used to store data in database.
Definition: TraceableDB.php:585
TraceableDB\DDLCreateTable
DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys=null, $fulltext_keys=null, $keys=null)
Create a table into database.
Definition: TraceableDB.php:483
TraceableDB\query
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
Definition: TraceableDB.php:321
TraceableDB\getPathOfRestore
getPathOfRestore()
Return full path of restore program.
Definition: TraceableDB.php:295
TraceableDB\DDLDescTable
DDLDescTable($table, $field="")
Return a pointer of line with description of a table or field.
Definition: TraceableDB.php:565
TraceableDB\DDLGetConnectId
DDLGetConnectId()
Return connexion ID.
Definition: TraceableDB.php:692
TraceableDB\fetch_row
fetch_row($resultset)
Return datas as an array.
Definition: TraceableDB.php:73
TraceableDB\order
order($sortfield=null, $sortorder=null)
Define sort criteria of request.
Definition: TraceableDB.php:203
TraceableDB\DDLDropField
DDLDropField($table, $field_name)
Drop a field from table.
Definition: TraceableDB.php:530
TraceableDB
TraceableDB class.
Definition: TraceableDB.php:10
TraceableDB\convertSQLFromMysql
static convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
Definition: TraceableDB.php:145
TraceableDB\DDLListTables
DDLListTables($database, $table='')
List tables into a database.
Definition: TraceableDB.php:181
TraceableDB\lasterror
lasterror()
Return last error label.
Definition: TraceableDB.php:237
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
TraceableDB\__construct
__construct($db)
Constructor.
Definition: TraceableDB.php:46
TraceableDB\ifsql
ifsql($test, $resok, $resko)
Format a SQL IF.
Definition: TraceableDB.php:61
TraceableDB\affected_rows
affected_rows($resultset)
Return the number o flines into the result of a request INSERT, DELETE or UPDATE.
Definition: TraceableDB.php:158
TraceableDB\startTracing
startTracing()
Start query tracing.
Definition: TraceableDB.php:337
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
TraceableDB\getListOfCollation
getListOfCollation()
Return list of available collation that can be used for database.
Definition: TraceableDB.php:553
TraceableDB\lastquery
lastquery()
Return last request executed with query()
Definition: TraceableDB.php:191
TraceableDB\lastqueryerror
lastqueryerror()
Return last query in error.
Definition: TraceableDB.php:682
TraceableDB\rollback
rollback($log='')
Cancel a transaction and go back to initial data values.
Definition: TraceableDB.php:306
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:87
TraceableDB\DDLAddField
DDLAddField($table, $field_name, $field_desc, $field_position="")
Create a new field into table.
Definition: TraceableDB.php:518
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:599
TraceableDB\getDriverInfo
getDriverInfo()
Return version of database client driver.
Definition: TraceableDB.php:456