CCI 프로그램 작성

기본적인 작성 순서는 다음과 같으며, prepared statement 사용을 위해서는 변수에 데이터를 바인딩하는 작업이 추가된다. 이를 예제 1 및 예제 2에 구현하였다. CCI에서는 SELECT_AUTO_COMMIT 파라미터가 ON으로 설정된 상태에서 SELECT 문에 대해서만 자동 커밋 모드를 지원하므로, 그렇지 않은 경우에는 cci_end_tran() 함수를 이용하여 명시적으로 트랜잭션을 커밋 또는 롤백해야 한다. CCI에서의 자동 커밋 모드 설정 및 사용에 관한 상세한 설명은 브로커별 파라미터의 SELECT_AUTO_COMMIT 및 cci_end_tran()를 참고한다.

예제 1

//Example to execute a simple query

#include <stdio.h>

#include "cas_cci.h"  

#define BUFSIZE  (1024)

 

int

main (void)

{

  int con = 0, req = 0, col_count = 0, i, ind;

  int error;

  char *data;

  T_CCI_ERROR cci_error;

  T_CCI_COL_INFO *col_info;

  T_CCI_SQLX_CMD cmd_type;

  char *query = "select * from code";

 

//getting a connection handle for a connection with a server

  con = cci_connect ("localhost", 44000, "demodb", "dba", "");

  if (con < 0)

    {

      printf ("cannot connect to database\n");

      return 1;

    }

 

//preparing the SQL statement

  req = cci_prepare (con, query, 0, &cci_error);

  if (req < 0)

    {

      printf ("prepare error: %d, %s\n", cci_error.err_code,

              cci_error.err_msg);

      goto handle_error;

    }

 

//getting column information when the prepared statement is the SELECT query

  col_info = cci_get_result_info (req, &cmd_type, &col_count);

  if (col_info == NULL)

    {

      printf ("get_result_info error: %d, %s\n", cci_error.err_code,

              cci_error.err_msg);

      goto handle_error;

    }

 

//Executing the prepared SQL statement

  error = cci_execute (req, 0, 0, &cci_error);

  if (error < 0)

    {

      printf ("execute error: %d, %s\n", cci_error.err_code,

              cci_error.err_msg);

      goto handle_error;

    }

  while (1)

    {

 

//Moving the cursor to access a specific tuple of results

      error = cci_cursor (req, 1, CCI_CURSOR_CURRENT, &cci_error);

      if (error == CCI_ER_NO_MORE_DATA)

        {

          break;

        }

      if (error < 0)

        {

          printf ("cursor error: %d, %s\n", cci_error.err_code,

                  cci_error.err_msg);

          goto handle_error;

        }

 

//Fetching the query result into a client buffer

      error = cci_fetch (req, &cci_error);

      if (error < 0)

        {

          printf ("fetch error: %d, %s\n", cci_error.err_code,

                  cci_error.err_msg);

          goto handle_error;

        }

      for (i = 1; i <= col_count; i++)

        {

 

//Getting data from the fetched result

          error = cci_get_data (req, i, CCI_A_TYPE_STR, &data, &ind);

          if (error < 0)

            {

              printf ("get_data error: %d, %d\n", error, i);

              goto handle_error;

            }

          printf ("%s\t|", data);

        }

      printf ("\n");

    }

 

//Closing the request handle

  error = cci_close_req_handle (req);

  if (error < 0)

    {

      printf ("close_req_handle error: %d, %s\n", cci_error.err_code,

              cci_error.err_msg);

      goto handle_error;

    }

 

//Disconnecting with the server

  error = cci_disconnect (con, &cci_error);

  if (error < 0)

    {

      printf ("error: %d, %s\n", cci_error.err_code, cci_error.err_msg);

      goto handle_error;

    }

 

  return 0;

 

handle_error:

  if (req > 0)

    cci_close_req_handle (req);

  if (con > 0)

    cci_disconnect (con, &cci_error);

 

  return 1;

}

예제 2

//Example to execute a query with a bind variable

 

char *query = "select * from nation where name = ?";

  char namebuf[128];

 

//getting a connection handle for a connection with a server

  con = cci_connect ("localhost", 44000, "demodb", "dba", "");

  if (con < 0)

    {

      printf ("cannot connect to database ");

      return 1;

    }

 

//preparing the SQL statement

  req = cci_prepare (con, query, 0, &cci_error);

  if (req < 0)

    {

      printf ("prepare error: %d, %s ", cci_error.err_code,

              cci_error.err_msg);

      goto handle_error;

    }

 

//Binding date into a value

  strcpy (namebuf, "Korea");

  error =

    cci_bind_param (req, 1, CCI_A_TYPE_STR, &namebuf, CCI_U_TYPE_STRING,

                    CCI_BIND_PTR);

  if (error < 0)

    {

      printf ("bind_param error: %d ", error);

      goto handle_error;

    }