PageRenderTime 43ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/db/mssql.php

http://seo-phpbb.googlecode.com/
PHP | 433 lines | 290 code | 62 blank | 81 comment | 48 complexity | b28cbc5d262936788f91b7dd9d905aa3 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package dbal
  5. * @version $Id: mssql.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. * MSSQL Database Abstraction Layer
  20. * Minimum Requirement is MSSQL 2000+
  21. * @package dbal
  22. */
  23. class dbal_mssql extends dbal
  24. {
  25. /**
  26. * Connect to server
  27. */
  28. function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  29. {
  30. $this->persistency = $persistency;
  31. $this->user = $sqluser;
  32. $this->server = $sqlserver . (($port) ? ':' . $port : '');
  33. $this->dbname = $database;
  34. @ini_set('mssql.charset', 'UTF-8');
  35. @ini_set('mssql.textlimit', 2147483647);
  36. @ini_set('mssql.textsize', 2147483647);
  37. if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
  38. {
  39. $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
  40. }
  41. else
  42. {
  43. $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
  44. }
  45. if ($this->db_connect_id && $this->dbname != '')
  46. {
  47. if (!@mssql_select_db($this->dbname, $this->db_connect_id))
  48. {
  49. @mssql_close($this->db_connect_id);
  50. return false;
  51. }
  52. }
  53. return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
  54. }
  55. /**
  56. * Version information about used database
  57. */
  58. function sql_server_info()
  59. {
  60. $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
  61. $row = false;
  62. if ($result_id)
  63. {
  64. $row = @mssql_fetch_assoc($result_id);
  65. @mssql_free_result($result_id);
  66. }
  67. if ($row)
  68. {
  69. return 'MSSQL<br />' . implode(' ', $row);
  70. }
  71. return 'MSSQL';
  72. }
  73. /**
  74. * SQL Transaction
  75. * @access private
  76. */
  77. function _sql_transaction($status = 'begin')
  78. {
  79. switch ($status)
  80. {
  81. case 'begin':
  82. return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
  83. break;
  84. case 'commit':
  85. return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
  86. break;
  87. case 'rollback':
  88. return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
  89. break;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Base query method
  95. *
  96. * @param string $query Contains the SQL query which shall be executed
  97. * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  98. * @return mixed When casted to bool the returned value returns true on success and false on failure
  99. *
  100. * @access public
  101. */
  102. function sql_query($query = '', $cache_ttl = 0)
  103. {
  104. if ($query != '')
  105. {
  106. global $cache;
  107. // EXPLAIN only in extra debug mode
  108. if (defined('DEBUG_EXTRA'))
  109. {
  110. $this->sql_report('start', $query);
  111. }
  112. $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
  113. $this->sql_add_num_queries($this->query_result);
  114. if ($this->query_result === false)
  115. {
  116. if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
  117. {
  118. $this->sql_error($query);
  119. }
  120. if (defined('DEBUG_EXTRA'))
  121. {
  122. $this->sql_report('stop', $query);
  123. }
  124. if ($cache_ttl && method_exists($cache, 'sql_save'))
  125. {
  126. $this->open_queries[(int) $this->query_result] = $this->query_result;
  127. $cache->sql_save($query, $this->query_result, $cache_ttl);
  128. }
  129. else if (strpos($query, 'SELECT') === 0 && $this->query_result)
  130. {
  131. $this->open_queries[(int) $this->query_result] = $this->query_result;
  132. }
  133. }
  134. else if (defined('DEBUG_EXTRA'))
  135. {
  136. $this->sql_report('fromcache', $query);
  137. }
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. return ($this->query_result) ? $this->query_result : false;
  144. }
  145. /**
  146. * Build LIMIT query
  147. */
  148. function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
  149. {
  150. $this->query_result = false;
  151. // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
  152. if ($total)
  153. {
  154. // We need to grab the total number of rows + the offset number of rows to get the correct result
  155. if (strpos($query, 'SELECT DISTINCT') === 0)
  156. {
  157. $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
  158. }
  159. else
  160. {
  161. $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
  162. }
  163. }
  164. $result = $this->sql_query($query, $cache_ttl);
  165. // Seek by $offset rows
  166. if ($offset)
  167. {
  168. $this->sql_rowseek($offset, $result);
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Return number of affected rows
  174. */
  175. function sql_affectedrows()
  176. {
  177. return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
  178. }
  179. /**
  180. * Fetch current row
  181. */
  182. function sql_fetchrow($query_id = false)
  183. {
  184. global $cache;
  185. if ($query_id === false)
  186. {
  187. $query_id = $this->query_result;
  188. }
  189. if (isset($cache->sql_rowset[$query_id]))
  190. {
  191. return $cache->sql_fetchrow($query_id);
  192. }
  193. if ($query_id === false)
  194. {
  195. return false;
  196. }
  197. $row = @mssql_fetch_assoc($query_id);
  198. // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
  199. if ($row)
  200. {
  201. foreach ($row as $key => $value)
  202. {
  203. $row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
  204. }
  205. }
  206. return $row;
  207. }
  208. /**
  209. * Seek to given row number
  210. * rownum is zero-based
  211. */
  212. function sql_rowseek($rownum, &$query_id)
  213. {
  214. global $cache;
  215. if ($query_id === false)
  216. {
  217. $query_id = $this->query_result;
  218. }
  219. if (isset($cache->sql_rowset[$query_id]))
  220. {
  221. return $cache->sql_rowseek($rownum, $query_id);
  222. }
  223. return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
  224. }
  225. /**
  226. * Get last inserted id after insert statement
  227. */
  228. function sql_nextid()
  229. {
  230. $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
  231. if ($result_id)
  232. {
  233. if ($row = @mssql_fetch_assoc($result_id))
  234. {
  235. @mssql_free_result($result_id);
  236. return $row['computed'];
  237. }
  238. @mssql_free_result($result_id);
  239. }
  240. return false;
  241. }
  242. /**
  243. * Free sql result
  244. */
  245. function sql_freeresult($query_id = false)
  246. {
  247. global $cache;
  248. if ($query_id === false)
  249. {
  250. $query_id = $this->query_result;
  251. }
  252. if (isset($cache->sql_rowset[$query_id]))
  253. {
  254. return $cache->sql_freeresult($query_id);
  255. }
  256. if (isset($this->open_queries[$query_id]))
  257. {
  258. unset($this->open_queries[$query_id]);
  259. return @mssql_free_result($query_id);
  260. }
  261. return false;
  262. }
  263. /**
  264. * Escape string used in sql query
  265. */
  266. function sql_escape($msg)
  267. {
  268. return str_replace("'", "''", $msg);
  269. }
  270. /**
  271. * Build LIKE expression
  272. * @access private
  273. */
  274. function _sql_like_expression($expression)
  275. {
  276. return $expression . " ESCAPE '\\'";
  277. }
  278. /**
  279. * return sql error array
  280. * @access private
  281. */
  282. function _sql_error()
  283. {
  284. $error = array(
  285. 'message' => @mssql_get_last_message(),
  286. 'code' => ''
  287. );
  288. // Get error code number
  289. $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
  290. if ($result_id)
  291. {
  292. $row = @mssql_fetch_assoc($result_id);
  293. $error['code'] = $row['code'];
  294. @mssql_free_result($result_id);
  295. }
  296. // Get full error message if possible
  297. $sql = 'SELECT CAST(description as varchar(255)) as message
  298. FROM master.dbo.sysmessages
  299. WHERE error = ' . $error['code'];
  300. $result_id = @mssql_query($sql);
  301. if ($result_id)
  302. {
  303. $row = @mssql_fetch_assoc($result_id);
  304. if (!empty($row['message']))
  305. {
  306. $error['message'] .= '<br />' . $row['message'];
  307. }
  308. @mssql_free_result($result_id);
  309. }
  310. return $error;
  311. }
  312. /**
  313. * Build db-specific query data
  314. * @access private
  315. */
  316. function _sql_custom_build($stage, $data)
  317. {
  318. return $data;
  319. }
  320. /**
  321. * Close sql connection
  322. * @access private
  323. */
  324. function _sql_close()
  325. {
  326. return @mssql_close($this->db_connect_id);
  327. }
  328. /**
  329. * Build db-specific report
  330. * @access private
  331. */
  332. function _sql_report($mode, $query = '')
  333. {
  334. switch ($mode)
  335. {
  336. case 'start':
  337. $html_table = false;
  338. @mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
  339. if ($result = @mssql_query($query, $this->db_connect_id))
  340. {
  341. @mssql_next_result($result);
  342. while ($row = @mssql_fetch_row($result))
  343. {
  344. $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
  345. }
  346. }
  347. @mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
  348. @mssql_free_result($result);
  349. if ($html_table)
  350. {
  351. $this->html_hold .= '</table>';
  352. }
  353. break;
  354. case 'fromcache':
  355. $endtime = explode(' ', microtime());
  356. $endtime = $endtime[0] + $endtime[1];
  357. $result = @mssql_query($query, $this->db_connect_id);
  358. while ($void = @mssql_fetch_assoc($result))
  359. {
  360. // Take the time spent on parsing rows into account
  361. }
  362. @mssql_free_result($result);
  363. $splittime = explode(' ', microtime());
  364. $splittime = $splittime[0] + $splittime[1];
  365. $this->sql_report('record_fromcache', $query, $endtime, $splittime);
  366. break;
  367. }
  368. }
  369. }
  370. ?>