Skip to content
echiong edited this page Oct 25, 2012 · 63 revisions

##Class CI_DB_driver

Database Driver Class

This is the platform-independent base DB implementation class. This class will not be called directly. Rather, the adapter class for the specific database will extend and instantiate it.

	###Direct known subclasses
		<a href="Class CI_DB_active_record.md">CI_DB_active_recordggg</a>

Package: [[CodeIgniter<a href="Package CodeIgniter.Drivers.md">Drivers]]
Category: Database
Author: ExpressionEngine Dev Team
Link: http://codeigniter.com/user_guide/database/
Located at system/database/DB_driver.php

##Methods summary


public __construct ( array $params )

Constructor. Accepts one parameter containing the database connection settings.

Parameters

$params array


public initialize ( )

Initialize Database Settings


public resource db_set_charset ( string $charset, string $collation )

Set client character set

Parameters

$charset string

$collation string

Returns resource


public string platform ( )

The name of the platform in use (mysql, mssql, etc...)

Returns string


public string version ( )

Database Version Number. Returns a string containing the version of the database being used

Returns string


public mixed query ( string $sql, array $binds = FALSE, mixed $return_object = TRUE )

Execute the query

Accepts an SQL string as input and returns a result object upon successful execution of a "read" type query. Returns boolean TRUE upon successful execution of a "write" type query. Returns boolean FALSE upon failure, and if the $db_debug variable is set to TRUE will raise an error.

Parameters

$sql string
An SQL query string

$binds array
An array of binding data

$return_object

Returns mixed


public string load_rdriver ( )

Load the result drivers

Returns string
the name of the result class


public mixed simple_query ( string $sql )

Simple Query This is a simplified version of the query() function. Internally we only use it when running transaction commands since they do not require all the features of the main query() function.

Parameters

$sql string
the sql query

Returns mixed


public trans_off ( )

Disable Transactions This permits transactions to be disabled at run-time.


public trans_strict ( mixed $mode = TRUE )

Enable/disable Transaction Strict Mode When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all groups will be rolled back. If strict mode is disabled, each group is treated autonomously, meaning a failure of one group will not affect any others


public trans_start ( mixed $test_mode = FALSE )

Start Transaction


public boolean trans_complete ( )

Complete Transaction

Returns boolean


public boolean trans_status ( )

Lets you retrieve the transaction flag to determine if it has failed

Returns boolean


public string compile_binds ( string $sql, array $binds )

Compile Bindings

Parameters

$sql string
the sql statement

$binds array
an array of bind data

Returns string


public boolean is_write_type ( string $sql )

Determines if a query is a "write" type.

Parameters

$sql string
An SQL query string

Returns boolean


public integer elapsed_time ( integer $decimals = 6 )

Calculate the aggregate query elapsed time

Parameters

$decimals integer
The number of decimal places

Returns integer


public integer total_queries ( )

Returns the total number of queries

Returns integer


public last_query ( )

Returns the last query that was executed


public mixed escape ( string $str )

"Smart" Escape String

Escapes data based on type Sets boolean and null types

Parameters

$str string

Returns mixed


public mixed escape_like_str ( string $str )

Escape LIKE String

Calls the individual driver for platform specific escaping for LIKE conditions

Parameters

$str string

Returns mixed


public string primary ( string $table = '' )

Primary

Retrieves the primary key. It assumes that the row in the first position is the primary key

Parameters

$table string
the table name

Returns string


public array list_tables ( mixed $constrain_by_prefix = FALSE )

Returns an array of table names

Returns array


public boolean table_exists ( mixed $table_name )

Determine if a particular table exists

Returns boolean


public array list_fields ( string $table = '' )

Fetch MySQL Field Names

Parameters

$table string
the table name

Returns array


public boolean field_exists ( string $field_name, string $table_name )

Determine if a particular field exists

Parameters

$field_name string

$table_name string

Returns boolean


public object field_data ( string $table = '' )

Returns an object with field data

Parameters

$table string
the table name

Returns object


public string insert_string ( string $table, array $data )

Generate an insert string

Parameters

$table string
the table upon which the query will be performed

$data array
an associative array data of key/values

Returns string


public string update_string ( string $table, array $data, mixed $where )

Generate an update string

Parameters

$table string
the table upon which the query will be performed

$data array
an associative array data of key/values

$where mixed
the "where" statement

Returns string


public boolean _has_operator ( string $str )

Tests whether the string has an SQL operator

Parameters

$str string

Returns boolean


public mixed call_function ( string $function )

Enables a native PHP function to be run, using a platform agnostic wrapper.

Parameters

$function string
the function name

Returns mixed


public cache_set_path ( string $path = '' )

Set Cache Directory Path

Parameters

$path string
the path to the cache directory


public cache_on ( )

Enable Query Caching


public cache_off ( )

Disable Query Caching


public cache_delete ( mixed $segment_one = '', mixed $segment_two = '' )

