Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix for Issue #276 #307

Merged
merged 14 commits into from

7 participants

@kylefarris

I've added the following methods to the Active Record class:

  • get_compiled_select()
  • get_compiled_insert()
  • get_compiled_update()
  • get_compiled_delete()

The above methods provide a public API allowing you to get the SQL from an AR query without having to execute them first. They all take two parameters: ($table = '', $reset = TRUE).

I've tested them, somewhat, using the following tests which utilize philsturgeon's cli spark

class Test_ar extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }

    public function test()
    {
        $this->load->spark('cli/2.0.0');
        $this->load->database();

        /*******************************
         * Test Select
         ******************************/
        // SELECT fee, bar, fo FROM foo WHERE fo='fum'
        $this->cli->write($this->db->where(array('fo'=>'fum'))->select(array('fee','bar','fo'))->from('foo')->get_compiled_select(false,false) . "\n");
        $this->db->reset_query();

        // SELECT * FROM testing WHERE code='awesome'
        $this->cli->write($this->db->where(array('code'=>'awesome'))->get_compiled_select('testing') . "\n");

        /*******************************
         * Test Insert
         ******************************/
        $this->db->set(array('bar'=>'baz'));

        // INSERT INTO foo (bar) VALUES ('baz')
        $this->cli->write($this->db->get_compiled_insert('foo',false) . "\n");

        /*******************************
         * Test Update
         ******************************/
        $this->db->set(array('fee'=>'fi','jack'=>'And the Beanstock'))->where(array('fo'=>'fum'));
        $this->db->from('foobar');

        // UPDATE (foo, foobar) SET bar='baz', fee='fi', jack='And the Beanstock' WHERE fo='fum'
        $this->cli->write($this->db->get_compiled_update() . "\n");

        /*******************************
         * Test Delete
         ******************************/
        $this->db->where(array('fo'=>'fum'));

        // DELETE FROM foo WHERE fo='fum'
        $this->cli->write($this->db->get_compiled_delete('foo'));
    }
}

You can run those tests by installing the cli spark, and putting that controller into one of your projects. Then run it in the command line like:

cd /home/project/path
php index.php test_ar test

Once/if this gets merged, I'll write some real unit tests for these methods.

I've also added the a publicly-visible method that allows you to reset the AR object: reset_query().

Naturally, changelog and user guide has been updated.

Kyle Farris and others added some commits
Kyle Farris Added get_compiled_select(), get_compiled_insert(), get_compiled_upda…
…te(), get_compiled_delete(), and reset_query() methods. to the Active Record class.
0c147b3
@kylefarris kylefarris Small formatting fix. 0a3176b
@kylefarris kylefarris Updated the changelog to reflect new Active Record methods: get_compi…
…led_select(), get_compiled_delete(), get_compiled_insert(), get_compiled_update(), and reset_query().
c1ee04b
@kylefarris kylefarris Updated the Active Record documentation to reflect new publicly visib…
…le Active Record methods: get_compiled_select(), get_compiled_delete(), get_compiled_insert(), get_compiled_update(), and reset_query().
0f35b6e
@NTICompass

+1'ing this request.

@gaker

For core classes, it's best to use protected so these variables can be overridden.

@gaker

you may be reverting a previous merge here with the int casting.

I'll make the change.

@gaker

Nitpick: unnecessary newline

Nitpicks are good.

@gaker

can you break this out to be more lines, less long?

eg:

$sql = $this->_insert(
    $this->_protect_identifiers(
        $this->ar_from[0], TRUE, NULL, FALSE),
         array_keys($this->ar_set), 
         array_values($this->ar_set
    )
);

Yeah, no problem.

@gaker

same as above. More lines, not one long line.

@gaker

Nitpick: adding unnecessary newline.

@gaker

also, if you can get my nitpicky items done, merge in the current develop branch, and we can easily merge it in! :D

Thanks so much Kyle!

@kylefarris

Alright, we should be good.

@kutothe

Nice! Good job :)

@kutothe

Is there anything holding up this from getting merged in, or does the review process simply take a little while?

@kylefarris

It probably just takes a while. A lot of code to merge in. Lots of pull-requests recently (which is a good thing!). Usually the easier ones (patches, bug fixes, typos, formatting, etc...) get merged in first because they don't require a lot of scrutinizing. I'd imagine new features are little bit more of a PITA.

@kutothe

Is there a nice clean way for me to merge this change into my CodeIgniter instance without causing problems when I pull updates from the develop branch?

