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

/branches/RunPolitics_576/system/database/drivers/odbc/odbc_driver.php

https://github.com/holsinger/openfloor
PHP | 453 lines | 144 code | 65 blank | 244 comment | 12 complexity | a8d5f425dfaab9fb7b34e6ae9fae0d3e 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. * ODBC 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_odbc_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 @odbc_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 @odbc_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. // Not needed for ODBC
  61. return TRUE;
  62. }
  63. // --------------------------------------------------------------------
  64. /**
  65. * Version number query string
  66. *
  67. * @access public
  68. * @return string
  69. */
  70. function _version()
  71. {
  72. return "SELECT version() AS ver";
  73. }
  74. // --------------------------------------------------------------------
  75. /**
  76. * Execute the query
  77. *
  78. * @access private called by the base class
  79. * @param string an SQL query
  80. * @return resource
  81. */
  82. function _execute($sql)
  83. {
  84. $sql = $this->_prep_query($sql);
  85. return @odbc_exec($this->conn_id, $sql);
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * Prep the query
  90. *
  91. * If needed, each database adapter can prep the query string
  92. *
  93. * @access private called by execute()
  94. * @param string an SQL query
  95. * @return string
  96. */
  97. function _prep_query($sql)
  98. {
  99. return $sql;
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * Begin Transaction
  104. *
  105. * @access public
  106. * @return bool
  107. */
  108. function trans_begin($test_mode = FALSE)
  109. {
  110. if ( ! $this->trans_enabled)
  111. {
  112. return TRUE;
  113. }
  114. // When transactions are nested we only begin/commit/rollback the outermost ones
  115. if ($this->_trans_depth > 0)
  116. {
  117. return TRUE;
  118. }
  119. // Reset the transaction failure flag.
  120. // If the $test_mode flag is set to TRUE transactions will be rolled back
  121. // even if the queries produce a successful result.
  122. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
  123. return odbc_autocommit($this->conn_id, FALSE);
  124. }
  125. // --------------------------------------------------------------------
  126. /**
  127. * Commit Transaction
  128. *
  129. * @access public
  130. * @return bool
  131. */
  132. function trans_commit()
  133. {
  134. if ( ! $this->trans_enabled)
  135. {
  136. return TRUE;
  137. }
  138. // When transactions are nested we only begin/commit/rollback the outermost ones
  139. if ($this->_trans_depth > 0)
  140. {
  141. return TRUE;
  142. }
  143. $ret = odbc_commit($this->conn_id);
  144. odbc_autocommit($this->conn_id, TRUE);
  145. return $ret;
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * Rollback Transaction
  150. *
  151. * @access public
  152. * @return bool
  153. */
  154. function trans_rollback()
  155. {
  156. if ( ! $this->trans_enabled)
  157. {
  158. return TRUE;
  159. }
  160. // When transactions are nested we only begin/commit/rollback the outermost ones
  161. if ($this->_trans_depth > 0)
  162. {
  163. return TRUE;
  164. }
  165. $ret = odbc_rollback($this->conn_id);
  166. odbc_autocommit($this->conn_id, TRUE);
  167. return $ret;
  168. }
  169. // --------------------------------------------------------------------
  170. /**
  171. * Escape String
  172. *
  173. * @access public
  174. * @param string
  175. * @return string
  176. */
  177. function escape_str($str)
  178. {
  179. // ODBC doesn't require escaping
  180. return $str;
  181. }
  182. // --------------------------------------------------------------------
  183. /**
  184. * Affected Rows
  185. *
  186. * @access public
  187. * @return integer
  188. */
  189. function affected_rows()
  190. {
  191. return @odbc_num_rows($this->conn_id);
  192. }
  193. // --------------------------------------------------------------------
  194. /**
  195. * Insert ID
  196. *
  197. * @access public
  198. * @return integer
  199. */
  200. function insert_id()
  201. {
  202. return @odbc_insert_id($this->conn_id);
  203. }
  204. // --------------------------------------------------------------------
  205. /**
  206. * "Count All" query
  207. *
  208. * Generates a platform-specific query string that counts all records in
  209. * the specified database
  210. *
  211. * @access public
  212. * @param string
  213. * @return string
  214. */
  215. function count_all($table = '')
  216. {
  217. if ($table == '')
  218. return '0';
  219. $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
  220. if ($query->num_rows() == 0)
  221. return '0';
  222. $row = $query->row();
  223. return $row->numrows;
  224. }
  225. // --------------------------------------------------------------------
  226. /**
  227. * Show table query
  228. *
  229. * Generates a platform-specific query string so that the table names can be fetched
  230. *
  231. * @access private
  232. * @return string
  233. */
  234. function _list_tables()
  235. {
  236. return "SHOW TABLES FROM `".$this->database."`";
  237. }
  238. // --------------------------------------------------------------------
  239. /**
  240. * Show column query
  241. *
  242. * Generates a platform-specific query string so that the column names can be fetched
  243. *
  244. * @access public
  245. * @param string the table name
  246. * @return string
  247. */
  248. function _list_columns($table = '')
  249. {
  250. return "SHOW COLUMNS FROM ".$this->_escape_table($table);
  251. }
  252. // --------------------------------------------------------------------
  253. /**
  254. * Field data query
  255. *
  256. * Generates a platform-specific query so that the column data can be retrieved
  257. *
  258. * @access public
  259. * @param string the table name
  260. * @return object
  261. */
  262. function _field_data($table)
  263. {
  264. return "SELECT TOP 1 FROM ".$this->_escape_table($table);
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * The error message string
  269. *
  270. * @access private
  271. * @return string
  272. */
  273. function _error_message()
  274. {
  275. return odbc_errormsg($this->conn_id);
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * The error message number
  280. *
  281. * @access private
  282. * @return integer
  283. */
  284. function _error_number()
  285. {
  286. return odbc_error($this->conn_id);
  287. }
  288. // --------------------------------------------------------------------
  289. /**
  290. * Escape Table Name
  291. *
  292. * This function adds backticks if the table name has a period
  293. * in it. Some DBs will get cranky unless periods are escaped
  294. *
  295. * @access private
  296. * @param string the table name
  297. * @return string
  298. */
  299. function _escape_table($table)
  300. {
  301. if (stristr($table, '.'))
  302. {
  303. $table = preg_replace("/\./", "`.`", $table);
  304. }
  305. return $table;
  306. }
  307. // --------------------------------------------------------------------
  308. /**
  309. * Insert statement
  310. *
  311. * Generates a platform-specific insert string from the supplied data
  312. *
  313. * @access public
  314. * @param string the table name
  315. * @param array the insert keys
  316. * @param array the insert values
  317. * @return string
  318. */
  319. function _insert($table, $keys, $values)
  320. {
  321. return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
  322. }
  323. // --------------------------------------------------------------------
  324. /**
  325. * Update statement
  326. *
  327. * Generates a platform-specific update string from the supplied data
  328. *
  329. * @access public
  330. * @param string the table name
  331. * @param array the update data
  332. * @param array the where clause
  333. * @return string
  334. */
  335. function _update($table, $values, $where)
  336. {
  337. foreach($values as $key => $val)
  338. {
  339. $valstr[] = $key." = ".$val;
  340. }
  341. return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
  342. }
  343. // --------------------------------------------------------------------
  344. /**
  345. * Delete statement
  346. *
  347. * Generates a platform-specific delete string from the supplied data
  348. *
  349. * @access public
  350. * @param string the table name
  351. * @param array the where clause
  352. * @return string
  353. */
  354. function _delete($table, $where)
  355. {
  356. return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
  357. }
  358. // --------------------------------------------------------------------
  359. /**
  360. * Limit string
  361. *
  362. * Generates a platform-specific LIMIT clause
  363. *
  364. * @access public
  365. * @param string the sql query string
  366. * @param integer the number of rows to limit the query to
  367. * @param integer the offset value
  368. * @return string
  369. */
  370. function _limit($sql, $limit, $offset)
  371. {
  372. // Does ODBC doesn't use the LIMIT clause?
  373. return $sql;
  374. }
  375. // --------------------------------------------------------------------
  376. /**
  377. * Close DB Connection
  378. *
  379. * @access public
  380. * @param resource
  381. * @return void
  382. */
  383. function _close($conn_id)
  384. {
  385. @odbc_close($conn_id);
  386. }
  387. }
  388. ?>