Building Custom SQL Queries
Customising SQL Statements Index |
Querying SQL Results |
Defining Custom SQL Statements
Functions that Utilise CodeIgniters Active Record Class
Many of the functions in the flexi auth library that perform CRUD operations to the libraries database tables allow for custom SQL statements to be returned or defined when running the query.
The custom SQL statements can be defined for applicable functions using one of two available methods, either via the Query Builder functions listed below, or via submitting an SQL formatted string or array directly to a functions corresponding 'sql_select' or 'sql_where' parameter.
The examples below explain how to set custom SQL statements using flexi auth functions that utilise CodeIgniters Active Record class.
Library Requirements
Available via the lite and standard libraries.
How it Works
When many functions are called, they make multiple SQL queries to prepare data required for the final output of the function.
If a CodeIgniter Active Record function like $this->db->where('x', 'y')
was called just before the flexi auth function, the defined WHERE statement would not necessarily be applied by CodeIgniter to the correct SQL query made by the flexi auth function. Instead it may be applied to one of the queries preparing data.
To ensure that the custom SQL statement is applied to the correct query, flexi auth stores any SQL statements defined via its Query Builder functions, and applies them just prior to making the SQL query that returns the functions final output.
Reference
SQL SELECT
Defines the table columns to be returned by the SQL SELECT statement.
Library Requirements
Available via the lite and standard libraries.
Function Name and Parameters
sql_select(columns, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
columns |
string | array |
Yes |
FALSE |
Sets the table columns to be returned by the SQL SELECT statement. |
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL SELECT statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$sql_select = array(
'example_column_1',
'example_column_2'
);
$this->flexi_auth->sql_select($sql_select);
$this->flexi_auth->get_users();
SQL WHERE
Defines the table column and comparision value to be used by the SQL WHERE statement.
Library Requirements
Available via the lite and standard libraries.
Function Names and Parameters
sql_where(columns, value, overwrite_existing)
sql_or_where(columns, value, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
column |
string | array |
Yes |
FALSE |
Sets the table column to be used by the SQL WHERE statement. |
value |
string | int |
No |
FALSE |
Sets the value to be compared against the defined table column used by the SQL WHERE statement. |
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL WHERE statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$sql_where = array(
'example_column_1' => 'example-value-1'
);
$this->flexi_auth->sql_where($sql_where);
$sql_or_where = array(
'example_column_2' => 'example-value-2'
);
$this->flexi_auth->sql_or_where($sql_or_where);
$this->flexi_auth->get_users();
SQL WHERE IN
Defines the table column and comparision values to be used by the SQL WHERE IN statement.
Library Requirements
Available via the lite and standard libraries.
Function Names and Parameters
sql_where_in(column, value, overwrite_existing)
sql_or_where_in(column, value, overwrite_existing)
sql_where_not_in(column, value, overwrite_existing)
sql_or_where_not_in(column, value, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
column |
string |
Yes |
FALSE |
Sets the table column to be used by the SQL WHERE IN statement. |
value |
string | int | array |
No |
FALSE |
Sets the value to be compared against the defined table column used by the SQL WHERE IN statement.
Multiple values can be set in SQL IN statement by using a comma delimited string or an array.
|
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL WHERE statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$sql_where_in = array(
'example-value-1a'
);
$this->flexi_auth->sql_where_in('example_column_1', $sql_where_in);
$sql_or_where_in = array(
'example-value-2a',
'example-value-2b',
'example-value-2c'
);
$this->flexi_auth->sql_or_where_in('example_column_2', $sql_or_where_in);
$sql_where_not_in = 'example-value-3a';
$this->flexi_auth->sql_where_not_in('example_column_3', $sql_where_not_in);
$sql_or_where_not_in = 'example-value-4a, example-value-4b';
$this->flexi_auth->sql_or_where_not_in('example_column_4', $sql_or_where_not_in);
$this->flexi_auth->get_users();
SQL WHERE LIKE
Defines the table column and comparision values to be used by the SQL WHERE LIKE statement.
Library Requirements
Available via the lite and standard libraries.
Function Names and Parameters
sql_like(column, value, wildcard_position)
sql_or_like(column, value, wildcard_position)
sql_not_like(column, value, wildcard_position)
sql_or_not_like(column, value, wildcard_position)
Name |
Data Type |
Required |
Default |
Description |
column |
string | array |
Yes |
FALSE |
Sets the table column to be used by the SQL WHERE LIKE statement. |
value |
string | int |
No |
FALSE |
Sets the value to be compared against the defined table column used by the SQL WHERE LIKE statement. |
wildcard_position |
string |
No |
'BOTH' |
Defines the type of LIKE statement that should be set.
The options are: 'BEFORE', 'AFTER' and 'BOTH'.
|
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL WHERE statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$this->flexi_auth->sql_like('example_column_1', 'example-value-1');
$this->flexi_auth->sql_or_like('example_column_2', 'example-value-2', 'before');
$this->flexi_auth->sql_not_like('example_column_3', 'example-value-3', 'after');
$this->flexi_auth->sql_or_not_like('example_column_4', 'example-value-4', 'both');
$this->flexi_auth->get_users();
SQL JOIN
Defines the table name and join relationship between the to-be-joined tables used by the SQL JOIN statement.
Library Requirements
Available via the lite and standard libraries.
Function Name and Parameters
sql_join(column, join_on, join_type, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
column |
string |
Yes |
FALSE |
Sets the table that is to be joined by the SQL JOIN statement. |
join_on |
string |
Yes |
FALSE |
Sets the two table columns to be used by the SQL JOIN statement. |
join_type |
string |
No |
FALSE |
Defines the type of join that should be set.
Options are: 'LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', and 'RIGHT OUTER'.
|
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL JOIN statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$sql_join = 'example_table_2';
$sql_join_on = 'example_table_1.example_column_1 = example_table_2.example_column_1';
$this->flexi_auth->sql_join($sql_join, $sql_join_on);
$this->flexi_auth->get_db_table_data('example_table_1');
SQL ORDER BY
Defines the table column and sort direction used by the SQL ORDER BY statement.
Library Requirements
Available via the lite and standard libraries.
Function Name and Parameters
sql_order_by(column, sort_direction, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
column |
string |
Yes |
FALSE |
Sets the columns for the SQL ORDER BY statement to sort the returned data by. |
sort_direction |
string |
No |
FALSE |
Defines what direction to sort the return data by.
Options are: 'ASC' and 'DESC'.
|
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL ORDER BY statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$this->flexi_auth->sql_order_by('example_column_1', 'DESC');
$this->flexi_auth->get_users();
SQL GROUP BY
Defines the table column used by the SQL GROUP BY statement.
Library Requirements
Available via the lite and standard libraries.
Function Name and Parameters
sql_group_by(columns, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
columns |
string | array |
Yes |
FALSE |
column or string |
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL GROUP BY statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$this->flexi_auth->sql_group_by('example_column_1');
$this->flexi_auth->get_users();
SQL LIMIT
Defines the limit value and offset value used by the SQL LIMIT statement.
Library Requirements
Available via the lite and standard libraries.
Function Name and Parameters
sql_limit(limit, offset, overwrite_existing)
Name |
Data Type |
Required |
Default |
Description |
limit |
int |
Yes |
FALSE |
Sets the limit value of SQL LIMIT statement. |
offset |
int |
Yes |
FALSE |
Sets the offset value of SQL LIMIT statement. |
overwrite_existing |
bool |
No |
FALSE |
Defines whether to overwrite any SQL LIMIT statements that have already been set. |
Notes
Check the user guide of a specific function to confirm whether it is compatible with this SQL function.
Example
$this->flexi_auth->sql_limit(10, 2);
$this->flexi_auth->get_users();
Clear SQL statements
Clears any custom user defined SQL Active Record functions.
Function Names
sql_clear()
Library Requirements
Available via the lite and standard libraries.
Example
$this->flexi_auth->sql_clear();