Database.interface.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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@capnetworks.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 <http://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 resultat si test egal
  32. * @param string $resko resultat si test non egal
  33. * @return string SQL string
  34. */
  35. function ifsql($test, $resok, $resko);
  36. /**
  37. * Return datas as an array
  38. *
  39. * @param resource $resultset Resultset of request
  40. * @return array Array
  41. */
  42. function fetch_row($resultset);
  43. /**
  44. * Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
  45. * Function to use to build INSERT, UPDATE or WHERE predica
  46. *
  47. * @param int $param Date TMS to convert
  48. * @return string Date in a string YYYYMMDDHHMMSS
  49. */
  50. function idate($param);
  51. /**
  52. * Return last error code
  53. *
  54. * @return string lasterrno
  55. */
  56. function lasterrno();
  57. /**
  58. * Start transaction
  59. *
  60. * @return int 1 if transaction successfuly opened or already opened, 0 if error
  61. */
  62. function begin();
  63. /**
  64. * Create a new database
  65. * Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated
  66. * We force to create database with charset this->forcecharset and collate this->forcecollate
  67. *
  68. * @param string $database Database name to create
  69. * @param string $charset Charset used to store data
  70. * @param string $collation Charset used to sort data
  71. * @param string $owner Username of database owner
  72. * @return resource resource defined if OK, null if KO
  73. */
  74. function DDLCreateDb($database, $charset = '', $collation = '', $owner = '');
  75. /**
  76. * Return version of database server into an array
  77. *
  78. * @return array Version array
  79. */
  80. function getVersionArray();
  81. /**
  82. * Convert a SQL request in Mysql syntax to native syntax
  83. *
  84. * @param string $line SQL request line to convert
  85. * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
  86. * @return string SQL request line converted
  87. */
  88. static function convertSQLFromMysql($line, $type = 'ddl');
  89. /**
  90. * Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE
  91. *
  92. * @param resource $resultset Curseur de la requete voulue
  93. * @return int Nombre de lignes
  94. * @see num_rows
  95. */
  96. function affected_rows($resultset);
  97. /**
  98. * Return description of last error
  99. *
  100. * @return string Error text
  101. */
  102. function error();
  103. /**
  104. * List tables into a database
  105. *
  106. * @param string $database Name of database
  107. * @param string $table Nmae of table filter ('xxx%')
  108. * @return array List of tables in an array
  109. */
  110. function DDLListTables($database, $table = '');
  111. /**
  112. * Return last request executed with query()
  113. *
  114. * @return string Last query
  115. */
  116. function lastquery();
  117. /**
  118. * Define sort criteria of request
  119. *
  120. * @param string $sortfield List of sort fields
  121. * @param string $sortorder Sort order
  122. * @return string String to provide syntax of a sort sql string
  123. */
  124. function order($sortfield = null, $sortorder = null);
  125. /**
  126. * Decrypt sensitive data in database
  127. *
  128. * @param string $value Value to decrypt
  129. * @return string Decrypted value if used
  130. */
  131. function decrypt($value);
  132. /**
  133. * Return datas as an array
  134. *
  135. * @param resource $resultset Resultset of request
  136. * @return array Array
  137. */
  138. function fetch_array($resultset);
  139. /**
  140. * Return last error label
  141. *
  142. * @return string lasterror
  143. */
  144. function lasterror();
  145. /**
  146. * Escape a string to insert data
  147. *
  148. * @param string $stringtoencode String to escape
  149. * @return string String escaped
  150. */
  151. function escape($stringtoencode);
  152. /**
  153. * Get last ID after an insert INSERT
  154. *
  155. * @param string $tab Table name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
  156. * @param string $fieldid Field name
  157. * @return int Id of row
  158. */
  159. function last_insert_id($tab, $fieldid = 'rowid');
  160. /**
  161. * Return full path of restore program
  162. *
  163. * @return string Full path of restore program
  164. */
  165. function getPathOfRestore();
  166. /**
  167. * Annulation d'une transaction et retour aux anciennes valeurs
  168. *
  169. * @param string $log Add more log to default log line
  170. * @return int 1 si annulation ok ou transaction non ouverte, 0 en cas d'erreur
  171. */
  172. function rollback($log = '');
  173. /**
  174. * Execute a SQL request and return the resultset
  175. *
  176. * @param string $query SQL query string
  177. * @param int $usesavepoint 0=Default mode, 1=Run a savepoint before and a rollbock to savepoint if error (this allow to have some request with errors inside global transactions).
  178. * 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.
  179. * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
  180. * @return resource Resultset of answer
  181. */
  182. function query($query, $usesavepoint = 0, $type = 'auto');
  183. /**
  184. * Connexion to server
  185. *
  186. * @param string $host database server host
  187. * @param string $login login
  188. * @param string $passwd password
  189. * @param string $name name of database (not used for mysql, used for pgsql)
  190. * @param int $port Port of database server
  191. * @return resource Database access handler
  192. * @see close
  193. */
  194. function connect($host, $login, $passwd, $name, $port = 0);
  195. /**
  196. * Define limits and offset of request
  197. *
  198. * @param int $limit Maximum number of lines returned (-1=conf->liste_limit, 0=no limit)
  199. * @param int $offset Numero of line from where starting fetch
  200. * @return string String with SQL syntax to add a limit and offset
  201. */
  202. function plimit($limit = 0, $offset = 0);
  203. /**
  204. * Return value of server parameters
  205. *
  206. * @param string $filter Filter list on a particular value
  207. * @return array Array of key-values (key=>value)
  208. */
  209. function getServerParametersValues($filter = '');
  210. /**
  211. * Return value of server status
  212. *
  213. * @param string $filter Filter list on a particular value
  214. * @return array Array of key-values (key=>value)
  215. */
  216. function getServerStatusValues($filter = '');
  217. /**
  218. * Return collation used in database
  219. *
  220. * @return string Collation value
  221. */
  222. function getDefaultCollationDatabase();
  223. /**
  224. * Return number of lines for result of a SELECT
  225. *
  226. * @param resource $resultset Resulset of requests
  227. * @return int Nb of lines
  228. * @see affected_rows
  229. */
  230. function num_rows($resultset);
  231. /**
  232. * Return full path of dump program
  233. *
  234. * @return string Full path of dump program
  235. */
  236. function getPathOfDump();
  237. /**
  238. * Return version of database client driver
  239. *
  240. * @return string Version string
  241. */
  242. function getDriverInfo();
  243. /**
  244. * Return generic error code of last operation.
  245. *
  246. * @return string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)
  247. */
  248. function errno();
  249. /**
  250. * Create a table into database
  251. *
  252. * @param string $table Nom de la table
  253. * @param array $fields Tableau associatif [nom champ][tableau des descriptions]
  254. * @param string $primary_key Nom du champ qui sera la clef primaire
  255. * @param string $type Type de la table
  256. * @param array $unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur
  257. * @param array $fulltext_keys Tableau des Nom de champs qui seront indexes en fulltext
  258. * @param array $keys Tableau des champs cles noms => valeur
  259. * @return int <0 if KO, >=0 if OK
  260. */
  261. function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null);
  262. /**
  263. * Return list of available charset that can be used to store data in database
  264. *
  265. * @return array List of Charset
  266. */
  267. function getListOfCharacterSet();
  268. /**
  269. * Create a new field into table
  270. *
  271. * @param string $table Name of table
  272. * @param string $field_name Name of field to add
  273. * @param string $field_desc Tableau associatif de description du champ a inserer[nom du parametre][valeur du parametre]
  274. * @param string $field_position Optionnel ex.: "after champtruc"
  275. * @return int <0 if KO, >0 if OK
  276. */
  277. function DDLAddField($table, $field_name, $field_desc, $field_position = "");
  278. /**
  279. * Drop a field from table
  280. *
  281. * @param string $table Name of table
  282. * @param string $field_name Name of field to drop
  283. * @return int <0 if KO, >0 if OK
  284. */
  285. function DDLDropField($table, $field_name);
  286. /**
  287. * Update format of a field into a table
  288. *
  289. * @param string $table Name of table
  290. * @param string $field_name Name of field to modify
  291. * @param string $field_desc Array with description of field format
  292. * @return int <0 if KO, >0 if OK
  293. */
  294. function DDLUpdateField($table, $field_name, $field_desc);
  295. /**
  296. * Return list of available collation that can be used for database
  297. *
  298. * @return array List of Collation
  299. */
  300. function getListOfCollation();
  301. /**
  302. * Return a pointer of line with description of a table or field
  303. *
  304. * @param string $table Name of table
  305. * @param string $field Optionnel : Name of field if we want description of field
  306. * @return resource Resource
  307. */
  308. function DDLDescTable($table, $field = "");
  309. /**
  310. * Return version of database server
  311. *
  312. * @return string Version string
  313. */
  314. function getVersion();
  315. /**
  316. * Return charset used to store data in database
  317. *
  318. * @return string Charset
  319. */
  320. function getDefaultCharacterSetDatabase();
  321. /**
  322. * Create a user and privileges to connect to database (even if database does not exists yet)
  323. *
  324. * @param string $dolibarr_main_db_host Ip serveur
  325. * @param string $dolibarr_main_db_user Nom user a creer
  326. * @param string $dolibarr_main_db_pass Mot de passe user a creer
  327. * @param string $dolibarr_main_db_name Database name where user must be granted
  328. * @return int <0 if KO, >=0 if OK
  329. */
  330. function DDLCreateUser(
  331. $dolibarr_main_db_host,
  332. $dolibarr_main_db_user,
  333. $dolibarr_main_db_pass,
  334. $dolibarr_main_db_name
  335. );
  336. /**
  337. * Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
  338. * 19700101020000 -> 3600 with TZ+1 and gmt=0
  339. * 19700101020000 -> 7200 whaterver is TZ if gmt=1
  340. *
  341. * @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
  342. * @param bool $gm 1=Input informations are GMT values, otherwise local to server TZ
  343. * @return int|string Date TMS or ''
  344. */
  345. function jdate($string, $gm=false);
  346. /**
  347. * Encrypt sensitive data in database
  348. * Warning: This function includes the escape, so it must use direct value
  349. *
  350. * @param string $fieldorvalue Field name or value to encrypt
  351. * @param int $withQuotes Return string with quotes
  352. * @return string XXX(field) or XXX('value') or field or 'value'
  353. */
  354. function encrypt($fieldorvalue, $withQuotes = 0);
  355. /**
  356. * Validate a database transaction
  357. *
  358. * @param string $log Add more log to default log line
  359. * @return int 1 if validation is OK or transaction level no started, 0 if ERROR
  360. */
  361. function commit($log = '');
  362. /**
  363. * List information of columns into a table.
  364. *
  365. * @param string $table Name of table
  366. * @return array Array with inforation on table
  367. */
  368. function DDLInfoTable($table);
  369. /**
  370. * Free last resultset used.
  371. *
  372. * @param resource $resultset Fre cursor
  373. * @return void
  374. */
  375. function free($resultset = null);
  376. /**
  377. * Close database connexion
  378. *
  379. * @return boolean True if disconnect successfull, false otherwise
  380. * @see connect
  381. */
  382. function close();
  383. /**
  384. * Return last query in error
  385. *
  386. * @return string lastqueryerror
  387. */
  388. function lastqueryerror();
  389. /**
  390. * Return connexion ID
  391. *
  392. * @return string Id connexion
  393. */
  394. function DDLGetConnectId();
  395. /**
  396. * Renvoie la ligne courante (comme un objet) pour le curseur resultset
  397. *
  398. * @param resource $resultset Curseur de la requete voulue
  399. * @return Object Object result line or false if KO or end of cursor
  400. */
  401. function fetch_object($resultset);
  402. /**
  403. * Select a database
  404. *
  405. * @param string $database Name of database
  406. * @return boolean true if OK, false if KO
  407. */
  408. function select_db($database);
  409. }