<commit_before><?php
namespace ToolkitApi;

class odbcsupp
{
    private $last_errorcode = ''; // SQL State
    private $last_errormsg = ''; // SQL Code with message
        
    // 'persistent' is one option
    public function connect($database, $user, $password, $options = null)
    {
        $connectFunc = 'odbc_connect'; // default
        if ($options) {
            if ((isset($options['persistent'])) && $options['persistent']) {
                $connectFunc = 'odbc_pconnect';
            }
        }
        
      // could be connect or pconnect
      $conn = $connectFunc ( $database, $user, $password );
      
      if (is_resource($conn)) {
          return $conn;
      } else {
        $this->setError();
        return false;
      }
    }
    
    public function disconnect( $conn )
    {
        if(is_resource($conn)) {
            odbc_close($conn);
        }
    }
    
    public function getErrorCode()
    {
        return $this->last_errorcode;
    }
    
    // added
    public function getErrorMsg()
    {
        return $this->last_errormsg;
    }
    
    protected function setError($conn = null)
    {
        // set error code and message based on last odbc connection/prepare/execute error.
    
        // @todo: consider using GET DIAGNOSTICS for even more message text:
        // http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
    
        if ($conn) {
            // specific connection resource was provided
            $this->setErrorCode(odbc_error($conn));
            $this->setErrorMsg(odbc_errormsg($conn));
        } else {
            // no specific statemtent. Get last error
            $this->setErrorCode(odbc_error());
            $this->setErrorMsg(odbc_errormsg());
        }
    }
    
    protected function setErrorCode($errorCode)
    {
        $this->last_errorcode = $errorCode;
    }
    
    protected function setErrorMsg($errorMsg)
    {
        $this->last_errormsg = $errorMsg;
    }
    
    /* this function used for special stored procedure call only  */
    public function execXMLStoredProcedure( $conn, $stmt, $bindArray )
    {
        $internalKey= $bindArray['internalKey'];
        $controlKey = $bindArray['controlKey'];
        $inputXml   = $bindArray['inputXml'];
        $outputXml  = $bindArray['outputXml'];
        $disconnect = $bindArray['disconnect'];
    
        $crsr = odbc_prepare ( $conn, $stmt);
        
        if( !$crsr ) { 
            $this->setError($conn);
            return false;
        }
        
        /* extension problem: sends an warning message into the php_log or to stdout 
         * about of number of result sets .  ( switch on return code of SQLExecute() 
         * SQL_SUCCESS_WITH_INFO  */
        $ret = @odbc_execute ( $crsr , array($internalKey, $controlKey, $inputXml ));
        if(!$ret) {
            $this->setError($conn);
            
            return "ODBC error code: " . $this->getErrorCode() . ' msg: ' . $this->getErrorMsg();
        }
        
        //disconnect operation cause crush in fetch ,
        //nothing appears as sql script.
        $row='';
        $outputXML = '';
        if(!$disconnect) {
            while( odbc_fetch_row($crsr)) {
                $tmp = odbc_result($crsr, 1);
                if($tmp) {
                    /*because of some problem in odbc Blob transfering 
                     * shoudl be executed some "clean" in returned data */
                    if(strstr($tmp , "</script>")){
                        $stopFetch = true;
                        $pos = strpos($tmp, "</script>");
                        $pos += strlen("</script>");
                        $row .= substr($tmp,0,$pos);
                        break;
                    } else {
                        $row .=$tmp;
                    }
                }
            }
            $outputXML = $row;
        }
        
        return $outputXML;
    }
    
    public function executeQuery($conn,  $stmt )
    {
        $crsr = odbc_exec($conn, $stmt);
        
        if(is_resource($crsr )) {      
            while( odbc_fetch_row( $crsr )) {  
                $row = odbc_result($crsr, 1);
                
                if(!$row) {
                    break;
                }
                
                $Txt[]=  $row;
            }
        } else {
            $this->setError($conn);
        }
        
        return $Txt;
    }
}<commit_msg>odbcsupp minor refactoring and adding docblocks<commit_after><?php
namespace ToolkitApi;

/**
 * Class odbcsupp
 *
 * @package ToolkitApi
 */
