Chapter 7. Utility APIs

Table of Contents

7.1. Header File
7.2. Structure
7.3. Utility API
7.3.1. TBConnect
7.3.2. TBDisconnect
7.3.3. TBExport
7.3.4. TBImport

Tibero provides utility-related C/C++ functions. Application developers can use the utility API to call utilities, which were previously executed at the command prompt, within application programs.

7.1. Header File

Utility API uses the following header files.

Header File NameDescription
tbutil.h

Declares utility API and defines related structures.

Defines the following structures:

  • struct sqlstr

  • struct TBExpImpMeta

  • struct TBExportIn

  • struct TBExportOut

  • struct TBExportStruct

  • struct TBImportIn

  • struct TBImportOut

  • struct TBImportStruct

sqlca.h

Declares the structure for utility errors to send to the application program.

Declares the following structure:

  • struct sqlca

sqlcli.hODBC standard header file. Defines standard API and macros.

7.2. Structure

Utility API can define the following structures:

  • sqlstr structure

    FieldUsageTypeDescription
    lengthInputSQLSMALLINT

    String length.

    dataInputSQLCHAR *

    String pointer.

  • TBExpImpMeta structure

    FieldUsageTypeDescription
    fieldTermInputSQLCHAR *

    Column (field) separator string.

    fieldTermLenInputSQLSMALLINT

    Column (field) separator string length.

    lineTermInputSQLCHAR *

    Record separator string.

    lineTermLenInputSQLSMALLINT

    Record separator string length.

    enclStartInputSQLCHAR *

    Start string of a data column.

    If set to a double quote ("), it is appended at the start of the column data.

    enclStartLenInputSQLSMALLINT

    Start string length.

    enclLenInputSQLCHAR *

    End string of a data column.

    If set to a double quote ("), it is appended to the end of the column data.

    enclEndLenInputSQLSMALLINT

    End string length.

    escapeInputSQLCHAR *

    ESCAPE string for column data.

    This is only used in TBImport structure.

    escapeLenInputSQLSMALLINT

    ESCAPE string length.

    This is used only in TBImport structure.

  • TBExportIn structure

    FieldUsageTypeDescription
    iMetaInputTBExpImpMeta *

    Pointer for TBExpImpMeta structure.

    Points to the input meta data required to extract data.

  • TBExportOut structure

    FieldUsageTypeDescription
    oRowsExportedOutputSQLINTEGERNumber of records extracted from a data file.
  • TBExportStruct structure

    FieldUsageTypeDescription
    piDataFileNameInputSQLCHAR *

    Path name of the file to store extracted data.

    iDataFileNameLenInputSQLSMALLINT

    Data file name length.

    When a file name ends with NULL, it is SQL_NTS.

    piActionStringInputsqlstr *

    SELECT statement to extract data from a table or view. The extracted data is saved in the column order of the SELECT statement.

    iFileTypeInputSQLSMALLINT

    Currently only SQL_DEL, which uses column separator, can be used.

    piMsgFileNameInputSQLCHAR *

    Name of the message file for storing errors and warnings that occur while extracting data and other useful information.

    iMsgFileNameLenInputSQLSMALLINT

    Length of the message file name.

    When the file ends with NULL, it is SQL_NTS.

    piExportInfoInInputTBExportIn *

    Pointer for TBExportIn structure.

    Points to the input meta data required to extract data.

    piExportInfoOutInputTBExportOut *

    Pointer for TBExportOut structure.

    Stores the output that occurred after extracting data.

  • TBImportIn structure

    FieldUsageTypeDescription
    iMetaInputTBExpImpMeta *

    Pointer for TBExpImpMeta structure.

    Points to the input meta data required to load data.

    iRowCountInputSQLINTEGER

    Number of records to load.

    If set to 0, all records in the data file are loaded.

    iSkipCountInputSQLINTEGER

    Number of records to skip when loading data.

    iCommitCountInputSQLINTEGER

    Amount of data to commit during data loading. Due to performance issues, the actual number of committed records may not be same as this value.

    iErrorCountInputSQLINTEGER

    Allowed maximum number of error records. If the number of error records created exceeds this limit while loading data, loading is suspended.

  • TBImportOut structure

    FieldUsageTypeDescription
    oRowsReadOutputSQLINTEGER

    Number of records read from a data file.

    oRowsSkippedOutputSQLINTEGER

    Number of records skipped from a data file.

    oRowsInsertedOutputSQLINTEGER

    Number of records inserted into the table or view.

    oRowsUpdatedOutputSQLINTEGER

    Number of records updated in the table or view.

    oRowsRejectedOutputSQLINTEGER

    Number of records that failed to load.

    oRowsCommittedOutputSQLINTEGER

    Number of records that were successfully committed.

  • TBImportStruct structure

    FieldUsageTypeDescription
    piDataFileNameInputSQLCHAR *

    Path of the data file to load.

    iDataFileNameLenInputSQLSMALLINT

    Data file name length.

    When the file name ends with NULL, it is SQL_NTS.

    piActionStringInputsqlstr *

    Column and table to load and detailed meta data.

    Uses the same string format as the tbLoader's control file. Refer to the control file format for tbLoader for more information.

    iFileTypeInputSQLSMALLINT

    Currently, only SQL_DEL, which uses a column separator, can be used.

    piMsgFileNameInputSQLCHAR *

    Name of the message file for storing errors and warnings that occur while extracting data and other useful information.

    iMsgFileNameLenInputSQLSMALLINT

    Length of the message file name.

    When the file name ends with NULL, it is SQL_NTS.

    piBadFileNameInputSQLCHAR *

    Error file that contains errors that occurred while loading data.

    iBadFileNameLenInputSQLSMALLINT

    Error file name length.

    When the file name ends with NULL, it is SQL_NTS.

    iDPLInputBOOL

    Option to use direct path load when loading data.

    iTrailNullColsInputBOOLOption to bind the last column value that is empty to NULL.
    piImportInfoInInputTBImportIn *

    Pointer for TBImportIn structure.

    Points to the input meta data required to load data.

    piImportInfoOutInputTBImportOut *

    Pointer for TBImportOut structure.

    Imports the output that occurred after loading data.

7.3. Utility API

The following is a list of utility API.

Function TypeFunctionHeader File
Database ConnectionTBConnecttbutil.h
Database DisconnectionTBDisconnecttbutil.h
Data ExtractionTBExporttbutil.h
Data LoadingTBImporttbutil.h

7.3.1. TBConnect

Connects to Tibero server with database connection information (SID, user name, and password).

A detailed description of the TBConnect function follows:

  • Syntax

    SQLRETURN SQL_API
    TBConnect(SQLCHAR *dnsname, SQLCHAR *username, SQLCHAR *pwd, 
              struct sqlca *pSqlca);
  • Parameter

    ParameterUsageDescription
    dnsnameInputSID set in the tbdsn.tbr (or tbnet_alias.tbr) file.
    usernameInputUser name.
    pwdInputPassword.
    pSqlcaOutputWhen the return code is not SQL_SUCCESS, contains utility error logs.
  • Return code

    Return CodeDescription
    SQL_SUCCESSCompleted successfully.
    SQL_SUCCESS_WITH_INFOCompleted successfully with warning message(s).
    SQL_ERRORCritical error has occurred.
  • Related function

    TBDisconnect

7.3.2. TBDisconnect

Disconnects a Tibero server with the database connection information (SID).

A detailed description of the TBDisconnect function follows:

  • Syntax

    SQLRETURN SQL_API
    TBDisconnect(SQLCHAR *dnsname, struct sqlca *pSqlca);
  • Parameter

    ParameterUsageDescription
    dnsnameInputSID set in the tbdsn.tbr(or tbnet_alias.tbr) file.
    pSqlcaOutputWhen the return code is not SQL_SUCCESS, contains utility error logs.
  • Return code

    Return CodeDescription
    SQL_SUCCESSCompleted successfully.
    SQL_SUCCESS_WITH_INFOCompleted successfully with warning message(s).
    SQL_ERRORCritical error has occurred.
  • Related function

    TBConnect

7.3.3. TBExport

Extracts data from the database to an external file. The file displays the extracted data as text. Columns and records are extracted by using column and record separators. The file is extracted as the data file type of tbLoader. The data can be specified with a SELECT statement, and this requires SELECT permissions on the target tables or views.

A detailed description of the TBExport follows:

  • Syntax

    SQLRETURN SQL_API
    TBExport(SQLINTEGER versionNumber, TBExportStruct *pParamStruct,
             struct sqlca *pSqlca);
  • Parameter

    ParameterUsageDescription
    versionNumberInput

    Version number of the utility library needed for backward-compatibility.

    Current version number is 1.

    pParamStructInput Output

    Specifies the data to extract as input and receives the result as output.

    Refer to "TBExportStruct structure" for more information.

    pSqlcaOutputWhen the return code is not SQL_SUCCESS, contains utility error logs.
  • Return code

    Return CodeDescription
    SQL_SUCCESSCompleted successfully.
    SQL_SUCCESS_WITH_INFOCompleted successfully with warning message(s).
    SQL_ERRORCritical error has occurred.
  • Example

    #include "tbutil.h"
    
    #define DNS_NAME  "DEFAULT"
    #define USER_NAME "SYS"
    #define PWD       "tibero"
    
    int main(int argc, char *argv[]) {
        SQLRETURN      rc = SQL_SUCCESS;
        SQLINTEGER     versionNumber = 1;
        SQLCHAR        dataFileName[256];
        SQLCHAR        actionString[256];
        SQLCHAR        msgFileName[256];
        SQLCHAR        fieldTerm[5];
        SQLCHAR        lineTerm[5];
        SQLCHAR        enclStart[5];
        SQLCHAR        enclEnd[5];
    
        struct sqlca   ca           = {"\0", 0, 0, {0, "\0"}, "\0",
                                       {0, 0, 0, 0, 0, 0}, "\0", "\0"};
        TBExportStruct exportStruct = {NULL, 0, NULL, NULL, NULL, 0, NULL,
                                       0, NULL, NULL};
        TBExportIn     exportIn     = {{NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0}};
        TBExportOut    exportOut    = {0};
    
        rc= TBConnect((SQLCHAR *)DNS_NAME, (SQLCHAR *)USER_NAME, (SQLCHAR *)PWD, &ca);
        
        if (rc != SQL_SUCCESS) return -1;
    
        strcpy((char *)dataFileName, "./all_tables.dat");
        strcpy((char *)actionString, "select * from all_tables");
        strcpy((char *)msgFileName, "./all_tables.log");
        strcpy((char *)fieldTerm, ",");
        strcpy((char *)lineTerm, "\n");
        strcpy((char *)enclStart, "\"");
        strcpy((char *)enclEnd, "\"");
    
        /* setting data file name */
        exportStruct.piDataFileName   = dataFileName;
        exportStruct.iDataFileNameLen = strlen((char *)dataFileName);
    
        /* setting action String */
        exportStruct.piActionString = calloc(1, sizeof(sqlstr));
        exportStruct.piActionString->data   = actionString;
        exportStruct.piActionString->length = strlen((char *)actionString);
    
        /* setting file type */
        exportStruct.iFileType = SQL_DEL;
    
        /* setting message file name */
        exportStruct.piMsgFileName   = msgFileName;
        exportStruct.iMsgFileNameLen = strlen((char *)msgFileName);
    
        /* setting field term, line term etc.. */
        exportIn.iMeta.fieldTerm    = fieldTerm;
        exportIn.iMeta.fieldTermLen = strlen((char *)fieldTerm);
        exportIn.iMeta.lineTerm     = lineTerm;
        exportIn.iMeta.lineTermLen  = strlen((char *)lineTerm);
        exportIn.iMeta.enclStart    = enclStart;
        exportIn.iMeta.enclStartLen = strlen((char *)enclStart);
        exportIn.iMeta.enclEnd      = enclEnd;
        exportIn.iMeta.enclEndLen   = strlen((char *)enclEnd);
    
        /* setting export input, output information */
        exportStruct.piExportInfoIn  = &exportIn;
        exportStruct.poExportInfoOut = &exportOut;
    
        /* setting file type */
        rc = TBExport(versionNumber, &exportStruct, &ca);
        if (rc != SQL_SUCCESS) return -1;
    
        /* disconnect */
        rc= TBDisconnect((SQLCHAR *)DNS_NAME, &ca);
        if (rc != SQL_SUCCESS) return -1;
    
        return 1;
    }

7.3.4. TBImport

Loads data from an external file to the database. Fixed-length and delimited record type files are supported by tbLoader. The INSERT permissions for the table or view is required to use this utility.

A detailed description of the TBImport function follows:

  • Syntax

    SQLRETURN SQL_API TBImport(SQLINTEGER versionNumber,
                               TBImportStruct *pParamStruct,
                               struct sqlca *pSqlca);
  • Parameter

    ParameterUsageDescription
    versionNumberInput

    Version number of the utility library needed for backward-compatibility.

    Current version number is 1.

    pParamStructInput Output

    Specifies the data to load as input and receives the result as output.

    Refer to TBImportStruct for more information.

    pSqlcaOutputWhen the return code is not SQL_SUCCESS, contains utility error logs.
  • Return code

    Return CodeDescription
    SQL_SUCCESSCompleted successfully.
    SQL_SUCCESS_WITH_INFOCompleted successfully with warning message(s).
    SQL_ERRORCritical error has occurred.
  • Example

    #include "tbutil.h"
    
    #define DNS_NAME  "DEFAULT"
    #define USER_NAME "SYS"
    #define PWD       "tibero"
    
    int main(int argc, char *argv[]) {
     SQLRETURN      rc = SQL_SUCCESS;
     SQLINTEGER     versionNumber = 1;
     SQLCHAR        dataFileName[256];
     SQLCHAR        actionString[256];
     SQLCHAR        msgFileName[256];
     SQLCHAR        badFileName[256];
     SQLCHAR        fieldTerm[5];
     SQLCHAR        lineTerm[5];
     SQLCHAR        enclStart[5];
     SQLCHAR        enclEnd[5];
     SQLCHAR        escape[5];
    
     struct sqlca   ca           = {"\0", 0, 0, {0, "\0"}, "\0",
                                   {0, 0, 0, 0, 0, 0}, "\0", "\0"};
     TBImportStruct importStruct = {NULL, 0, NULL, NULL, NULL, 0, NULL,
                                    0, NULL, 0, 0, NULL, NULL};
     TBImportIn     importIn     = {{NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0},
                                    0, 0, 0, 0};
     TBImportOut    importOut    = {0, 0, 0, 0, 0, 0};
    
     rc= TBConnect((SQLCHAR *)DNS_NAME, (SQLCHAR *)USER_NAME, (SQLCHAR *)PWD, &ca);
     if (rc != SQL_SUCCESS) return -1;
    
     strcpy((char *)actionString, "LOAD DATA "
                                "APPEND "
                                "INTO TABLE DEPT "
                                "MULTI INSERT INDEXES "
                                "(position, deptno, dname CONSTANT \"co dep\", loc)");
     strcpy((char *)dataFileName, "./test.dat");
     strcpy((char *)msgFileName, "./test.log");
     strcpy((char *)badFileName, "./test.bad");
     strcpy((char *)fieldTerm,   ",b");
     strcpy((char *)lineTerm,    "abbb\n");
     strcpy((char *)enclStart,   "{$");
     strcpy((char *)enclEnd,     "$}");
     strcpy((char *)escape,      "XX");
    
     /* setting data file name */
     importStruct.piDataFileName   = dataFileName;
     importStruct.iDataFileNameLen = strlen((char *)dataFileName);
    
     /* setting action String */
     importStruct.piActionString = calloc(1, sizeof(sqlstr));
     importStruct.piActionString->data   = actionString;
     importStruct.piActionString->length = strlen((char *)actionString);
    
     /* setting file type */
     importStruct.iFileType = SQL_DEL;
    
     /* setting message file name */
     importStruct.piMsgFileName   = msgFileName;
     importStruct.iMsgFileNameLen = strlen((char *)msgFileName);
    
     /* setting bad data file name */
     importStruct.piBadFileName   = badFileName;
     importStruct.iBadFileNameLen = strlen((char *)badFileName);
    
     /* turn on DPL mode */
     importStruct.iDPL            = 2;
    
     /* setting field term, line term etc.. */
     importIn.iMeta.fieldTerm    = fieldTerm;
     importIn.iMeta.fieldTermLen = strlen((char *)fieldTerm);
     importIn.iMeta.lineTerm     = lineTerm;
     importIn.iMeta.lineTermLen  = strlen((char *)lineTerm);
     importIn.iMeta.enclStart    = enclStart;
     importIn.iMeta.enclStartLen = strlen((char *)enclStart);
     importIn.iMeta.enclEnd      = enclEnd;
     importIn.iMeta.enclEndLen   = strlen((char *)enclEnd);
     importIn.iMeta.escape       = escape;
     importIn.iMeta.escapeLen    = strlen((char *)escape);
    
     importIn.iRowcount          = 0;
     importIn.iSkipcount         = 0;
     importIn.iCommitcount       = 2;
     importIn.iErrorcount        = 50;
    
     /* setting export input, output information */
     importStruct.piImportInfoIn  = &importIn;
     importStruct.poImportInfoOut = &importOut;
    
     /* setting file type */
     rc = TBImport(versionNumber, &importStruct, &ca);
     if (rc != SQL_SUCCESS) return -1;    
    
       fprintf(stdout, "oRowsRead[%ld]", importOut.oRowsRead);
       fprintf(stdout, "oRowsSkipped[%ld]", importOut.oRowsSkipped);
       fprintf(stdout, "oRowsInserted[%ld]", importOut.oRowsInserted);
       fprintf(stdout, "oRowsUpdated[%ld]", importOut.oRowsUpdated);
       fprintf(stdout, "oRowsRejected[%ld]", importOut.oRowsRejected);
       fprintf(stdout, "oRowsCommitted[%ld]", importOut.oRowsCommitted);
    
       /* disconnect */
       rc= TBDisconnect((SQLCHAR *)DNS_NAME, &ca);
       if (rc != SQL_SUCCESS) return -1;
    
       return 1;
    }