CCI 프로그램 작성

기본적인 작성 순서는 다음과 같으며, prepared statement 사용을 위해서는 변수에 데이터를 바인딩하는 작업이 추가된다. 이를 예제 1 및 예제 2에 구현하였다.

브로커 파라미터인 CCI_DEFAULT_AUTOCOMMIT으로 응용 프로그램 시작 시 자동 커밋 모드의 기본값을 설정할 수 있으며, 브로커 파라미터 설정을 생략하면 기본값은 ON이다. 응용 프로그램 내에서 자동 커밋 모드를 변경하려면 cci_set_autocommit() 함수를 이용하며, 자동 커밋 모드가 OFF이면 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", 33000, "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", 33000, "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;

    }

예제 3

#include <stdio.h>

#include "cas_cci.h"

 

//Example to use connection/statement pool in CCI

int main ()

{

  T_CCI_PROPERTIES *ps = NULL;

  T_CCI_DATASOURCE *ds = NULL;

  T_CCI_ERROR err;

  T_CCI_CONN cons[20];

  int rc = 1, i;

 

  ps = cci_property_create ();

  if (ps == NULL)

    {

      fprintf (stderr, "Could not create T_CCI_PROPERTIES.\n");

      rc = 0;

      goto cci_pool_end;

    }

 

  cci_property_set (ps, "user", "dba");

  cci_property_set (ps, "url", "cci:cubrid:localhost:33000:demodb:::");

  cci_property_set (ps, "pool_size", "10");

  cci_property_set (ps, "max_wait", "1200");

  cci_property_set (ps, "pool_prepared_statement", "true");

  cci_property_set (ps, "default_autocommit", "false");

  cci_property_set (ps, "default_isolation", "TRAN_REP_CLASS_UNCOMMIT_INSTANCE");

  cci_property_set (ps, "default_lock_timeout", "10");

  cci_property_set (ps, "login_timeout", "300000");

  cci_property_set (ps, "query_timeout", "3000");

 

  ds = cci_datasource_create (ps, &err);

  if (ds == NULL)

    {

      fprintf (stderr, "Could not create T_CCI_DATASOURCE.\n");

      fprintf (stderr, "E[%d,%s]\n", err.err_code, err.err_msg);

      rc = 0;

      goto cci_pool_end;

    }

 

  for (i = 0; i < 3; i++)

    {

      cons[i] = cci_datasource_borrow (ds, &err);

      if (cons[i] < 0)

        {

          fprintf (stderr,

                   "Could not borrow a connection from the data source.\n");

          fprintf (stderr, "E[%d,%s]\n", err.err_code, err.err_msg);

          continue;

        }

      // put working code here.

      cci_work (cons[i]);

    }

 

  sleep (1);

 

  for (i = 0; i < 3; i++)

    {

      if (cons[i] < 0)

        {

          continue;

        }

      cci_datasource_release (ds, cons[i], &err);

    }

cci_pool_end:

  cci_property_destroy (ps);

  cci_datasource_destroy (ds);

 

  return 0;

}

 

// working code

int cci_work (T_CCI_CONN con)

{

  T_CCI_ERROR err;

  char sql[4096];

  int req, res, error, ind;

  int data;

 

  cci_set_autocommit (con, CCI_AUTOCOMMIT_TRUE);

  cci_set_lock_timeout (con, 100, &err);

  cci_set_isolation_level (con, TRAN_REP_CLASS_COMMIT_INSTANCE, &err);

 

  error = 0;

  snprintf (sql, 4096, "SELECT host_year FROM record WHERE athlete_code=11744");

  req = cci_prepare (con, sql, 0, &err);

  if (req < 0)

    {

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

      return error;

    }

 

  res = cci_execute (req, 0, 0, &err);

  if (res < 0)

    {

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

      goto cci_work_end;

    }

 

  while (1)

    {

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

      if (error == CCI_ER_NO_MORE_DATA)

        {

          break;

        }

      if (error < 0)

        {

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

          goto cci_work_end;

        }

 

      error = cci_fetch (req, &err);

      if (error < 0)

        {

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

          goto cci_work_end;

        }

 

      error = cci_get_data (req, 1, CCI_A_TYPE_INT, &data, &ind);

      if (error < 0)

        {

          printf ("get data error: %d\n", error);

          goto cci_work_end;

        }

      printf ("%d\n", data);

    }

 

  error = 1;

cci_work_end:

  cci_close_req_handle (req);

  return error;

}