<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Name of Class as mentioned in $hook['post_controller]
class Db_log extends CI_Hooks{
 
    function __construct() {
       // Anything except exit() :P
    }
 
    // Name of function same as mentioned in Hooks Config
    function logQueries() {
        $CI = & get_instance();
 
        $times = $CI->db->query_times;                   // Get execution time of all the queries executed by controller
        
        foreach ($CI->db->queries as $key => $query) {
            $qr_string = strtolower($query); 
            if(preg_match('/\insert\b/', $qr_string)) {
                $CI->db->insert('_log_query',array(
                                                "type"=>"insert",
                                                "query"=>$query,
                                                "executionTime"=>$times[$key],
                                                'companyID'=>element('compID', $CI->session->userdata('logged_in')),
                                                'userID'=>element('role', $CI->session->userdata('logged_in')),
                                                'userIP'=>$_SERVER['REMOTE_ADDR']
                                            ));
            }elseif(preg_match('/\bupdate\b/i', $qr_string)) {
                $CI->db->insert('_log_query',array(
                                                "type"=>"update",
                                                "query"=>$query,
                                                "executionTime  "=>$times[$key],
                                                'companyID'=>element('compID', $CI->session->userdata('logged_in')),
                                                'userID'=>element('role', $CI->session->userdata('logged_in')),
                                                'userIP'=>$_SERVER['REMOTE_ADDR']
                                            ));
            }
        }
    }
 
}