Delete the cache files associated with a particular URI


public cache_delete_all ( )

Delete All cache files


public _cache_init ( )

Initialize the Cache Class


public close ( )

Close DB Connection


public string display_error ( string $error = '', string $swap = '', boolean $native = FALSE )

Display an error message

Parameters

$error string
the error message

$swap string
any "swap" values

$native boolean
whether to localize the message

Returns string
sends the application/error_db.php template


public mixed protect_identifiers ( mixed $item, mixed $prefix_single = FALSE )

Protect Identifiers

This function adds backticks if appropriate based on db type

Parameters

$item mixed
the item to escape

$prefix_single

Returns mixed
the item with backticks


public string _protect_identifiers ( string $item, boolean $prefix_single = FALSE, mixed $protect_identifiers = NULL, boolean $field_exists = TRUE )

Protect Identifiers

This function is used extensively by the Active Record class, and by a couple functions in this class. It takes a column or table name (optionally with an alias) and inserts the table prefix onto it. Some logic is necessary in order to deal with column names that include the path. Consider a query like this:

SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table

Or a query with aliasing:

SELECT m.member_id, m.member_name FROM members AS m

Since the column name can include up to four segments (host, DB, table, column) or also have an alias prefix, we need to do a bit of work to figure this out and insert the table prefix (if it exists) in the proper position, and escape only the correct identifiers.

Parameters

$item string

$prefix_single boolean

$protect_identifiers mixed

$field_exists boolean

Returns string

<table class="summary" id="properties">
<caption>Properties summary</caption>
<tr data-order="username" id="$username">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$username

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="password" id="$password">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$password

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="hostname" id="$hostname">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$hostname

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="database" id="$database">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$database

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="dbdriver" id="$dbdriver">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$dbdriver

'mysql'
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="dbprefix" id="$dbprefix">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$dbprefix

''
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="char_set" id="$char_set">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$char_set

'utf8'
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="dbcollat" id="$dbcollat">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$dbcollat

'utf8_general_ci'
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="autoinit" id="$autoinit">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$autoinit

TRUE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="swap_pre" id="$swap_pre">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$swap_pre

''
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="port" id="$port">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$port

''
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="pconnect" id="$pconnect">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$pconnect

FALSE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="conn_id" id="$conn_id">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$conn_id

FALSE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="result_id" id="$result_id">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$result_id

FALSE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="db_debug" id="$db_debug">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$db_debug

FALSE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="benchmark" id="$benchmark">
	<td class="attributes"><code>
		public  
		integer
	</code></td>

	<td class="name">

$benchmark

0
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="query_count" id="$query_count">
	<td class="attributes"><code>
		public  
		integer
	</code></td>

	<td class="name">

$query_count

0
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="bind_marker" id="$bind_marker">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$bind_marker

'?'
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="save_queries" id="$save_queries">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$save_queries

TRUE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="queries" id="$queries">
	<td class="attributes"><code>
		public  
		array
	</code></td>

	<td class="name">

$queries

array()
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="query_times" id="$query_times">
	<td class="attributes"><code>
		public  
		array
	</code></td>

	<td class="name">

$query_times

array()
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="data_cache" id="$data_cache">
	<td class="attributes"><code>
		public  
		array
	</code></td>

	<td class="name">

$data_cache

array()
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="trans_enabled" id="$trans_enabled">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$trans_enabled

TRUE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="trans_strict" id="$trans_strict">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$trans_strict

TRUE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="_trans_depth" id="$_trans_depth">
	<td class="attributes"><code>
		public  
		integer
	</code></td>

	<td class="name">

$_trans_depth

0
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="_trans_status" id="$_trans_status">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$_trans_status

TRUE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="cache_on" id="$cache_on">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$cache_on

FALSE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="cachedir" id="$cachedir">
	<td class="attributes"><code>
		public  
		string
	</code></td>

	<td class="name">

$cachedir

''
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="cache_autodel" id="$cache_autodel">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$cache_autodel

FALSE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="CACHE" id="$CACHE">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$CACHE

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="_protect_identifiers" id="$_protect_identifiers">
	<td class="attributes"><code>
		public  
		boolean
	</code></td>

	<td class="name">

$_protect_identifiers

TRUE
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="_reserved_identifiers" id="$_reserved_identifiers">
	<td class="attributes"><code>
		public  
		array
	</code></td>

	<td class="name">

$_reserved_identifiers

array('*')
#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="stmt_id" id="$stmt_id">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$stmt_id

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="curs_id" id="$curs_id">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$curs_id

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
<tr data-order="limit_used" id="$limit_used">
	<td class="attributes"><code>
		public  
		mixed
	</code></td>

	<td class="name">

$limit_used

#
		<div class="description detailed">
			

		</div>
	</div></td>
</tr>
</table>

Packages

Classes

Exceptions

Functions

Clone this wiki locally