GBase 8s GCI Programming Interface Guide

Published on 2025-02-28

In enterprise database application development, efficient data access and manipulation are key to boosting application performance. The GBase 8s database system offers the GCI (GBase Client Interface) programming interface, allowing developers to conveniently access and operate databases using the C language. This guide provides a comprehensive overview of using GCI, covering environment setup, program compilation, and how to run GCI programs, enabling developers to quickly master client-side development for GBase 8s databases.

01 Introduction to the Installation Locations of IDS and GCI

1. Install IDS and CSDK (this guide assumes IDS and CSDK are installed together). Example path: /home/gbase

2. Extract the GCI package (no specific location required). Example path: /home/gci

3. Navigate to the etc directory and modify client.ksh

cd /home/gci/etc vim client.ksh

GCICLIENTDIR=/home/gci (GCI path)

GBASEDBTCSDK=/home/gbase (CSDK path)

GBASEDBTSERVER=ol_gbasedbt (instance name)

02 Environment Variables Required for Compiling GCI Programs and Their Meanings

1. export DBUSER=gbasedbt export DBPWD=Big4ifmx

Username and password for connecting to the database. Not required if DBUSER and DBPWD are not used in the code.

2. GBASEDBTDIR= /home/gbase (CSDK installation path)

Other potentially useful environment variables (use as needed):

1. export DBDATE=Y4MD- Sets the date format

2. Sets the character format

3. Sets the path for linking libraries required during compilation

export LD_LIBRARY_PATH=${GBASEDBTDIR}/lib:${GBASEDBTDIR}/lib/esql:${GBASEDBTDIR}/lib/cli

03 CMakeLists Configuration

This section only describes the modifications needed in CMakeLists to add a test case to the GCI demo directory. For example, to add a test case named test.c:

cd /home/gci/demovim CMakeList.txtAdd the following content to CMakeList.txt: # Compile the source file test.c to generate the executable testADD_EXECUTABLE(test test.c)# Specify the libraries that this executable test needs to link againstTARGET_LINK_LIBRARIES(test ${LIB_CLN_NAME})

04 Demo Source Code Analysis

Provides a sample code for a GCI program, including database login and SQL statement execution, and explains the functions of key code segments.

1. Call Interfaces and Flow

2. Demo Example

#include "gci.h" // GCI interface definition fileGCIEnv *envhp = NULL; GCISvcCtx *svchp = NULL;GCIError *errhp = NULL;GCIStmt *stmtp = NULL;// Login to the databaseint logdb(){GCItext *dbname = (GCItext *)"testdb"; // Database name (modify as needed; ensure this database exists in the instance)GCItext *user= (GCItext *)"root"; // Username (modify as needed)GCItext *pswd= (GCItext *)"111111"; // Password (modify as needed)if (GCIEnvCreate(&envhp, GCI_THREADED|GCI_OBJECT,(dvoid *)0,0,0,0,0,(dvoid **)0) != GCI_SUCCESS){printf("GCIEnvCreate: create env handle failed!\n");}if (GCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp,GCI_HTYPE_SVCCTX,0,(dvoid **)0) != GCI_SUCCESS){printf("GCIHandleAlloc: allocate svcctx handle failed!\n");}if (GCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp,GCI_HTYPE_ERROR,0,(dvoid **)0) != GCI_SUCCESS){printf("GCIHandleAlloc: allocate error handle failed!\n");}// Call the login interface to connect to the databaseif (GCILogon(envhp, errhp, &svchp, user, strlen((char *)user), pswd, strlen((char *)pswd), dbname, strlen((char *)dbname) != GCI_SUCCESS){printf("logon database failed!\n");}}// Execute SQL statements{GCIText sql[256] = {0};// Allocate a statement handleif (GCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmtp,GCI_HTYPE_STMT,0,(dvoid **)0) != GCI_SUCCESS){printf("GCIHandleAlloc: allocate stmt handle failed!\n");}// Prepare the SQL statementstrcpy((char *)sql, "create table t_table(id int)");GCIStmtPrepare(stmtp, errhp, sql, strlen((char *)sql), 0 ,0);// Execute the prepared SQL statementGCIStmtExecute(svchp, stmtp, errhp, 1,0, NULL, NULL, GCI_COMMIT_ON_SUCCESS);}

05 Compiling and Running GCI Programs

1. Set Environment Variables

cd /home/gci/demo source /home/gci/etc/client.ksh

If necessary, set the username and password: export DBUSER DBPWD

2. Execute the Script Files clean.sh and build_linux_debug_shared.sh, Then Run make

./clean.sh./build_linux_debug_shared.shmake

3. Source the profile file or ksh file (ol_gbasedbt.ksh) in the IDS installation directory (e.g., /home/gbase)

source ol_gbasedbt.ksh

4. Run the executable file. Example: ./test

Through the detailed introduction provided in this guide, you should now have a comprehensive understanding of how to perform database programming with GCI on GBase 8s databases. GCI offers an efficient and flexible method for database access, helping developers enhance the development efficiency and performance of database applications. We hope this guide serves as a valuable assistant in your database development with GBase 8s.