Welcome Guest, Not a member yet? Register   Sign In
troubleshooting queries
#1

[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?
#2

[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);

    }
#3

[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
#4

[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.
#5

[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();




Theme © iAndrew 2016 - Forum software by © MyBB