\CI_DB_active_record

Active Record Class

This is the platform-independent base Active Record implementation class.

Summary

Methods
Properties
Constants
__construct()
initialize()
db_set_charset()
platform()
version()
query()
load_rdriver()
simple_query()
trans_off()
trans_strict()
trans_start()
trans_complete()
trans_status()
compile_binds()
is_write_type()
elapsed_time()
total_queries()
last_query()
escape()
escape_like_str()
primary()
list_tables()
table_exists()
list_fields()
field_exists()
field_data()
insert_string()
update_string()
_has_operator()
call_function()
cache_set_path()
cache_on()
cache_off()
cache_delete()
cache_delete_all()
_cache_init()
close()
display_error()
protect_identifiers()
_protect_identifiers()
select()
select_max()
select_min()
select_avg()
select_sum()
distinct()
from()
join()
where()
or_where()
where_in()
or_where_in()
where_not_in()
or_where_not_in()
like()
not_like()
or_like()
or_not_like()
group_by()
having()
or_having()
order_by()
limit()
offset()
set()
get()
count_all_results()
get_where()
insert_batch()
set_insert_batch()
insert()
replace()
update()
update_batch()
set_update_batch()
empty_table()
truncate()
delete()
dbprefix()
set_dbprefix()
_object_to_array()
_object_to_array_batch()
start_cache()
stop_cache()
flush_cache()
$username
$password
$hostname
$database
$dbdriver
$dbprefix
$char_set
$dbcollat
$autoinit
$swap_pre
$port
$pconnect
$conn_id
$result_id
$db_debug
$benchmark
$query_count
$bind_marker
$save_queries
$queries
$query_times
$data_cache
$trans_enabled
$trans_strict
$_trans_depth
$_trans_status
$cache_on
$cachedir
$cache_autodel
$CACHE
$_protect_identifiers
$_reserved_identifiers
$stmt_id
$curs_id
$limit_used
$ar_select
$ar_distinct
$ar_from
$ar_join
$ar_where
$ar_like
$ar_groupby
$ar_having
$ar_keys
$ar_limit
$ar_offset
$ar_order
$ar_orderby
$ar_set
$ar_wherein
$ar_aliased_tables
$ar_store_array
$ar_caching
$ar_cache_exists
$ar_cache_select
$ar_cache_from
$ar_cache_join
$ar_cache_where
$ar_cache_like
$ar_cache_groupby
$ar_cache_having
$ar_cache_orderby
$ar_cache_set
$ar_no_escape
$ar_cache_no_escape
No constants found
_reset_select()
_max_min_avg_sum()
_create_alias_from_table()
_where()
_where_in()
_like()
_having()
_track_aliases()
_compile_select()
_merge_cache()
_reset_run()
_reset_write()
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$username

$username : 

Type

$password

$password : 

Type

$hostname

$hostname : 

Type

$database

$database : 

Type

$dbdriver

$dbdriver : 

Type

$dbprefix

$dbprefix : 

Type

$char_set

$char_set : 

Type

$dbcollat

$dbcollat : 

Type

$autoinit

$autoinit : 

Type

$swap_pre

$swap_pre : 

Type

$port

$port : 

Type

$pconnect

$pconnect : 

Type

$conn_id

$conn_id : 

Type

$result_id

$result_id : 

Type

$db_debug

$db_debug : 

Type

$benchmark

$benchmark : 

Type

$query_count

$query_count : 

Type

$bind_marker

$bind_marker : 

Type

$save_queries

$save_queries : 

Type

$queries

$queries : 

Type

$query_times

$query_times : 

Type

$data_cache

$data_cache : 

Type

$trans_enabled

$trans_enabled : 

Type

$trans_strict

$trans_strict : 

Type

$_trans_depth

$_trans_depth : 

Type

$_trans_status

$_trans_status : 

Type

$cache_on

$cache_on : 

Type

$cachedir

$cachedir : 

Type

$cache_autodel

$cache_autodel : 

Type

$CACHE

$CACHE : 

Type

$_protect_identifiers

