TraceableDB.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/core/db/DoliDB.class.php';
  3. /**
  4. * TraceableDB class
  5. *
  6. * Used to log queries into DebugBar
  7. */
  8. class TraceableDB extends DoliDB
  9. {
  10. /**
  11. * @var DoliDb Database handler
  12. */
  13. public $db; // cannot be protected because of parent declaration
  14. /**
  15. * @var array Queries array
  16. */
  17. public $queries;
  18. /**
  19. * @var int Request start time
  20. */
  21. protected $startTime;
  22. /**
  23. * @var int Request start memory
  24. */
  25. protected $startMemory;
  26. /**
  27. * @var string type
  28. */
  29. public $type;
  30. /**
  31. * @const Database label
  32. */
  33. const LABEL = ''; // TODO: the right value should be $this->db::LABEL (but this is a constant? o_O)
  34. /**
  35. * @const Version min database
  36. */
  37. const VERSIONMIN = ''; // TODO: the same thing here, $this->db::VERSIONMIN is the right value
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. public function __construct($db)
  44. {
  45. $this->db = $db;
  46. $this->type = $this->db->type;
  47. $this->queries = array();
  48. }
  49. /**
  50. * Format a SQL IF
  51. *
  52. * @param string $test Test string (example: 'cd.statut=0', 'field IS NULL')
  53. * @param string $resok resultat si test egal
  54. * @param string $resko resultat si test non egal
  55. * @return string SQL string
  56. */
  57. public function ifsql($test, $resok, $resko)
  58. {
  59. return $this->db->ifsql($test, $resok, $resko);
  60. }
  61. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  62. /**
  63. * Return datas as an array
  64. *
  65. * @param resource $resultset Resultset of request
  66. * @return array Array
  67. */
  68. public function fetch_row($resultset)
  69. {
  70. // phpcs:enable
  71. return $this->db->fetch_row($resultset);
  72. }
  73. /**
  74. * Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
  75. * Function to use to build INSERT, UPDATE or WHERE predica
  76. *
  77. * @param int $param Date TMS to convert
  78. * @param mixed $gm 'gmt'=Input informations are GMT values, 'tzserver'=Local to server TZ
  79. * @return string Date in a string YYYY-MM-DD HH:MM:SS
  80. */
  81. public function idate($param, $gm = 'tzserver')
  82. {
  83. return $this->db->idate($param, $gm);
  84. }
  85. /**
  86. * Return last error code
  87. *
  88. * @return string lasterrno
  89. */
  90. public function lasterrno()
  91. {
  92. return $this->db->lasterrno();
  93. }
  94. /**
  95. * Start transaction
  96. *
  97. * @param string $textinlog Add a small text into log. '' by default.
  98. * @return int 1 if transaction successfuly opened or already opened, 0 if error
  99. */
  100. public function begin($textinlog = '')
  101. {
  102. return $this->db->begin($textinlog);
  103. }
  104. /**
  105. * Create a new database
  106. * Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated
  107. * We force to create database with charset this->forcecharset and collate this->forcecollate
  108. *
  109. * @param string $database Database name to create
  110. * @param string $charset Charset used to store data
  111. * @param string $collation Charset used to sort data
  112. * @param string $owner Username of database owner
  113. * @return resource resource defined if OK, null if KO
  114. */
  115. public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '')
  116. {
  117. return $this->db->DDLCreateDb($database, $charset, $collation, $owner);
  118. }
  119. /**
  120. * Return version of database server into an array
  121. *
  122. * @return array Version array
  123. */
  124. public function getVersionArray()
  125. {
  126. return $this->db->getVersionArray();
  127. }
  128. /**
  129. * Convert a SQL request in Mysql syntax to native syntax
  130. *
  131. * @param string $line SQL request line to convert
  132. * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
  133. * @return string SQL request line converted
  134. */
  135. public static function convertSQLFromMysql($line, $type = 'ddl')
  136. {
  137. return self::$db->convertSQLFromMysql($line);
  138. }
  139. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  140. /**
  141. * Return the number o flines into the result of a request INSERT, DELETE or UPDATE
  142. *
  143. * @param resource $resultset Curseur de la requete voulue
  144. * @return int Number of lines
  145. * @see num_rows()
  146. */
  147. public function affected_rows($resultset)
  148. {
  149. // phpcs:enable
  150. return $this->db->affected_rows($resultset);
  151. }
  152. /**
  153. * Return description of last error
  154. *
  155. * @return string Error text
  156. */
  157. public function error()
  158. {
  159. return $this->db->error();
  160. }
  161. /**
  162. * List tables into a database
  163. *
  164. * @param string $database Name of database
  165. * @param string $table Nmae of table filter ('xxx%')
  166. * @return array List of tables in an array
  167. */
  168. public function DDLListTables($database, $table = '')
  169. {
  170. return $this->db->DDLListTables($database, $table);
  171. }
  172. /**
  173. * Return last request executed with query()
  174. *
  175. * @return string Last query
  176. */
  177. public function lastquery()
  178. {
  179. return $this->db->lastquery();
  180. }
  181. /**
  182. * Define sort criteria of request
  183. *
  184. * @param string $sortfield List of sort fields
  185. * @param string $sortorder Sort order
  186. * @return string String to provide syntax of a sort sql string
  187. */
  188. public function order($sortfield = null, $sortorder = null)
  189. {
  190. return $this->db->order($sortfield, $sortorder);
  191. }
  192. /**
  193. * Decrypt sensitive data in database
  194. *
  195. * @param string $value Value to decrypt
  196. * @return string Decrypted value if used
  197. */
  198. public function decrypt($value)
  199. {
  200. return $this->db->decrypt($value);
  201. }
  202. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  203. /**
  204. * Return datas as an array
  205. *
  206. * @param resource $resultset Resultset of request
  207. * @return array Array
  208. */
  209. public function fetch_array($resultset)
  210. {
  211. // phpcs:enable
  212. return $this->db->fetch_array($resultset);
  213. }
  214. /**
  215. * Return last error label
  216. *
  217. * @return string lasterror
  218. */
  219. public function lasterror()
  220. {
  221. return $this->db->lasterror();
  222. }
  223. /**
  224. * Escape a string to insert data
  225. *
  226. * @param string $stringtoencode String to escape
  227. * @return string String escaped
  228. */
  229. public function escape($stringtoencode)
  230. {
  231. return $this->db->escape($stringtoencode);
  232. }
  233. /**
  234. * Escape a string to insert data
  235. *
  236. * @param string $stringtoencode String to escape
  237. * @return string String escaped
  238. * @deprecated
  239. */
  240. public function escapeunderscore($stringtoencode)
  241. {
  242. return $this->db->escapeunderscore($stringtoencode);
  243. }
  244. /**
  245. * Escape a string to insert data into a like
  246. *
  247. * @param string $stringtoencode String to escape
  248. * @return string String escaped
  249. */
  250. public function escapeforlike($stringtoencode)
  251. {
  252. return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode);
  253. }
  254. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  255. /**
  256. * Get last ID after an insert INSERT
  257. *
  258. * @param string $tab Table name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
  259. * @param string $fieldid Field name
  260. * @return int Id of row
  261. */
  262. public function last_insert_id($tab, $fieldid = 'rowid')
  263. {
  264. // phpcs:enable
  265. return $this->db->last_insert_id($tab, $fieldid);
  266. }
  267. /**
  268. * Return full path of restore program
  269. *
  270. * @return string Full path of restore program
  271. */
  272. public function getPathOfRestore()
  273. {
  274. return $this->db->getPathOfRestore();
  275. }
  276. /**
  277. * Cancel a transaction and go back to initial data values
  278. *
  279. * @param string $log Add more log to default log line
  280. * @return resource|int 1 if cancelation is ok or transaction not open, 0 if error
  281. */
  282. public function rollback($log = '')
  283. {
  284. return $this->db->rollback($log);
  285. }
  286. /**
  287. * Execute a SQL request and return the resultset
  288. *
  289. * @param string $query SQL query string
  290. * @param int $usesavepoint 0=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).
  291. * Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints.
  292. * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
  293. * @param int $result_mode Result mode
  294. * @return resource Resultset of answer
  295. */
  296. public function query($query, $usesavepoint = 0, $type = 'auto', $result_mode = 0)
  297. {
  298. $this->startTracing();
  299. $resql = $this->db->query($query, $usesavepoint, $type, $result_mode);
  300. $this->endTracing($query, $resql);
  301. return $resql;
  302. }
  303. /**
  304. * Start query tracing
  305. *
  306. * @return void
  307. */
  308. protected function startTracing()
  309. {
  310. $this->startTime = microtime(true);
  311. $this->startMemory = memory_get_usage(true);
  312. }
  313. /**
  314. * End query tracing
  315. *
  316. * @param string $sql query string
  317. * @param string $resql query result
  318. * @return void
  319. */
  320. protected function endTracing($sql, $resql)
  321. {
  322. $endTime = microtime(true);
  323. $duration = $endTime - $this->startTime;
  324. $endMemory = memory_get_usage(true);
  325. $memoryDelta = $endMemory - $this->startMemory;
  326. $this->queries[] = array(
  327. 'sql' => $sql,
  328. 'duration' => $duration,
  329. 'memory_usage' => $memoryDelta,
  330. 'is_success' => $resql ? true : false,
  331. 'error_code' => $resql ? null : $this->db->lasterrno(),
  332. 'error_message' => $resql ? null : $this->db->lasterror()
  333. );
  334. }
  335. /**
  336. * Connexion to server
  337. *
  338. * @param string $host database server host
  339. * @param string $login login
  340. * @param string $passwd password
  341. * @param string $name name of database (not used for mysql, used for pgsql)
  342. * @param int $port Port of database server
  343. * @return resource Database access handler
  344. * @see close()
  345. */
  346. public function connect($host, $login, $passwd, $name, $port = 0)
  347. {
  348. return $this->db->connect($host, $login, $passwd, $name, $port);
  349. }
  350. /**
  351. * Define limits and offset of request
  352. *
  353. * @param int $limit Maximum number of lines returned (-1=conf->liste_limit, 0=no limit)
  354. * @param int $offset Numero of line from where starting fetch
  355. * @return string String with SQL syntax to add a limit and offset
  356. */
  357. public function plimit($limit = 0, $offset = 0)
  358. {
  359. return $this->db->plimit($limit, $offset);
  360. }
  361. /**
  362. * Return value of server parameters
  363. *
  364. * @param string $filter Filter list on a particular value
  365. * @return array Array of key-values (key=>value)
  366. */
  367. public function getServerParametersValues($filter = '')
  368. {
  369. return $this->db->getServerParametersValues($filter);
  370. }
  371. /**
  372. * Return value of server status
  373. *
  374. * @param string $filter Filter list on a particular value
  375. * @return array Array of key-values (key=>value)
  376. */
  377. public function getServerStatusValues($filter = '')
  378. {
  379. return $this->db->getServerStatusValues($filter);
  380. }
  381. /**
  382. * Return collation used in database
  383. *
  384. * @return string Collation value
  385. */
  386. public function getDefaultCollationDatabase()
  387. {
  388. return $this->db->getDefaultCollationDatabase();
  389. }
  390. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  391. /**
  392. * Return number of lines for result of a SELECT
  393. *
  394. * @param resource $resultset Resulset of requests
  395. * @return int Nb of lines
  396. * @see affected_rows()
  397. */
  398. public function num_rows($resultset)
  399. {
  400. // phpcs:enable
  401. return $this->db->num_rows($resultset);
  402. }
  403. /**
  404. * Return full path of dump program
  405. *
  406. * @return string Full path of dump program
  407. */
  408. public function getPathOfDump()
  409. {
  410. return $this->db->getPathOfDump();
  411. }
  412. /**
  413. * Return version of database client driver
  414. *
  415. * @return string Version string
  416. */
  417. public function getDriverInfo()
  418. {
  419. return $this->db->getDriverInfo();
  420. }
  421. /**
  422. * Return generic error code of last operation.
  423. *
  424. * @return string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)
  425. */
  426. public function errno()
  427. {
  428. return $this->db->errno();
  429. }
  430. /**
  431. * Create a table into database
  432. *
  433. * @param string $table Name of table
  434. * @param array $fields Tableau associatif [nom champ][tableau des descriptions]
  435. * @param string $primary_key Nom du champ qui sera la clef primaire
  436. * @param string $type Type de la table
  437. * @param array $unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur
  438. * @param array $fulltext_keys Tableau des Nom de champs qui seront indexes en fulltext
  439. * @param array $keys Tableau des champs cles noms => valeur
  440. * @return int <0 if KO, >=0 if OK
  441. */
  442. public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
  443. {
  444. return $this->db->DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys, $fulltext_keys, $keys);
  445. }
  446. /**
  447. * Drop a table into database
  448. *
  449. * @param string $table Name of table
  450. * @return int <0 if KO, >=0 if OK
  451. */
  452. public function DDLDropTable($table)
  453. {
  454. return $this->db->DDLDropTable($table);
  455. }
  456. /**
  457. * Return list of available charset that can be used to store data in database
  458. *
  459. * @return array List of Charset
  460. */
  461. public function getListOfCharacterSet()
  462. {
  463. return $this->db->getListOfCharacterSet();
  464. }
  465. /**
  466. * Create a new field into table
  467. *
  468. * @param string $table Name of table
  469. * @param string $field_name Name of field to add
  470. * @param string $field_desc Tableau associatif de description du champ a inserer[nom du parametre][valeur du parametre]
  471. * @param string $field_position Optionnel ex.: "after champtruc"
  472. * @return int <0 if KO, >0 if OK
  473. */
  474. public function DDLAddField($table, $field_name, $field_desc, $field_position = "")
  475. {
  476. return $this->db->DDLAddField($table, $field_name, $field_desc, $field_position);
  477. }
  478. /**
  479. * Drop a field from table
  480. *
  481. * @param string $table Name of table
  482. * @param string $field_name Name of field to drop
  483. * @return int <0 if KO, >0 if OK
  484. */
  485. public function DDLDropField($table, $field_name)
  486. {
  487. return $this->db->DDLDropField($table, $field_name);
  488. }
  489. /**
  490. * Update format of a field into a table
  491. *
  492. * @param string $table Name of table
  493. * @param string $field_name Name of field to modify
  494. * @param string $field_desc Array with description of field format
  495. * @return int <0 if KO, >0 if OK
  496. */
  497. public function DDLUpdateField($table, $field_name, $field_desc)
  498. {
  499. return $this->db->DDLUpdateField($table, $field_name, $field_desc);
  500. }
  501. /**
  502. * Return list of available collation that can be used for database
  503. *
  504. * @return array List of Collation
  505. */
  506. public function getListOfCollation()
  507. {
  508. return $this->db->getListOfCollation();
  509. }
  510. /**
  511. * Return a pointer of line with description of a table or field
  512. *
  513. * @param string $table Name of table
  514. * @param string $field Optionnel : Name of field if we want description of field
  515. * @return resource Resource
  516. */
  517. public function DDLDescTable($table, $field = "")
  518. {
  519. return $this->db->DDLDescTable($table, $field);
  520. }
  521. /**
  522. * Return version of database server
  523. *
  524. * @return string Version string
  525. */
  526. public function getVersion()
  527. {
  528. return $this->db->getVersion();
  529. }
  530. /**
  531. * Return charset used to store data in database
  532. *
  533. * @return string Charset
  534. */
  535. public function getDefaultCharacterSetDatabase()
  536. {
  537. return $this->db->getDefaultCharacterSetDatabase();
  538. }
  539. /**
  540. * Create a user and privileges to connect to database (even if database does not exists yet)
  541. *
  542. * @param string $dolibarr_main_db_host Ip serveur
  543. * @param string $dolibarr_main_db_user Nom user a creer
  544. * @param string $dolibarr_main_db_pass Mot de passe user a creer
  545. * @param string $dolibarr_main_db_name Database name where user must be granted
  546. * @return int <0 if KO, >=0 if OK
  547. */
  548. public function DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
  549. {
  550. return $this->db->DDLCreateUser($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name);
  551. }
  552. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  553. /**
  554. * Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
  555. * 19700101020000 -> 3600 with TZ+1 and gmt=0
  556. * 19700101020000 -> 7200 whaterver is TZ if gmt=1
  557. *
  558. * @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
  559. * @param bool $gm 1=Input informations are GMT values, otherwise local to server TZ
  560. * @return int|string Date TMS or ''
  561. */
  562. public function jdate($string, $gm = false)
  563. {
  564. // phpcs:enable
  565. return $this->db->jdate($string, $gm);
  566. }
  567. /**
  568. * Encrypt sensitive data in database
  569. * Warning: This function includes the escape and add the SQL simple quotes on strings.
  570. *
  571. * @param string $fieldorvalue Field name or value to encrypt
  572. * @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
  573. * @return string XXX(field) or XXX('value') or field or 'value'
  574. */
  575. public function encrypt($fieldorvalue, $withQuotes = 1)
  576. {
  577. return $this->db->encrypt($fieldorvalue, $withQuotes);
  578. }
  579. /**
  580. * Validate a database transaction
  581. *
  582. * @param string $log Add more log to default log line
  583. * @return int 1 if validation is OK or transaction level no started, 0 if ERROR
  584. */
  585. public function commit($log = '')
  586. {
  587. return $this->db->commit($log);
  588. }
  589. /**
  590. * List information of columns into a table.
  591. *
  592. * @param string $table Name of table
  593. * @return array Array with inforation on table
  594. */
  595. public function DDLInfoTable($table)
  596. {
  597. return $this->db->DDLInfoTable($table);
  598. }
  599. /**
  600. * Free last resultset used.
  601. *
  602. * @param resource $resultset Fre cursor
  603. * @return void
  604. */
  605. public function free($resultset = null)
  606. {
  607. $this->db->free($resultset);
  608. }
  609. /**
  610. * Close database connexion
  611. *
  612. * @return boolean True if disconnect successfull, false otherwise
  613. * @see connect()
  614. */
  615. public function close()
  616. {
  617. return $this->db->close();
  618. }
  619. /**
  620. * Return last query in error
  621. *
  622. * @return string lastqueryerror
  623. */
  624. public function lastqueryerror()
  625. {
  626. return $this->db->lastqueryerror();
  627. }
  628. /**
  629. * Return connexion ID
  630. *
  631. * @return string Id connexion
  632. */
  633. public function DDLGetConnectId()
  634. {
  635. return $this->db->DDLGetConnectId();
  636. }
  637. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  638. /**
  639. * Returns the current line (as an object) for the resultset cursor
  640. *
  641. * @param resource|Connection $resultset Handler of the desired SQL request
  642. * @return Object Object result line or false if KO or end of cursor
  643. */
  644. public function fetch_object($resultset)
  645. {
  646. // phpcs:enable
  647. return $this->db->fetch_object($resultset);
  648. }
  649. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  650. /**
  651. * Select a database
  652. *
  653. * @param string $database Name of database
  654. * @return boolean true if OK, false if KO
  655. */
  656. public function select_db($database)
  657. {
  658. // phpcs:enable
  659. return $this->db->select_db($database);
  660. }
  661. }