<?php  
/**
 * Class to connect Sphinx and run queries on RT Index
 */
class sphinxConn {
    const HOST = "127.0.0.1";// "192.168.30.38";
    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())
    {
        try{
        $sphinxHost = empty($config['sphinx_host']) ? self::HOST : $config['sphinx_host'];
        $sphinxPort = empty($config['sphinx_port']) ? self::PORT : $config['sphinx_port'];
        $this->Conn = new PDO( 'mysql:host='.$sphinxHost.';port='.$sphinxPort.';', '', '',array(PDO::ATTR_TIMEOUT => "1") );
        }catch (\Exception $e){
            var_dump($e->getMessage());
            $this->Conn = false;
        }
    }
  /**
   * Get Assoc Array of sphinx query
   * @param type $query
   */
    public function getArray($query) {
        $result = false;
        if($this->Conn){
            try{
              
              $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 false;
            }
        }
        return $result;
    }
  /**
   * Fetch Single column value
   * @param type $query
   * @return type
   */
    public function getColumn($query) {
        if(!$this->Conn){
            return false;
        }
        try{
              $result = false;
              $sphinxQuery = $this->Conn->query($query);
              if (is_object($sphinxQuery)) {
                  $result = $sphinxQuery->fetchAll(PDO::FETCH_COLUMN);
              }
          } catch (\Exception $ex) {
              var_dump($ex->getMessage());
              return false;
          }
          return $result[0];

    }
  
   /**
   * Get Assoc Row of sphinx query
   * @param type $query
   */
    public function getRow($query) {
        if(!$this->Conn){
            return false;
        }
          try{
              $result = false;
              $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 false;
          }
          return $result[0];
    }
    /**
     * Get quote string
     */
    public function getQuote($str) {
        if(!$this->Conn){
            return false;
        }
        return $this->Conn->quote($str);
    }
    /**
     * Get Data With Meta Info
     * @param type $query
     * @return type
     */
    public function getDataWithMetaInfo($query) {
        if(!$this->Conn){
            return false;
        }
        $rows = $facets = $meta_map = [];
        try{
          $stmt = $this->Conn->prepare($query);
          $stmt->execute();
          $rows = $stmt->fetchAll();
          $meta = $meta_map = [];
          $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 false;
        }
        return array("result"=>$rows,"metaInfo"=>$meta_map);
    }
}