@kylefarris

Yeah, they merge it in... Wonder what's taking so long?

@kylefarris kylefarris closed this
@kylefarris kylefarris reopened this
@kylefarris

Grr... that "Comment and Close" button is annoying...

@NTICompass

Has this been pulled in yet? Someone told me this was accepted. I want to get my Subquery library updated & working.

@derekjones

Kyle I was going to try to review this before EECI, and I see that now the request cannot be automatically merged; any chance of updating the user guide changes for the pull request?

@kylefarris

What do I need to do get it to be able to be automatically-merged?

@derekjones

I believe you'll just need to update and merge the 'isue-276' branch in your fork with the official 'develop' branch.

@kutothe

Do you also need to update the documentation of the new .rst files instead of the old .html documentation file?

@derekjones

Yes, that would be part of the merge, that would likely need to be resolved manually by incorporating those few changes into the .rst source files.

@kylefarris

Thanks for your help guys! I've made the necessary changes.

Can we please get this merged in quick so I don't have to re-update from development again. Thanks!

@derekjones derekjones commented on the diff
application/config/migration.php
((125 lines not shown))
+
+
+/*
+|--------------------------------------------------------------------------
+| Migrations Path
+|--------------------------------------------------------------------------
+|
+| Path to your migrations folder.
+| Typically, it will be within your application path.
+| Also, writing permission is required within the migrations path.
+|
+*/
+$config['migration_path'] = APPPATH . 'migrations/';
+
+
+/* End of file migration.php */
/* Location: ./application/config/migration.php */

Not really sure what GitHub is showing here. I don't see any diffs in the current develop branch and your isue-276 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@derekjones derekjones merged commit 12e93fe into from
@atiredmachine

damn, there goes compatibility with my MySQL-specific Active Record add-on library.

@NTICompass

Is this going to be put into the next stable release? I need to tell users of my Subquery library to use the "dev" version of CodeIgniter. Not that that's a bad thing, I'm just curious.

@alxndrmlr

Any news on this... I'm looking for this functionality to extend https://github.com/IgnitedDatatables/Ignited-Datatables/ for filtering multiple aggregate queries that I want to join on to the main record set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 26, 2011
  1. Added get_compiled_select(), get_compiled_insert(), get_compiled_upda…

    Kyle Farris authored
    …te(), get_compiled_delete(), and reset_query() methods. to the Active Record class.
  2. @kylefarris

    Small formatting fix.

    kylefarris authored
  3. @kylefarris

    Updated the changelog to reflect new Active Record methods: get_compi…

    kylefarris authored
    …led_select(), get_compiled_delete(), get_compiled_insert(), get_compiled_update(), and reset_query().
  4. @kylefarris

    Updated the Active Record documentation to reflect new publicly visib…

    kylefarris authored
    …le Active Record methods: get_compiled_select(), get_compiled_delete(), get_compiled_insert(), get_compiled_update(), and reset_query().
Commits on Aug 31, 2011
  1. @kylefarris
  2. @kylefarris
  3. @kylefarris
  4. @kylefarris

    One more formatting fix.

    kylefarris authored
Commits on Oct 14, 2011
  1. trying to resolve some merge issues

    Kyle Farris authored
  2. Merged with development

    Kyle Farris authored
  3. @kylefarris

    Fixed merge error.

    kylefarris authored
  4. @kylefarris

    REALLY fixed merge problem.

    kylefarris authored
  5. @kylefarris
Commits on Oct 15, 2011
  1. @kylefarris
