Database.interface.php 17 KB

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