$_protect_identifiers : 

Type

$_reserved_identifiers

$_reserved_identifiers : 

Type

$stmt_id

$stmt_id : 

Type

$curs_id

$curs_id : 

Type

$limit_used

$limit_used : 

Type

$ar_select

$ar_select : 

Type

$ar_distinct

$ar_distinct : 

Type

$ar_from

$ar_from : 

Type

$ar_join

$ar_join : 

Type

$ar_where

$ar_where : 

Type

$ar_like

$ar_like : 

Type

$ar_groupby

$ar_groupby : 

Type

$ar_having

$ar_having : 

Type

$ar_keys

$ar_keys : 

Type

$ar_limit

$ar_limit : 

Type

$ar_offset

$ar_offset : 

Type

$ar_order

$ar_order : 

Type

$ar_orderby

$ar_orderby : 

Type

$ar_set

$ar_set : 

Type

$ar_wherein

$ar_wherein : 

Type

$ar_aliased_tables

$ar_aliased_tables : 

Type

$ar_store_array

$ar_store_array : 

Type

$ar_caching

$ar_caching : 

Type

$ar_cache_exists

$ar_cache_exists : 

Type

$ar_cache_select

$ar_cache_select : 

Type

$ar_cache_from

$ar_cache_from : 

Type

$ar_cache_join

$ar_cache_join : 

Type

$ar_cache_where

$ar_cache_where : 

Type

$ar_cache_like

$ar_cache_like : 

Type

$ar_cache_groupby

$ar_cache_groupby : 

Type

$ar_cache_having

$ar_cache_having : 

Type

$ar_cache_orderby

$ar_cache_orderby : 

Type

$ar_cache_set

$ar_cache_set : 

Type

$ar_no_escape

$ar_no_escape : 

Type

$ar_cache_no_escape

$ar_cache_no_escape : 

Type

Methods

__construct()

__construct(  $params) 

Constructor. Accepts one parameter containing the database connection settings.

Parameters

$params

initialize()

initialize() : void

Initialize Database Settings

db_set_charset()

db_set_charset(  $charset,   $collation) : resource

Set client character set

Parameters

$charset
$collation

Returns

resource

platform()

platform() : string

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

..)

Returns

string

version()

version() : string

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

Returns

string

query()

query(  $sql,   $binds = FALSE,   $return_object = TRUE) : mixed

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
$binds
$return_object

Returns

mixed

load_rdriver()

load_rdriver() : string

Load the result drivers

Returns

string —

the name of the result class

simple_query()

simple_query(  $sql) : mixed

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

Returns

mixed

trans_off()

trans_off() : void

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

trans_strict()

trans_strict(  $mode = TRUE) : void

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

Parameters

$mode

trans_start()

trans_start(  $test_mode = FALSE) : void

Start Transaction

Parameters

$test_mode

trans_complete()

trans_complete() : boolean

Complete Transaction

Returns

boolean

trans_status()

trans_status() : boolean

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

Returns

boolean

compile_binds()

compile_binds(  $sql,   $binds) : string

Compile Bindings

Parameters

$sql
$binds

Returns

string

is_write_type()

is_write_type(  $sql) : boolean

Determines if a query is a "write" type.

Parameters

$sql

Returns

boolean

elapsed_time()

elapsed_time(  $decimals = 6) : integer

Calculate the aggregate query elapsed time

Parameters

$decimals

Returns

integer

total_queries()

total_queries() : integer

Returns the total number of queries

Returns

integer

last_query()

last_query() : void

Returns the last query that was executed

escape()

escape(  $str) : mixed

"Smart" Escape String

Escapes data based on type Sets boolean and null types

Parameters

$str

Returns

mixed

escape_like_str()

escape_like_str(  $str) : mixed

Escape LIKE String

Calls the individual driver for platform specific escaping for LIKE conditions

Parameters

$str

Returns

mixed

primary()

primary(  $table = '') : string

Primary

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

Parameters

$table

Returns

string

list_tables()

list_tables(  $constrain_by_prefix = FALSE) : array

