SQLGetCursorNameTest.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 SQLGetCursorNameTest.cpp
  15.  */
  16. #include <common.hpp>
  17. using namespace std;
  18. #define GCN_MESSAGE_LENGTH 50
  19. SQLHSTMT    GCN_hstmt;
  20. SQLHDESC    GCN_hdesc;
  21. SQLHENV     GCN_henv;
  22. SQLHDBC     GCN_hdbc;
  23. void GCN_DisplayError(SQLSMALLINT GCN_HandleType, 
  24.       SQLHDESC GCN_InputHandle);
  25. /** 
  26.  * Test to assign a user-defined name to a cursor that is 
  27.  * associated with an active SQL statement handle
  28.  *
  29.  * Tests:
  30.  * -# if there is no user-defined cursor name, then try to 
  31.  *    get user-definedcursor name 
  32.  * -# get cursor name in normal case
  33.  *
  34.  * @return Zero, if test succeeded
  35.  */
  36. int SQLGetCursorNameTest()
  37. {
  38.   SQLRETURN   retcode;
  39.   SQLCHAR     SQLStmt [120];
  40.   SQLCHAR     CursorName [80];
  41.   SQLSMALLINT CNameSize;
  42.   ndbout << endl << "Start SQLGetCursorName Testing" << endl;
  43.   //************************************
  44.   //** Allocate An Environment Handle **
  45.   //************************************
  46.   retcode = SQLAllocHandle(SQL_HANDLE_ENV, 
  47.    SQL_NULL_HANDLE, 
  48.    &GCN_henv);
  49.   
  50. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  51.     ndbout << "Allocated an environment Handle!" << endl;
  52.   
  53.   //*********************************************
  54.   //** Set the ODBC application Version to 3.x **
  55.   //*********************************************
  56.   retcode = SQLSetEnvAttr(GCN_henv, 
  57.   SQL_ATTR_ODBC_VERSION, 
  58.   (SQLPOINTER) SQL_OV_ODBC3, 
  59.   SQL_IS_UINTEGER);
  60.   
  61.   if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  62.     ndbout << "Set the ODBC application Version to 3.x!" << endl;
  63.   //**********************************
  64.   //** Allocate A Connection Handle **
  65.   //**********************************
  66.   retcode = SQLAllocHandle(SQL_HANDLE_DBC, 
  67.    GCN_henv, 
  68.    &GCN_hdbc);
  69.   if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  70.     ndbout << "Allocated a connection Handle!" << endl;
  71.   
  72.   // *******************
  73.   // ** Connect to DB **
  74.   // *******************
  75.   retcode = SQLConnect(GCN_hdbc, 
  76.        (SQLCHAR *) connectString(), 
  77.        SQL_NTS, 
  78.        (SQLCHAR *) "", 
  79.        SQL_NTS, 
  80.        (SQLCHAR *) "", 
  81.        SQL_NTS);
  82.   
  83.   if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  84.   ndbout << "Connected to DB : OK!" << endl;
  85.   else 
  86.     {  
  87.       ndbout << "Failure to Connect DB!" << endl;
  88.       return NDBT_FAILED;
  89.     }
  90.   //*******************************
  91.   //** Allocate statement handle **
  92.   //*******************************
  93.   
  94.   retcode = SQLAllocHandle(SQL_HANDLE_STMT, 
  95.    GCN_hdbc, 
  96.    &GCN_hstmt); 
  97.   if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
  98.     ndbout << "Allocated a statement handle!" << endl;
  99.  
  100.   //************************
  101.   //** Define a statement **
  102.   //************************
  103.   strcpy((char *) SQLStmt, 
  104.  "SELECT * FROM Customers WHERE Address = 'LM Vag 8'");
  105.   //*************************
  106.   //** Prepare a statement **
  107.   //************************* 
  108.   retcode = SQLPrepare(GCN_hstmt, 
  109.        SQLStmt, 
  110.        SQL_NTS);
  111.   //*************************************************************************
  112.   //** if there is no user-defined cursor name, try to get the cursor name **
  113.   //*************************************************************************
  114.   retcode = SQLGetCursorName(GCN_hstmt, 
  115.      CursorName, 
  116.      sizeof(CursorName), 
  117.      &CNameSize);
  118.   if (retcode != SQL_SUCCESS)
  119.     {
  120.       ndbout << endl << "retcode =" << retcode << endl;
  121.       GCN_DisplayError(SQL_HANDLE_STMT, GCN_hstmt);
  122.     }
  123.   else 
  124.       ndbout << endl << "The cursor name is : " << (char *) CursorName << endl;
  125.   //*************************
  126.   //** Set the cursor name **
  127.   //*************************
  128.   retcode = SQLSetCursorName(GCN_hstmt, 
  129.      (char *)"Customer_CURSOR", 
  130.      SQL_NTS);
  131.   //***************************
  132.   //** Execute the statement **
  133.   //***************************
  134.   retcode = SQLExecute(GCN_hstmt);
  135.   //**********************************************
  136.   //** retrieve and display the new cursor name **
  137.   //**********************************************
  138.   retcode = SQLGetCursorName(GCN_hstmt, 
  139.      CursorName, 
  140.      sizeof(CursorName), 
  141.      &CNameSize);
  142.   if (retcode != SQL_SUCCESS)
  143.     {
  144.       ndbout << endl << "retcode =" << retcode << endl;
  145.       GCN_DisplayError(SQL_HANDLE_STMT, GCN_hstmt);
  146.     }
  147.   else 
  148.       ndbout << endl << "The cursor name is : " << (char *) CursorName << endl;
  149.   //****************
  150.   // Free Handles **
  151.   //****************
  152.   SQLDisconnect(GCN_hdbc);
  153.   SQLFreeHandle(SQL_HANDLE_STMT, GCN_hstmt);
  154.   SQLFreeHandle(SQL_HANDLE_DBC, GCN_hdbc);
  155.   SQLFreeHandle(SQL_HANDLE_ENV, GCN_henv);
  156.   
  157.   return NDBT_OK;
  158.  }
  159. void GCN_DisplayError(SQLSMALLINT GCN_HandleType, SQLHDESC GCN_InputHandle)
  160. {
  161.   SQLINTEGER  NativeError;
  162.   SQLCHAR     Sqlstate[5], Msg[GCN_MESSAGE_LENGTH];
  163.   SQLRETURN SQLSTATEs;
  164.   SQLSMALLINT i, MsgLen;
  165.   i = 1;
  166.   ndbout << "-------------------------------------------------" << endl;
  167.   ndbout << "Error diagnostics:" << endl;
  168.   
  169.   while ((SQLSTATEs = SQLGetDiagRec(GCN_HandleType, 
  170.     GCN_InputHandle, i, 
  171.     Sqlstate, 
  172.     &NativeError, 
  173.     Msg, 
  174.     sizeof(Msg), 
  175.     &MsgLen)) 
  176.  != SQL_NO_DATA)                   
  177.     {
  178.       ndbout << "the HandleType is:" << GCN_HandleType << endl;
  179.       ndbout << "the InputHandle is :" << (long)GCN_InputHandle << endl;
  180.       ndbout << "the Msg is: " << (char *) Msg << endl;
  181.       ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  182.       
  183.       i ++;
  184.     }
  185.   ndbout << "-------------------------------------------------" << endl;
  186. }