PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jelix/plugins/db/mysql/mysql.dbconnection.php

http://github.com/jelix/jelix
PHP | 188 lines | 101 code | 25 blank | 62 comment | 11 complexity | 66dafee3a5c573d548552acbba2d0a5e MD5 | raw file
Possible License(s): BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage db_driver
  5. * @author G?Šrald Croes, Laurent Jouanneau
  6. * @contributor Laurent Jouanneau
  7. * @contributor Sylvain de Vathaire, Julien Issler
  8. * @copyright 2001-2005 CopixTeam, 2005-2012 Laurent Jouanneau
  9. * @copyright 2009 Julien Issler
  10. * This class was get originally from the Copix project (CopixDbConnectionMysql, Copix 2.3dev20050901, http://www.copix.org)
  11. * Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  12. * Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  13. * and this class was adapted for Jelix by Laurent Jouanneau
  14. *
  15. * @link http://www.jelix.org
  16. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  17. */
  18. require_once(dirname(__FILE__).'/mysql.dbresultset.php');
  19. /**
  20. *
  21. * @package jelix
  22. * @subpackage db_driver
  23. */
  24. class mysqlDbConnection extends jDbConnection {
  25. protected $_charsets =array( 'UTF-8'=>'utf8', 'ISO-8859-1'=>'latin1');
  26. function __construct($profile){
  27. // because of the @, we need to test mysl existance, else there would be
  28. // a stop without error
  29. if(!function_exists('mysql_connect')){
  30. throw new jException('jelix~db.error.nofunction','mysql');
  31. }
  32. parent::__construct($profile);
  33. }
  34. /**
  35. * Enclose the field name
  36. * @param string $fieldName the field name
  37. * @return string the enclosed field name
  38. * @since 1.1.1
  39. */
  40. public function encloseName($fieldName){
  41. return '`'.$fieldName.'`';
  42. }
  43. /**
  44. * Begin a transaction
  45. */
  46. public function beginTransaction (){
  47. $this->_doExec ('SET AUTOCOMMIT=0');
  48. $this->_doExec ('BEGIN');
  49. }
  50. /**
  51. * Commit since the last begin
  52. */
  53. public function commit (){
  54. $this->_doExec ('COMMIT');
  55. $this->_doExec ('SET AUTOCOMMIT=1');
  56. }
  57. /**
  58. * Rollback since the last BEGIN
  59. */
  60. public function rollback (){
  61. $this->_doExec ('ROLLBACK');
  62. $this->_doExec ('SET AUTOCOMMIT=1');
  63. }
  64. /**
  65. *
  66. */
  67. public function prepare ($query){
  68. throw new jException('jelix~db.error.feature.unsupported', array('mysql','prepare'));
  69. }
  70. public function errorInfo(){
  71. return array( 'HY000' ,mysql_errno($this->_connection), mysql_error($this->_connection));
  72. }
  73. public function errorCode(){
  74. return mysql_errno($this->_connection);
  75. }
  76. protected function _connect (){
  77. $funcconnect= ($this->profile['persistent']? 'mysql_pconnect':'mysql_connect');
  78. if($cnx = @$funcconnect ($this->profile['host'], $this->profile['user'], $this->profile['password'])){
  79. if(isset($this->profile['force_encoding']) && $this->profile['force_encoding'] == true
  80. && isset($this->_charsets[jApp::config()->charset])){
  81. mysql_query("SET NAMES '".$this->_charsets[jApp::config()->charset]."'", $cnx);
  82. }
  83. return $cnx;
  84. }else{
  85. throw new jException('jelix~db.error.connection',$this->profile['host']);
  86. }
  87. }
  88. protected function _disconnect (){
  89. return mysql_close ($this->_connection);
  90. }
  91. protected function _doQuery ($query){
  92. // here and not during the connect, in case there would be multiple active connections
  93. if(!mysql_select_db ($this->profile['database'], $this->_connection)){
  94. if(mysql_errno($this->_connection))
  95. throw new jException('jelix~db.error.database.unknown',$this->profile['database']);
  96. else
  97. throw new jException('jelix~db.error.connection.closed',$this->profile['name']);
  98. }
  99. if ($qI = mysql_query ($query, $this->_connection)){
  100. return new mysqlDbResultSet ($qI);
  101. }else{
  102. throw new jException('jelix~db.error.query.bad', mysql_error($this->_connection).'('.$query.')');
  103. }
  104. }
  105. protected function _doExec($query){
  106. if(!mysql_select_db ($this->profile['database'], $this->_connection))
  107. throw new jException('jelix~db.error.database.unknown',$this->profile['database']);
  108. if ($qI = mysql_query ($query, $this->_connection)){
  109. return mysql_affected_rows($this->_connection);
  110. }else{
  111. throw new jException('jelix~db.error.query.bad', mysql_error($this->_connection).'('.$query.')');
  112. }
  113. }
  114. protected function _doLimitQuery ($queryString, $offset, $number){
  115. $queryString.= ' LIMIT '.$offset.','.$number;
  116. $this->lastQuery = $queryString;
  117. $result = $this->_doQuery($queryString);
  118. return $result;
  119. }
  120. public function lastInsertId($fromSequence=''){// on n'a pas besoin de l'argument pour mysql
  121. return mysql_insert_id ($this->_connection);
  122. }
  123. /**
  124. * tell mysql to be autocommit or not
  125. * @param boolean $state the state of the autocommit value
  126. * @return void
  127. */
  128. protected function _autoCommitNotify ($state){
  129. $this->query ('SET AUTOCOMMIT='.($state ? '1' : '0'));
  130. }
  131. /**
  132. * @return string escaped text or binary string
  133. */
  134. protected function _quote($text, $binary) {
  135. return mysql_real_escape_string($text, $this->_connection);
  136. }
  137. /**
  138. *
  139. * @param integer $id the attribut id
  140. * @return string the attribute value
  141. * @see PDO::getAttribute()
  142. */
  143. public function getAttribute($id) {
  144. switch($id) {
  145. case self::ATTR_CLIENT_VERSION:
  146. return mysql_get_client_info();
  147. case self::ATTR_SERVER_VERSION:
  148. return mysql_get_server_info($this->_connection);
  149. break;
  150. case self::ATTR_SERVER_INFO:
  151. return mysql_get_host_info($this->_connection);
  152. }
  153. return "";
  154. }
  155. /**
  156. *
  157. * @param integer $id the attribut id
  158. * @param string $value the attribute value
  159. * @see PDO::setAttribute()
  160. */
  161. public function setAttribute($id, $value) {
  162. }
  163. }