Returns an array of table names

Parameters

$constrain_by_prefix

Returns

array

table_exists()

table_exists(  $table_name) : boolean

Determine if a particular table exists

Parameters

$table_name

Returns

boolean

list_fields()

list_fields(  $table = '') : array

Fetch MySQL Field Names

Parameters

$table

Returns

array

field_exists()

field_exists(  $field_name,   $table_name) : boolean

Determine if a particular field exists

Parameters

$field_name
$table_name

Returns

boolean

field_data()

field_data(  $table = '') : object

Returns an object with field data

Parameters

$table

Returns

object

insert_string()

insert_string(  $table,   $data) : string

Generate an insert string

Parameters

$table
$data

Returns

string

update_string()

update_string(  $table,   $data,   $where) : string

Generate an update string

Parameters

$table
$data
$where

Returns

string

_has_operator()

_has_operator(  $str) : boolean

Tests whether the string has an SQL operator

Parameters

$str

Returns

boolean

call_function()

call_function(  $function) : mixed

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

Parameters

$function

Returns

mixed

cache_set_path()

cache_set_path(  $path = '') : void

Set Cache Directory Path

Parameters

$path

cache_on()

cache_on() : void

Enable Query Caching

cache_off()

cache_off() : void

Disable Query Caching

cache_delete()

cache_delete(  $segment_one = '',   $segment_two = '') : void

Delete the cache files associated with a particular URI

Parameters

$segment_one
$segment_two

cache_delete_all()

cache_delete_all() : void

Delete All cache files

_cache_init()

_cache_init() : void

Initialize the Cache Class

close()

close() : void

Close DB Connection

display_error()

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

Display an error message

Parameters

$error
$swap
$native

Returns

string —

sends the application/error_db.php template

protect_identifiers()

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

Protect Identifiers

This function adds backticks if appropriate based on db type

Parameters

$item
$prefix_single

Returns

mixed —

the item with backticks

_protect_identifiers()

_protect_identifiers(  $item,   $prefix_single = FALSE,   $protect_identifiers = NULL,   $field_exists = TRUE) : string

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
$prefix_single
$protect_identifiers
$field_exists

Returns

string

select()

select(  $select = '*',   $escape = NULL) : object

Select

Generates the SELECT portion of the query

Parameters

$select
$escape

Returns

object

select_max()

select_max(  $select = '',   $alias = '') : object

Select Max

Generates a SELECT MAX(field) portion of a query

Parameters

$select
$alias

Returns

object

select_min()

select_min(  $select = '',   $alias = '') : object

Select Min

Generates a SELECT MIN(field) portion of a query

Parameters

$select
$alias

Returns

object

select_avg()

select_avg(  $select = '',   $alias = '') : object

Select Average

Generates a SELECT AVG(field) portion of a query

Parameters

$select
$alias

Returns

object

select_sum()

select_sum(  $select = '',   $alias = '') : object

Select Sum

Generates a SELECT SUM(field) portion of a query

Parameters

$select
$alias

Returns

object

distinct()

distinct(  $val = TRUE) : object

DISTINCT

Sets a flag which tells the query string compiler to add DISTINCT

Parameters

$val

Returns

object

from()

from(  $from) : object

From

Generates the FROM portion of the query

Parameters

$from

Returns

object

join()

join(  $table,   $cond,   $type = '') : object

Join

Generates the JOIN portion of the query

Parameters

$table
$cond
$type

Returns

object

where()

where(  $key,   $value = NULL,   $escape = TRUE) : object

Where

Generates the WHERE portion of the query. Separates multiple calls with AND

Parameters

$key
$value
$escape

Returns

object

or_where()

or_where(  $key,   $value = NULL,   $escape = TRUE) : object

OR Where

Generates the WHERE portion of the query. Separates multiple calls with OR

Parameters

$key
$value
$escape

Returns

object

where_in()

where_in(  $key = NULL,   $values = NULL) : object

Where_in

Generates a WHERE field IN ('item', 'item') SQL query joined with AND if appropriate

