User Guide | Building Custom SQL Queries

A guide to customising the SQL statements used by flexi cart functions to return database data.

Building Custom SQL Queries

Customising SQL Statements Index | Querying SQL Results | Defining Custom SQL Statements | Misc. Admin Functions

Functions that Utilise CodeIgniters Active Record Class

Many of the functions in the flexi cart admin library that perform CRUD operations to the carts 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 cart functions that utilise CodeIgniters Active Record class.


Library Requirements

Available via the admin library only.

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 cart function, the defined WHERE statement would not necessarily be applied by CodeIgniter to the correct SQL query made by the flexi cart 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 cart 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 admin library only.

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

This function is typically compatible with SELECT query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
// This example sets the SQL SELECT statement using an array.
// Read the defining SQL documentation for further information on setting SQL statements.

$sql_select = array(
'example_column_1', 
'example_column_2'
);

$this->flexi_cart_admin->sql_select($sql_select);

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT `example_column_1`, `example_column_2` 
// FROM (`example_table`)

SQL WHERE

Defines the table column and comparision value to be used by the SQL WHERE statement.


Library Requirements

Available via the admin library only.

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

This function is typically compatible with SELECT, UPDATE and DELETE query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
// This example sets the SQL WHERE statement using arrays.
// Read the defining SQL documentation for further information on setting SQL statements.

// SQL WHERE
$sql_where = array(
'example_column_1' => 'example-value-1'
);
$this->flexi_cart_admin->sql_where($sql_where);

// SQL OR WHERE
$sql_or_where = array(
'example_column_2' => 'example-value-2'
);
$this->flexi_cart_admin->sql_or_where($sql_or_where);

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT * 
// FROM (`example_table`) 
// WHERE `example_column_1` = 'example-value-1' 
// 	OR `example_column_2` = 'example-value-2'

SQL WHERE IN

Defines the table column and comparision values to be used by the SQL WHERE IN statement.


Library Requirements

Available via the admin library only.

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

This function is typically compatible with SELECT, UPDATE and DELETE query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
// This example sets the SQL WHERE statement using strings and arrays.

// SQL WHERE IN (Set as an array)
$sql_where_in = array(
'example-value-1a'
);
$this->flexi_cart_admin->sql_where_in('example_column_1', $sql_where_in);

// SQL OR WHERE IN (Set as an array)
$sql_or_where_in = array(
'example-value-2a', 
'example-value-2b', 
'example-value-2c'
);
$this->flexi_cart_admin->sql_or_where_in('example_column_2', $sql_or_where_in);

// SQL WHERE NOT IN (Set as a string)
$sql_where_not_in = 'example-value-3a';
$this->flexi_cart_admin->sql_where_not_in('example_column_3', $sql_where_not_in);

// SQL OR WHERE NOT IN (Set as a string)
$sql_or_where_not_in = 'example-value-4a, example-value-4b';
$this->flexi_cart_admin->sql_or_where_not_in('example_column_4', $sql_or_where_not_in);

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT *
// FROM (`example_table`)
// WHERE `example_column_1` IN ('example-value-1a') 
// 	OR `example_column_2` IN ('example-value-2a', 'example-value-2b', 'example-value-2c') 
// 	AND `example_column_3` NOT IN ('example-value-3a') 
// 	OR `example_column_4` NOT IN ('example-value-4a, example-value-4b') 

SQL WHERE LIKE

Defines the table column and comparision values to be used by the SQL WHERE LIKE statement.


Library Requirements

Available via the admin library only.

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

This function is typically compatible with SELECT, UPDATE and DELETE query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
// SQL LIKE with the wildcard positioned on both sides.
$this->flexi_cart_admin->sql_like('example_column_1', 'example-value-1');

// SQL OR LIKE with the wildcard positioned at the start.
$this->flexi_cart_admin->sql_or_like('example_column_2', 'example-value-2', 'before');

// SQL NOT LIKE the wildcard positioned at the end
$this->flexi_cart_admin->sql_not_like('example_column_3', 'example-value-3', 'after');

// SQL OR NOT LIKE with the wildcard positioned on both sides
$this->flexi_cart_admin->sql_or_not_like('example_column_4', 'example-value-4', 'both');

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT *
// FROM (`example_table`)
// WHERE  `example_column_1`  LIKE '%example-value-1%'
// 	OR  `example_column_2`  LIKE '%example-value-2'
// 	AND  `example_column_3` NOT LIKE 'example-value-3%'
// 	OR  `example_column_4` NOT LIKE '%example-value-4%'

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 admin library only.

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

This function is typically compatible with SELECT, UPDATE and DELETE query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
// SQL JOIN table name.
$sql_join = 'example_table_2';

// SQL JOIN condition.
$sql_join_on = 'example_table_1.example_column_1 = example_table_2.example_column_1';

$this->flexi_cart_admin->sql_join($sql_join, $sql_join_on);

$this->flexi_cart_admin->get_db_table_data('example_table_1');

// Produces:
// SELECT *
// FROM (`example_table_1`)
// 	JOIN `example_table_2` ON `example_table_1`.`example_column_1` = `example_table_2`.`example_column_1`

SQL ORDER BY

Defines the table column and sort direction used by the SQL ORDER BY statement.


Library Requirements

Available via the admin library only.

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

This function is typically compatible with SELECT query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
$this->flexi_cart_admin->sql_order_by('example_column_1', 'DESC');

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT *
// FROM (`example_table`)
// ORDER BY `example_column_1` DESC

SQL GROUP BY

Defines the table column used by the SQL GROUP BY statement.


Library Requirements

Available via the admin library only.

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

This function is typically compatible with SELECT query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
$this->flexi_cart_admin->sql_group_by('example_column_1');

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT *
// FROM (`example_table`)
// GROUP BY `example_column_1`

SQL LIMIT

Defines the limit value and offset value used by the SQL LIMIT statement.


Library Requirements

Available via the admin library only.

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

This function is typically compatible with SELECT query functions within the admin library.

Check the functions user guide to confirm whether a function is compatible.

Example
$this->flexi_cart_admin->sql_limit(10, 2);

$this->flexi_cart_admin->get_db_table_data('example_table');

// Produces:
// SELECT *
// FROM (`example_table`)
// LIMIT 2, 10

Clear SQL statements

Clears any custom user defined SQL Active Record functions.


Function Names
sql_clear()
Library Requirements

Available via the admin library only.

Example
$this->flexi_cart_admin->sql_clear();