PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/db/firebird.php

http://seo-phpbb.googlecode.com/
PHP | 491 lines | 391 code | 40 blank | 60 comment | 22 complexity | dbb22a57e4436203f9afdfdef7e7f56f MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package dbal
  5. * @version $Id: firebird.php 8479 2008-03-29 00:22:48Z naderman $
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
  18. /**
  19. * Firebird/Interbase Database Abstraction Layer
  20. * Minimum Requirement is Firebird 2.0
  21. * @package dbal
  22. */
  23. class dbal_firebird extends dbal
  24. {
  25. var $last_query_text = '';
  26. var $service_handle = false;
  27. var $affected_rows = 0;
  28. /**
  29. * Connect to server
  30. */
  31. function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  32. {
  33. $this->persistency = $persistency;
  34. $this->user = $sqluser;
  35. $this->server = $sqlserver . (($port) ? ':' . $port : '');
  36. $this->dbname = $database;
  37. $this->db_connect_id = ($this->persistency) ? @ibase_pconnect($this->server . ':' . $this->dbname, $this->user, $sqlpassword, false, false, 3) : @ibase_connect($this->server . ':' . $this->dbname, $this->user, $sqlpassword, false, false, 3);
  38. $this->service_handle = (function_exists('ibase_service_attach')) ? @ibase_service_attach($this->server, $this->user, $sqlpassword) : false;
  39. return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
  40. }
  41. /**
  42. * Version information about used database
  43. */
  44. function sql_server_info()
  45. {
  46. if ($this->service_handle !== false && function_exists('ibase_server_info'))
  47. {
  48. return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION);
  49. }
  50. return 'Firebird/Interbase';
  51. }
  52. /**
  53. * SQL Transaction
  54. * @access private
  55. */
  56. function _sql_transaction($status = 'begin')
  57. {
  58. switch ($status)
  59. {
  60. case 'begin':
  61. return true;
  62. break;
  63. case 'commit':
  64. return @ibase_commit();
  65. break;
  66. case 'rollback':
  67. return @ibase_rollback();
  68. break;
  69. }
  70. return true;
  71. }
  72. /**
  73. * Base query method
  74. *
  75. * @param string $query Contains the SQL query which shall be executed
  76. * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  77. * @return mixed When casted to bool the returned value returns true on success and false on failure
  78. *
  79. * @access public
  80. */
  81. function sql_query($query = '', $cache_ttl = 0)
  82. {
  83. if ($query != '')
  84. {
  85. global $cache;
  86. // EXPLAIN only in extra debug mode
  87. if (defined('DEBUG_EXTRA'))
  88. {
  89. $this->sql_report('start', $query);
  90. }
  91. $this->last_query_text = $query;
  92. $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
  93. $this->sql_add_num_queries($this->query_result);
  94. if ($this->query_result === false)
  95. {
  96. $array = array();
  97. // We overcome Firebird's 32767 char limit by binding vars
  98. if (strlen($query) > 32767)
  99. {
  100. if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
  101. {
  102. if (strlen($regs[3]) > 32767)
  103. {
  104. preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
  105. $inserts = $vals[0];
  106. unset($vals);
  107. foreach ($inserts as $key => $value)
  108. {
  109. if (!empty($value) && $value[0] === "'" && strlen($value) > 32769) // check to see if this thing is greater than the max + 'x2
  110. {
  111. $inserts[$key] = '?';
  112. $array[] = str_replace("''", "'", substr($value, 1, -1));
  113. }
  114. }
  115. $query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
  116. }
  117. }
  118. else if (preg_match('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|\\d+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data))
  119. {
  120. if (strlen($data[3]) > 32767)
  121. {
  122. $update = $data[1];
  123. $where = $data[4];
  124. preg_match_all('/(\\w++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[3], $temp, PREG_SET_ORDER);
  125. unset($data);
  126. $cols = array();
  127. foreach ($temp as $value)
  128. {
  129. if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32769) // check to see if this thing is greater than the max + 'x2
  130. {
  131. $array[] = str_replace("''", "'", substr($value[2], 1, -1));
  132. $cols[] = $value[1] . '=?';
  133. }
  134. else
  135. {
  136. $cols[] = $value[1] . '=' . $value[2];
  137. }
  138. }
  139. $query = $update . implode(', ', $cols) . ' ' . $where;
  140. unset($cols);
  141. }
  142. }
  143. }
  144. if (!function_exists('ibase_affected_rows') && (preg_match('/^UPDATE ([\w_]++)\s+SET [\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\s*[\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+\s+(WHERE.*)?$/s', $query, $regs) || preg_match('/^DELETE FROM ([\w_]++)\s*(WHERE\s*.*)?$/s', $query, $regs)))
  145. {
  146. $affected_sql = 'SELECT COUNT(*) as num_rows_affected FROM ' . $regs[1];
  147. if (!empty($regs[2]))
  148. {
  149. $affected_sql .= ' ' . $regs[2];
  150. }
  151. if (!($temp_q_id = @ibase_query($this->db_connect_id, $affected_sql)))
  152. {
  153. return false;
  154. }
  155. $temp_result = @ibase_fetch_assoc($temp_q_id);
  156. @ibase_free_result($temp_q_id);
  157. $this->affected_rows = ($temp_result) ? $temp_result['NUM_ROWS_AFFECTED'] : false;
  158. }
  159. if (sizeof($array))
  160. {
  161. $p_query = @ibase_prepare($this->db_connect_id, $query);
  162. array_unshift($array, $p_query);
  163. $this->query_result = call_user_func_array('ibase_execute', $array);
  164. unset($array);
  165. if ($this->query_result === false)
  166. {
  167. $this->sql_error($query);
  168. }
  169. }
  170. else if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false)
  171. {
  172. $this->sql_error($query);
  173. }
  174. if (defined('DEBUG_EXTRA'))
  175. {
  176. $this->sql_report('stop', $query);
  177. }
  178. if (!$this->transaction)
  179. {
  180. if (function_exists('ibase_commit_ret'))
  181. {
  182. @ibase_commit_ret();
  183. }
  184. else
  185. {
  186. // way cooler than ibase_commit_ret :D
  187. @ibase_query('COMMIT RETAIN;');
  188. }
  189. }
  190. if ($cache_ttl && method_exists($cache, 'sql_save'))
  191. {
  192. $this->open_queries[(int) $this->query_result] = $this->query_result;
  193. $cache->sql_save($query, $this->query_result, $cache_ttl);
  194. }
  195. else if (strpos($query, 'SELECT') === 0 && $this->query_result)
  196. {
  197. $this->open_queries[(int) $this->query_result] = $this->query_result;
  198. }
  199. }
  200. else if (defined('DEBUG_EXTRA'))
  201. {
  202. $this->sql_report('fromcache', $query);
  203. }
  204. }
  205. else
  206. {
  207. return false;
  208. }
  209. return ($this->query_result) ? $this->query_result : false;
  210. }
  211. /**
  212. * Build LIMIT query
  213. */
  214. function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
  215. {
  216. $this->query_result = false;
  217. $query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
  218. return $this->sql_query($query, $cache_ttl);
  219. }
  220. /**
  221. * Return number of affected rows
  222. */
  223. function sql_affectedrows()
  224. {
  225. // PHP 5+ function
  226. if (function_exists('ibase_affected_rows'))
  227. {
  228. return ($this->db_connect_id) ? @ibase_affected_rows($this->db_connect_id) : false;
  229. }
  230. else
  231. {
  232. return $this->affected_rows;
  233. }
  234. }
  235. /**
  236. * Fetch current row
  237. */
  238. function sql_fetchrow($query_id = false)
  239. {
  240. global $cache;
  241. if ($query_id === false)
  242. {
  243. $query_id = $this->query_result;
  244. }
  245. if (isset($cache->sql_rowset[$query_id]))
  246. {
  247. return $cache->sql_fetchrow($query_id);
  248. }
  249. if ($query_id === false)
  250. {
  251. return false;
  252. }
  253. $row = array();
  254. $cur_row = @ibase_fetch_object($query_id, IBASE_TEXT);
  255. if (!$cur_row)
  256. {
  257. return false;
  258. }
  259. foreach (get_object_vars($cur_row) as $key => $value)
  260. {
  261. $row[strtolower($key)] = (is_string($value)) ? trim(str_replace(array("\\0", "\\n"), array("\0", "\n"), $value)) : $value;
  262. }
  263. return (sizeof($row)) ? $row : false;
  264. }
  265. /**
  266. * Seek to given row number
  267. * rownum is zero-based
  268. */
  269. function sql_rowseek($rownum, &$query_id)
  270. {
  271. global $cache;
  272. if ($query_id === false)
  273. {
  274. $query_id = $this->query_result;
  275. }
  276. if (isset($cache->sql_rowset[$query_id]))
  277. {
  278. return $cache->sql_rowseek($rownum, $query_id);
  279. }
  280. if ($query_id === false)
  281. {
  282. return;
  283. }
  284. $this->sql_freeresult($query_id);
  285. $query_id = $this->sql_query($this->last_query_text);
  286. if ($query_id === false)
  287. {
  288. return false;
  289. }
  290. // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
  291. for ($i = 0; $i < $rownum; $i++)
  292. {
  293. if (!$this->sql_fetchrow($query_id))
  294. {
  295. return false;
  296. }
  297. }
  298. return true;
  299. }
  300. /**
  301. * Get last inserted id after insert statement
  302. */
  303. function sql_nextid()
  304. {
  305. $query_id = $this->query_result;
  306. if ($query_id !== false && $this->last_query_text != '')
  307. {
  308. if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#i', $this->last_query_text, $tablename))
  309. {
  310. $sql = 'SELECT GEN_ID(' . $tablename[1] . '_gen, 0) AS new_id FROM RDB$DATABASE';
  311. if (!($temp_q_id = @ibase_query($this->db_connect_id, $sql)))
  312. {
  313. return false;
  314. }
  315. $temp_result = @ibase_fetch_assoc($temp_q_id);
  316. @ibase_free_result($temp_q_id);
  317. return ($temp_result) ? $temp_result['NEW_ID'] : false;
  318. }
  319. }
  320. return false;
  321. }
  322. /**
  323. * Free sql result
  324. */
  325. function sql_freeresult($query_id = false)
  326. {
  327. global $cache;
  328. if ($query_id === false)
  329. {
  330. $query_id = $this->query_result;
  331. }
  332. if (isset($cache->sql_rowset[$query_id]))
  333. {
  334. return $cache->sql_freeresult($query_id);
  335. }
  336. if (isset($this->open_queries[(int) $query_id]))
  337. {
  338. unset($this->open_queries[(int) $query_id]);
  339. return @ibase_free_result($query_id);
  340. }
  341. return false;
  342. }
  343. /**
  344. * Escape string used in sql query
  345. */
  346. function sql_escape($msg)
  347. {
  348. return str_replace("'", "''", $msg);
  349. }
  350. /**
  351. * Build LIKE expression
  352. * @access private
  353. */
  354. function _sql_like_expression($expression)
  355. {
  356. return $expression . " ESCAPE '\\'";
  357. }
  358. /**
  359. * Build db-specific query data
  360. * @access private
  361. */
  362. function _sql_custom_build($stage, $data)
  363. {
  364. return $data;
  365. }
  366. /**
  367. * return sql error array
  368. * @access private
  369. */
  370. function _sql_error()
  371. {
  372. return array(
  373. 'message' => @ibase_errmsg(),
  374. 'code' => (@function_exists('ibase_errcode') ? @ibase_errcode() : '')
  375. );
  376. }
  377. /**
  378. * Close sql connection
  379. * @access private
  380. */
  381. function _sql_close()
  382. {
  383. if ($this->service_handle !== false)
  384. {
  385. @ibase_service_detach($this->service_handle);
  386. }
  387. return @ibase_close($this->db_connect_id);
  388. }
  389. /**
  390. * Build db-specific report
  391. * @access private
  392. */
  393. function _sql_report($mode, $query = '')
  394. {
  395. switch ($mode)
  396. {
  397. case 'start':
  398. break;
  399. case 'fromcache':
  400. $endtime = explode(' ', microtime());
  401. $endtime = $endtime[0] + $endtime[1];
  402. $result = @ibase_query($this->db_connect_id, $query);
  403. while ($void = @ibase_fetch_object($result, IBASE_TEXT))
  404. {
  405. // Take the time spent on parsing rows into account
  406. }
  407. @ibase_free_result($result);
  408. $splittime = explode(' ', microtime());
  409. $splittime = $splittime[0] + $splittime[1];
  410. $this->sql_report('record_fromcache', $query, $endtime, $splittime);
  411. break;
  412. }
  413. }
  414. }
  415. ?>