Parameters

$key
$values

Returns

object

or_where_in()

or_where_in(  $key = NULL,   $values = NULL) : object

Where_in_or

Generates a WHERE field IN ('item', 'item') SQL query joined with OR if appropriate

Parameters

$key
$values

Returns

object

where_not_in()

where_not_in(  $key = NULL,   $values = NULL) : object

Where_not_in

Generates a WHERE field NOT IN ('item', 'item') SQL query joined with AND if appropriate

Parameters

$key
$values

Returns

object

or_where_not_in()

or_where_not_in(  $key = NULL,   $values = NULL) : object

Where_not_in_or

Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR if appropriate

Parameters

$key
$values

Returns

object

like()

like(  $field,   $match = '',   $side = 'both') : object

Like

Generates a %LIKE% portion of the query. Separates multiple calls with AND

Parameters

$field
$match
$side

Returns

object

not_like()

not_like(  $field,   $match = '',   $side = 'both') : object

Not Like

Generates a NOT LIKE portion of the query. Separates multiple calls with AND

Parameters

$field
$match
$side

Returns

object

or_like()

or_like(  $field,   $match = '',   $side = 'both') : object

OR Like

Generates a %LIKE% portion of the query. Separates multiple calls with OR

Parameters

$field
$match
$side

Returns

object

or_not_like()

or_not_like(  $field,   $match = '',   $side = 'both') : object

OR Not Like

Generates a NOT LIKE portion of the query. Separates multiple calls with OR

Parameters

$field
$match
$side

Returns

object

group_by()

group_by(  $by) : object

GROUP BY

Parameters

$by

Returns

object

having()

having(  $key,   $value = '',   $escape = TRUE) : object

Sets the HAVING value

Separates multiple calls with AND

Parameters

$key
$value
$escape

Returns

object

or_having()

or_having(  $key,   $value = '',   $escape = TRUE) : object

Sets the OR HAVING value

Separates multiple calls with OR

Parameters

$key
$value
$escape

Returns

object

order_by()

order_by(  $orderby,   $direction = '') : object

Sets the ORDER BY value

Parameters

$orderby
$direction

Returns

object

limit()

limit(  $value,   $offset = '') : object

Sets the LIMIT value

Parameters

$value
$offset

Returns

object

offset()

offset(  $offset) : object

Sets the OFFSET value

Parameters

$offset

Returns

object

set()

set(  $key,   $value = '',   $escape = TRUE) : object

The "set" function. Allows key/value pairs to be set for inserting or updating

Parameters

$key
$value
$escape

Returns

object

get()

get(  $table = '',   $limit = null,   $offset = null) : object

Get

Compiles the select statement based on the other functions called and runs the query

Parameters

$table
$limit
$offset

Returns

object

count_all_results()

count_all_results(  $table = '') : string

"Count All Results" query

Generates a platform-specific query string that counts all records returned by an Active Record query.

Parameters

$table

Returns

string

get_where()

get_where(  $table = '',   $where = null,   $limit = null,   $offset = null) : object

Get_Where

Allows the where clause, limit and offset to be added directly

Parameters

$table
$where
$limit
$offset

Returns

object

insert_batch()

insert_batch(  $table = '',   $set = NULL) : object

Insert_Batch

Compiles batch insert strings and runs the queries

Parameters

$table
$set

Returns

object

set_insert_batch()

set_insert_batch(  $key,   $value = '',   $escape = TRUE) : object

The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts

Parameters

$key
$value
$escape

Returns

object

insert()

insert(  $table = '',   $set = NULL) : object

Insert

Compiles an insert string and runs the query

Parameters

$table
$set

Returns

object

replace()

replace(  $table = '',   $set = NULL) : object

Replace

Compiles an replace into string and runs the query

Parameters

$table
$set

Returns

object

update()

update(  $table = '',   $set = NULL,   $where = NULL,   $limit = NULL) : object

Update

Compiles an update string and runs the query

Parameters

$table
$set
$where
$limit

Returns

object

update_batch()

