Subversion Repositories Sigmater

Rev

Blame | Last modification | View Log | Download | RSS feed

<?php

function db_connect ($user, $pwd, $alias) {     
        
        global $ocierr;
        
        $dbconn = OCINLogon($user, $pwd, $alias);
        
        $ocierr = OCIError();
        
        return $dbconn;
  
}

function fetch_into_list ($stmt,$assoc) {
                
        $arr_bid_list = '';
        
    if ($assoc) {
        
        while (OCIFetchInto($stmt, $arr, OCI_ASSOC+OCI_RETURN_NULLS)) {
                $arr_bid_list[] = $arr;
        }
    }
    else {

        while (OCIFetchInto($stmt, $arr, OCI_RETURN_NULLS)) {
                $arr_bid_list[] = $arr;
        }
    }

    return $arr_bid_list;
}

function exec_sql($conn, $sql, $autocommit = 0, $options = '') {
                
        global $ocierr;
        
        $ocierr = false;
        
        if (is_array($options) && isset($options['INS_BLOB'])) {
                
                global $GL_OCI_LOB;
                
                $GL_OCI_LOB = OCINewDescriptor($conn, OCI_D_LOB);
                
                $sql .= ' returning '.$options['INS_BLOB'].' into :oci_lob';
        }
        
    if (!($dbstmt = @OCIParse($conn, $sql))) {

                $msg = ' OCIParse fallita';
                
                $ocierr = array('message' => $msg, 'code' => 'OCIPARSE', 'sqltext' => $sql);
        
        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
                return array(false, $msg.': '.$sql);
        else
                return false;
    }
        
        if (isset($GL_OCI_LOB)) {
                
                OCIBindByName($dbstmt, ':oci_lob', $GL_OCI_LOB, -1, OCI_B_BLOB);
        }
        
    if ($autocommit == 1)
                $ret = @OCIExecute($dbstmt, OCI_COMMIT_ON_SUCCESS);
    else
        $ret = @OCIExecute($dbstmt, OCI_DEFAULT);
        
    if (!$ret) {

        $ocierr = OCIError($dbstmt);
                
                if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
                        return array(false, $ocierr['message']);
                else
                return false;
    }

        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
        return array($dbstmt, '');
    else        
        return $dbstmt;
}

function exec_select($conn, $sql) {

        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION) {

                list($stmt, $err) = exec_sql($conn, $sql);
        
                if ($stmt === false) {
                        return array('', $err);
                }
        
                $nrighe = @OCIFetchStatement($stmt, $result);
        
                if ($nrighe == 0)
                        return array('', 'NODATA');
                else
                        return array($result, '');              

        }
        else {

        $stmt = exec_sql($conn, $sql);

        if ($stmt === false)
                return false;

        $nrighe = @OCIFetchStatement($stmt, $result);

        if ($nrighe == 0)
                        return 'NODATA';
        else
            return $result;                     
        }                   
}

function oracle_date($formato = '') {

    global $conn;

    $f = ($formato == '') ? 'dd/mm/yyyy hh24:mi:ss' : $formato;

    $sql = "select to_char(sysdate, '$f') A from dual";

    $stmt = exec_sql($conn, $sql, 0);

    if(!$stmt) {

        return false;
    }
    else {
                
                OCIFetch($stmt);
        $date = OCIResult($stmt, 'A');
        @OCIFreeStatement($stmt);
        return $date;
    }
}

function exec_select_format($conn, $sql, $assoc = false) {

        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION) {

                list($stmt, $err) = exec_sql($conn, $sql);
        
                if ($stmt === false)
                        return array('', $err);
        
                $result = fetch_into_list ($stmt, $assoc);
        
                if ($result == '')
                        return array('', 'NODATA');
                else
                        return array($result, '');
        }
        else {
                
        $stmt = exec_sql($conn, $sql);

        if ($stmt === false)
            return false;

        $result = fetch_into_list ($stmt, $assoc);

        OCIFreeStatement($stmt);

        if ($result == '')
            return 'NODATA';
        else
            return $result;
        }
}

