PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Modifications Since 08/www/events/system/database/drivers/mssql/mssql_driver.php

https://github.com/holsinger/openfloor
PHP | 481 lines | 276 code | 43 blank | 162 comment | 7 complexity | 34cd0ae9c054f994124ddc41f88e6dfa MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * MS SQL Database Adapter Class
  18. *
  19. * Note: _DB is an extender class that the app controller
  20. * creates dynamically based on whether the active record
  21. * class is being used or not.
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Drivers
  25. * @category Database
  26. * @author Rick Ellis
  27. * @link http://www.codeigniter.com/user_guide/database/
  28. */
  29. class CI_DB_mssql_driver extends CI_DB {
  30. /**
  31. * Non-persistent database connection
  32. *
  33. * @access private called by the base class
  34. * @return resource
  35. */
  36. function db_connect()
  37. {
  38. return @mssql_connect($this->hostname, $this->username, $this->password);
  39. }
  40. // --------------------------------------------------------------------
  41. /**
  42. * Persistent database connection
  43. *
  44. * @access private called by the base class
  45. * @return resource
  46. */
  47. function db_pconnect()
  48. {
  49. return @mssql_pconnect($this->hostname, $this->username, $this->password);
  50. }
  51. // --------------------------------------------------------------------
  52. /**
  53. * Select the database
  54. *
  55. * @access private called by the base class
  56. * @return resource
  57. */
  58. function db_select()
  59. {
  60. return @mssql_select_db($this->database, $this->conn_id);
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * Execute the query
  65. *
  66. * @access private called by the base class
  67. * @param string an SQL query
  68. * @return resource
  69. */
  70. function _execute($sql)
  71. {
  72. $sql = $this->_prep_query($sql);
  73. return @mssql_query($sql, $this->conn_id);
  74. }
  75. // --------------------------------------------------------------------
  76. /**
  77. * Prep the query
  78. *
  79. * If needed, each database adapter can prep the query string
  80. *
  81. * @access private called by execute()
  82. * @param string an SQL query
  83. * @return string
  84. */
  85. function _prep_query($sql)
  86. {
  87. return $sql;
  88. }
  89. // --------------------------------------------------------------------
  90. /**
  91. * Begin Transaction
  92. *
  93. * @access public
  94. * @return bool
  95. */
  96. function trans_begin($test_mode = FALSE)
  97. {
  98. if ( ! $this->trans_enabled)
  99. {
  100. return TRUE;
  101. }
  102. // When transactions are nested we only begin/commit/rollback the outermost ones
  103. if ($this->_trans_depth > 0)
  104. {
  105. return TRUE;
  106. }
  107. // Reset the transaction failure flag.
  108. // If the $test_mode flag is set to TRUE transactions will be rolled back
  109. // even if the queries produce a successful result.
  110. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
  111. $this->simple_query('BEGIN TRAN');
  112. return TRUE;
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Commit Transaction
  117. *
  118. * @access public
  119. * @return bool
  120. */
  121. function trans_commit()
  122. {
  123. if ( ! $this->trans_enabled)
  124. {
  125. return TRUE;
  126. }
  127. // When transactions are nested we only begin/commit/rollback the outermost ones
  128. if ($this->_trans_depth > 0)
  129. {
  130. return TRUE;
  131. }
  132. $this->simple_query('COMMIT TRAN');
  133. return TRUE;
  134. }
  135. // --------------------------------------------------------------------
  136. /**
  137. * Rollback Transaction
  138. *
  139. * @access public
  140. * @return bool
  141. */
  142. function trans_rollback()
  143. {
  144. if ( ! $this->trans_enabled)
  145. {
  146. return TRUE;
  147. }
  148. // When transactions are nested we only begin/commit/rollback the outermost ones
  149. if ($this->_trans_depth > 0)
  150. {
  151. return TRUE;
  152. }
  153. $this->simple_query('ROLLBACK TRAN');
  154. return TRUE;
  155. }
  156. // --------------------------------------------------------------------
  157. /**
  158. * Escape String
  159. *
  160. * @access public
  161. * @param string
  162. * @return string
  163. */
  164. function escape_str($str)
  165. {
  166. // Escape single quotes
  167. return str_replace("'", "''", $str);
  168. }
  169. // --------------------------------------------------------------------
  170. /**
  171. * Affected Rows
  172. *
  173. * @access public
  174. * @return integer
  175. */
  176. function affected_rows()
  177. {
  178. return @mssql_rows_affected($this->conn_id);
  179. }
  180. // --------------------------------------------------------------------
  181. /**
  182. * Insert ID
  183. *
  184. * Returns the last id created in the Identity column.
  185. *
  186. * @access public
  187. * @return integer
  188. */
  189. function insert_id()
  190. {
  191. $ver = self::_parse_major_version($this->version());
  192. $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
  193. $query = $this->query($sql);
  194. $row = $query->row();
  195. return $row->last_id;
  196. }
  197. // --------------------------------------------------------------------
  198. /**
  199. * Parse major version
  200. *
  201. * Grabs the major version number from the
  202. * database server version string passed in.
  203. *
  204. * @access private
  205. * @param string $version
  206. * @return int16 major version number
  207. */
  208. function _parse_major_version($version)
  209. {
  210. preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
  211. return $ver_info[1]; // return the major version b/c that's all we're interested in.
  212. }
  213. // --------------------------------------------------------------------
  214. /**
  215. * Version number query string
  216. *
  217. * @access public
  218. * @return string
  219. */
  220. function _version()
  221. {
  222. return "SELECT @@VERSION AS ver";
  223. }
  224. // --------------------------------------------------------------------
  225. /**
  226. * "Count All" query
  227. *
  228. * Generates a platform-specific query string that counts all records in
  229. * the specified database
  230. *
  231. * @access public
  232. * @param string
  233. * @return string
  234. */
  235. function count_all($table = '')
  236. {
  237. if ($table == '')
  238. return '0';
  239. $query = $this->query("SELECT COUNT(*) AS numrows FROM ".$this->dbprefix.$table);
  240. if ($query->num_rows() == 0)
  241. return '0';
  242. $row = $query->row();
  243. return $row->numrows;
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * List table query
  248. *
  249. * Generates a platform-specific query string so that the table names can be fetched
  250. *
  251. * @access private
  252. * @return string
  253. */
  254. function _list_tables()
  255. {
  256. return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
  257. }
  258. // --------------------------------------------------------------------
  259. /**
  260. * List column query
  261. *
  262. * Generates a platform-specific query string so that the column names can be fetched
  263. *
  264. * @access private
  265. * @param string the table name
  266. * @return string
  267. */
  268. function _list_columns($table = '')
  269. {
  270. return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
  271. }
  272. // --------------------------------------------------------------------
  273. /**
  274. * Field data query
  275. *
  276. * Generates a platform-specific query so that the column data can be retrieved
  277. *
  278. * @access public
  279. * @param string the table name
  280. * @return object
  281. */
  282. function _field_data($table)
  283. {
  284. return "SELECT TOP 1 * FROM ".$this->_escape_table($table);
  285. }
  286. // --------------------------------------------------------------------
  287. /**
  288. * The error message string
  289. *
  290. * @access private
  291. * @return string
  292. */
  293. function _error_message()
  294. {
  295. // Are errros even supported in MS SQL?
  296. return '';
  297. }
  298. // --------------------------------------------------------------------
  299. /**
  300. * The error message number
  301. *
  302. * @access private
  303. * @return integer
  304. */
  305. function _error_number()
  306. {
  307. // Are error numbers supported?
  308. return '';
  309. }
  310. // --------------------------------------------------------------------
  311. /**
  312. * Escape Table Name
  313. *
  314. * This function adds backticks if the table name has a period
  315. * in it. Some DBs will get cranky unless periods are escaped
  316. *
  317. * @access private
  318. * @param string the table name
  319. * @return string
  320. */
  321. function _escape_table($table)
  322. {
  323. // I don't believe this is necessary with MS SQL. Not sure, though. - Rick
  324. /*
  325. if (stristr($table, '.'))
  326. {
  327. $table = preg_replace("/\./", "`.`", $table);
  328. }
  329. */
  330. return $table;
  331. }
  332. // --------------------------------------------------------------------
  333. /**
  334. * Insert statement
  335. *
  336. * Generates a platform-specific insert string from the supplied data
  337. *
  338. * @access public
  339. * @param string the table name
  340. * @param array the insert keys
  341. * @param array the insert values
  342. * @return string
  343. */
  344. function _insert($table, $keys, $values)
  345. {
  346. return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
  347. }
  348. // --------------------------------------------------------------------
  349. /**
  350. * Update statement
  351. *
  352. * Generates a platform-specific update string from the supplied data
  353. *
  354. * @access public
  355. * @param string the table name
  356. * @param array the update data
  357. * @param array the where clause
  358. * @return string
  359. */
  360. function _update($table, $values, $where)
  361. {
  362. foreach($values as $key => $val)
  363. {
  364. $valstr[] = $key." = ".$val;
  365. }
  366. return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
  367. }
  368. // --------------------------------------------------------------------
  369. /**
  370. * Delete statement
  371. *
  372. * Generates a platform-specific delete string from the supplied data
  373. *
  374. * @access public
  375. * @param string the table name
  376. * @param array the where clause
  377. * @return string
  378. */
  379. function _delete($table, $where)
  380. {
  381. return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
  382. }
  383. // --------------------------------------------------------------------
  384. /**
  385. * Limit string
  386. *
  387. * Generates a platform-specific LIMIT clause
  388. *
  389. * @access public
  390. * @param string the sql query string
  391. * @param integer the number of rows to limit the query to
  392. * @param integer the offset value
  393. * @return string
  394. */
  395. function _limit($sql, $limit, $offset)
  396. {
  397. $i = $limit + $offset;
  398. return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
  399. }
  400. // --------------------------------------------------------------------
  401. /**
  402. * Close DB Connection
  403. *
  404. * @access public
  405. * @param resource
  406. * @return void
  407. */
  408. function _close($conn_id)
  409. {
  410. @mssql_close($conn_id);
  411. }
  412. }
  413. ?>