/* -------------------------------------------------------------------------- ISAM to SQL Tutorial The goal of this tutorial is to introduce the most basic c-tree Plus ISAM API to accomplish creating and manipulating a table through the c-tree Server. From a functional point of view this application will perform the following: 1. Logon onto a session 2. Add 1 table with some fields 3. Populate the table with a few records 4. Display the contents of the table Once this table has been built, it is also ready to use in c-treeSQL. Use the c-treeSQL Import Utility, ctsqlimp, to link the table to your c-treeSQL Server. You will then be able to query your data through c-treeSQL statements! This example creates and stores a UUID value for a list of names, and stores them in a c-tree Plus data file. Several concepts are demonstrated. 1. How to build an ISAM table compatible with c-treeSQL. 2. How to create and store a universally unique identifier (UUID) with c-tree. 3. How to properly construct and use a CT_ARRAY field for later import to c-treeSQL. The table consists of 3 fields, a 'pad' field, a GUID field, and a name field. In this example, the GUID field demonstrates how to properly use a CT_ARRAY field as a c-treeSQL BINARY field. Notice in particular, the added four byte length header to the field. While this value is transparent to the c-treeSQL user, it is imperative that this header be properly constructed with the correct value to be imported into c-treeSQL. -------------------------------------------------------------------------- */ /** Preprocessor definitions and includes **/ #include #include #include "ctreep.h"/* All necessary c-tree Plus headers */ #define END_OF_FILE INOT_ERR /** Global declarations **/ /* Data File Number */ COUNT guid_no; ISEG guid_seg = { 12,16,INTSEG }; IIDX guid_idx = { 16, /* Length of index */ 0, /* key type */ 0, /* Dup Flag */ 1, /* NULL key flag */ 0, /* Empty Char */ 1, /* Number of segments */ &guid_seg, /* Pointer to Segment Array */ NULL, /* Index Name */ NULL, /* Optional Index Name */ NULL, /* Alternate Collating Sequence */ NULL /* Option pointer to pad byte */ }; /* IFIL Definitions */ IFIL guid_dat = { "GUID8", /* data file name ("dat" is always assumed)*/ -1, /* data file number */ 52, /* data record length */ 8192, /* data extension size */ ctSHARED, /* data file mode */ 1, /* number of indices */ 8192, /* index extension size */ ctSHARED, /* index file mode */ &guid_idx, /* pointer to index array */ "Delflag", /* pointer to first field name (r-tree) */ "Buffer" /* pointer to last field name (r-tree) */ }; /* Xtd8 File Definitions - we will use HUGE files in this example */ XCREblk xcreblk[2] = { { ctFILEPOS8 , 0, 0, 0, 0, 1048576}, { ctFILEPOS8 , 0, 0, 0, 0, 1048576} }; /* Data Record Definitions */ DATOBJ doda[] = { {"pad",NULL,CT_FSTRING,8}, {"uuid",NULL,CT_ARRAY,20}, {"name",NULL,CT_FSTRING,24} }; /* Names for records */ COUNT name_count = 6; typedef struct { TEXT *name; } name_text; name_text name_list[]= { (pTEXT) "Craig", (pTEXT) "Ray", (pTEXT) "Jeff", (pTEXT) "Jon", (pTEXT) "Randal", (pTEXT) "Marco" }; /** Function declarations **/ #ifdef PROTOTYPE VOID initialize(void), define(void), manage(void), done(void); VOID Add_Records(void), Display_Records(void), Delete_Records(void); VOID doError(TEXT *); #else VOID initialize(), define(), manage(), done(); VOID Add_Records(), Display_Records(), Delete_Records(); VOID doError(); #endif /************************************************************************ * main() - The main() function implements the concept of * * "Init, define, manage and you're done..." * * * ************************************************************************/ #ifdef PROTOTYPE NINT main (NINT argc, pTEXT argv[]) #else NINT main (argc, argv) NINT argc; pTEXT argv[]; #endif { initialize(); define(); manage(); done(); getchar(); exit(0); } /************************************************************************ * initialize() - Perform the minimum requirement of logging onto * * the c-tree Server * * * ************************************************************************/ #ifdef PROTOTYPE VOID initialize(VOID) #else VOID initialize() #endif { COUNT retval=0; #ifdef ctThrds NINT trc; #endif ctrt_printf("INIT\n"); /* Initialize c-tree Plus and log on to Server */ ctrt_printf("\tLogon to Session...\n"); #ifdef ctThrds if (trc = ctThrdInit(3, 0L, NULL)) { ctrt_printf("\nERROR-> initialize(): ctThrdInit() \n"); ctrt_printf("\nerror = %d\n", trc); ctrt_printf("*** Execution aborted *** \nPress Enter key to exit..."); getchar(); exit(0); } #endif if (retval = InitISAMXtd(16, 16, 16, 16, 0, "ADMIN", "ADMIN", "FAIRCOMS")) doError("initialize(): InitISAMXtd()"); } /************************************************************************ * define() - Open the data file, if it exists, otherwise create and * * re-Open the table. * * * ************************************************************************/ #ifdef PROTOTYPE VOID define(VOID) #else VOID define() #endif { ctrt_printf("DEFINE\n"); /** Open data file **/ ctrt_printf("\tOpen Data File...\n"); if (OpenIFile(&guid_dat)) { /** Create Data File **/ if (CreateIFileXtd8(&guid_dat, NULL, NULL, 0, NULL, NULL, xcreblk)) doError("define(); CreateIFileXtd8()"); guid_no = guid_dat.tfilno; if (PutDODA(guid_no, doda, (UCOUNT) 3)) doError("define(); PutDODA 8()"); CloseIFile(&guid_dat); if (OpenIFile(&guid_dat)) doError("define(); Re-OpenIFileXtd8()"); } guid_no = guid_dat.tfilno; } /************************************************************************ * manage() - This function performs simple record functions of add, * * delete, and gets * * * ************************************************************************/ #ifdef PROTOTYPE VOID manage(VOID) #else VOID manage() #endif { ctrt_printf("MANAGE\n"); Delete_Records(); /* delete any existing records */ Add_Records(); /* populate the table with data */ Display_Records(); /* show contents of table */ } /************************************************************************ * Add_Records() - This function adds records to a table in the * * database from a static structure called * * RECORD_DATA * * * ************************************************************************/ #ifdef PROTOTYPE VOID Add_Records(VOID) #else VOID Add_Records() #endif { VRLEN offset; TEXT inpbuf[256]; TEXT name_buf[24]; long length = 16; COUNT i = 0; #ifdef WIN32 GUID new_uuid; #else uuid_t new_uuid; #endif ctrt_printf("\tAdd Records...\n"); /* Add records to table */ for (i=0;i %s \n", mesg); ctrt_printf("\nisam_err = %d, isam_fil = %d, sysiocod = %d\n", isam_err, isam_fil, sysiocod); ctrt_printf("*** Execution aborted *** \nPress Enter key to exit..."); getchar(); exit(0); }