// Funzione By Matteo Merani per Apache 2
function exec_sql_blob($conn, $sql, $autocommit = 0, $options,$file_blob) {

  global $ocierr;
  $ocierr = false;

  if (is_array($options) && isset($options['INS_BLOB'])) {
        $GL_OCI_LOB = OCINewDescriptor($conn, OCI_D_LOB);
        $sql .= ' returning '.$options['INS_BLOB'].' into :oci_lob';
  }

  if (!($dbstmt = OCIParse($conn, $sql))) {
        $msg = ' OCIParse fallita';
        $ocierr = array('message' => $msg, 'code' => 'OCIPARSE', 'sqltext' => $sql);
        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
              return array(false, $msg.': '.$sql);
        else
               return false;
  }

  if (isset($GL_OCI_LOB)) {
        OCIBindByName($dbstmt, ':oci_lob', $GL_OCI_LOB, -1, OCI_B_BLOB);

  }
  if ($autocommit == 1)
        $ret = @OCIExecute($dbstmt, OCI_COMMIT_ON_SUCCESS);
  else
        $ret = @OCIExecute($dbstmt, OCI_DEFAULT);

  if (!$ret) {
        $ocierr = OCIError($dbstmt);
        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
                return array(false, $ocierr['message']);
        else
               return false;
  }
  if(strlen($file_blob)>0){
        if (isset($GL_OCI_LOB)) {
                if (!$GL_OCI_LOB->save($file_blob)) {
                $msg = ' Salvataggio Blob fallito';
                $ocierr = array('message' => $msg, 'code' => 'SAVEBLOB', 'sqltext' => $sql);
                return false;
                }
        }
  }

  // liberazione della risorsa
  if (isset($GL_OCI_LOB)) {
        $GL_OCI_LOB->free();
  }
  
  if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
        return array($dbstmt, '');
   else
        return $dbstmt;
}
  
// Funzione By Matteo Merani per Apache 2
function exec_sql_blob_file($conn, $sql, $autocommit = 0, $options,$file_blob) {

  global $ocierr;
  $ocierr = false;

  if (is_array($options) && isset($options['INS_BLOB'])) {
        $GL_OCI_LOB = OCINewDescriptor($conn, OCI_D_LOB);
        $sql .= ' returning '.$options['INS_BLOB'].' into :oci_lob';
  }

  if (!($dbstmt = OCIParse($conn, $sql))) {
        $msg = ' OCIParse fallita';
        $ocierr = array('message' => $msg, 'code' => 'OCIPARSE', 'sqltext' => $sql);
        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
              return array(false, $msg.': '.$sql);
        else
               return false;
  }

  if (isset($GL_OCI_LOB)) {
        OCIBindByName($dbstmt, ':oci_lob', $GL_OCI_LOB, -1, OCI_B_BLOB);

  }
  if ($autocommit == 1)
        $ret = @OCIExecute($dbstmt, OCI_COMMIT_ON_SUCCESS);
  else
        $ret = @OCIExecute($dbstmt, OCI_DEFAULT);

  if (!$ret) {
        $ocierr = OCIError($dbstmt);
        if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
                return array(false, $ocierr['message']);
        else
               return false;
  }
  if (isset($GL_OCI_LOB)) {
        if (!$GL_OCI_LOB->savefile($file_blob)) {
        $msg = ' Salvataggio Blob fallito';
        $ocierr = array('message' => $msg, 'code' => 'SAVEBLOB', 'sqltext' => $sql);
        return false;
        }
  }

  // liberazione della risorsa
  if (isset($GL_OCI_LOB)) {
        $GL_OCI_LOB->free();
  }

  if (defined('OCI_DB_OLD_VERSION') && OCI_DB_OLD_VERSION)
        return array($dbstmt, '');
   else
        return $dbstmt;
}
?>