<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Log_underscore_model extends MY_Model{
	function __construct(){
		parent::__construct();
		
		$this->table = "";
		
		$this->join = "";
	}
	
	function sync($params=array()){
		if(isset($params['QUERY']) && strlen($params['QUERY']) > 0){
			switch($this->db->dbdriver){
				case "sqlsrv":
					$sql = "
						SELECT TABLE_NAME
						FROM INFORMATION_SCHEMA.TABLES
						WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '".$this->db->database."' AND TABLE_NAME LIKE 'Log[_][_][_]%'
					";
					$field_name = "TABLE_NAME";
					break;
				case "mysql":
				case "mysqli":
					$sql = "
						SELECT table_name 
						FROM information_schema.tables 
						WHERE table_schema='".$this->db->database."' AND TABLE_NAME LIKE 'Log\_\_\_%'
					";
					$field_name = "table_name";
					break;
			}
			
			if(isset($sql, $field_name)){
				$exec = $this->db->query($sql);
				$schema_array = $exec->result();
				
				$params['QUERY'] = $this->fm->sql_translate("MSSQL", $params['QUERY']);
				
				foreach($schema_array as $schema){
					$table = $schema->$field_name;
					
					unset($data);
					$data['CMD'] = $params['QUERY'];
					if(isset($params['TABLE'])){
						$table_index = in_array($this->db->dbdriver, array("sqlsrv"))? '[TABLE]':'TABLE';
						$data[$table_index] = $params['TABLE'];
					}
					if(isset($params['FKEY'])) $data['FKEY'] = $params['FKEY'];
					if(isset($params['LKEY'])) $data['LKEY'] = $params['LKEY'];
					
					$this->db->insert($table, $data);
				}
			}
		}
	}
}