class odbcsupp
{
    private $last_errorcode = '';
    private $last_errormsg = '';

    /**
     * 
     * @todo should perhaps handle this method differently if $options are not passed
     * 
     * @param $database
     * @param $user
     * @param $password
     * @param null $options
     * @return bool|resource
     */
    public function connect($database, $user, $password, $options = null)
    {
        if ($options) {
            if ((isset($options['persistent'])) && $options['persistent']) {
                $conn = odbc_pconnect($database, $user, $password);
            } else {
                $conn = odbc_connect($database, $user, $password);
            }

            if (is_resource($conn)) {
                return $conn;
            }
        }
        
        $this->setError();
        return false;
    }

    /**
     * @param $conn
     */
    public function disconnect($conn)
    {
        if (is_resource($conn)) {
            odbc_close($conn);
        }
    }

    /**
     * set error code and message based on last odbc connection/prepare/execute error.
     * 
     * @todo: consider using GET DIAGNOSTICS for even more message text:
     * http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
     * 
     * @param null $conn
     */
    protected function setError($conn = null)
    {
        // is conn resource provided, or do we get last error?
        if ($conn) {
            $this->setErrorCode(odbc_error($conn));
            $this->setErrorMsg(odbc_errormsg($conn));
        } else {
            $this->setErrorCode(odbc_error());
            $this->setErrorMsg(odbc_errormsg());
        }
    }

    /**
     * @param $errorCode
     */
    protected function setErrorCode($errorCode)
    {
        $this->last_errorcode = $errorCode;
    }

    /**
     * @return string
     */
    public function getErrorCode()
    {
        return $this->last_errorcode;
    }

    /**
     * @param $errorMsg
     */
    protected function setErrorMsg($errorMsg)
    {
        $this->last_errormsg = $errorMsg;
    }

    /**
     * @return string
     */
    public function getErrorMsg()
    {
        return $this->last_errormsg;
    }
    
    /**
     * this function used for special stored procedure call only
     * 
     * @param $conn
     * @param $stmt
     * @param $bindArray
     * @return string
     */
    public function execXMLStoredProcedure($conn, $stmt, $bindArray)
    {
        $crsr = odbc_prepare($conn, $stmt);
        
        if (!$crsr) { 
            $this->setError($conn);
            return false;
        }
        
        // extension problem: sends warning message into the php_log or stdout 
        // about number of result sets. (switch on return code of SQLExecute() 
        // SQL_SUCCESS_WITH_INFO
        if (!@odbc_execute($crsr , array($bindArray['internalKey'], $bindArray['controlKey'], $bindArray['inputXml']))) {
            $this->setError($conn);
            return "ODBC error code: " . $this->getErrorCode() . ' msg: ' . $this->getErrorMsg();
        }
        
        // disconnect operation cause crush in fetch, nothing appears as sql script.
        $row='';
        $outputXML = '';
        if (!$bindArray['disconnect']) {
            while (odbc_fetch_row($crsr)) {
                $tmp = odbc_result($crsr, 1);
                
                if ($tmp) {
                    // because of ODBC problem blob transferring should execute some "clean" on returned data
                    if (strstr($tmp , "</script>")) {
                        $pos = strpos($tmp, "</script>");
                        $pos += strlen("</script>"); // @todo why append this value?
                        $row .= substr($tmp, 0, $pos);
                        break;
                    } else {
                        $row .= $tmp;
                    }
                }
            }
            $outputXML = $row;
        }
        
        return $outputXML;
    }

    /**
     * @param $conn
     * @param $stmt
     * @return array
     */
    public function executeQuery($conn, $stmt)
    {
        $crsr = odbc_exec($conn, $stmt);
        
        if (is_resource($crsr)) {      
            while (odbc_fetch_row($crsr)) {  
                $row = odbc_result($crsr, 1);
                
                if (!$row) {
                    break;
                }
                
                $Txt[]=  $row;
            }
        } else {
            $this->setError($conn);
        }
        
        return $Txt;
    }
}
{'fraction_non_alphanumeric': 0.12544919960797124, 'fraction_numerical': 0.0015245562452357616, 'mean_word_length': 1.4828331981616654, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1423513', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2609, 'n_words': 581}