Exemplo n.º 1
0
 protected function getSQLRelayCursor($sql, $array = null)
 {
     $cur = sqlrcur_alloc($this->_conn);
     if ($array) {
         list($sql, $array) = SQLBind::parseSQL($this->_connectionManagement, $sql, $array);
         sqlrcur_prepareQuery($cur, $sql);
         $bindCount = 1;
         foreach ($array as $key => $value) {
             $field = strval($bindCount++);
             sqlrcur_inputBind($cur, $field, $value);
         }
         $success = sqlrcur_executeQuery($cur);
         sqlrcon_endSession($this->_conn);
     } else {
         $success = sqlrcur_sendQuery($cur, $sql);
         sqlrcon_endSession($this->_conn);
     }
     if (!$success) {
         throw new DatasetException(sqlrcur_errorMessage($cur));
     }
     sqlrcur_lowerCaseColumnNames($cur);
     return $cur;
 }
 /**
  * Log explain of each query in log files after truncating plan_table
  * Only if debugExplain wanted and Oracle type database used
  *
  * @access protected
  * @param  query $sql
  * @see    self::execute()
  * @link   http://www.toutenligne.com/index.php?contenu=sql_explain&menu=sql
  */
 function _logExplain($sql)
 {
     if ($this->_bDebugExplain == true && $this->isOracle()) {
         /** TRUNCATE IN ORDER TO CLEAN PLAN_TABLE
             CAN BE REMOVED IF NO_WAIT ERRORS       **/
         $sSQLExplain = "TRUNCATE TABLE PLAN_TABLE";
         sqlrcur_sendQuery($this->curs_id, $sSQLExplain);
         /** In order to identify connexion file **/
         if ($this->_sFileExplainId == '') {
             $this->_sFileExplainId = date('Y-m-d H:i:s') . '-' . (microtime() + mt_rand(0, 999));
         }
         /** In order identify request in explain **/
         $iTime = microtime() + mt_rand(0, 999);
         /** Calculate execution time of the request on the server **/
         list($usec, $sec) = explode(" ", microtime());
         $time_Start = (double) $usec + (double) $sec;
         /** EXPLAIN QUERY **/
         $query = "EXPLAIN PLAN \r\n\t\t              SET STATEMENT_ID = '" . $iTime . "' \r\n\t\t              FOR " . $sql;
         sqlrcur_prepareQuery($this->curs_id, $query);
         sqlrcur_executeQuery($this->curs_id);
         list($usec, $sec) = explode(" ", microtime());
         $time_end = (double) $usec + (double) $sec;
         $_elapsedTime += $time_end - $time_Start;
         /** GETTING BACK INFORMATIONS **/
         $sSQLExplain = "SELECT  ID, PARENT_ID, OPERATION, OPTIONS, OBJECT_NAME, OBJECT_TYPE, \r\n\t                                COST, CARDINALITY, BYTES, OPTIMIZER\r\n                            FROM    PLAN_TABLE\r\n                            WHERE   STATEMENT_ID = '" . $iTime . "'\r\n                            ORDER   BY id, parent_id";
         sqlrcur_prepareQuery($this->curs_id, $sSQLExplain);
         $iResult = sqlrcur_executeQuery($this->curs_id);
         $iNbFirstResult = sqlrcur_rowCount($this->curs_id);
         $i = 0;
         $aResult = array();
         while ($i < $iNbFirstResult) {
             $aResult[] = sqlrcur_getRowAssoc($this->curs_id, $i);
             $i++;
         }
         /** Writing in log file **/
         $filepath = BASEPATH . '/logs/SQL/Explain/' . $this->_sFileExplainId . '.log';
         if (!($fp = @fopen($filepath, "a"))) {
             return FALSE;
         }
         fwrite($fp, str_replace("\n", ' ', $sql) . ' ( ' . round($_elapsedTime, 4) . " sec )\n");
         foreach ($aResult as $aExplain) {
             $message = $aExplain['ID'] . '|' . $aExplain['PARENT_ID'] . '|' . $aExplain['OPERATION'] . '|' . $aExplain['OPTIONS'] . '|' . $aExplain['OBJECT_NAME'] . '|' . $aExplain['OBJECT_TYPE'] . '|' . $aExplain['COST'] . '|' . $aExplain['CARDINALITY'] . '|' . $aExplain['BYTES'] . '|' . $aExplain['OPTIMIZER'] . "\n";
             flock($fp, LOCK_EX);
             fwrite($fp, $message);
             flock($fp, LOCK_UN);
         }
         fclose($fp);
         @chmod($filepath, 0666);
     }
 }