PageRenderTime 69ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix-legacy/db/jDbConnection.class.php

https://github.com/gmarrot/jelix
PHP | 409 lines | 157 code | 46 blank | 206 comment | 20 complexity | b656f6d14d67a754198a011f75f91100 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage db
  5. * @author Laurent Jouanneau, Gerald Croes
  6. * @contributor Julien Issler
  7. * @copyright 2005-2012 Laurent Jouanneau
  8. * @copyright 2007-2009 Julien Issler
  9. * @copyright 2001-2005 CopixTeam
  10. * This class was get originally from the Copix project (CopixDbConnection, Copix 2.3dev20050901, http://www.copix.org)
  11. * However only few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  12. * Initial authors of this Copix classes are Gerald Croes and Laurent Jouanneau,
  13. *
  14. * @link http://www.jelix.org
  15. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  16. */
  17. /**
  18. * @package jelix
  19. * @subpackage db
  20. */
  21. abstract class jDbConnection {
  22. const FETCH_OBJ = 5;
  23. const FETCH_CLASS = 8;
  24. const FETCH_INTO = 9;
  25. const ATTR_AUTOCOMMIT = 0;
  26. const ATTR_PREFETCH = 1;
  27. const ATTR_TIMEOUT = 2;
  28. const ATTR_ERRMODE = 3;
  29. const ATTR_SERVER_VERSION = 4;
  30. const ATTR_SERVER_INFO = 6;
  31. const ATTR_CLIENT_VERSION = 5;
  32. const ATTR_CONNECTION_STATUS = 7;
  33. const ATTR_CASE = 8;
  34. const ATTR_CURSOR = 10;
  35. const ATTR_ORACLE_NULLS = 11;
  36. const ATTR_PERSISTENT = 12;
  37. const ATTR_DRIVER_NAME = 16;
  38. const CURSOR_FWDONLY = 0;
  39. const CURSOR_SCROLL = 1;
  40. /**
  41. * profile properties used by the connector
  42. * @var array
  43. */
  44. public $profile;
  45. /**
  46. * The database type name (mysql, pgsql ...)
  47. * It is not the driver name. Several drivers could connect to the same database
  48. * type. This type name is often used to know whish SQL language we should use.
  49. * @var string
  50. */
  51. public $dbms;
  52. /**
  53. * driver name
  54. * @var string
  55. */
  56. public $driverName = '';
  57. /**
  58. * The last error message if any
  59. * @var string
  60. */
  61. public $msgError = '';
  62. /**
  63. * last executed query
  64. */
  65. public $lastQuery;
  66. /**
  67. * Are we using an automatic commit ?
  68. * @var boolean
  69. */
  70. private $_autocommit = true;
  71. /**
  72. * the internal connection.
  73. */
  74. protected $_connection = null;
  75. protected $_debugMode = false;
  76. /**
  77. * do a connection to the database, using properties of the given profile
  78. * @param array $profile profile properties
  79. */
  80. function __construct($profile) {
  81. $this->profile = & $profile;
  82. $this->dbms = $this->driverName = $profile['driver'];
  83. $this->_connection = $this->_connect();
  84. #ifnot ENABLE_OPTIMIZED_SOURCE
  85. $this->_debugMode = true;
  86. #endif
  87. }
  88. function __destruct() {
  89. if ($this->_connection !== null) {
  90. $this->_disconnect ();
  91. }
  92. }
  93. function disconnect() {
  94. if ($this->_connection !== null) {
  95. $this->_disconnect ();
  96. }
  97. }
  98. /**
  99. * Launch a SQL Query which returns rows (typically, a SELECT statement)
  100. * @param string $queryString the SQL query
  101. * @param integer $fetchmode FETCH_OBJ, FETCH_CLASS or FETCH_INTO
  102. * @param string|object $param class name if FETCH_CLASS, an object if FETCH_INTO. else null.
  103. * @param array $ctoargs arguments for the constructor if FETCH_CLASS
  104. * @return jDbResultSet|boolean False if the query has failed.
  105. */
  106. public function query ($queryString, $fetchmode = self::FETCH_OBJ, $arg1 = null, $ctoargs = null) {
  107. $this->lastQuery = $queryString;
  108. if ($this->_debugMode) {
  109. $log = new jSQLLogMessage($queryString);
  110. $result = $this->_doQuery ($queryString);
  111. $log->endQuery();
  112. jLog::log($log,'sql');
  113. }
  114. else {
  115. $result = $this->_doQuery ($queryString);
  116. }
  117. if ($fetchmode != self::FETCH_OBJ) {
  118. $result->setFetchMode($fetchmode, $arg1, $ctoargs);
  119. }
  120. return $result;
  121. }
  122. /**
  123. * Launch a SQL Query with limit parameter, so it returns only a subset of a result
  124. * @param string $queryString the SQL query
  125. * @param integer $limitOffset the offset of the first row to return
  126. * @param integer $limitCount the maximum of number of rows to return
  127. * @return jDbResultSet|boolean SQL Select. False if the query has failed.
  128. */
  129. public function limitQuery ($queryString, $limitOffset, $limitCount){
  130. $this->lastQuery = $queryString;
  131. if ($this->_debugMode) {
  132. $log = new jSQLLogMessage($queryString);
  133. $result = $this->_doLimitQuery ($queryString, intval($limitOffset), intval($limitCount));
  134. $log->endQuery();
  135. $log->setRealQuery($this->lastQuery);
  136. jLog::log($log,'sql');
  137. return $result;
  138. }
  139. else {
  140. return $this->_doLimitQuery ($queryString, intval($limitOffset), intval($limitCount));
  141. }
  142. }
  143. /**
  144. * Launch a SQL Query (update, delete..) which doesn't return rows
  145. * @param string $query the SQL query
  146. * @return integer the number of affected rows. False if the query has failed.
  147. */
  148. public function exec ($query) {
  149. $this->lastQuery = $query;
  150. if ($this->_debugMode) {
  151. $log = new jSQLLogMessage($query);
  152. $result = $this->_doExec ($query);
  153. $log->endQuery();
  154. jLog::log($log,'sql');
  155. return $result;
  156. }
  157. else {
  158. return $this->_doExec ($query);
  159. }
  160. }
  161. /**
  162. * Escape and quotes strings.
  163. * @param string $text string to quote
  164. * @param int $parameter_type unused, just for compatibility with PDO
  165. * @return string escaped string
  166. */
  167. public function quote ($text, $parameter_type = 0) {
  168. // for compatibility with older jelix version
  169. if ($parameter_type === false || $parameter_type === true)
  170. trigger_error("signature of jDbConnection::quote has changed, you should use quote2()", E_USER_WARNING);
  171. return "'".$this->_quote($text, false)."'";
  172. }
  173. /**
  174. * Escape and quotes strings. if null, will only return the text "NULL"
  175. * @param string $text string to quote
  176. * @param boolean $checknull if true, check if $text is a null value, and then return NULL
  177. * @param boolean $binary set to true if $text contains a binary string
  178. * @return string escaped string
  179. * @since 1.2
  180. */
  181. public function quote2 ($text, $checknull=true, $binary=false) {
  182. if ($checknull)
  183. return (is_null ($text) ? 'NULL' : "'".$this->_quote($text, $binary)."'");
  184. else
  185. return "'".$this->_quote($text, $binary)."'";
  186. }
  187. /**
  188. * enclose the field name
  189. * @param string $fieldName the field name
  190. * @return string the enclosed field name
  191. * @since 1.1.1
  192. */
  193. public function encloseName ($fieldName) {
  194. return $fieldName;
  195. }
  196. /**
  197. * Prefix the given table with the prefix specified in the connection's profile
  198. * If there's no prefix for the connection's profile, return the table's name unchanged.
  199. *
  200. * @param string $table the table's name
  201. * @return string the prefixed table's name
  202. * @author Julien Issler
  203. * @since 1.0
  204. */
  205. public function prefixTable($table_name){
  206. if(!isset($this->profile['table_prefix']))
  207. return $table_name;
  208. return $this->profile['table_prefix'].$table_name;
  209. }
  210. /**
  211. * Check if the current connection has a table prefix set
  212. *
  213. * @return boolean
  214. * @author Julien Issler
  215. * @since 1.0
  216. */
  217. public function hasTablePrefix(){
  218. return (isset($this->profile['table_prefix']) && $this->profile['table_prefix'] != '');
  219. }
  220. /**
  221. * sets the autocommit state
  222. * @param boolean $state the status of autocommit
  223. */
  224. public function setAutoCommit($state=true){
  225. $this->_autocommit = $state;
  226. $this->_autoCommitNotify ($this->_autocommit);
  227. }
  228. /**
  229. * begin a transaction. Call it before query, limitQuery, exec
  230. * And then commit() or rollback()
  231. */
  232. abstract public function beginTransaction ();
  233. /**
  234. * validate all queries and close a transaction
  235. */
  236. abstract public function commit ();
  237. /**
  238. * cancel all queries of a transaction and close the transaction
  239. */
  240. abstract public function rollback ();
  241. /**
  242. * prepare a query
  243. * @param string $query a sql query with parameters
  244. * @return statement a statement
  245. */
  246. abstract public function prepare ($query);
  247. /**
  248. * @return string the last error description
  249. */
  250. abstract public function errorInfo();
  251. /**
  252. * @return integer the last error code
  253. */
  254. abstract public function errorCode();
  255. /**
  256. * return the id value of the last inserted row.
  257. * Some driver need a sequence name, so give it at first parameter
  258. * @param string $fromSequence the sequence name
  259. * @return integer the id value
  260. */
  261. abstract public function lastInsertId($fromSequence='');
  262. /**
  263. *
  264. * @param integer $id the attribut id
  265. * @return string the attribute value
  266. * @see PDO::getAttribute()
  267. */
  268. abstract public function getAttribute($id);
  269. /**
  270. *
  271. * @param integer $id the attribut id
  272. * @param string $value the attribute value
  273. * @see PDO::setAttribute()
  274. */
  275. abstract public function setAttribute($id, $value);
  276. /**
  277. * return the maximum value of the given primary key in a table
  278. * @param string $fieldName the name of the primary key
  279. * @param string $tableName the name of the table
  280. * @return integer the maximum value
  281. */
  282. public function lastIdInTable($fieldName, $tableName){
  283. $rs = $this->query ('SELECT MAX('.$fieldName.') as ID FROM '.$tableName);
  284. if (($rs !== null) && $r = $rs->fetch ()){
  285. return $r->ID;
  286. }
  287. return 0;
  288. }
  289. /**
  290. * Notify the changes on autocommit
  291. * Drivers may overload this
  292. * @param boolean $state the new state of autocommit
  293. */
  294. abstract protected function _autoCommitNotify ($state);
  295. /**
  296. * return a connection identifier or false/null if there is an error
  297. * @return integer connection identifier
  298. */
  299. abstract protected function _connect ();
  300. /**
  301. * do a disconnection
  302. * (no need to do a test on the connection id)
  303. */
  304. abstract protected function _disconnect ();
  305. /**
  306. * do a query which return results
  307. * @return jDbResultSet/boolean
  308. */
  309. abstract protected function _doQuery ($queryString);
  310. /**
  311. * do a query which return nothing
  312. * @return jDbResultSet/boolean
  313. */
  314. abstract protected function _doExec ($queryString);
  315. /**
  316. * do a query which return a limited number of results
  317. * @return jDbResultSet/boolean
  318. */
  319. abstract protected function _doLimitQuery ($queryString, $offset, $number);
  320. /**
  321. * do the escaping of a string.
  322. * you should override it into the driver
  323. * @param string $text the text to escape
  324. * @param boolean $binary true if the content of the string is a binary content
  325. */
  326. protected function _quote($text, $binary){
  327. return addslashes($text);
  328. }
  329. /**
  330. * @var jDbTools
  331. * @since 1.2
  332. */
  333. protected $_tools = null;
  334. /**
  335. * @return jDbTools
  336. * @since 1.2
  337. */
  338. public function tools () {
  339. if (!$this->_tools) {
  340. require_once(jApp::config()->_pluginsPathList_db[$this->driverName].$this->driverName.'.dbtools.php');
  341. $class = $this->driverName.'DbTools';
  342. $this->_tools = new $class($this);
  343. }
  344. return $this->_tools;
  345. }
  346. /**
  347. * @var jDbSchema
  348. * @since 1.2
  349. */
  350. protected $_schema = null;
  351. /**
  352. * @return jDbSchema
  353. * @since 1.2
  354. */
  355. public function schema () {
  356. if (!$this->_schema) {
  357. require_once(jApp::config()->_pluginsPathList_db[$this->driverName].$this->driverName.'.dbschema.php');
  358. $class = $this->driverName.'DbSchema';
  359. $this->_schema = new $class($this);
  360. }
  361. return $this->_schema;
  362. }
  363. }