update_batch(  $table = '',   $set = NULL,   $index = NULL) : object

Update_Batch

Compiles an update string and runs the query

Parameters

$table
$set
$index

Returns

object

set_update_batch()

set_update_batch(  $key,   $index = '',   $escape = TRUE) : object

The "set_update_batch" function. Allows key/value pairs to be set for batch updating

Parameters

$key
$index
$escape

Returns

object

empty_table()

empty_table(  $table = '') : object

Empty Table

Compiles a delete string and runs "DELETE FROM table"

Parameters

$table

Returns

object

truncate()

truncate(  $table = '') : object

Truncate

Compiles a truncate string and runs the query If the database does not support the truncate() command This function maps to "DELETE FROM table"

Parameters

$table

Returns

object

delete()

delete(  $table = '',   $where = '',   $limit = NULL,   $reset_data = TRUE) : object

Delete

Compiles a delete string and runs the query

Parameters

$table
$where
$limit
$reset_data

Returns

object

dbprefix()

dbprefix(  $table = '') : string

DB Prefix

Prepends a database prefix if one exists in configuration

Parameters

$table

Returns

string

set_dbprefix()

set_dbprefix(  $prefix = '') : string

Set DB Prefix

Set's the DB Prefix to something new without needing to reconnect

Parameters

$prefix

Returns

string

_object_to_array()

_object_to_array(  $object) : array

Object to Array

Takes an object as input and converts the class variables to array key/vals

Parameters

$object

Returns

array

_object_to_array_batch()

_object_to_array_batch(  $object) : array

Object to Array

Takes an object as input and converts the class variables to array key/vals

Parameters

$object

Returns

array

start_cache()

start_cache() : void

Start Cache

Starts AR caching

stop_cache()

stop_cache() : void

Stop Cache

Stops AR caching

flush_cache()

flush_cache() : void

Flush Cache

Empties the AR cache

_reset_select()

_reset_select() : void

Resets the active record values. Called by the get() function

This function is used extensively by every db driver.

_max_min_avg_sum()

_max_min_avg_sum(  $select = '',   $alias = '',   $type = 'MAX') : object

Processing Function for the four functions above:

select_max() select_min() select_avg() select_sum()

Parameters

$select
$alias
$type

Returns

object

_create_alias_from_table()

_create_alias_from_table(  $item) : string

Determines the alias name based on the table

Parameters

$item

Returns

string

_where()

_where(  $key,   $value = NULL,   $type = 'AND ',   $escape = NULL) : object

Where

Called by where() or or_where()

Parameters

$key
$value
$type
$escape

Returns

object

_where_in()

_where_in(  $key = NULL,   $values = NULL,   $not = FALSE,   $type = 'AND ') : object

Where_in

Called by where_in, where_in_or, where_not_in, where_not_in_or

Parameters

$key
$values
$not
$type

Returns

object

_like()

_like(  $field,   $match = '',   $type = 'AND ',   $side = 'both',   $not = '') : object

Like

Called by like() or orlike()

Parameters

$field
$match
$type
$side
$not

Returns

object

_having()

_having(  $key,   $value = '',   $type = 'AND ',   $escape = TRUE) : object

Sets the HAVING values

Called by having() or or_having()

Parameters

$key
$value
$type
$escape

Returns

object

_track_aliases()

_track_aliases(  $table) : string

Track Aliases

Used to track SQL statements written with aliased tables.

Parameters

$table

Returns

string

_compile_select()

_compile_select(  $select_override = FALSE) : string

Compile the SELECT statement

Generates a query string based on which functions were used. Should not be called directly. The get() function calls it.

Parameters

$select_override

Returns

string

_merge_cache()

_merge_cache() : void

Merge Cache

When called, this function merges any cached AR arrays with locally called ones.

_reset_run()

_reset_run(  $ar_reset_items) : void

Resets the active record values. Called by the get() function

Parameters

$ar_reset_items

_reset_write()

_reset_write() : void

Resets the active record "write" values.

Called by the insert() update() insert_batch() update_batch() and delete() functions