This page is out of date. Refresh to see the latest.
View
140 application/config/migration.php
@@ -1,71 +1,71 @@
-<?php defined('BASEPATH') OR exit('No direct script access allowed');
-/*
-|--------------------------------------------------------------------------
-| Enable/Disable Migrations
-|--------------------------------------------------------------------------
-|
-| Migrations are disabled by default for security reasons.
-| You should enable migrations whenever you intend to do a schema migration
-| and disable it back when you're done.
-|
-*/
-$config['migration_enabled'] = FALSE;
-
-/*
-|--------------------------------------------------------------------------
-| Migrations table
-|--------------------------------------------------------------------------
-|
-| This is the name of the table that will store the current migrations state.
-| When migrations runs it will store in a database table which migration
-| level the system is at. It then compares the migration level in the this
-| table to the $config['migration_version'] if they are not the same it
-| will migrate up. This must be set.
-|
-*/
-$config['migration_table'] = 'migrations';
-
-
-/*
-|--------------------------------------------------------------------------
-| Auto Migrate To Latest
-|--------------------------------------------------------------------------
-|
-| If this is set to TRUE when you load the migrations class and have
-| $config['migration_enabled'] set to TRUE the system will auto migrate
-| to your latest migration (whatever $config['migration_version'] is
-| set to). This way you do not have to call migrations anywhere else
-| in your code to have the latest migration.
-|
-*/
-$config['migration_auto_latest'] = FALSE;
-
-
-/*
-|--------------------------------------------------------------------------
-| Migrations version
-|--------------------------------------------------------------------------
-|
-| This is used to set migration version that the file system should be on.
-| If you run $this->migration->latest() this is the version that schema will
-| be upgraded / downgraded to.
-|
-*/
-$config['migration_version'] = 0;
-
-
-/*
-|--------------------------------------------------------------------------
-| Migrations Path
-|--------------------------------------------------------------------------
-|
-| Path to your migrations folder.
-| Typically, it will be within your application path.
-| Also, writing permission is required within the migrations path.
-|
-*/
-$config['migration_path'] = APPPATH . 'migrations/';
-
-
-/* End of file migration.php */
+<?php defined('BASEPATH') OR exit('No direct script access allowed');
+/*
+|--------------------------------------------------------------------------
+| Enable/Disable Migrations
+|--------------------------------------------------------------------------
+|
+| Migrations are disabled by default for security reasons.
+| You should enable migrations whenever you intend to do a schema migration
+| and disable it back when you're done.
+|
+*/
+$config['migration_enabled'] = FALSE;
+
+/*
+|--------------------------------------------------------------------------
+| Migrations table
+|--------------------------------------------------------------------------
+|
+| This is the name of the table that will store the current migrations state.
+| When migrations runs it will store in a database table which migration
+| level the system is at. It then compares the migration level in the this
+| table to the $config['migration_version'] if they are not the same it
+| will migrate up. This must be set.
+|
+*/
+$config['migration_table'] = 'migrations';
+
+
+/*
+|--------------------------------------------------------------------------
+| Auto Migrate To Latest
+|--------------------------------------------------------------------------
+|
+| If this is set to TRUE when you load the migrations class and have
+| $config['migration_enabled'] set to TRUE the system will auto migrate
+| to your latest migration (whatever $config['migration_version'] is
+| set to). This way you do not have to call migrations anywhere else
+| in your code to have the latest migration.
+|
+*/
+$config['migration_auto_latest'] = FALSE;
+
+
+/*
+|--------------------------------------------------------------------------
+| Migrations version
+|--------------------------------------------------------------------------
+|
+| This is used to set migration version that the file system should be on.
+| If you run $this->migration->latest() this is the version that schema will
+| be upgraded / downgraded to.
+|
+*/
+$config['migration_version'] = 0;
+
+
+/*
+|--------------------------------------------------------------------------
+| Migrations Path
+|--------------------------------------------------------------------------
+|
+| Path to your migrations folder.
+| Typically, it will be within your application path.
+| Also, writing permission is required within the migrations path.
+|
+*/
+$config['migration_path'] = APPPATH . 'migrations/';
+
+
+/* End of file migration.php */
/* Location: ./application/config/migration.php */
View
311 system/database/DB_active_rec.php
@@ -28,39 +28,42 @@
*/
class CI_DB_active_record extends CI_DB_driver {
- var $ar_select = array();
- var $ar_distinct = FALSE;
- var $ar_from = array();
- var $ar_join = array();
- var $ar_where = array();
- var $ar_like = array();
- var $ar_groupby = array();
- var $ar_having = array();
- var $ar_keys = array();
- var $ar_limit = FALSE;
- var $ar_offset = FALSE;
- var $ar_order = FALSE;
- var $ar_orderby = array();
- var $ar_set = array();
- var $ar_wherein = array();
- var $ar_aliased_tables = array();
- var $ar_store_array = array();
+ protected $return_delete_sql = FALSE;
+ protected $reset_delete_data = FALSE;
+
+ protected $ar_select = array();
+ protected $ar_distinct = FALSE;
+ protected $ar_from = array();
+ protected $ar_join = array();
+ protected $ar_where = array();
+ protected $ar_like = array();
+ protected $ar_groupby = array();
+ protected $ar_having = array();
+ protected $ar_keys = array();
+ protected $ar_limit = FALSE;
+ protected $ar_offset = FALSE;
+ protected $ar_order = FALSE;
+ protected $ar_orderby = array();
+ protected $ar_set = array();
+ protected $ar_wherein = array();
+ protected $ar_aliased_tables = array();
+ protected $ar_store_array = array();
// Active Record Caching variables
- var $ar_caching = FALSE;
- var $ar_cache_exists = array();
- var $ar_cache_select = array();
- var $ar_cache_from = array();
- var $ar_cache_join = array();
- var $ar_cache_where = array();
- var $ar_cache_like = array();
- var $ar_cache_groupby = array();
- var $ar_cache_having = array();
- var $ar_cache_orderby = array();
- var $ar_cache_set = array();
+ protected $ar_caching = FALSE;
+ protected $ar_cache_exists = array();
+ protected $ar_cache_select = array();
+ protected $ar_cache_from = array();
+ protected $ar_cache_join = array();
+ protected $ar_cache_where = array();
+ protected $ar_cache_like = array();
+ protected $ar_cache_groupby = array();
+ protected $ar_cache_having = array();
+ protected $ar_cache_orderby = array();
+ protected $ar_cache_set = array();
- var $ar_no_escape = array();
- var $ar_cache_no_escape = array();
+ protected $ar_no_escape = array();
+ protected $ar_cache_no_escape = array();
// --------------------------------------------------------------------
@@ -660,7 +663,7 @@ protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $n
$prefix = (count($this->ar_like) == 0) ? '' : $type;
$v = $this->escape_like_str($v);
-
+
if ($side == 'none')
{
$like_statement = $prefix." $k $not LIKE '{$v}'";
@@ -931,7 +934,37 @@ public function set($key, $value = '', $escape = TRUE)
return $this;
}
+
+ // --------------------------------------------------------------------
+ /**
+ * Get SELECT query string
+ *
+ * Compiles a SELECT query string and returns the sql.
+ *
+ * @access public
+ * @param string the table name to select from (optional)
+ * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
+ * @return string
+ */
+ public function get_compiled_select($table = '', $reset = TRUE)
+ {
+ if ($table != '')
+ {
+ $this->_track_aliases($table);
+ $this->from($table);
+ }
+
+ $select = $this->_compile_select();
+
+ if ($reset === TRUE)
+ {
+ $this->_reset_select();
+ }
+
+ return $select;
+ }
+
// --------------------------------------------------------------------
/**
@@ -1148,6 +1181,41 @@ public function set_insert_batch($key, $value = '', $escape = TRUE)
return $this;
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Get INSERT query string
+ *
+ * Compiles an insert query and returns the sql
+ *
+ * @access public
+ * @param string the table to insert into
+ * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
+ * @return string
+ */
+ public function get_compiled_insert($table = '', $reset = TRUE)
+ {
+ if ($this->_validate_insert($table) === FALSE)
+ {
+ return FALSE;
+ }
+
+ $sql = $this->_insert(
+ $this->_protect_identifiers(
+ $this->ar_from[0], TRUE, NULL, FALSE
+ ),
+ array_keys($this->ar_set),
+ array_values($this->ar_set)
+ );
+
+ if ($reset === TRUE)
+ {
+ $this->_reset_write();
+ }
+
+ return $sql;
+ }
// --------------------------------------------------------------------
@@ -1156,17 +1224,50 @@ public function set_insert_batch($key, $value = '', $escape = TRUE)
*
* Compiles an insert string and runs the query
*
+ * @access public
* @param string the table to insert data into
* @param array an associative array of insert values
* @return object
*/
- function insert($table = '', $set = NULL)
+ public function insert($table = '', $set = NULL)
{
if ( ! is_null($set))
{
$this->set($set);
}
+
+ if ($this->_validate_insert($table) === FALSE)
+ {
+ return FALSE;
+ }
+
+ $sql = $this->_insert(
+ $this->_protect_identifiers(
+ $this->ar_from[0], TRUE, NULL, FALSE
+ ),
+ array_keys($this->ar_set),
+ array_values($this->ar_set)
+ );
+ $this->_reset_write();
+ return $this->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Validate Insert
+ *
+ * This method is used by both insert() and get_compiled_insert() to
+ * validate that the there data is actually being set and that table
+ * has been chosen to be inserted into.
+ *
+ * @access public
+ * @param string the table to insert data into
+ * @return string
+ */
+ protected function _validate_insert($table = '')
+ {
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
@@ -1186,14 +1287,13 @@ function insert($table = '', $set = NULL)
}
return FALSE;
}
-
- $table = $this->ar_from[0];
}
-
- $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
-
- $this->_reset_write();
- return $this->query($sql);
+ else
+ {
+ $this->ar_from[0] = $table;
+ }
+
+ return TRUE;
}
// --------------------------------------------------------------------
@@ -1242,7 +1342,39 @@ public function replace($table = '', $set = NULL)
$this->_reset_write();
return $this->query($sql);
}
+
+ // --------------------------------------------------------------------
+ /**
+ * Get UPDATE query string
+ *
+ * Compiles an update query and returns the sql
+ *
+ * @access public
+ * @param string the table to update
+ * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
+ * @return string
+ */
+ public function get_compiled_update($table = '', $reset = TRUE)
+ {
+ // Combine any cached components with the current statements
+ $this->_merge_cache();
+
+ if ($this->_validate_update($table) === FALSE)
+ {
+ return FALSE;
+ }
+
+ $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
+
+ if ($reset === TRUE)
+ {
+ $this->_reset_write();
+ }
+
+ return $sql;
+ }
+
// --------------------------------------------------------------------
/**
@@ -1265,6 +1397,42 @@ public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
$this->set($set);
}
+ if ($this->_validate_update($table) === FALSE)
+ {
+ return FALSE;
+ }
+
+ if ($where != NULL)
+ {
+ $this->where($where);
+ }
+
+ if ($limit != NULL)
+ {
+ $this->limit($limit);
+ }
+
+ $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
+
+ $this->_reset_write();
+ return $this->query($sql);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Validate Update
+ *
+ * This method is used by both update() and get_compiled_update() to
+ * validate that data is actually being set and that a table has been
+ * chosen to be update.
+ *
+ * @access public
+ * @param string the table to update data on
+ * @return string
+ */
+ protected function _validate_update($table = '')
+ {
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
@@ -1284,27 +1452,13 @@ public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
}
return FALSE;
}
-
- $table = $this->ar_from[0];
- }
-
- if ($where != NULL)
- {
- $this->where($where);
}
-
- if ($limit != NULL)
+ else
{
- $this->limit($limit);
+ $this->ar_from[0] = $table;
}
-
- $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
-
- $this->_reset_write();
- return $this->query($sql);
}
-
-
+
// --------------------------------------------------------------------
/**
@@ -1503,7 +1657,27 @@ public function truncate($table = '')
return $this->query($sql);
}
+
+ // --------------------------------------------------------------------
+ /**
+ * Get DELETE query string
+ *
+ * Compiles a delete query string and returns the sql
+ *
+ * @access public
+ * @param string the table to delete from
+ * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
+ * @return string
+ */
+ public function get_compiled_delete($table = '', $reset = TRUE)
+ {
+ $this->return_delete_sql = TRUE;
+ $sql = $this->delete($table, '', NULL, $reset);
+ $this->return_delete_sql = FALSE;
+ return $sql;
+ }
+
// --------------------------------------------------------------------
/**
@@ -1576,10 +1750,15 @@ public function delete($table = '', $where = '', $limit = NULL, $reset_data = TR
{
$this->_reset_write();
}
+
+ if ($this->return_delete_sql === true)
+ {
+ return $sql;
+ }
return $this->query($sql);
}
-
+
// --------------------------------------------------------------------
/**
@@ -1659,7 +1838,7 @@ protected function _track_aliases($table)
}
}
}
-
+
// --------------------------------------------------------------------
/**
@@ -1965,6 +2144,22 @@ protected function _merge_cache()
$this->ar_no_escape = $this->ar_cache_no_escape;
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Reset Active Record values.
+ *
+ * Publicly-visible method to reset the AR values.
+ *
+ * @access public
+ * @return void
+ */
+ public function reset_query()
+ {
+ $this->_reset_select();
+ $this->_reset_write();
+ }
// --------------------------------------------------------------------
View
3  user_guide_src/source/changelog.rst
@@ -46,6 +46,9 @@ Release Date: Not Released
$this->db->like() in the :doc:`Database
Driver <database/active_record>`.
- Added $this->db->insert_batch() support to the OCI8 (Oracle) driver.
+ - Added new :doc:`Active Record <database/active_record>` methods that return
+ the SQL string of queries without executing them: get_compiled_select(),
+ get_compiled_insert(), get_compiled_update(), get_compiled_delete().
- Libraries
View
108 user_guide_src/source/database/active_record.rst
@@ -54,6 +54,36 @@ $query, which can be used to show the results::
Please visit the :doc:`result functions <results>` page for a full
discussion regarding result generation.
+$this->db->get_compiled_select()
+================================
+
+Compiles the selection query just like `$this->db->get()`_ but does not *run*
+the query. This method simply returns the SQL query as a string.
+
+Example::
+
+ $sql = $this->db->get_compiled_select('mytable');
+ echo $sql;
+
+ // Produces string: SELECT * FROM mytable
+
+The second parameter enables you to set whether or not the active record query
+will be reset (by default it will be&mdash;just like `$this->db->get()`)::
+
+ echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE);
+ // Produces string: SELECT * FROM mytable LIMIT 20, 10
+ // (in MySQL. Other databases have slightly different syntax)
+
+ echo $this->db->select('title, content, date')->get_compiled_select();
+
+ // Produces string: SELECT title, content, date FROM mytable
+
+The key thing to notice in the above example is that the second query did not
+utlize `$this->db->from()`_ and did not pass a table name into the first
+parameter. The reason for this outcome is because the query has not been
+executed using `$this->db->get()`_ which resets values or reset directly
+using `$this-db->reset_query()`_.
+
$this->db->get_where()
======================
@@ -540,6 +570,41 @@ object.
.. note:: All values are escaped automatically producing safer queries.
+$this->db->get_compiled_insert()
+================================
+Compiles the insertion query just like `$this->db->insert()`_ but does not
+*run* the query. This method simply returns the SQL query as a string.
+
+Example::
+
+ $data = array(
+ 'title' => 'My title',
+ 'name' => 'My Name',
+ 'date' => 'My date'
+ );
+
+ $sql = $this->db->set($data)->get_compiled_insert('mytable');
+ echo $sql;
+
+ // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
+
+The second parameter enables you to set whether or not the active record query
+will be reset (by default it will be--just like `$this->db->insert()`_)::
+
+ echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE);
+
+ // Produces string: INSERT INTO mytable (title) VALUES ('My Title')
+
+ echo $this->db->set('content', 'My Content')->get_compiled_insert();
+
+ // Produces string: INSERT INTO mytable (title, content) VALUES ('My Title', 'My Content')
+
+The key thing to notice in the above example is that the second query did not
+utlize `$this->db->from()`_ nor did it pass a table name into the first
+parameter. The reason this worked is because the query has not been executed
+using `$this->db->insert()`_ which resets values or reset directly using
+`$this->db->reset_query()`_.
+
$this->db->insert_batch()
=========================
@@ -717,6 +782,14 @@ array of values, the third parameter is the where key.
.. note:: All values are escaped automatically producing safer queries.
+$this->db->get_compiled_update()
+================================
+
+This works exactly the same way as ``$this->db->get_compiled_insert()`` except
+that it produces an UPDATE SQL string instead of an INSERT SQL string.
+
+For more information view documentation for `$this->get_compiled_insert()`_.
+
*************
Deleting Data
@@ -784,6 +857,13 @@ Generates a truncate SQL string and runs the query.
.. note:: If the TRUNCATE command isn't available, truncate() will
execute as "DELETE FROM table".
+
+$this->db->get_compiled_delete()
+================================
+This works exactly the same way as ``$this->db->get_compiled_insert()`` except
+that it produces a DELETE SQL string instead of an INSERT SQL string.
+
+For more information view documentation for `$this->get_compiled_insert()`_.
***************
Method Chaining
@@ -854,3 +934,31 @@ Here's a usage example::
where, like, group_by, having, order_by, set
+
+*******************
+Reset Active Record
+*******************
+
+Resetting Active Record allows you to start fresh with your query without
+executing it first using a method like $this->db->get() or $this->db->insert().
+Just like the methods that execute a query, this will *not* reset items you've
+cached using `Active Record Caching`_.
+
+This is useful in situations where you are using Active Record to generate SQL
+(ex. ``$this->db->get_compiled_select()``) but then choose to, for instance,
+run the query::
+
+ // Note that the second parameter of the get_compiled_select method is FALSE
+ $sql = $this->db->select(array('field1','field2'))
+ ->where('field3',5)
+ ->get_compiled_select('mytable', FALSE);
+
+ // ...
+ // Do something crazy with the SQL code... like add it to a cron script for
+ // later execution or something...
+ // ...
+
+ $data = $this->db->get()->result_array();
+
+ // Would execute and return an array of results of the following query:
+ // SELECT field1, field1 from mytable where field3 = 5;
Something went wrong with that request. Please try again.