Database.interface.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
  3. * Copyright (C) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2014-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * Class to manage Dolibarr database access for an SQL database
  24. */
  25. interface Database
  26. {
  27. /**
  28. * Format a SQL IF
  29. *
  30. * @param string $test Test string (example: 'cd.statut=0', 'field IS NULL')
  31. * @param string $resok result if test is equal
  32. * @param string $resko result if test is not equal
  33. * @return string SQL string
  34. */
  35. public function ifsql($test, $resok, $resko);
  36. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  37. /**
  38. * Return datas as an array
  39. *
  40. * @param resource $resultset Resultset of request
  41. * @return array Array
  42. */
  43. public function fetch_row($resultset);
  44. // phpcs:enable
  45. /**
  46. * Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
  47. * Function to use to build INSERT, UPDATE or WHERE predica
  48. *
  49. * @param int $param Date TMS to convert
  50. * @return string Date in a string YYYYMMDDHHMMSS
  51. */
  52. public function idate($param);
  53. /**
  54. * Return last error code
  55. *
  56. * @return string lasterrno
  57. */
  58. public function lasterrno();
  59. /**
  60. * Start transaction
  61. *
  62. * @param string $textinlog Add a small text into log. '' by default.
  63. * @return int 1 if transaction successfuly opened or already opened, 0 if error
  64. */
  65. public function begin($textinlog = '');
  66. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  67. /**
  68. * Create a new database
  69. * Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated
  70. * We force to create database with charset this->forcecharset and collate this->forcecollate
  71. *
  72. * @param string $database Database name to create
  73. * @param string $charset Charset used to store data
  74. * @param string $collation Charset used to sort data
  75. * @param string $owner Username of database owner
  76. * @return resource resource defined if OK, null if KO
  77. */
  78. public function DDLCreateDb($database, $charset = '', $collation = '', $owner = '');
  79. // phpcs:enable
  80. /**
  81. * Return version of database server into an array
  82. *
  83. * @return array Version array
  84. */
  85. public function getVersionArray();
  86. /**
  87. * Convert a SQL request in Mysql syntax to native syntax
  88. *
  89. * @param string $line SQL request line to convert
  90. * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
  91. * @return string SQL request line converted
  92. */
  93. public static function convertSQLFromMysql($line, $type = 'ddl');
  94. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  95. /**
  96. * Return the number of lines in the result of a request INSERT, DELETE or UPDATE
  97. *
  98. * @param resource $resultset Cursor of the desired request
  99. * @return int Number of lines
  100. * @see num_rows()
  101. */
  102. public function affected_rows($resultset);
  103. // phpcs:enable
  104. /**
  105. * Return description of last error
  106. *
  107. * @return string Error text
  108. */
  109. public function error();
  110. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  111. /**
  112. * List tables into a database
  113. *
  114. * @param string $database Name of database
  115. * @param string $table Name of table filter ('xxx%')
  116. * @return array List of tables in an array
  117. */
  118. public function DDLListTables($database, $table = '');
  119. // phpcs:enable
  120. /**
  121. * Return last request executed with query()
  122. *
  123. * @return string Last query
  124. */
  125. public function lastquery();
  126. /**
  127. * Define sort criteria of request
  128. *
  129. * @param string $sortfield List of sort fields
  130. * @param string $sortorder Sort order
  131. * @return string String to provide syntax of a sort sql string
  132. */
  133. public function order($sortfield = null, $sortorder = null);
  134. /**
  135. * Decrypt sensitive data in database
  136. *
  137. * @param string $value Value to decrypt
  138. * @return string Decrypted value if used
  139. */
  140. public function decrypt($value);
  141. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  142. /**
  143. * Return datas as an array
  144. *
  145. * @param resource $resultset Resultset of request
  146. * @return array Array
  147. */
  148. public function fetch_array($resultset);
  149. // phpcs:enable
  150. /**
  151. * Return last error label
  152. *
  153. * @return string lasterror
  154. */
  155. public function lasterror();
  156. /**
  157. * Escape a string to insert data
  158. *
  159. * @param string $stringtoencode String to escape
  160. * @return string String escaped
  161. */
  162. public function escape($stringtoencode);
  163. /**
  164. * Escape a string to insert data
  165. *
  166. * @param string $stringtoencode String to escape
  167. * @return string String escaped
  168. * @deprecated
  169. */
  170. public function escapeunderscore($stringtoencode);
  171. /**
  172. * Escape a string to insert data into a like
  173. *
  174. * @param string $stringtoencode String to escape
  175. * @return string String escaped
  176. */
  177. public function escapeforlike($stringtoencode);
  178. /**
  179. * Sanitize a string for SQL forging
  180. *
  181. * @param string $stringtosanitize String to escape
  182. * @return string String escaped
  183. */
  184. public function sanitize($stringtosanitize);
  185. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  186. /**
  187. * Get last ID after an insert INSERT
  188. *
  189. * @param string $tab Table name concerned by insert. Not used under MySql but required for compatibility with Postgresql
  190. * @param string $fieldid Field name
  191. * @return int Id of row
  192. */
  193. public function last_insert_id($tab, $fieldid = 'rowid');
  194. // phpcs:enable
  195. /**
  196. * Return full path of restore program
  197. *
  198. * @return string Full path of restore program
  199. */
  200. public function getPathOfRestore();
  201. /**
  202. * Canceling a transaction and returning to old values
  203. *
  204. * @param string $log Add more log to default log line
  205. * @return int 1 if cancelation ok or transaction not open, 0 if error
  206. */
  207. public function rollback($log = '');
  208. /**
  209. * Execute a SQL request and return the resultset
  210. *
  211. * @param string $query SQL query string
  212. * @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).
  213. * 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.
  214. * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
  215. * @param int $result_mode Result mode
  216. * @return bool|resource Resultset of answer or false
  217. */
  218. public function query($query, $usesavepoint = 0, $type = 'auto', $result_mode = 0);
  219. /**
  220. * Connexion to server
  221. *
  222. * @param string $host database server host
  223. * @param string $login login
  224. * @param string $passwd password
  225. * @param string $name name of database (not used for mysql, used for pgsql)
  226. * @param int $port Port of database server
  227. * @return resource Database access handler
  228. * @see close()
  229. */
  230. public function connect($host, $login, $passwd, $name, $port = 0);
  231. /**
  232. * Define limits and offset of request
  233. *
  234. * @param int $limit Maximum number of lines returned (-1=conf->liste_limit, 0=no limit)
  235. * @param int $offset Numero of line from where starting fetch
  236. * @return string String with SQL syntax to add a limit and offset
  237. */
  238. public function plimit($limit = 0, $offset = 0);
  239. /**
  240. * Return value of server parameters
  241. *
  242. * @param string $filter Filter list on a particular value
  243. * @return array Array of key-values (key=>value)
  244. */
  245. public function getServerParametersValues($filter = '');
  246. /**
  247. * Return value of server status
  248. *
  249. * @param string $filter Filter list on a particular value
  250. * @return array Array of key-values (key=>value)
  251. */
  252. public function getServerStatusValues($filter = '');
  253. /**
  254. * Return collation used in database
  255. *
  256. * @return string Collation value
  257. */
  258. public function getDefaultCollationDatabase();
  259. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  260. /**
  261. * Return number of lines for result of a SELECT
  262. *
  263. * @param resource $resultset Resulset of requests
  264. * @return int Nb of lines
  265. * @see affected_rows()
  266. */
  267. public function num_rows($resultset);
  268. // phpcs:enable
  269. /**
  270. * Return full path of dump program
  271. *
  272. * @return string Full path of dump program
  273. */
  274. public function getPathOfDump();
  275. /**
  276. * Return version of database client driver
  277. *
  278. * @return string Version string
  279. */
  280. public function getDriverInfo();
  281. /**
  282. * Return generic error code of last operation.
  283. *
  284. * @return string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)
  285. */
  286. public function errno();
  287. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  288. /**
  289. * Create a table into database
  290. *
  291. * @param string $table Name of table
  292. * @param array $fields Associative table [field name][table of descriptions]
  293. * @param string $primary_key Name of the field that will be the primary key
  294. * @param string $type Type of the table
  295. * @param array $unique_keys Associative array Name of fields that will be unique key => value
  296. * @param array $fulltext_keys Field name table that will be indexed in fulltext
  297. * @param array $keys Table of key fields names => value
  298. * @return int <0 if KO, >=0 if OK
  299. */
  300. public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null);
  301. // phpcs:enable
  302. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  303. /**
  304. * Drop a table into database
  305. *
  306. * @param string $table Name of table
  307. * @return int <0 if KO, >=0 if OK
  308. */
  309. public function DDLDropTable($table);
  310. // phpcs:enable
  311. /**
  312. * Return list of available charset that can be used to store data in database
  313. *
  314. * @return array List of Charset
  315. */
  316. public function getListOfCharacterSet();
  317. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  318. /**
  319. * Create a new field into table
  320. *
  321. * @param string $table Name of table
  322. * @param string $field_name Name of field to add
  323. * @param string $field_desc Associative array of description of the field to insert [parameter name][parameter value]
  324. * @param string $field_position Optional ex .: "after field stuff"
  325. * @return int <0 if KO, >0 if OK
  326. */
  327. public function DDLAddField($table, $field_name, $field_desc, $field_position = "");
  328. // phpcs:enable
  329. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  330. /**
  331. * Drop a field from table
  332. *
  333. * @param string $table Name of table
  334. * @param string $field_name Name of field to drop
  335. * @return int <0 if KO, >0 if OK
  336. */
  337. public function DDLDropField($table, $field_name);
  338. // phpcs:enable
  339. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  340. /**
  341. * Update format of a field into a table
  342. *
  343. * @param string $table Name of table
  344. * @param string $field_name Name of field to modify
  345. * @param string $field_desc Array with description of field format
  346. * @return int <0 if KO, >0 if OK
  347. */
  348. public function DDLUpdateField($table, $field_name, $field_desc);
  349. // phpcs:enable
  350. /**
  351. * Return list of available collation that can be used for database
  352. *
  353. * @return array List of Collation
  354. */
  355. public function getListOfCollation();
  356. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  357. /**
  358. * Return a pointer of line with description of a table or field
  359. *
  360. * @param string $table Name of table
  361. * @param string $field Optional : Name of field if we want description of field
  362. * @return resource Resource
  363. */
  364. public function DDLDescTable($table, $field = "");
  365. // phpcs:enable
  366. /**
  367. * Return version of database server
  368. *
  369. * @return string Version string
  370. */
  371. public function getVersion();
  372. /**
  373. * Return charset used to store data in database
  374. *
  375. * @return string Charset
  376. */
  377. public function getDefaultCharacterSetDatabase();
  378. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  379. /**
  380. * Create a user and privileges to connect to database (even if database does not exists yet)
  381. *
  382. * @param string $dolibarr_main_db_host Server IP
  383. * @param string $dolibarr_main_db_user Username to create
  384. * @param string $dolibarr_main_db_pass User password to create
  385. * @param string $dolibarr_main_db_name Database name where user must be granted
  386. * @return int <0 if KO, >=0 if OK
  387. */
  388. public function DDLCreateUser(
  389. $dolibarr_main_db_host,
  390. $dolibarr_main_db_user,
  391. $dolibarr_main_db_pass,
  392. $dolibarr_main_db_name
  393. );
  394. // phpcs:enable
  395. /**
  396. * Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
  397. * 19700101020000 -> 3600 with TZ+1 and gmt=0
  398. * 19700101020000 -> 7200 whaterver is TZ if gmt=1
  399. *
  400. * @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
  401. * @param bool $gm 1=Input informations are GMT values, otherwise local to server TZ
  402. * @return int|string Date TMS or ''
  403. */
  404. public function jdate($string, $gm = false);
  405. /**
  406. * Encrypt sensitive data in database
  407. * Warning: This function includes the escape and add the SQL simple quotes on strings.
  408. *
  409. * @param string $fieldorvalue Field name or value to encrypt
  410. * @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
  411. * @return string XXX(field) or XXX('value') or field or 'value'
  412. */
  413. public function encrypt($fieldorvalue, $withQuotes = 1);
  414. /**
  415. * Validate a database transaction
  416. *
  417. * @param string $log Add more log to default log line
  418. * @return int 1 if validation is OK or transaction level no started, 0 if ERROR
  419. */
  420. public function commit($log = '');
  421. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  422. /**
  423. * List information of columns into a table.
  424. *
  425. * @param string $table Name of table
  426. * @return array Array with information on table
  427. */
  428. public function DDLInfoTable($table);
  429. // phpcs:enable
  430. /**
  431. * Free last resultset used.
  432. *
  433. * @param resource $resultset Free cursor
  434. * @return void
  435. */
  436. public function free($resultset = null);
  437. /**
  438. * Close database connexion
  439. *
  440. * @return boolean True if disconnect successfull, false otherwise
  441. * @see connect()
  442. */
  443. public function close();
  444. /**
  445. * Return last query in error
  446. *
  447. * @return string lastqueryerror
  448. */
  449. public function lastqueryerror();
  450. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  451. /**
  452. * Return connexion ID
  453. *
  454. * @return string Id connexion
  455. */
  456. public function DDLGetConnectId();
  457. // phpcs:enable
  458. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  459. /**
  460. * Returns the current line (as an object) for the resultset cursor
  461. *
  462. * @param resource|Connection $resultset Handler of the desired request
  463. * @return Object Object result line or false if KO or end of cursor
  464. */
  465. public function fetch_object($resultset);
  466. // phpcs:enable
  467. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  468. /**
  469. * Select a database
  470. *
  471. * @param string $database Name of database
  472. * @return boolean true if OK, false if KO
  473. */
  474. public function select_db($database);
  475. // phpcs:enable
  476. }