그누보드 오라클 연동 기본 lib 파일 정보
그누보드 오라클 연동 기본 lib 파일
첨부파일
본문
영카트랑 oracle 이랑 연동해야 할 일이 있어서 간단히 만들어보았습니다.
가능한 영카트 기본 함수에 충실하도록 작성하였습니다.
함수 비교
기존 함수 ==> 오라클용 함수
sql_connect          ==> sql_oci_connect
sql_select_db        == > 없음
sql_query              ==> sql_oci_query
sql_fetch               ==> sql_oci_fetch
sql_fetch_array      ==> sql_oci_fetch_array
sql_free_result       ==>  sql_oci_free_result
- 추가함수
sql_oci_paging
set_lowercase (오라클 쿼리시 db컬럼명이 대문자가 디폴트입니다. 소문자로 변경해줍니다.)
오라클에서 페이징시에 limit를 지원하지 않음으로, 페이징 하는 부분은 따로 함수를 호출해주세요.
기존 그누보드 페이징 
$result = sql_query("select * from board limit $from_record, $rows ");
오라클 사용시 페이징
$result = sql_oci_paging($sql, $from_record, $rows);
* 사용방법
- 오라클에 쿼리를 해야 하는 페이지에 아래와 같이 추가
include_once("$g4[path]/lib/common.oralib.php"); //오라클 공통
sql_oci_connect();
- connection 해제를 위해 하단에 아래와 같이 추가
sql_oci_free_result();
- 오라클db의 인코딩과 그누보드의 인코딩이 틀리다면, connection 할때 인코딩 방법을 지정하세요. utf-8인 경우 AL32UTF8
<?
if (!defined('_GNUBOARD_')) exit;
/*************************************************************************
**
** 오라클 DB 관련 함수 모음
**
*************************************************************************/
$oci_conn;   //db conncion 
$oci_result;  //result set
$oci_stmt; //statement
$oci_error; //error
$oci_fetch_mode = OCI_ASSOC;
// DB 연결
function sql_oci_connect()
{
    global $g4, $oci_conn;
   $oci_conn = oci_connect( "scott", "tiger", "", "인코딩방법");
   return $oci_conn;
}
// oracle 쿼리 처리
function sql_oci_query($sql, $error=TRUE)
{
 global $oci_conn;
 $oci_stmt = oci_parse($oci_conn, $sql); 
 
    if ($error) {
        @oci_execute($oci_stmt, OCI_COMMIT_ON_SUCCESS) or die("<p>$sql<p>" .oci_error($stmt)." : <p>error file : $_SERVER[PHP_SELF]");
    } else {
        @oci_execute($oci_stmt, OCI_COMMIT_ON_SUCCESS);
 }
    return $oci_stmt;
}
// 페이징 쿼리
function sql_oci_paging($sql, $startRow, $rows, $error=TRUE)
{ 
 global $oci_conn;
 $endRow = $startRow + $rows;
 $paging_query = "
        SELECT * 
         FROM ( SELECT TT.*, 
                       rownum+rnum-1 as TOTAL_CNT   
         FROM ( SELECT TT.*, 
                       rownum rnum       
                  FROM (            
      $sql
    ) TT               
    ORDER BY rnum DESC ) TT               
     ORDER BY rnum )                
    WHERE rnum BETWEEN  $startRow AND $endRow
   
  ";
  
 $oci_stmt = oci_parse($oci_conn, $paging_query); 
    if ($error) {
        @oci_execute($oci_stmt, OCI_COMMIT_ON_SUCCESS) or die("<p>$paging_query<p>" .oci_error($stmt)." : <p>error file : $_SERVER[PHP_SELF]");
    } else {
        @oci_execute($oci_stmt, OCI_COMMIT_ON_SUCCESS);
 }
    return $oci_stmt;
}
// 쿼리를 실행한 후 결과값에서 한행을 얻는다.
function sql_oci_fetch($sql, $error=TRUE)
{
    $oci_stmt = sql_oci_query($sql, $error);
    //$row = @sql_fetch_array($result) or die("<p>$sql<p>" . mysql_errno() . " : " .  mysql_error() . "<p>error file : $_SERVER[PHP_SELF]");
    $row = sql_oci_fetch_array($oci_stmt);
    return $row;
}
// 결과값에서 한행 연관배열(이름으로)로 얻는다.
function sql_oci_fetch_array($result)
{
 $row = @oci_fetch_assoc($result);
 set_lowercase($row);
    return $row;
}
function set_lowercase(&$row) {
 if(is_array($row)) {
  foreach ($row as $key => $value) {
   $lowerkey = strtolower($key);
   $row[$lowerkey] = $value;
  }
}
}
// $result에 대한 메모리(memory)에 있는 내용을 모두 제거한다.
// sql_free_result()는 결과로부터 얻은 질의 값이 커서 많은 메모리를 사용할 염려가 있을 때 사용된다.
// 단, 결과 값은 스크립트(script) 실행부가 종료되면서 메모리에서 자동적으로 지워진다.
function sql_oci_free_result()
{
  global $oci_db, $oci_stmt;
  if($oci_stmt) oci_free_statement($oci_stmt);
  if($oci_conn) {
   oci_commit($oci_conn);
   oci_close($oci_conn);
  }
}
?>
4
댓글 4개


기회되면 적용해봐야겠네요...
