DoliDB.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /*
  3. * Copyright (C) 2013-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  4. * Copyright (C) 2014-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/db/DoliDB.class.php
  21. * \brief Class file to manage Dolibarr database access
  22. */
  23. require_once DOL_DOCUMENT_ROOT .'/core/db/Database.interface.php';
  24. /**
  25. * Class to manage Dolibarr database access
  26. */
  27. abstract class DoliDB implements Database
  28. {
  29. /** @var resource Database handler */
  30. public $db;
  31. /** @var string Database type */
  32. public $type;
  33. /** @var string Charset used to force charset when creating database */
  34. public $forcecharset='utf8';
  35. /** @var string Collate used to force collate when creating database */
  36. public $forcecollate='utf8_general_ci';
  37. /** @var resource Resultset of last query */
  38. private $_results;
  39. /** @var bool true if connected, else false */
  40. public $connected;
  41. /** @var bool true if database selected, else false */
  42. public $database_selected;
  43. /** @var string Selected database name */
  44. public $database_name;
  45. /** @var string Database username */
  46. public $database_user;
  47. /** @var string Database host */
  48. public $database_host;
  49. /** @var int Database port */
  50. public $database_port;
  51. /** @var int >=1 if a transaction is opened, 0 otherwise */
  52. public $transaction_opened;
  53. /** @var string Last successful query */
  54. public $lastquery;
  55. /** @var string Last failed query */
  56. public $lastqueryerror;
  57. /** @var string Last error message */
  58. public $lasterror;
  59. /** @var int Last error number */
  60. public $lasterrno;
  61. /** @var bool Status */
  62. public $ok;
  63. /** @var string */
  64. public $error;
  65. /**
  66. * Format a SQL IF
  67. *
  68. * @param string $test Test string (example: 'cd.statut=0', 'field IS NULL')
  69. * @param string $resok resultat si test egal
  70. * @param string $resko resultat si test non egal
  71. * @return string SQL string
  72. */
  73. function ifsql($test,$resok,$resko)
  74. {
  75. return 'IF('.$test.','.$resok.','.$resko.')';
  76. }
  77. /**
  78. * Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
  79. * Function to use to build INSERT, UPDATE or WHERE predica
  80. *
  81. * @param int $param Date TMS to convert
  82. * @return string Date in a string YYYYMMDDHHMMSS
  83. */
  84. function idate($param)
  85. {
  86. return dol_print_date($param,"%Y%m%d%H%M%S");
  87. }
  88. /**
  89. * Return last error code
  90. *
  91. * @return string lasterrno
  92. */
  93. function lasterrno()
  94. {
  95. return $this->lasterrno;
  96. }
  97. /**
  98. * Start transaction
  99. *
  100. * @return int 1 if transaction successfuly opened or already opened, 0 if error
  101. */
  102. function begin()
  103. {
  104. if (! $this->transaction_opened)
  105. {
  106. $ret=$this->query("BEGIN");
  107. if ($ret)
  108. {
  109. $this->transaction_opened++;
  110. dol_syslog("BEGIN Transaction",LOG_DEBUG);
  111. dol_syslog('',0,1);
  112. }
  113. return $ret;
  114. }
  115. else
  116. {
  117. $this->transaction_opened++;
  118. dol_syslog('',0,1);
  119. return 1;
  120. }
  121. }
  122. /**
  123. * Validate a database transaction
  124. *
  125. * @param string $log Add more log to default log line
  126. * @return int 1 if validation is OK or transaction level no started, 0 if ERROR
  127. */
  128. function commit($log='')
  129. {
  130. dol_syslog('',0,-1);
  131. if ($this->transaction_opened<=1)
  132. {
  133. $ret=$this->query("COMMIT");
  134. if ($ret)
  135. {
  136. $this->transaction_opened=0;
  137. dol_syslog("COMMIT Transaction".($log?' '.$log:''),LOG_DEBUG);
  138. return 1;
  139. }
  140. else
  141. {
  142. return 0;
  143. }
  144. }
  145. else
  146. {
  147. $this->transaction_opened--;
  148. return 1;
  149. }
  150. }
  151. /**
  152. * Annulation d'une transaction et retour aux anciennes valeurs
  153. *
  154. * @param string $log Add more log to default log line
  155. * @return resource|int 1 si annulation ok ou transaction non ouverte, 0 en cas d'erreur
  156. */
  157. function rollback($log='')
  158. {
  159. dol_syslog('',0,-1);
  160. if ($this->transaction_opened<=1)
  161. {
  162. $ret=$this->query("ROLLBACK");
  163. $this->transaction_opened=0;
  164. dol_syslog("ROLLBACK Transaction".($log?' '.$log:''),LOG_DEBUG);
  165. return $ret;
  166. }
  167. else
  168. {
  169. $this->transaction_opened--;
  170. return 1;
  171. }
  172. }
  173. /**
  174. * Define limits and offset of request
  175. *
  176. * @param int $limit Maximum number of lines returned (-1=conf->liste_limit, 0=no limit)
  177. * @param int $offset Numero of line from where starting fetch
  178. * @return string String with SQL syntax to add a limit and offset
  179. */
  180. function plimit($limit=0,$offset=0)
  181. {
  182. global $conf;
  183. if (empty($limit)) return "";
  184. if ($limit < 0) $limit=$conf->liste_limit;
  185. if ($offset > 0) return " LIMIT $offset,$limit ";
  186. else return " LIMIT $limit ";
  187. }
  188. /**
  189. * Return version of database server into an array
  190. *
  191. * @return array Version array
  192. */
  193. function getVersionArray()
  194. {
  195. return preg_split("/[\.,-]/",$this->getVersion());
  196. }
  197. /**
  198. * Return last request executed with query()
  199. *
  200. * @return string Last query
  201. */
  202. function lastquery()
  203. {
  204. return $this->lastquery;
  205. }
  206. /**
  207. * Define sort criteria of request
  208. *
  209. * @param string $sortfield List of sort fields, separated by comma. Example: 't1.fielda, t2.fieldb'
  210. * @param 'ASC'|'DESC' $sortorder Sort order
  211. * @return string String to provide syntax of a sort sql string
  212. */
  213. function order($sortfield=null,$sortorder=null)
  214. {
  215. if (! empty($sortfield))
  216. {
  217. $return='';
  218. $fields=explode(',',$sortfield);
  219. foreach($fields as $val)
  220. {
  221. if (! $return) $return.=' ORDER BY ';
  222. else $return.=',';
  223. $return.=preg_replace('/[^0-9a-z_\.]/i','',$val);
  224. // Only ASC and DESC values are valid SQL
  225. if (strtoupper($sortorder) === 'ASC') {
  226. $return .= ' ASC';
  227. } elseif (strtoupper($sortorder) === 'DESC') {
  228. $return .= ' DESC';
  229. }
  230. }
  231. return $return;
  232. }
  233. else
  234. {
  235. return '';
  236. }
  237. }
  238. /**
  239. * Return last error label
  240. *
  241. * @return string Last error
  242. */
  243. function lasterror()
  244. {
  245. return $this->lasterror;
  246. }
  247. /**
  248. * Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
  249. * 19700101020000 -> 3600 with TZ+1 and gmt=0
  250. * 19700101020000 -> 7200 whaterver is TZ if gmt=1
  251. *
  252. * @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
  253. * @param bool $gm 1=Input informations are GMT values, otherwise local to server TZ
  254. * @return int|string Date TMS or ''
  255. */
  256. function jdate($string, $gm=false)
  257. {
  258. if ($string==0 || $string=="0000-00-00 00:00:00") return '';
  259. $string=preg_replace('/([^0-9])/i','',$string);
  260. $tmp=$string.'000000';
  261. $date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
  262. return $date;
  263. }
  264. /**
  265. * Return last query in error
  266. *
  267. * @return string lastqueryerror
  268. */
  269. function lastqueryerror()
  270. {
  271. return $this->lastqueryerror;
  272. }
  273. }