SQLGetInfoTest.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:6k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13.  /**
  14.  * @file SQLGetInfoTest.cpp
  15.  */
  16. #include <common.hpp>
  17. using namespace std;
  18. SQLHDBC     GI_hdbc;
  19. SQLHSTMT    GI_hstmt;
  20. SQLHENV     GI_henv;
  21. #define GI_MESSAGE_LENGTH 200
  22. SQLCHAR   Msg[GI_MESSAGE_LENGTH];
  23.        
  24. void SQLGetInfoTest_DisplayError(SQLSMALLINT GI_HandleType, 
  25.  SQLHDBC GI_InputHandle);
  26. /** 
  27.  * Test to retrieve general information about the driver and
  28.  * the data source an application is currently connected to.
  29.  *
  30.  * Tests:
  31.  * -# Test The value of FunctionId is not in table 27
  32.  * @return Zero, if test succeeded
  33.  */
  34. int SQLGetInfoTest()
  35. {
  36.   SQLRETURN retcode;
  37.   SQLINTEGER    InfoValuePtr;
  38.   SQLSMALLINT   SLPStringLengthPtr;
  39.   ndbout << endl << "Start SQLGetInfo Testing" << endl;
  40.   //******************************************************
  41.   //** The value of FunctionId is not in Table 27, then **
  42.   //** an exception condition is raised                 **
  43.   //******************************************************
  44.   //************************************
  45.   //** Allocate An Environment Handle **
  46.   //************************************
  47.   retcode = SQLAllocHandle(SQL_HANDLE_ENV, 
  48.    SQL_NULL_HANDLE, 
  49.    &GI_henv);
  50.   
  51. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  52.     ndbout << "Allocated an environment Handle!" << endl;
  53.   
  54.   //*********************************************
  55.   //** Set the ODBC application Version to 3.x **
  56.   //*********************************************
  57.   retcode = SQLSetEnvAttr(GI_henv, 
  58.   SQL_ATTR_ODBC_VERSION, 
  59.   (SQLPOINTER) SQL_OV_ODBC3, 
  60.   SQL_IS_UINTEGER);
  61.   
  62. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  63.     ndbout << "Set the ODBC application Version to 3.x!" << endl;
  64.   //**********************************
  65.   //** Allocate A Connection Handle **
  66.   //**********************************
  67.   retcode = SQLAllocHandle(SQL_HANDLE_DBC, 
  68.    GI_henv, 
  69.    &GI_hdbc);
  70.   if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  71.     ndbout << "Allocated a connection Handle!" << endl;
  72.   
  73.   // *******************
  74.   // ** Connect to DB **
  75.   // *******************
  76.   retcode = SQLConnect(GI_hdbc, 
  77.        (SQLCHAR *) connectString(), 
  78.        SQL_NTS, 
  79.        (SQLCHAR *) "", 
  80.        SQL_NTS, 
  81.        (SQLCHAR *) "", 
  82.        SQL_NTS);
  83.   
  84.   // **********************
  85.   // ** GET INFO FROM DB **
  86.   // *********************
  87.   retcode = SQLGetInfo(GI_hdbc, 
  88.        SQL_DATABASE_NAME, 
  89.        &InfoValuePtr, 
  90.        sizeof(InfoValuePtr), 
  91.        &SLPStringLengthPtr);
  92.   if (retcode == SQL_SUCCESS)
  93.     ndbout << endl << "Database Name:" << InfoValuePtr << endl;
  94.   else
  95.     {
  96.       ndbout << endl << "retcode = SQLGetInfo() = " << retcode <<endl;
  97.       SQLGetInfoTest_DisplayError(SQL_HANDLE_STMT, GI_hstmt);
  98.     }
  99.   retcode = SQLGetInfo(GI_hdbc, 
  100.        SQL_DRIVER_NAME, 
  101.        &InfoValuePtr, 
  102.        sizeof(InfoValuePtr), 
  103.        &SLPStringLengthPtr);
  104.   if (retcode == SQL_SUCCESS)
  105.     ndbout << endl << "Driver Name:" << InfoValuePtr << endl;
  106.   else
  107.     {
  108.       ndbout << endl << "retcode = SQLGetInfo() = " << retcode <<endl;
  109.       SQLGetInfoTest_DisplayError(SQL_HANDLE_STMT, GI_hstmt);
  110.     }
  111.   // **************************
  112.   // ** INPUT WRONG InfoType **
  113.   // **************************
  114.   retcode = SQLGetInfo(GI_hdbc, 
  115.        8888, 
  116.        &InfoValuePtr, 
  117.        sizeof(InfoValuePtr), 
  118.        &SLPStringLengthPtr);
  119.  if (retcode == -2)
  120.    {
  121.      ndbout << endl <<"retcode = " << retcode << endl;
  122.      ndbout << "System reported -2. Please check your test programme" 
  123.     << " about the connectionhandle." << endl;
  124.      SQLGetInfoTest_DisplayError(SQL_HANDLE_STMT, GI_hstmt);
  125.    }
  126.   else if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  127.     {
  128.       ndbout << endl << "retcode = " << retcode << endl;
  129.       ndbout << "The information of InfoType is not in Table 28," 
  130.      << " but SQLGetInfo() executeed succeddfully." 
  131.      << " Check the function!" <<endl;
  132.       SQLGetInfoTest_DisplayError(SQL_HANDLE_STMT, GI_hstmt);
  133.     }
  134.  else if (retcode == SQL_ERROR)
  135.     {
  136.       ndbout << endl << "retcode = " << retcode << endl;
  137.       ndbout << "Input a wrong InfoType. The system found the" 
  138.      << " information of InfoType is not in Table 28." 
  139.      << " Test successful!" << endl;
  140.     }
  141.   else 
  142.     ndbout << endl;
  143.   // *********************************
  144.   // ** Disconnect and Free Handles **
  145.   // *********************************  
  146.   SQLDisconnect(GI_hdbc);
  147.   SQLFreeHandle(SQL_HANDLE_STMT, GI_hstmt);
  148.   SQLFreeHandle(SQL_HANDLE_DBC, GI_hdbc);
  149.   SQLFreeHandle(SQL_HANDLE_ENV, GI_henv);
  150.   return NDBT_OK;
  151.  }
  152. void SQLGetInfoTest_DisplayError(SQLSMALLINT GI_HandleType, 
  153.  SQLHDBC GI_InputHandle)
  154. {
  155.   SQLRETURN   SQLSTATEs;
  156.   SQLINTEGER    NativeError;
  157.   SQLCHAR Sqlstate[50];
  158.   SQLSMALLINT   i, MsgLen;
  159.   i = 1;
  160.   
  161.   ndbout << "-------------------------------------------------" << endl;
  162.   ndbout << "Error diagnostics:" << endl;
  163.   
  164.   while ((SQLSTATEs = SQLGetDiagRec(GI_HandleType, 
  165.     GI_InputHandle, 
  166.     i, 
  167.     Sqlstate, 
  168.     &NativeError, 
  169.     Msg, 
  170.     sizeof(Msg), 
  171.     &MsgLen)) 
  172.  != SQL_NO_DATA)                   
  173. {
  174.      ndbout << "the GI_HandleType is:" << GI_HandleType << endl;
  175.      ndbout << "the GI_InputHandle is :" << (long)GI_InputHandle << endl;
  176.      ndbout << "the Msg is :" << (char *) Msg << endl;
  177.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  178.      i ++;
  179.      break;
  180.                                                          }
  181.   ndbout << "-------------------------------------------------" << endl;
  182. }