PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/includes/db/mssql_odbc.php

https://github.com/naderman/phpbb-orchestra
PHP | 427 lines | 273 code | 65 blank | 89 comment | 50 complexity | b801b290ae5ca8c8d117c2832d9bdc33 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package dbal
  5. * @version $Id$
  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. * Unified ODBC functions
  20. * Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
  21. * Here we only support MSSQL Server 2000+ because of the provided schema
  22. *
  23. * @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
  24. * If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
  25. * @note odbc.defaultbinmode may affect UTF8 characters
  26. *
  27. * @package dbal
  28. */
  29. class dbal_mssql_odbc extends dbal
  30. {
  31. var $last_query_text = '';
  32. /**
  33. * Connect to server
  34. */
  35. function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  36. {
  37. $this->persistency = $persistency;
  38. $this->user = $sqluser;
  39. $this->dbname = $database;
  40. $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
  41. $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
  42. $max_size = @ini_get('odbc.defaultlrl');
  43. if (!empty($max_size))
  44. {
  45. $unit = strtolower(substr($max_size, -1, 1));
  46. $max_size = (int) $max_size;
  47. if ($unit == 'k')
  48. {
  49. $max_size = floor($max_size / 1024);
  50. }
  51. else if ($unit == 'g')
  52. {
  53. $max_size *= 1024;
  54. }
  55. else if (is_numeric($unit))
  56. {
  57. $max_size = floor((int) ($max_size . $unit) / 1048576);
  58. }
  59. $max_size = max(8, $max_size) . 'M';
  60. @ini_set('odbc.defaultlrl', $max_size);
  61. }
  62. $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
  63. return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
  64. }
  65. /**
  66. * Version information about used database
  67. * @param bool $raw if true, only return the fetched sql_server_version
  68. * @param bool $use_cache If true, it is safe to retrieve the value from the cache
  69. * @return string sql server version
  70. */
  71. function sql_server_info($raw = false, $use_cache = true)
  72. {
  73. global $cache;
  74. if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
  75. {
  76. $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
  77. $row = false;
  78. if ($result_id)
  79. {
  80. $row = @odbc_fetch_array($result_id);
  81. @odbc_free_result($result_id);
  82. }
  83. $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
  84. if (!empty($cache) && $use_cache)
  85. {
  86. $cache->put('mssqlodbc_version', $this->sql_server_version);
  87. }
  88. }
  89. if ($raw)
  90. {
  91. return $this->sql_server_version;
  92. }
  93. return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
  94. }
  95. /**
  96. * SQL Transaction
  97. * @access private
  98. */
  99. function _sql_transaction($status = 'begin')
  100. {
  101. switch ($status)
  102. {
  103. case 'begin':
  104. return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
  105. break;
  106. case 'commit':
  107. return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
  108. break;
  109. case 'rollback':
  110. return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
  111. break;
  112. }
  113. return true;
  114. }
  115. /**
  116. * Base query method
  117. *
  118. * @param string $query Contains the SQL query which shall be executed
  119. * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  120. * @return mixed When casted to bool the returned value returns true on success and false on failure
  121. *
  122. * @access public
  123. */
  124. function sql_query($query = '', $cache_ttl = 0)
  125. {
  126. if ($query != '')
  127. {
  128. global $cache;
  129. // EXPLAIN only in extra debug mode
  130. if (defined('DEBUG_EXTRA'))
  131. {
  132. $this->sql_report('start', $query);
  133. }
  134. $this->last_query_text = $query;
  135. $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
  136. $this->sql_add_num_queries($this->query_result);
  137. if ($this->query_result === false)
  138. {
  139. if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
  140. {
  141. $this->sql_error($query);
  142. }
  143. if (defined('DEBUG_EXTRA'))
  144. {
  145. $this->sql_report('stop', $query);
  146. }
  147. if ($cache_ttl && method_exists($cache, 'sql_save'))
  148. {
  149. $this->open_queries[(int) $this->query_result] = $this->query_result;
  150. $cache->sql_save($query, $this->query_result, $cache_ttl);
  151. }
  152. else if (strpos($query, 'SELECT') === 0 && $this->query_result)
  153. {
  154. $this->open_queries[(int) $this->query_result] = $this->query_result;
  155. }
  156. }
  157. else if (defined('DEBUG_EXTRA'))
  158. {
  159. $this->sql_report('fromcache', $query);
  160. }
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. return $this->query_result;
  167. }
  168. /**
  169. * Build LIMIT query
  170. */
  171. function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
  172. {
  173. $this->query_result = false;
  174. // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
  175. if ($total)
  176. {
  177. // We need to grab the total number of rows + the offset number of rows to get the correct result
  178. if (strpos($query, 'SELECT DISTINCT') === 0)
  179. {
  180. $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
  181. }
  182. else
  183. {
  184. $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
  185. }
  186. }
  187. $result = $this->sql_query($query, $cache_ttl);
  188. // Seek by $offset rows
  189. if ($offset)
  190. {
  191. $this->sql_rowseek($offset, $result);
  192. }
  193. return $result;
  194. }
  195. /**
  196. * Return number of affected rows
  197. */
  198. function sql_affectedrows()
  199. {
  200. return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
  201. }
  202. /**
  203. * Fetch current row
  204. * @note number of bytes returned depends on odbc.defaultlrl php.ini setting. If it is limited to 4K for example only 4K of data is returned max.
  205. */
  206. function sql_fetchrow($query_id = false, $debug = false)
  207. {
  208. global $cache;
  209. if ($query_id === false)
  210. {
  211. $query_id = $this->query_result;
  212. }
  213. if (isset($cache->sql_rowset[$query_id]))
  214. {
  215. return $cache->sql_fetchrow($query_id);
  216. }
  217. return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
  218. }
  219. /**
  220. * Seek to given row number
  221. * rownum is zero-based
  222. */
  223. function sql_rowseek($rownum, &$query_id)
  224. {
  225. global $cache;
  226. if ($query_id === false)
  227. {
  228. $query_id = $this->query_result;
  229. }
  230. if (isset($cache->sql_rowset[$query_id]))
  231. {
  232. return $cache->sql_rowseek($rownum, $query_id);
  233. }
  234. if ($query_id === false)
  235. {
  236. return false;
  237. }
  238. $this->sql_freeresult($query_id);
  239. $query_id = $this->sql_query($this->last_query_text);
  240. if ($query_id === false)
  241. {
  242. return false;
  243. }
  244. // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
  245. for ($i = 0; $i < $rownum; $i++)
  246. {
  247. if (!$this->sql_fetchrow($query_id))
  248. {
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. /**
  255. * Get last inserted id after insert statement
  256. */
  257. function sql_nextid()
  258. {
  259. $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
  260. if ($result_id)
  261. {
  262. if (@odbc_fetch_array($result_id))
  263. {
  264. $id = @odbc_result($result_id, 1);
  265. @odbc_free_result($result_id);
  266. return $id;
  267. }
  268. @odbc_free_result($result_id);
  269. }
  270. return false;
  271. }
  272. /**
  273. * Free sql result
  274. */
  275. function sql_freeresult($query_id = false)
  276. {
  277. global $cache;
  278. if ($query_id === false)
  279. {
  280. $query_id = $this->query_result;
  281. }
  282. if (isset($cache->sql_rowset[$query_id]))
  283. {
  284. return $cache->sql_freeresult($query_id);
  285. }
  286. if (isset($this->open_queries[(int) $query_id]))
  287. {
  288. unset($this->open_queries[(int) $query_id]);
  289. return @odbc_free_result($query_id);
  290. }
  291. return false;
  292. }
  293. /**
  294. * Escape string used in sql query
  295. */
  296. function sql_escape($msg)
  297. {
  298. return str_replace(array("'", "\0"), array("''", ''), $msg);
  299. }
  300. /**
  301. * Build LIKE expression
  302. * @access private
  303. */
  304. function _sql_like_expression($expression)
  305. {
  306. return $expression . " ESCAPE '\\'";
  307. }
  308. /**
  309. * Build db-specific query data
  310. * @access private
  311. */
  312. function _sql_custom_build($stage, $data)
  313. {
  314. return $data;
  315. }
  316. /**
  317. * return sql error array
  318. * @access private
  319. */
  320. function _sql_error()
  321. {
  322. return array(
  323. 'message' => @odbc_errormsg(),
  324. 'code' => @odbc_error()
  325. );
  326. }
  327. /**
  328. * Close sql connection
  329. * @access private
  330. */
  331. function _sql_close()
  332. {
  333. return @odbc_close($this->db_connect_id);
  334. }
  335. /**
  336. * Build db-specific report
  337. * @access private
  338. */
  339. function _sql_report($mode, $query = '')
  340. {
  341. switch ($mode)
  342. {
  343. case 'start':
  344. break;
  345. case 'fromcache':
  346. $endtime = explode(' ', microtime());
  347. $endtime = $endtime[0] + $endtime[1];
  348. $result = @odbc_exec($this->db_connect_id, $query);
  349. while ($void = @odbc_fetch_array($result))
  350. {
  351. // Take the time spent on parsing rows into account
  352. }
  353. @odbc_free_result($result);
  354. $splittime = explode(' ', microtime());
  355. $splittime = $splittime[0] + $splittime[1];
  356. $this->sql_report('record_fromcache', $query, $endtime, $splittime);
  357. break;
  358. }
  359. }
  360. }
  361. ?>