Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page: CUBRID 8.2.1 |  CUBRID 8.3.0 |  CUBRID 8.3.1 |  CUBRID 8.4.0 |  CUBRID 8.4.1 | 

CCI Sample

Introduction

The sample program is to create a simple application using CCI through the connection to the demodb database deployed by default during the CUBRID installation. Follow the processes of connection to CAS, query preparation, query execution, response handling and disconnection in the sample. The sample is created in a way that uses dynamic links on Linux.

The following is schema information of the olympic table in the demodb database used in the sample.

csql> ;sc olympic

=== <Help: Sechma of a Class> ===

 <Class Name>

    olympic

 <Attributes>

    host_year                 INTEGER NOT NULL

    host_nation               CHARACTER VARYING(40) NOT NULL

    host_city                 CHARACTER VARYING(20) NOT NULL

    opening_date              DATE NOT NULL

    closing_date              DATE NOT NULL

    mascot                    CHARACTER VARYING(20)

    slogan                    CHARACTER VARYING(40)

    introduction              CHARACTER VARYING(1500)

 

 <Constraints>

    PRIMARY KEY pk_olympic_host_year ON olympic (host_year)

Preparation

Make sure that the demodb database and the Broker are running before you execute the sample program. You can start the demodb database and the Broker by executing the cubrid utility.

The following example shows how to run a database server and broker by executing the cubrid utility.

[tester@testdb ~]$ cubrid server start demodb

@ cubrid master start

++ cubrid master start: success

@ cubrid server start: demodb

 

This may take a long time depending on the amount of recovery works to do.

 

CUBRID 2008 R4.0

 

++ cubrid server start: success

[tester@testdb ~]$ cubrid broker start

@ cubrid broker start

++ cubrid broker start: success

Build

With the program source and the Makefile ready, executing "make" will create an executable file called "test." If you use a static library, there is no need to deploy additional files and the execution will be faster. However, it increases the program size and memory usage. If you use a dynamic library, there will be some performance overhead, but the program size and memory usage can be optimized.

The following is a command line example. It builds the test program using the dynamic library instead of "make" on Linux.

cc -o test test.c -I$CUBRID/include -L$CUBRID/lib -lnsl -lcascci

Sample Code

#include <stdio.h>

#include <cas_cci.h>

char *cci_client_name = "test";

int main (int argc, char *argv[])

{

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

    T_CCI_ERROR error;

    T_CCI_COL_INFO *res_col_info;

    T_CCI_SQLX_CMD cmd_type;

    char *buffer, db_ver[16];

    printf("Program started!\n");

    if ((con=cci_connect("localhost", 30000, "demodb", "PUBLIC", ""))<0) {

        printf( "%s(%d): cci_connect fail\n", __FILE__, __LINE__);

        return -1;

    }

   

    if ((res=cci_get_db_version(con, db_ver, sizeof(db_ver)))<0) {

        printf( "%s(%d): cci_get_db_version fail\n", __FILE__, __LINE__);

        goto handle_error;

    }

    printf("DB Version is %s\n",db_ver);

    if ((req=cci_prepare(con, "select * from event", 0,&error))<0) {

        printf( "%s(%d): cci_prepare fail(%d)\n", __FILE__, __LINE__,error.err_code);

        goto handle_error;

    }

    printf("Prepare ok!(%d)\n",req);

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

    if (!res_col_info) {

        printf( "%s(%d): cci_get_result_info fail\n", __FILE__, __LINE__);

        goto handle_error;

    }

   

    printf("Result column information\n"

           "========================================\n");

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

        printf("name:%s  type:%d(precision:%d scale:%d)\n",

            CCI_GET_RESULT_INFO_NAME(res_col_info, i),

            CCI_GET_RESULT_INFO_TYPE(res_col_info, i),

            CCI_GET_RESULT_INFO_PRECISION(res_col_info, i),

            CCI_GET_RESULT_INFO_SCALE(res_col_info, i));

    }

    printf("========================================\n");

    if ((res=cci_execute(req, 0, 0, &error))<0) {

        printf( "%s(%d): cci_execute fail(%d)\n", __FILE__, __LINE__,error.err_code);

        goto handle_error;

    }

    if ((res=cci_fetch_size(req, 100))<0) {

        printf( "%s(%d): cci_fetch_size fail\n", __FILE__, __LINE__);

        goto handle_error;

    }

   

    while (1) {

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

        if (res == CCI_ER_NO_MORE_DATA) {

            printf("Query END!\n");

            break;

        }

        if (res<0) {

            printf( "%s(%d): cci_cursor fail(%d)\n", __FILE__, __LINE__,error.err_code);

            goto handle_error;

        }

       

        if ((res=cci_fetch(req, &error))<0) {

            printf( "%s(%d): cci_fetch fail(%d)\n", __FILE__, __LINE__,error.err_code);

            goto handle_error;

        }

       

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

            if ((res=cci_get_data(req, i, CCI_A_TYPE_STR, &buffer, &ind))<0) {

                printf( "%s(%d): cci_get_data fail\n", __FILE__, __LINE__);

                goto handle_error;

            }

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

        }

        printf("\n");

    }

    if ((res=cci_close_req_handle(req))<0) {

           printf( "%s(%d): cci_close_req_handle fail", __FILE__, __LINE__);

           goto handle_error;

    }

    if ((res=cci_disconnect(con, &error))<0) {

          printf( "%s(%d): cci_disconnect fail(%d)", __FILE__, __LINE__,error.err_code);

          goto handle_error;

    }

    printf("Program ended!\n");

    return 0;

   

handle_error:

    if (req > 0)

        cci_close_req_handle(req);

    if (con > 0)

        cci_disconnect(con, &error);

    printf("Program failed!\n");

    return -1;

}