Metadata Info Using ODBC

From Ingres Community Wiki

Jump to: navigation, search
/*
** File:  getinfo.c
**
** This program assumes you have an ODBC DSN configured to make a connection
** to the database.  The DSN name is provided as a command-line argument:
**
**     seliitab myDSN
**
** ...connects you to the database defined by the ODBC DSN name "myDSN".
**
** The program connects to the target database defined by "myDSN", and returns information
** about the driver version, ODBC version, and supported ODBC data types.
**
*/
# ifdef _WIN32
# include <windows.h>
# endif
# include <stdio.h>
# include <sql.h>
# include <sqlext.h>
void check_error( SQLSMALLINT, SQLHANDLE, SQLRETURN, int, char *);
void print_error( SQLSMALLINT, SQLHANDLE, SQLRETURN, int, char *);
#define CHECK_HANDLE( htype, hndl, rc ) if ( rc != SQL_SUCCESS ) {\
check_error (htype,hndl,rc,__LINE__,__FILE__);exit(0); }
int main( int argc, char **argv)
{ 
    RETCODE rc,ret;
    HENV henv;
    HDBC hdbc; 
    HSTMT hstmt;
    SQLINTEGER orind1 = SQL_NTS, orind2 = 0, orind3 = 0;
    int i;
    SQLCHAR type_name[80];
    SQLSMALLINT data_type;
    SQLSMALLINT searchable;
    SQLINTEGER colunn_size;       
    SQLCHAR  szNum[20];
    SQLCHAR  szUsername[60]; 
    if (argc != 2)
    {
        printf("Usage: test dsnName\n");
        exit(0);
    }
    rc = SQLAllocEnv(&henv);
    CHECK_HANDLE( SQL_HANDLE_ENV, henv, rc);
    rc = SQLAllocConnect(henv, &hdbc);
    CHECK_HANDLE( SQL_HANDLE_ENV, henv, rc);
    printf("connecting to the data source\n");
    rc = SQLConnect(hdbc,argv[1],SQL_NTS,0,SQL_NTS, 0,SQL_NTS);
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc);
    rc = SQLAllocStmt(hdbc, &hstmt);
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc);
    rc = SQLGetInfo(hdbc, SQL_ODBC_VER, szNum, sizeof(szNum), NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc);
    printf("ODBC version is %s\n",szNum);
    rc = SQLGetInfo(hdbc, SQL_DRIVER_VER, szNum, sizeof(szNum), NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc);
    printf("Driver version is %s\n",szNum);
    rc = SQLGetInfo(hdbc, SQL_USER_NAME, szUsername, sizeof(szUsername), NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc);
    printf("User name is %s\n",szUsername);
    rc = SQLGetTypeInfo( hstmt, SQL_ALL_TYPES);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc);
    while (1)
    {
        rc = SQLFetch(hstmt);
        if (rc == SQL_NO_DATA) 
        {
            printf("EOD\n");
            break;
        }
        else if (rc == SQL_ERROR)
            CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc);
        ret = SQLGetData(hstmt,1,SQL_C_CHAR, type_name, 80, 
                &orind1);
        ret = SQLGetData(hstmt,2,SQL_C_LONG, &data_type, 0, 
                &orind2);
        ret = SQLGetData(hstmt,9,SQL_C_DEFAULT, &searchable, 0, 
                &orind3);
        printf("col name %s data type %d searchable %d\n",type_name, data_type,
             searchable);
        if (!SQL_SUCCEEDED(ret))
            CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, ret);
    }
    rc = SQLFreeStmt(hstmt, SQL_DROP);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc);
    printf("disconnecting\n");
    rc = SQLDisconnect(hdbc);
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc);
    rc = SQLFreeConnect(hdbc);
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc);
    rc = SQLFreeEnv(henv);
    CHECK_HANDLE( SQL_HANDLE_ENV, henv, rc);
    return(0);   
}
void print_err(RETCODE rc, HENV henv, HDBC hdbc, SQLHSTMT hstmt)
{
    SQLCHAR    SqlState[6], SQLStmt[100], Msg[SQL_MAX_MESSAGE_LENGTH];
    SQLINTEGER NativeError;
    SQLSMALLINT i=1, MsgLen;
    RETCODE ret;
    printf("Error number is %d\n", rc);
    ret = SQL_SUCCESS;
    if (hstmt == SQL_NULL_HSTMT)
    {
        while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
        {
            ret = SQLError(henv, hdbc, hstmt, SqlState,
                &NativeError, Msg, sizeof(Msg), &MsgLen);
            if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
            {
                printf("State: %s  Native Error: %d\n %s\n",
                    SqlState,NativeError,Msg);
            }
        }
        exit (-1);
    }
    while (SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, i,
            SqlState, &NativeError, Msg, sizeof(Msg), &MsgLen) != SQL_NO_DATA)
    {
	printf("State: %s  Native Error Code: %d\n %s\n",
	    SqlState,NativeError,Msg);
        i++;
    }
    exit(-1);
}
void check_error( SQLSMALLINT htype, SQLHANDLE hndl,
    SQLRETURN frc, int line, char * file )
{
   SQLCHAR         cli_sqlstate[SQL_SQLSTATE_SIZE + 1]; 
   SQLINTEGER      cli_sqlcode; 
   switch (frc)
   {
   case SQL_SUCCESS:
     break; 
   case SQL_INVALID_HANDLE:
     printf("check_error> SQL_INVALID HANDLE \n");
     break;
   case SQL_ERROR:
     printf("check_error> SQL_ERROR\n");
     break;
   case SQL_SUCCESS_WITH_INFO:
     printf("check_error>  SQL_SUCCESS_WITH_INFO\n");        
     break;
   case SQL_NO_DATA_FOUND:
     printf("check_error> SQL_NO_DATA_FOUND\n");
     break;
   default:
     printf("check_error> Received rc from api rc=%i\n",frc);
     break;
   } /*end switch*/
   print_error(htype,hndl,frc,line,file);
}  /* end check_error*/
/******************************************************************/
/* print_error                                                    */
/* calls SQLGetDiagRec()displays SQLSTATE and message             */
/******************************************************************/
void print_error( SQLSMALLINT htype, SQLHANDLE hndl, SQLRETURN frc,
    int line, char * file)
{
   SQLCHAR     buffer[SQL_MAX_MESSAGE_LENGTH + 1] ;
   SQLCHAR     sqlstate[SQL_SQLSTATE_SIZE + 1] ;
   SQLINTEGER  sqlcode ;
   SQLSMALLINT length, i ;
   SQLRETURN   prc;
   printf("return code = %d reported from file: %s, line: %d\n",         
       frc, file, line );
   i = 1 ;
   while ( SQLGetDiagRec( htype, hndl, i, sqlstate,
       &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1,
       &length ) == SQL_SUCCESS )
   {
       printf( "SQLSTATE: %s\n", sqlstate ) ;
      printf( "Native Error Code: %ld\n", sqlcode ) ;
      printf( "buffer: %s \n", buffer ) ;
      i++ ;
   }
}  /* end print_error */
Personal tools
Developing With