CodeIgniter Forums
troubleshooting queries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: troubleshooting queries (/showthread.php?tid=5635)



troubleshooting queries - El Forum - 01-29-2008

[eluser]mrtopher[/eluser]
Is there a function in the database class that I can call to see the generated SQL output to the screen instead of having it processed?


troubleshooting queries - El Forum - 01-29-2008

[eluser]xwero[/eluser]
I think that would be a problem because the action methods (get,insert,update,delete) have the final methods for building the query or you should add the methods view_get, view_insert, view_update and view_delete to the AR library
Code:
function view_get($table = '', $limit = null, $offset = null)
    {
        if ($table != '')
        {
            $this->from($table);
        }
        
        if ( ! is_null($limit))
        {
            $this->limit($limit, $offset);
        }
            
        return $this->_compile_select();

    }

function view_insert($table = '', $set = NULL)
    {
        if ( ! is_null($set))
        {
            $this->set($set);
        }
    
        if (count($this->ar_set) == 0)
        {
            if ($this->db_debug)
            {
                return $this->display_error('db_must_use_set');
            }
            return FALSE;
        }

        if ($table == '')
        {
            if ( ! isset($this->ar_from[0]))
            {
                if ($this->db_debug)
                {
                    return $this->display_error('db_must_set_table');
                }
                return FALSE;
            }
            
            $table = $this->ar_from[0];
        }
                    
        return $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
                
    }

function view_update($table = '', $set = NULL, $where = null)
    {
        if ( ! is_null($set))
        {
            $this->set($set);
        }
    
        if (count($this->ar_set) == 0)
        {
            if ($this->db_debug)
            {
                return $this->display_error('db_must_use_set');
            }
            return FALSE;
        }

        if ($table == '')
        {
            if ( ! isset($this->ar_from[0]))
            {
                if ($this->db_debug)
                {
                    return $this->display_error('db_must_set_table');
                }
                return FALSE;
            }
            
            $table = $this->ar_from[0];
        }
        
        if ($where != null)
        {
            $this->where($where);
        }
        
        return $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where);
        
    }

function view_delete($table = '', $where = '')
    {
        if ($table == '')
        {
            if ( ! isset($this->ar_from[0]))
            {
                if ($this->db_debug)
                {
                    return $this->display_error('db_must_set_table');
                }
                return FALSE;
            }
            
            $table = $this->ar_from[0];
        }

        if ($where != '')
        {
            $this->where($where);
        }

        if (count($this->ar_where) == 0)
        {
            if ($this->db_debug)
            {
                return $this->display_error('db_del_must_use_where');
            }
            return FALSE;
        }        
        
        return $this->_delete($this->dbprefix.$table, $this->ar_where);

    }



troubleshooting queries - El Forum - 01-30-2008

[eluser]tonanbarbarian[/eluser]
if you turn on the profiler it will show you the queries that were run, except of course if you redirect
Enable Profiler


troubleshooting queries - El Forum - 01-30-2008

[eluser]John Fuller[/eluser]
But if the SQL is not processed then there is no output. Wow, that is deep. Kind of like, what comes first, the chicken or the egg? My brain hurts.


troubleshooting queries - El Forum - 01-30-2008

[eluser]xwero[/eluser]
As i understand mrthopers question correctly he doesn't want to execute the queries so enabling the profiler is no option.

Maybe an easier solution would be having a variable in the AR library that can be set before building the sql statement because the only difference with the view_ methods and the regular methods is the return of the compiled statement and the execution of the statement
Code:
var $view_sql = false;
function get($table = '', $limit = null, $offset = null)
    {
        if ($table != '')
        {
            $this->from($table);
        }
        
        if ( ! is_null($limit))
        {
            $this->limit($limit, $offset);
        }
            
        $sql = $this->_compile_select();

        if($this->view_sql)
                {
                   return $sql;
                 }
                 $result = $this->query($sql);
         $this->_reset_select();
         return $result;
                
    }
In the models you could do
Code:
$this->db->view_sql = true;
$this->db->where('a','b');
echo $this->db->get();