admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 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, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Database Driver Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 21 | * This is the platform-independent base DB implementation class. |
| 22 | * This class will not be called directly. Rather, the adapter |
| 23 | * class for the specific database will extend and instantiate it. |
| 24 | * |
| 25 | * @package CodeIgniter |
| 26 | * @subpackage Drivers |
| 27 | * @category Database |
| 28 | * @author Rick Ellis |
| 29 | * @link http://www.codeigniter.com/user_guide/database/ |
| 30 | */ |
| 31 | class CI_DB_driver { |
| 32 | |
| 33 | var $username; |
| 34 | var $password; |
| 35 | var $hostname; |
| 36 | var $database; |
| 37 | var $dbdriver = 'mysql'; |
| 38 | var $dbprefix = ''; |
| 39 | var $port = ''; |
| 40 | var $pconnect = FALSE; |
| 41 | var $conn_id = FALSE; |
| 42 | var $result_id = FALSE; |
| 43 | var $db_debug = FALSE; |
| 44 | var $benchmark = 0; |
| 45 | var $query_count = 0; |
| 46 | var $bind_marker = '?'; |
| 47 | var $queries = array(); |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 48 | var $data_cache = array(); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 49 | var $trans_enabled = TRUE; |
| 50 | var $_trans_depth = 0; |
| 51 | var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 52 | var $cache_on = FALSE; |
| 53 | var $cachedir = ''; |
admin | c5f7fa3 | 2006-10-06 02:10:23 +0000 | [diff] [blame] | 54 | var $cache_autodel = FALSE; |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 55 | var $CACHE; // The cache class object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 56 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 57 | |
| 58 | // These are use with Oracle |
| 59 | var $stmt_id; |
| 60 | var $curs_id; |
| 61 | var $limit_used; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 62 | |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 63 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Constructor. Accepts one parameter containing the database |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 67 | * connection settings. |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 68 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 69 | * Database settings can be passed as discreet |
| 70 | * parameters or as a data source name in the first |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 71 | * parameter. DSNs must have this prototype: |
| 72 | * $dsn = 'driver://username:password@hostname/database'; |
| 73 | * |
| 74 | * @param mixed. Can be an array or a DSN string |
| 75 | */ |
| 76 | function CI_DB_driver($params) |
| 77 | { |
| 78 | $this->initialize($params); |
| 79 | log_message('debug', 'Database Driver Class Initialized'); |
| 80 | } |
| 81 | |
| 82 | // -------------------------------------------------------------------- |
| 83 | |
| 84 | /** |
| 85 | * Initialize Database Settings |
| 86 | * |
| 87 | * @access private Called by the constructor |
| 88 | * @param mixed |
| 89 | * @return void |
| 90 | */ |
| 91 | function initialize($params = '') |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 92 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 93 | if (is_array($params)) |
| 94 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 95 | $defaults = array( |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 96 | 'hostname' => '', |
| 97 | 'username' => '', |
| 98 | 'password' => '', |
| 99 | 'database' => '', |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 100 | 'conn_id' => FALSE, |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 101 | 'dbdriver' => 'mysql', |
| 102 | 'dbprefix' => '', |
| 103 | 'port' => '', |
| 104 | 'pconnect' => FALSE, |
| 105 | 'db_debug' => FALSE, |
| 106 | 'cachedir' => '', |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 107 | 'cache_on' => FALSE |
| 108 | ); |
| 109 | |
| 110 | foreach ($defaults as $key => $val) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 111 | { |
| 112 | $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; |
| 113 | } |
| 114 | } |
| 115 | elseif (strpos($params, '://')) |
| 116 | { |
| 117 | if (FALSE === ($dsn = @parse_url($params))) |
| 118 | { |
| 119 | log_message('error', 'Invalid DB Connection String'); |
| 120 | |
| 121 | if ($this->db_debug) |
| 122 | { |
| 123 | return $this->display_error('db_invalid_connection_str'); |
| 124 | } |
| 125 | return FALSE; |
| 126 | } |
| 127 | |
| 128 | $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']); |
| 129 | $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']); |
| 130 | $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); |
| 131 | $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); |
| 132 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 133 | |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 134 | // If an existing DB connection resource is supplied |
| 135 | // there is no need to connect and select the database |
| 136 | if (is_resource($this->conn_id)) |
| 137 | { |
| 138 | return TRUE; |
| 139 | } |
| 140 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 141 | // Connect to the database |
| 142 | $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 143 | |
| 144 | // No connection? Throw an error |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 145 | if ( ! $this->conn_id) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 146 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 147 | log_message('error', 'Unable to connect to the database'); |
| 148 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 149 | if ($this->db_debug) |
| 150 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 151 | $this->display_error('db_unable_to_connect'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 152 | } |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 153 | return FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 154 | } |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 155 | |
| 156 | // Select the database |
| 157 | if ( ! $this->db_select()) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 158 | { |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 159 | log_message('error', 'Unable to select database: '.$this->database); |
| 160 | |
| 161 | if ($this->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 162 | { |
admin | 25701d7 | 2006-10-24 22:34:33 +0000 | [diff] [blame^] | 163 | $this->display_error('db_unable_to_select', $this->database); |
| 164 | } |
| 165 | return FALSE; |
| 166 | } |
| 167 | |
| 168 | return TRUE; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 169 | } |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 170 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 174 | * The name of the platform in use (mysql, mssql, etc...) |
| 175 | * |
| 176 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 177 | * @return string |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 178 | */ |
| 179 | function platform() |
| 180 | { |
| 181 | return $this->dbdriver; |
| 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 187 | * Database Version Number. Returns a string containing the |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 188 | * version of the database being used |
| 189 | * |
| 190 | * @access public |
| 191 | * @return string |
| 192 | */ |
| 193 | function version() |
| 194 | { |
| 195 | if (FALSE === ($sql = $this->_version())) |
| 196 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 197 | if ($this->db_debug) |
| 198 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 199 | return $this->display_error('db_unsupported_function'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 200 | } |
| 201 | return FALSE; |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 202 | } |
| 203 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 204 | if ($this->dbdriver == 'oci8') |
| 205 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 206 | return $sql; |
| 207 | } |
| 208 | |
| 209 | $query = $this->query($sql); |
| 210 | $row = $query->row(); |
| 211 | return $row->ver; |
| 212 | } |
| 213 | |
| 214 | // -------------------------------------------------------------------- |
| 215 | |
| 216 | /** |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 217 | * Execute the query |
| 218 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 219 | * Accepts an SQL string as input and returns a result object upon |
| 220 | * successful execution of a "read" type query. Returns boolean TRUE |
| 221 | * upon successful execution of a "write" type query. Returns boolean |
| 222 | * FALSE upon failure, and if the $db_debug variable is set to TRUE |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 223 | * will raise an error. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 224 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 225 | * @access public |
| 226 | * @param string An SQL query string |
| 227 | * @param array An array of binding data |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 228 | * @return mixed |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 229 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 230 | function query($sql, $binds = FALSE, $return_object = TRUE) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 231 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 232 | if ($sql == '') |
| 233 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 234 | if ($this->db_debug) |
| 235 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 236 | log_message('error', 'Invalid query: '.$sql); |
| 237 | return $this->display_error('db_invalid_query'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 238 | } |
| 239 | return FALSE; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 240 | } |
| 241 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 242 | // Is query caching enabled? If the query is a "read type" |
| 243 | // we will load the caching class and return the previously |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 244 | // cached query if it exists |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 245 | if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 246 | { |
| 247 | if ($this->_cache_init()) |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 248 | { |
admin | 36e6442 | 2006-10-22 01:30:50 +0000 | [diff] [blame] | 249 | $this->load_rdriver(); |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 250 | if (FALSE !== ($cache = $this->CACHE->read($sql))) |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 251 | { |
| 252 | return $cache; |
| 253 | } |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 257 | // Compile binds if needed |
| 258 | if ($binds !== FALSE) |
| 259 | { |
| 260 | $sql = $this->compile_binds($sql, $binds); |
| 261 | } |
| 262 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 263 | // Save the query for debugging |
| 264 | $this->queries[] = $sql; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 265 | |
| 266 | // Start the Query Timer |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 267 | $time_start = list($sm, $ss) = explode(' ', microtime()); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 268 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 269 | // Run the Query |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 270 | if (FALSE === ($this->result_id = $this->simple_query($sql))) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 271 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 272 | // This will trigger a rollback if transactions are being used |
| 273 | $this->_trans_failure = TRUE; |
| 274 | |
| 275 | if ($this->db_debug) |
| 276 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 277 | log_message('error', 'Query error: '.$this->_error_message()); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 278 | return $this->display_error( |
| 279 | array( |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 280 | 'Error Number: '.$this->_error_number(), |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 281 | $this->_error_message(), |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 282 | $sql |
| 283 | ) |
| 284 | ); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 285 | } |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 286 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 287 | return FALSE; |
| 288 | } |
| 289 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 290 | // Stop and aggregate the query time results |
| 291 | $time_end = list($em, $es) = explode(' ', microtime()); |
| 292 | $this->benchmark += ($em + $es) - ($sm + $ss); |
| 293 | |
| 294 | // Increment the query counter |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 295 | $this->query_count++; |
| 296 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 297 | // Was the query a "write" type? |
| 298 | // If so we'll simply return true |
| 299 | if ($this->is_write_type($sql) === TRUE) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 300 | { |
| 301 | // If caching is enabled we'll auto-cleanup any |
| 302 | // existing files related to this particular URI |
admin | a658e31 | 2006-10-06 01:29:36 +0000 | [diff] [blame] | 303 | if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 304 | { |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 305 | $this->CACHE->delete(); |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 306 | } |
| 307 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 308 | return TRUE; |
| 309 | } |
| 310 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 311 | // Return TRUE if we don't need to create a result object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 312 | // Currently only the Oracle driver uses this when stored |
| 313 | // procedures are used |
| 314 | if ($return_object !== TRUE) |
| 315 | { |
| 316 | return TRUE; |
| 317 | } |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 318 | |
| 319 | // Load and instantiate the result driver |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 320 | |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 321 | $driver = $this->load_rdriver(); |
| 322 | $RES = new $driver(); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 323 | $RES->conn_id = $this->conn_id; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 324 | $RES->result_id = $this->result_id; |
| 325 | |
| 326 | if ($this->dbdriver == 'oci8') |
| 327 | { |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 328 | $RES->stmt_id = $this->stmt_id; |
| 329 | $RES->curs_id = NULL; |
| 330 | $RES->limit_used = $this->limit_used; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 331 | } |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 332 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 333 | // Is query caching enabled? If so, we'll serialize the |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 334 | // result object and save it to a cache file. |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 335 | if ($this->cache_on == TRUE AND $this->_cache_init()) |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 336 | { |
| 337 | // We'll create a new instance of the result object |
| 338 | // only without the platform specific driver since |
| 339 | // we can't use it with cached data (the query result |
| 340 | // resource ID won't be any good once we've cached the |
| 341 | // result object, so we'll have to compile the data |
| 342 | // and save it) |
| 343 | $CR = new CI_DB_result(); |
| 344 | $CR->num_rows = $RES->num_rows(); |
| 345 | $CR->result_object = $RES->result_object(); |
| 346 | $CR->result_array = $RES->result_array(); |
admin | a658e31 | 2006-10-06 01:29:36 +0000 | [diff] [blame] | 347 | |
| 348 | // Reset these since cached objects can not utilize resource IDs. |
| 349 | $CR->conn_id = NULL; |
| 350 | $CR->result_id = NULL; |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 351 | |
| 352 | $this->CACHE->write($sql, $CR); |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 353 | } |
| 354 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 355 | return $RES; |
| 356 | } |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Load the result drivers |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 362 | * |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 363 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 364 | * @return string the name of the result class |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 365 | */ |
| 366 | function load_rdriver() |
| 367 | { |
| 368 | $driver = 'CI_DB_'.$this->dbdriver.'_result'; |
| 369 | |
| 370 | if ( ! class_exists($driver)) |
| 371 | { |
| 372 | include_once(BASEPATH.'database/DB_result'.EXT); |
| 373 | include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); |
| 374 | } |
| 375 | |
| 376 | return $driver; |
| 377 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 382 | * Simple Query |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 383 | * This is a simplified version of the query() function. Internally |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 384 | * we only use it when running transaction commands since they do |
| 385 | * not require all the features of the main query() function. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 386 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 387 | * @access public |
| 388 | * @param string the sql query |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 389 | * @return mixed |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 390 | */ |
| 391 | function simple_query($sql) |
| 392 | { |
| 393 | if ( ! $this->conn_id) |
| 394 | { |
| 395 | $this->initialize(); |
| 396 | } |
| 397 | |
admin | 4003718 | 2006-10-11 19:16:58 +0000 | [diff] [blame] | 398 | return $this->_execute($sql); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // -------------------------------------------------------------------- |
| 402 | |
| 403 | /** |
| 404 | * Disable Transactions |
| 405 | * This permits transactions to be disabled at run-time. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 406 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 407 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 408 | * @return void |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 409 | */ |
| 410 | function trans_off() |
| 411 | { |
| 412 | $this->trans_enabled = FALSE; |
| 413 | } |
| 414 | |
| 415 | // -------------------------------------------------------------------- |
| 416 | |
| 417 | /** |
| 418 | * Start Transaction |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 419 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 420 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 421 | * @return void |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 422 | */ |
| 423 | function trans_start($test_mode = FALSE) |
| 424 | { |
| 425 | if ( ! $this->trans_enabled) |
| 426 | { |
| 427 | return FALSE; |
| 428 | } |
| 429 | |
| 430 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 431 | if ($this->_trans_depth > 0) |
| 432 | { |
| 433 | $this->_trans_depth += 1; |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | $this->trans_begin($test_mode); |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
| 441 | |
| 442 | /** |
| 443 | * Complete Transaction |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 444 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 445 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 446 | * @return bool |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 447 | */ |
| 448 | function trans_complete() |
| 449 | { |
| 450 | if ( ! $this->trans_enabled) |
| 451 | { |
| 452 | return FALSE; |
| 453 | } |
| 454 | |
| 455 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 456 | if ($this->_trans_depth > 1) |
| 457 | { |
| 458 | $this->_trans_depth -= 1; |
| 459 | return TRUE; |
| 460 | } |
| 461 | |
| 462 | // The query() function will set this flag to TRUE in the event that a query failed |
| 463 | if ($this->_trans_failure === TRUE) |
| 464 | { |
| 465 | $this->trans_rollback(); |
| 466 | |
| 467 | if ($this->db_debug) |
| 468 | { |
| 469 | return $this->display_error('db_transaction_failure'); |
| 470 | } |
| 471 | return FALSE; |
| 472 | } |
| 473 | |
| 474 | $this->trans_commit(); |
| 475 | return TRUE; |
| 476 | } |
| 477 | |
| 478 | // -------------------------------------------------------------------- |
| 479 | |
| 480 | /** |
| 481 | * Lets you retrieve the transaction flag to determine if it has failed |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 482 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 483 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 484 | * @return bool |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 485 | */ |
| 486 | function trans_status() |
| 487 | { |
| 488 | return $this->_trans_failure; |
| 489 | } |
| 490 | |
| 491 | // -------------------------------------------------------------------- |
| 492 | |
| 493 | /** |
| 494 | * Compile Bindings |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 495 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 496 | * @access public |
| 497 | * @param string the sql statement |
| 498 | * @param array an array of bind data |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 499 | * @return string |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 500 | */ |
| 501 | function compile_binds($sql, $binds) |
| 502 | { |
| 503 | if (FALSE === strpos($sql, $this->bind_marker)) |
| 504 | { |
| 505 | return $sql; |
| 506 | } |
| 507 | |
| 508 | if ( ! is_array($binds)) |
| 509 | { |
| 510 | $binds = array($binds); |
| 511 | } |
| 512 | |
| 513 | foreach ($binds as $val) |
| 514 | { |
| 515 | $val = $this->escape($val); |
| 516 | |
| 517 | // Just in case the replacement string contains the bind |
| 518 | // character we'll temporarily replace it with a marker |
| 519 | $val = str_replace($this->bind_marker, '{%bind_marker%}', $val); |
| 520 | $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1); |
| 521 | } |
| 522 | |
| 523 | return str_replace('{%bind_marker%}', $this->bind_marker, $sql); |
| 524 | } |
| 525 | |
| 526 | // -------------------------------------------------------------------- |
| 527 | |
| 528 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 529 | * Determines if a query is a "write" type. |
| 530 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 531 | * @access public |
| 532 | * @param string An SQL query string |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 533 | * @return boolean |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 534 | */ |
| 535 | function is_write_type($sql) |
| 536 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 537 | if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 538 | { |
| 539 | return FALSE; |
| 540 | } |
| 541 | return TRUE; |
| 542 | } |
| 543 | |
| 544 | // -------------------------------------------------------------------- |
| 545 | |
| 546 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 547 | * Calculate the aggregate query elapsed time |
| 548 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 549 | * @access public |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 550 | * @param integer The number of decimal places |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 551 | * @return integer |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 552 | */ |
| 553 | function elapsed_time($decimals = 6) |
| 554 | { |
| 555 | return number_format($this->benchmark, $decimals); |
| 556 | } |
| 557 | |
| 558 | // -------------------------------------------------------------------- |
| 559 | |
| 560 | /** |
| 561 | * Returns the total number of queries |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 562 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 563 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 564 | * @return integer |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 565 | */ |
| 566 | function total_queries() |
| 567 | { |
| 568 | return $this->query_count; |
| 569 | } |
| 570 | |
| 571 | // -------------------------------------------------------------------- |
| 572 | |
| 573 | /** |
| 574 | * Returns the last query that was executed |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 575 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 576 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 577 | * @return void |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 578 | */ |
| 579 | function last_query() |
| 580 | { |
| 581 | return end($this->queries); |
| 582 | } |
| 583 | |
| 584 | // -------------------------------------------------------------------- |
| 585 | |
| 586 | /** |
| 587 | * "Smart" Escape String |
| 588 | * |
| 589 | * Escapes data based on type |
| 590 | * Sets boolean and null types |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 591 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 592 | * @access public |
| 593 | * @param string |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 594 | * @return integer |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 595 | */ |
| 596 | function escape($str) |
| 597 | { |
| 598 | switch (gettype($str)) |
| 599 | { |
| 600 | case 'string' : $str = "'".$this->escape_str($str)."'"; |
| 601 | break; |
| 602 | case 'boolean' : $str = ($str === FALSE) ? 0 : 1; |
| 603 | break; |
| 604 | default : $str = ($str === NULL) ? 'NULL' : $str; |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | return $str; |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 609 | } |
| 610 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 611 | // -------------------------------------------------------------------- |
| 612 | |
| 613 | /** |
| 614 | * Primary |
| 615 | * |
| 616 | * Retrieves the primary key. It assumes that the row in the first |
| 617 | * position is the primary key |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 618 | * |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 619 | * @access public |
| 620 | * @param string the table name |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 621 | * @return string |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 622 | */ |
| 623 | function primary($table = '') |
| 624 | { |
admin | 606f99c | 2006-10-11 23:48:41 +0000 | [diff] [blame] | 625 | $fields = $this->list_fields($table); |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 626 | |
| 627 | if ( ! is_array($fields)) |
| 628 | { |
| 629 | return FALSE; |
| 630 | } |
| 631 | |
| 632 | return current($fields); |
| 633 | } |
| 634 | |
| 635 | // -------------------------------------------------------------------- |
| 636 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 637 | /** |
| 638 | * Returns an array of table names |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 639 | * |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 640 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 641 | * @return array |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 642 | */ |
| 643 | function list_tables() |
| 644 | { |
| 645 | // Is there a cached result? |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 646 | if (isset($this->data_cache['table_names'])) |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 647 | { |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 648 | return $this->data_cache['table_names']; |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | if (FALSE === ($sql = $this->_list_tables())) |
| 652 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 653 | if ($this->db_debug) |
| 654 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 655 | return $this->display_error('db_unsupported_function'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 656 | } |
| 657 | return FALSE; |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | $retval = array(); |
| 661 | $query = $this->query($sql); |
| 662 | |
| 663 | if ($query->num_rows() > 0) |
| 664 | { |
| 665 | foreach($query->result_array() as $row) |
| 666 | { |
| 667 | if (isset($row['TABLE_NAME'])) |
| 668 | { |
| 669 | $retval[] = $row['TABLE_NAME']; |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | $retval[] = array_shift($row); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 678 | $this->data_cache['table_names'] = $retval; |
| 679 | return $this->data_cache['table_names']; |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // -------------------------------------------------------------------- |
| 683 | |
| 684 | /** |
| 685 | * Determine if a particular table exists |
| 686 | * @access public |
| 687 | * @return boolean |
| 688 | */ |
| 689 | function table_exists($table_name) |
| 690 | { |
| 691 | return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; |
| 692 | } |
| 693 | |
| 694 | // -------------------------------------------------------------------- |
| 695 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 696 | /** |
| 697 | * Fetch MySQL Field Names |
| 698 | * |
| 699 | * @access public |
| 700 | * @param string the table name |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 701 | * @return array |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 702 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 703 | function list_fields($table = '') |
| 704 | { |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 705 | // Is there a cached result? |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 706 | if (isset($this->data_cache['field_names'][$table])) |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 707 | { |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 708 | return $this->data_cache['field_names'][$table]; |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 709 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 710 | |
| 711 | if ($table == '') |
| 712 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 713 | if ($this->db_debug) |
| 714 | { |
| 715 | return $this->display_error('db_field_param_missing'); |
| 716 | } |
| 717 | return FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 718 | } |
| 719 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 720 | if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) |
| 721 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 722 | if ($this->db_debug) |
| 723 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 724 | return $this->display_error('db_unsupported_function'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 725 | } |
| 726 | return FALSE; |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 727 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 728 | |
| 729 | $query = $this->query($sql); |
| 730 | |
| 731 | $retval = array(); |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 732 | foreach($query->result_array() as $row) |
| 733 | { |
| 734 | if (isset($row['COLUMN_NAME'])) |
| 735 | { |
| 736 | $retval[] = $row['COLUMN_NAME']; |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | $retval[] = current($row); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 741 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 742 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 743 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 744 | $this->data_cache['field_names'][$table] = $retval; |
| 745 | return $this->data_cache['field_names'][$table]; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 746 | } |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 747 | |
| 748 | // -------------------------------------------------------------------- |
| 749 | |
| 750 | /** |
| 751 | * Determine if a particular field exists |
| 752 | * @access public |
| 753 | * @param string |
| 754 | * @param string |
| 755 | * @return boolean |
| 756 | */ |
| 757 | function field_exists($field_name, $table_name) |
| 758 | { |
| 759 | return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; |
| 760 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 761 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 762 | // -------------------------------------------------------------------- |
| 763 | |
| 764 | /** |
| 765 | * DEPRECATED - use list_fields() |
| 766 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 767 | function field_names($table = '') |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 768 | { |
| 769 | return $this->list_fields($table); |
| 770 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 771 | |
| 772 | // -------------------------------------------------------------------- |
| 773 | |
| 774 | /** |
| 775 | * Returns an object with field data |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 776 | * |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 777 | * @access public |
| 778 | * @param string the table name |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 779 | * @return object |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 780 | */ |
| 781 | function field_data($table = '') |
| 782 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 783 | if ($table == '') |
| 784 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 785 | if ($this->db_debug) |
| 786 | { |
| 787 | return $this->display_error('db_field_param_missing'); |
| 788 | } |
| 789 | return FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 790 | } |
| 791 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 792 | $query = $this->query($this->_field_data($this->dbprefix.$table)); |
| 793 | return $query->field_data(); |
| 794 | } |
| 795 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 796 | // -------------------------------------------------------------------- |
| 797 | |
| 798 | /** |
| 799 | * Generate an insert string |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 800 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 801 | * @access public |
| 802 | * @param string the table upon which the query will be performed |
| 803 | * @param array an associative array data of key/values |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 804 | * @return string |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 805 | */ |
| 806 | function insert_string($table, $data) |
| 807 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 808 | $fields = array(); |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 809 | $values = array(); |
| 810 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 811 | foreach($data as $key => $val) |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 812 | { |
| 813 | $fields[] = $key; |
| 814 | $values[] = $this->escape($val); |
| 815 | } |
| 816 | |
| 817 | return $this->_insert($this->dbprefix.$table, $fields, $values); |
| 818 | } |
| 819 | |
| 820 | // -------------------------------------------------------------------- |
| 821 | |
| 822 | /** |
| 823 | * Generate an update string |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 824 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 825 | * @access public |
| 826 | * @param string the table upon which the query will be performed |
| 827 | * @param array an associative array data of key/values |
| 828 | * @param mixed the "where" statement |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 829 | * @return string |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 830 | */ |
| 831 | function update_string($table, $data, $where) |
| 832 | { |
| 833 | if ($where == '') |
| 834 | return false; |
| 835 | |
| 836 | $fields = array(); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 837 | foreach($data as $key => $val) |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 838 | { |
| 839 | $fields[$key] = $this->escape($val); |
| 840 | } |
| 841 | |
| 842 | if ( ! is_array($where)) |
| 843 | { |
| 844 | $dest = array($where); |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | $dest = array(); |
| 849 | foreach ($where as $key => $val) |
| 850 | { |
| 851 | $prefix = (count($dest) == 0) ? '' : ' AND '; |
| 852 | |
| 853 | if ($val != '') |
| 854 | { |
| 855 | if ( ! $this->_has_operator($key)) |
| 856 | { |
| 857 | $key .= ' ='; |
| 858 | } |
| 859 | |
| 860 | $val = ' '.$this->escape($val); |
| 861 | } |
| 862 | |
| 863 | $dest[] = $prefix.$key.$val; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | return $this->_update($this->dbprefix.$table, $fields, $dest); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 868 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 869 | |
| 870 | // -------------------------------------------------------------------- |
| 871 | |
| 872 | /** |
| 873 | * Enables a native PHP function to be run, using a platform agnostic wrapper. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 874 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 875 | * @access public |
| 876 | * @param string the function name |
| 877 | * @param mixed any parameters needed by the function |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 878 | * @return mixed |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 879 | */ |
| 880 | function call_function($function) |
| 881 | { |
| 882 | $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; |
| 883 | |
| 884 | if (FALSE === strpos($driver, $function)) |
| 885 | { |
| 886 | $function = $driver.$function; |
| 887 | } |
| 888 | |
| 889 | if ( ! function_exists($function)) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 890 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 891 | if ($this->db_debug) |
| 892 | { |
| 893 | return $this->display_error('db_unsupported_function'); |
| 894 | } |
| 895 | return FALSE; |
| 896 | } |
| 897 | else |
| 898 | { |
admin | ee54c11 | 2006-09-28 17:13:38 +0000 | [diff] [blame] | 899 | $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; |
| 900 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 901 | return call_user_func_array($function, $args); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
admin | c5f7fa3 | 2006-10-06 02:10:23 +0000 | [diff] [blame] | 904 | |
| 905 | // -------------------------------------------------------------------- |
| 906 | |
| 907 | /** |
| 908 | * Set Cache Directory Path |
| 909 | * |
| 910 | * @access public |
| 911 | * @param string the path to the cache directory |
| 912 | * @return void |
| 913 | */ |
| 914 | function cache_set_path($path = '') |
| 915 | { |
| 916 | $this->cachedir = $path; |
| 917 | } |
| 918 | |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 919 | // -------------------------------------------------------------------- |
| 920 | |
| 921 | /** |
| 922 | * Enable Query Caching |
| 923 | * |
| 924 | * @access public |
| 925 | * @return void |
| 926 | */ |
| 927 | function cache_on() |
| 928 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 929 | $this->cache_on = TRUE; |
| 930 | return TRUE; |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | // -------------------------------------------------------------------- |
| 934 | |
| 935 | /** |
| 936 | * Disable Query Caching |
| 937 | * |
| 938 | * @access public |
| 939 | * @return void |
| 940 | */ |
| 941 | function cache_off() |
| 942 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 943 | $this->cache_on = FALSE; |
| 944 | return FALSE; |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 945 | } |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 946 | |
admin | c5f7fa3 | 2006-10-06 02:10:23 +0000 | [diff] [blame] | 947 | |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 948 | // -------------------------------------------------------------------- |
| 949 | |
| 950 | /** |
admin | c5f7fa3 | 2006-10-06 02:10:23 +0000 | [diff] [blame] | 951 | * Delete the cache files associated with a particular URI |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 952 | * |
| 953 | * @access public |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 954 | * @return void |
| 955 | */ |
admin | 1b0ab46 | 2006-10-10 23:30:21 +0000 | [diff] [blame] | 956 | function cache_delete($segment_one = '', $segment_two = '') |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 957 | { |
admin | c5f7fa3 | 2006-10-06 02:10:23 +0000 | [diff] [blame] | 958 | if ( ! $this->_cache_init()) |
| 959 | { |
| 960 | return FALSE; |
| 961 | } |
admin | 1b0ab46 | 2006-10-10 23:30:21 +0000 | [diff] [blame] | 962 | return $this->CACHE->delete($segment_one, $segment_two); |
admin | c5f7fa3 | 2006-10-06 02:10:23 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | // -------------------------------------------------------------------- |
| 966 | |
| 967 | /** |
| 968 | * Delete All cache files |
| 969 | * |
| 970 | * @access public |
| 971 | * @return void |
| 972 | */ |
| 973 | function cache_delete_all() |
| 974 | { |
| 975 | if ( ! $this->_cache_init()) |
| 976 | { |
| 977 | return FALSE; |
| 978 | } |
| 979 | |
| 980 | return $this->CACHE->delete_all(); |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 981 | } |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 982 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 983 | // -------------------------------------------------------------------- |
| 984 | |
| 985 | /** |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 986 | * Initialize the Cache Class |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 987 | * |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 988 | * @access private |
| 989 | * @return void |
| 990 | */ |
| 991 | function _cache_init() |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 992 | { |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 993 | if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 994 | { |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 995 | return TRUE; |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 996 | } |
| 997 | |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 998 | if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT)) |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 999 | { |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame] | 1000 | return $this->cache_off(); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
admin | c2e3cd7 | 2006-10-05 04:22:30 +0000 | [diff] [blame] | 1003 | $this->CACHE = new CI_DB_Cache; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 1004 | return TRUE; |
| 1005 | } |
| 1006 | |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 1007 | |
| 1008 | // -------------------------------------------------------------------- |
| 1009 | |
| 1010 | /** |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1011 | * Close DB Connection |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1012 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1013 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1014 | * @return void |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1015 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 1016 | function close() |
| 1017 | { |
| 1018 | if (is_resource($this->conn_id)) |
| 1019 | { |
| 1020 | $this->_close($this->conn_id); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1021 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1022 | $this->conn_id = FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 1023 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1024 | |
| 1025 | // -------------------------------------------------------------------- |
| 1026 | |
| 1027 | /** |
| 1028 | * Display an error message |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1029 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1030 | * @access public |
| 1031 | * @param string the error message |
| 1032 | * @param string any "swap" values |
| 1033 | * @param boolean whether to localize the message |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1034 | * @return string sends the application/error_db.php template |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1035 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1036 | function display_error($error = '', $swap = '', $native = FALSE) |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 1037 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1038 | $LANG = new CI_Language(); |
| 1039 | $LANG->load('db'); |
| 1040 | |
| 1041 | $heading = 'MySQL Error'; |
| 1042 | |
| 1043 | if ($native == TRUE) |
| 1044 | { |
| 1045 | $message = $error; |
| 1046 | } |
| 1047 | else |
| 1048 | { |
| 1049 | $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; |
| 1050 | } |
| 1051 | |
| 1052 | if ( ! class_exists('CI_Exceptions')) |
| 1053 | { |
| 1054 | include_once(BASEPATH.'libraries/Exceptions'.EXT); |
| 1055 | } |
| 1056 | |
| 1057 | $error = new CI_Exceptions(); |
| 1058 | echo $error->show_error('An Error Was Encountered', $message, 'error_db'); |
| 1059 | exit; |
| 1060 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 1061 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1062 | |
| 1063 | } |
| 1064 | |
| 1065 | ?> |