<?php
class Qrylog extends Cmanage {
  function __construct(){
    parent::__construct(TB_query_log);

    $this->DB->query_record = false; //query log 기록 금지

    $this->DB->tableExists(TB_query_log);
  }

  public function getQueryList($fields, $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    $orderby="query_date desc";
    $limit=array();

    $rows = $this->getDataList($fields, $where, $orderby, $limit);

    return $rows;
  }

  public function getQueryPage($sqldata, $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }

    $where->add("query_date >= ?", ($sqldata['sdate']?$sqldata['sdate']:_SDATENULL_)." 00:00:00.000");
    $where->add("query_date <= ?", ($sqldata['fdate']?$sqldata['fdate']:_FDATENULL_)." 23:59:59.999");

    if( $sqldata['stxt'] ){
      $subwhere = $where->sub_where("or");
      if( $sqldata['sfld'] ){
        $subwhere->add("{$sqldata['sfld']} like ?", "%{$sqldata['stxt']}%");
      }else{
        $subwhere->add("host_name    like ?", "%{$sqldata['stxt']}%");
        $subwhere->add("db_name      like ?", "%{$sqldata['stxt']}%");
        $subwhere->add("query_string like ?", "%{$sqldata['stxt']}%");
        $subwhere->add("member_id    like ?", "%{$sqldata['stxt']}%");
      }
    }

    $orderby = "query_date desc";
    if( $sqldata['ofld'] && $sqldata['oasc'] ){
      $orderby = "{$sqldata['ofld']} {$sqldata['oasc']}";
    }

    $query = array(
      "mode"     => $sqldata['mode'],
      "viewnum"  => $sqldata['viewnum'],
      "sdate"    => $sqldata['sdate']?:"",
      "fdate"    => $sqldata['fdate']?:"",
      "page"     => $sqldata['page'],
      "sfld"     => $sqldata['sfld'],
      "stxt"     => $sqldata['stxt'],
      "ofld"     => $sqldata['ofld'],
      "oasc"     => $sqldata['oasc'],
    );

    $lists = $this->getDataPage("*", $query, $where ,$orderby);
    return $lists;
  }

  public function getQuery( $cust_id ){
    $urow = Array("cust_id" => $cust_id);
    $where = "";
    $row = $this->getData("*", $urow, $where);
    return $row;
  }

}