PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/includes/db/mssql_odbc.php

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