Source for file MysqlDriver.php

Documentation is available at MysqlDriver.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24.  
  25. import('sylar.common.system.ConfigBox');
  26.  
  27. /** Import the interface implemented from this class */
  28.  
  29. import('sylar.common.db.DataBaseDriver');
  30. import('sylar.common.db.DataBaseConfiguration');
  31. import('sylar.common.system.Logger');
  32. import('sylar.common.system.ExceptionInSylar');
  33.  
  34. /**
  35. * Connessione MySQL.
  36. * Esegue la connessione ad un database, in questo caso specifico MySQL.
  37. * Permette di connettersi direttamente al DB predefinito se non vengono
  38. * specificati parametri diversi.
  39. *
  40. * @package Sylar
  41. * @version 1.0
  42. * @since 03-2005
  43. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  44. * @copyright Sylar Development Team
  45. */
  46. class Sylar_MysqlDriver implements Sylar_DataBaseDriver
  47. {
  48. /**
  49. * Param container
  50. * it's an array that contains all information on DB connection.
  51. *
  52. * @todo create special class container for the param
  53. *
  54. * @var array
  55. */
  56. protected $db;
  57.  
  58.  
  59. /**
  60. * Constructor.
  61. * It loads the configuration info for database from config file
  62. *
  63. * @since 03-2005
  64. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  65. *
  66. * @return void
  67. * @param string $useDB the label of db in use
  68. *
  69. * @see DataBaseConfiguration
  70. * @see sylar_loader.php
  71. */
  72. function __construct(Sylar_DataBaseConfiguration $dbConfiguration=null){
  73. if($dbConfiguration == null){
  74. try{
  75. $this->setConnectionInfo(Sylar_DataBaseManager::getDefaultDbConfiguration());
  76. }catch (ExceptionInSylar $ex){
  77. throw new ExceptionInSylar("Wrong Db Configuration. Check the application configuration file.", 1 );
  78. }
  79. }else{
  80. $this->setConnectionInfo($dbConfiguration);
  81. }
  82. }
  83.  
  84. /**
  85. * Destructor.
  86. *
  87. * @todo empy memory remove results if set
  88. *
  89. * @since 03-2005
  90. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  91. *
  92. * @return void
  93. * @param string $useDB the label of db in use
  94. */
  95. function __destruct(){
  96. if($this->isConnected()) $this->disconnect();
  97. }
  98.  
  99. /**
  100. * Set Connection Info
  101. * Retrive and load the connection info from configuration file
  102. *
  103. * @since 16/feb/08
  104. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  105. *
  106. * @return boolean
  107. * @param string $useDB
  108. */
  109. public function setConnectionInfo(Sylar_DataBaseConfiguration $dbConf){
  110. if( !$dbConf->checkConfiguration() ){
  111. throw new ExceptionInSylar("Wrong Db Configuration. Check the application configuration file.", 1 );
  112. return false;
  113. }
  114. $this->db["user"] = $dbConf->getUsername();
  115. $this->db["password"] = $dbConf->getPassword();
  116. $this->db["db"] = $dbConf->getSchema();
  117. $this->db["host"] = $dbConf->getHost();
  118. return true;
  119. }
  120.  
  121. /**
  122. * Check connection status
  123. * Return true if the connection with database is up, false otherwise.
  124. *
  125. * @since 12-2005
  126. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  127. *
  128. * @return boolean
  129. */
  130. public function isConnected () {
  131. return @is_resource( $this->db["connection"] ) ? true : false;
  132. }
  133.  
  134. /**
  135. * Open a connection.
  136. * Open a connect to Database MySQL adn return the resource to MySQL
  137. * connection or FALSE on failure.
  138. *
  139. * @since 03-2005
  140. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  141. *
  142. * @return resource
  143. */
  144. public function connect(){
  145. $this->db["connection"] = @mysql_connect($this->db["host"], $this->db["user"], $this->db["password"]);
  146. if ($this->db["connection"]){
  147. return $this->db["connection"];
  148. }else{
  149. throw new ExceptionInSylar("MySQL Err.no: ".mysql_errno()." MySQL Err.: ".mysql_error()."\n".__METHOD__." Line: ".__LINE__ );
  150. return false;
  151. }
  152. }
  153.  
  154.  
  155. /**
  156. * Close the connection.
  157. * It close the connection to Db if opened.
  158. *
  159. * @since 03-2005
  160. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  161. *
  162. * @return boolean
  163. */
  164. public function disconnect(){
  165. if($this->isConnected()){
  166. if(!@mysql_close($this->db["connection"])){
  167. throw new ExceptionInSylar("MySQL Err.no: ".mysql_errno()." MySQL Err.: ".mysql_error()."\n".__METHOD__." Line: ".__LINE__ );
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173.  
  174.  
  175. /**
  176. * Exec a SQL query.
  177. * The connection to database must be opened. It exec a query and return
  178. * MySQL result resource, or FALSE on error.
  179. *
  180. * It also return a boolean value if the query is an update/insert/delete
  181. *
  182. * @since 03-2005
  183. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  184. *
  185. * @return resource/boolean
  186. * @param string $query la query SQL da eseguire sul DB impostato nel costruttore
  187. */
  188. public function execQuery($query){
  189. try{
  190. // Purge possible old data in result
  191. $this->resetResults();
  192. $this->db["results"] = @mysql_db_query($this->db["db"], $query, $this->db["connection"]);
  193. if (!$this->db["results"]) {
  194. throw new ExceptionInSylar('Error in MySQL execQuery. Errno: '.mysql_errno()." Err: ".mysql_error() );
  195. return false;
  196. }else{
  197. return $this->db["results"];
  198. }
  199. }catch (ExceptionInSylar $ex){
  200. // send exception out
  201. throw $ex;
  202. return false;
  203. }
  204. }
  205.  
  206. /**
  207. * connect exec and disconnect
  208. * open connection, exec query and close the connection.
  209. * It connect to database and execute the query. It returns a resurse result
  210. *
  211. * @since 03-2005
  212. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  213. *
  214. * @return resource
  215. * @param string $query la query SQL da eseguire sul DB impostato nel costruttore
  216. */
  217. public function execSmartQuery($query){
  218. $log = new Sylar_Logger();
  219. try{
  220. // Purge possible old data in result
  221. $this->resetResults();
  222. if( !$this->connect() ){ return false; }
  223. $this->db["results"] = $this->execQuery($query);
  224. if (!$this->db["results"]) {
  225. throw new ExceptionInSylar("MySQL Err.no: ".mysql_errno()." MySQL Err.: ".mysql_error()."\n".__METHOD__." Line: ".__LINE__ );
  226. }
  227. $this->disconnect();
  228. return $this->db["results"];
  229. }catch (ExceptionInSylar $ex){
  230. // Not log in db
  231. //$log->logEvent($ex->getMessage(), "FATAL");
  232. //echo $ex->getMessage();
  233. // disconnect in case of exceptions
  234. if($this->isConnected()){
  235. $this->disconnect();
  236. }
  237. // send exception out
  238. throw $ex;
  239. return false;
  240. }
  241. }
  242.  
  243.  
  244. /**
  245. * Get the last ID.
  246. * it returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query on success
  247. * On error returns false
  248. *
  249. * @since 03-2005
  250. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  251. *
  252. * @return int
  253. */
  254. public function getLastId(){
  255. if($this->isConnected()){
  256. return @mysql_insert_id($this->db["connection"]);
  257. }else{
  258. throw new ExceptionInSylar("MySQL Err.no: ".mysql_errno()." MySQL Err.: ".mysql_error()."\n".__METHOD__." Line: ".__LINE__ );
  259. return false;
  260. }
  261. }
  262.  
  263. /**
  264. * Affected Row.
  265. * Get the number of affected rows by the last INSERT, UPDATE,
  266. * REPLACE or DELETE query
  267. * on error return false
  268. *
  269. * @since 03-2005
  270. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  271. *
  272. * @return int
  273. */
  274. public function affectedRow(){
  275. if($this->isConnected()){
  276. return @mysql_affected_rows($this->db["connection"]);
  277. }else{
  278. throw new ExceptionInSylar("MySQL Err.no: ".mysql_errno()." MySQL Err.: ".mysql_error()."\n".__METHOD__." Line: ".__LINE__ );
  279. return false;
  280. }
  281. }
  282.  
  283.  
  284. /**
  285. * Read a particoular value.
  286. * returns the value of the field named $fieldName at row $rowNumber
  287. *
  288. * @since 03-2005
  289. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  290. *
  291. * @return mixed
  292. * @param string $fieldName name ov field to read
  293. * @param int $rowNumber number of row starting from 0.
  294. */
  295. public function readField($rowNumber, $fieldName){
  296. return @mysql_result($this->db["results"], $rowNumber, $fieldName);
  297. }
  298.  
  299.  
  300. /**
  301. * read a row in an array.
  302. * returns an array with all field of row in associative indices Array with columns name
  303. *
  304. * @since 02-2007
  305. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  306. *
  307. * @return array
  308. */
  309. public function fetchArrayByName(){
  310. return @mysql_fetch_array($this->db["results"], MYSQL_ASSOC);
  311. }
  312. /**
  313. * read a row in an array.
  314. * returns an array with all field of row in number indices Array
  315. *
  316. * @since 02-2007
  317. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  318. *
  319. * @return array
  320. */
  321. public function fetchArrayByNum(){
  322. // it's the same of @mysql_fetch_array($this->db["results"], MYSQL_NUM);
  323. return @mysql_fetch_row($this->db["results"]);
  324. }
  325.  
  326. /**
  327. * total rows number.
  328. * the number of rows returned by a query like select
  329. *
  330. * @since 03-2005
  331. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  332. *
  333. * @return integer
  334. */
  335. public function resultRows(){
  336. $this->db["rows"] = @mysql_num_rows($this->db["results"]);
  337. return $this->db["rows"];
  338. }
  339.  
  340. /**
  341. * Escapes string for use in SQL.
  342. * It escapes special characters in a string for use in a SQL statement
  343. *
  344. * It returns a string with escape character, on errore returns false
  345. *
  346. * @since 03-2008
  347. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  348. *
  349. * @return string
  350. * @param string $string the string to quote
  351. * @param boolean $autoDisconnect disconnect when finish. Default is TRUE
  352. */
  353. public function quoteString($string, $autoDisconnect=true){
  354. try{
  355. if(!$this->isConnected()){
  356. if( !$this->connect() ){ return false; }
  357. }
  358. $string = mysql_real_escape_string($string);
  359. if($autoDisconnect){
  360. $this->disconnect();
  361. }
  362. return $string;
  363. }catch (ExceptionInSylar $ex){
  364. throw $ex;
  365. return false;
  366. }
  367. }
  368.  
  369.  
  370.  
  371.  
  372. // Protected Methods
  373. // --------------------------------
  374.  
  375.  
  376. /**
  377. * Reset Results
  378. *
  379. * It resets all data in the result variables.
  380. *
  381. * @since 03/2008
  382. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  383. *
  384. * @return boolean
  385. */
  386. protected function resetResults(){
  387. unset($this->db["results"]);
  388. return true;
  389. }
  390.  
  391.  
  392. }
  393.  
  394. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:19 +0200 by phpDocumentor 1.3.0RC3