<?php  
/**
 * Class to connect Sphinx and run queries on RT Index
 */
class sphinx {
    const HOST = "127.0.0.1";
    const PORT = "22139";
    private $Conn;


  /**
   * Constructor to initialize connection
   * @param type $config
   */
  public function __construct($config = array())
  {
    $this->_initialize($config);
  }
  /**
   * Initialize connection
   * @param type $config
   */
  private function _initialize($config = array())
  {
      $sphinxHost = empty($config['sphinx_host']) ? self::HOST : $config['sphinx_host'];
      $sphinxPort = empty($config['sphinx_port']) ? self::PORT : $config['sphinx_port'];
      try{
        $this->Conn = new PDO( 'mysql:host='.$sphinxHost.';port='.$sphinxPort.';', '', '',array(PDO::ATTR_TIMEOUT => "1", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
      }catch (PDOException $e) {
        $this->Conn = null;
    }
  }
  /**
   * Get Assoc Array of sphinx query
   * @param type $query
   */
  public function getArray($query) {
        try{
            $result = false;
            if($this->Conn){
                $sphinxQuery = $this->Conn->query($query);
                //var_dump($this->Conn->errorInfo());
                if (is_object($sphinxQuery)) {
                    $result = $sphinxQuery->fetchAll(PDO::FETCH_ASSOC);
                }
            }
        } catch (\Exception $ex) {
            //var_dump($ex->getMessage());
        }
        return $result;
  }
  /**
   * Fetch Single column value
   * @param type $query
   * @return type
   */
  public function getColumn($query) {
      try{
            $result = false;
            if($this->Conn){
                $sphinxQuery = $this->Conn->query($query);
                if (is_object($sphinxQuery)) {
                    $result = $sphinxQuery->fetchAll(PDO::FETCH_COLUMN);
                }
            }
        } catch (\Exception $ex) {
            //var_dump($ex->getMessage());
        }
        return $result[0];
        
  }
  
   /**
   * Get Assoc Row of sphinx query
   * @param type $query
   */
  public function getRow($query) {
        try{
            $result = false;
            if($this->Conn){
                $sphinxQuery = $this->Conn->query($query);
                if (is_object($sphinxQuery)) {
                    $result = $sphinxQuery->fetchAll(PDO::FETCH_ASSOC);
                }
            }
        } catch (\Exception $ex) {
            //var_dump($ex->getMessage());
        }
        return isset($result[0]) ? $result[0] : false;
  }
  /**
   * Get query data with Given Facet
   * @param type $query
   * @param type $facet
   * @return type
   */
    public function getDataWithFacet($query,$facet) {
      $rows = $facets = $meta_map = $meta = [];
      try{
        if($this->Conn){
            $stmt = $this->Conn->prepare($query);
            $stmt->execute();
            $rows = $stmt->fetchAll();
            $i = 0;
            while ($stmt->nextRowset()) {
                $facets[$facet[$i]] = $stmt->fetchAll();
                $i++;
            }
        }
        
        // $meta = $meta_map = [];
        if($this->Conn){
            $metaobj = $this->Conn->query("SHOW META");
            if($metaobj){
                $meta = $metaobj->fetchAll();
                foreach ($meta as $m) {
                    $meta_map[$m['Variable_name']] = $m['Value'];
                }   
            }
        }
      } catch (\Exception $ex) {
        //var_dump($ex->getMessage());
      }
      return array("result"=>$rows,"facet"=>$facets,"metaInfo"=>$meta_map,"metaData"=>$meta);
    }
    /**
     * Get quote string
     */
    public function getQuote($str) {
        if($this->Conn){
            return $this->Conn->quote($str);
        }else{
            return addslashes($str);
        }
    }
}