PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Release/Last/Framework/Lib/db/DBMysql.php

https://github.com/sergiygladkyy/OEF
PHP | 495 lines | 248 code | 76 blank | 171 comment | 39 complexity | f3bc4fa1b20baf3c2e66416081a2ca44 MD5 | raw file
  1. <?php
  2. import('lib/db/DBManager');
  3. /**
  4. * Управление соединением с БД
  5. */
  6. class DBMysql implements DBManager
  7. {
  8. protected static $instance = null;
  9. protected $conn = null;
  10. protected $prefix = '';
  11. protected $charset = 'utf8';
  12. protected $dbname = null;
  13. /**
  14. * Create new instance
  15. *
  16. * @param array $options
  17. * @return this
  18. */
  19. public static function createInstance(array $options)
  20. {
  21. if(is_null(self::$instance))
  22. {
  23. self::$instance = new DBMysql($options);
  24. }
  25. return self::$instance;
  26. }
  27. /**
  28. * Get this instance
  29. *
  30. * @throws Exception
  31. * @return this
  32. */
  33. public static function getInstance()
  34. {
  35. if(is_null(self::$instance))
  36. {
  37. throw new Exception(__METHOD__.": Instance is not exists");
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * Construct
  43. *
  44. * @throws Exception
  45. * @param array& $options
  46. * @return this
  47. */
  48. protected function __construct(array& $options)
  49. {
  50. if (!function_exists('mysql_connect')) throw new Exception(__METHOD__.': The MySQL adapter is not available');
  51. if (empty($options['dbserver']) ||
  52. empty($options['dbusername']) ||
  53. !isset($options['dbpass']) ||
  54. empty($options['dbname'])
  55. )
  56. {
  57. throw new Exception(__METHOD__.': db configuration is wrong');
  58. }
  59. extract($options, EXTR_PREFIX_ALL, "");
  60. if (!empty($_dbprefix)) $this->prefix = $_dbprefix;
  61. if (!empty($_dbcharset)) $this->charset = $_dbcharset;
  62. $this->connect($_dbserver, $_dbusername, $_dbpass);
  63. $this->selectDB($_dbname);
  64. }
  65. /**
  66. * Close connection
  67. *
  68. * @return void
  69. */
  70. public function __destruct()
  71. {
  72. if (!is_null($this->conn))
  73. {
  74. mysql_close($this->conn);
  75. }
  76. }
  77. /**
  78. * Connect to database
  79. *
  80. * @throws Exception
  81. * @param string $dbserver
  82. * @param string $dbusername
  83. * @param string $dbpass
  84. * @return void
  85. */
  86. protected function connect($dbserver, $dbusername, $dbpass)
  87. {
  88. $this->conn = mysql_connect($dbserver, $dbusername, $dbpass);
  89. if (!$this->conn)
  90. {
  91. throw new Exception(__METHOD__.": Can't connect to mysql database on $dbserver thru $dbusername with password $dbpass");
  92. }
  93. if (!mysql_query("SET CHARACTER SET ".$this->charset)) throw new Exception(__METHOD__.": Can't set encoding");
  94. if (!mysql_query("SET character_set_client = ".$this->charset)) throw new Exception(__METHOD__.": Can't set encoding");
  95. if (!mysql_query("SET character_set_results = ".$this->charset)) throw new Exception(__METHOD__.": Can't set encoding");
  96. if (!mysql_query("SET character_set_connection = ".$this->charset)) throw new Exception(__METHOD__.": Can't set encoding");
  97. }
  98. /**
  99. * Select database
  100. *
  101. * @throws Exception
  102. * @param string $dbname
  103. * @return void
  104. */
  105. public function selectDB($dbname)
  106. {
  107. if (!mysql_select_db("$dbname", $this->conn))
  108. {
  109. throw new Exception(__METHOD__.": Can't select mysql database $dbname");
  110. }
  111. $this->dbname = $dbname;
  112. }
  113. /**
  114. * Reconnection to other database
  115. *
  116. * @param string $dbserver
  117. * @param string $dbusername
  118. * @param string $dbpass
  119. * @param string $dbname
  120. * @return void
  121. */
  122. public function reconnection($dbserver, $dbusername, $dbpass, $dbname)
  123. {
  124. mysql_close($this->conn);
  125. $this->connect($dbserver, $dbusername, $dbpass);
  126. $this->selectDB($dbname);
  127. }
  128. /**
  129. * (non-PHPdoc)
  130. * @see lib/db/mDBManager#fetchRow($res)
  131. */
  132. public function fetchRow($res)
  133. {
  134. return mysql_fetch_row($res);
  135. }
  136. /**
  137. * (non-PHPdoc)
  138. * @see lib/db/mDBManager#fetchAssoc($res)
  139. */
  140. public function fetchAssoc($res)
  141. {
  142. return mysql_fetch_assoc($res);
  143. }
  144. /**
  145. * (non-PHPdoc)
  146. * @see lib/db/mDBManager#fetchArray($res)
  147. */
  148. public function fetchArray($res)
  149. {
  150. return mysql_fetch_array($res);
  151. }
  152. /**
  153. * (non-PHPdoc)
  154. * @see lib/db/DBManager#fetchObject($res)
  155. */
  156. public function fetchObject($res)
  157. {
  158. return mysql_fetch_object($res);
  159. }
  160. /**
  161. * (non-PHPdoc)
  162. * @see lib/db/mDBManager#getNumRows()
  163. */
  164. public function getNumRows()
  165. {
  166. return mysql_num_rows($this->conn);
  167. }
  168. /**
  169. * (non-PHPdoc)
  170. * @see lib/db/DBManager#getAffectedRows()
  171. */
  172. public function getAffectedRows()
  173. {
  174. return mysql_affected_rows($this->conn);
  175. }
  176. /**
  177. * (non-PHPdoc)
  178. * @see lib/db/mDBManager#getInsertId()
  179. */
  180. public function getInsertId()
  181. {
  182. return mysql_insert_id($this->conn);
  183. }
  184. /**
  185. * (non-PHPdoc)
  186. * @see lib/db/DBManager#getErrno()
  187. */
  188. public function getErrno()
  189. {
  190. return mysql_errno($this->conn);
  191. }
  192. /**
  193. * (non-PHPdoc)
  194. * @see lib/db/mDBManager#getError()
  195. */
  196. public function getError()
  197. {
  198. return mysql_error($this->conn);
  199. }
  200. /**
  201. * (non-PHPdoc)
  202. * @see lib/db/DBManager#realEscapeString($str)
  203. */
  204. public function realEscapeString($str)
  205. {
  206. return mysql_real_escape_string($str, $this->conn);
  207. }
  208. /**
  209. * (non-PHPdoc)
  210. * @see lib/db/DBManager#executeQuery($query)
  211. */
  212. public function executeQuery($query)
  213. {
  214. return mysql_query($query, $this->conn);
  215. }
  216. /**
  217. * (non-PHPdoc)
  218. * @see lib/db/DBManager#freeResult($linkRes)
  219. */
  220. public function freeResult($linkRes)
  221. {
  222. return mysql_free_result($linkRes);
  223. }
  224. /**
  225. * Get first row from result as object
  226. *
  227. * @param string $query
  228. * @param array $options - not supported
  229. * @return object or null
  230. */
  231. public function loadObject($query, array $options = array())
  232. {
  233. if (!($res = $this->executeQuery($query))) return null;
  234. if ($object = $this->fetchObject($res))
  235. {
  236. return $object;
  237. }
  238. return null;
  239. }
  240. /**
  241. * Get result as list object
  242. *
  243. * @param string $query
  244. * @param array $options
  245. * array(
  246. * 'key' => <primary key fild name>, // return array with row index == key
  247. * )
  248. * @return array or null
  249. */
  250. public function loadObjectList($query, array $options = array())
  251. {
  252. if (!($res = $this->executeQuery($query))) return null;
  253. $list = array();
  254. if (!empty($options['key']))
  255. {
  256. while ($row = $this->fetchObject($res)) $list[$row->$options['key']] = $row;
  257. }
  258. else
  259. {
  260. while ($row = $this->fetchObject($res)) $list[] = $row;
  261. }
  262. return $list;
  263. }
  264. /**
  265. * Get first row from result as numeric array
  266. *
  267. * @param string $query
  268. * @param array $options - not supported
  269. * @return array or null
  270. */
  271. public function loadRow($query, array $options = array())
  272. {
  273. if (!($res = $this->executeQuery($query))) return null;
  274. if ($row = $this->fetchRow($res))
  275. {
  276. return $row;
  277. }
  278. return null;
  279. }
  280. /**
  281. * Get result as list numeric arrays
  282. *
  283. * @param string $query
  284. * @param array $options
  285. * array(
  286. * 'key' => <primary key index>, // return array with row index == key
  287. * 'field' => <field index> // return numeric array with value == field value
  288. * )
  289. * 'key' XOR 'field'
  290. * @return array or null
  291. */
  292. public function loadRowList($query, array $options = array())
  293. {
  294. if (!($res = $this->executeQuery($query))) return null;
  295. $list = array();
  296. if (!empty($options['key']))
  297. {
  298. while ($row = $this->fetchRow($res)) $list[$row[$options['key']]] = $row;
  299. }
  300. elseif (!empty($options['field']))
  301. {
  302. while ($row = $this->fetchRow($res)) $list[] = $row[$options['field']];
  303. }
  304. else
  305. {
  306. while ($row = $this->fetchRow($res)) $list[] = $row;
  307. }
  308. return $list;
  309. }
  310. /**
  311. * Get first row from result as assoc array
  312. *
  313. * @param string $query
  314. * @param array $options - not supported
  315. * @return array or null
  316. */
  317. public function loadAssoc($query, array $options = array())
  318. {
  319. if (!($res = $this->executeQuery($query))) return null;
  320. if ($row = $this->fetchAssoc($res))
  321. {
  322. return $row;
  323. }
  324. return null;
  325. }
  326. /**
  327. * Get result as list assoc arrays
  328. *
  329. * @param string $query
  330. * @param array $options
  331. * array(
  332. * 'key' => <primary key name>, // return array with row index == key
  333. * 'field' => <field name> // return numeric array with value == field value
  334. * )
  335. * 'key' XOR 'field'
  336. * @return array or null
  337. */
  338. public function loadAssocList($query, array $options = array())
  339. {
  340. if (!($res = $this->executeQuery($query))) return null;
  341. $list = array();
  342. if (!empty($options['key']))
  343. {
  344. while ($row = $this->fetchAssoc($res)) $list[$row[$options['key']]] = $row;
  345. }
  346. elseif (!empty($options['field']))
  347. {
  348. while ($row = $this->fetchAssoc($res)) $list[] = $row[$options['field']];
  349. }
  350. else
  351. {
  352. while ($row = $this->fetchAssoc($res)) $list[] = $row;
  353. }
  354. return $list;
  355. }
  356. /**
  357. * Get first row from result as numeric and assoc array
  358. *
  359. * @param string $query
  360. * @param array $options - not supported
  361. * @return object or null
  362. */
  363. public function loadArray($query, array $options = array())
  364. {
  365. if (!($res = $this->executeQuery($query))) return null;
  366. if ($row = $this->fetchArray($res))
  367. {
  368. return $row;
  369. }
  370. return null;
  371. }
  372. /**
  373. * Get result as list numeric and assoc arrays
  374. *
  375. * @param string $query
  376. * @param array $options
  377. * array(
  378. * 'key' => <primary key name or index>, // return array with row index == key
  379. * 'field' => <field name or index> // return numeric array with value == field value
  380. * )
  381. * 'key' XOR 'field'
  382. * @return array or null
  383. */
  384. public function loadArrayList($query, array $options = array())
  385. {
  386. if (!($res = $this->executeQuery($query))) return null;
  387. $list = array();
  388. if (!empty($options['key']))
  389. {
  390. while ($row = $this->fetchArray($res)) $list[$row[$options['key']]] = $row;
  391. }
  392. elseif (!empty($options['field']))
  393. {
  394. while ($row = $this->fetchArray($res)) $list[] = $row[$options['field']];
  395. }
  396. else
  397. {
  398. while ($row = $this->fetchArray($res)) $list[] = $row;
  399. }
  400. return $list;
  401. }
  402. /**
  403. * (non-PHPdoc)
  404. * @see lib/db/DBManager#getAutoIncrementValue($table, $options)
  405. */
  406. public function getAutoIncrementValue($table, array $options = array())
  407. {
  408. $query = "SELECT `AUTO_INCREMENT` FROM information_schema.TABLES ".
  409. "WHERE TABLE_SCHEMA='".$this->dbname."' AND TABLE_NAME='".$table."'";
  410. if (!($res = $this->executeQuery($query))) return null;
  411. if ($row = $this->fetchArray($res))
  412. {
  413. return $row[0];
  414. }
  415. return null;
  416. }
  417. }