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

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 SQLTransactTest.cpp
  15.  */
  16. #include <common.hpp>
  17. #define STR_MESSAGE_LENGTH 200
  18. #define STR_NAME_LEN 20
  19. #define STR_PHONE_LEN 20
  20. #define STR_ADDRESS_LEN 20
  21. using namespace std;
  22. SQLHDBC     STR_hdbc;
  23. SQLHSTMT    STR_hstmt;
  24. SQLHENV     STR_henv;
  25. SQLHDESC    STR_hdesc;
  26.        
  27. void Transact_DisplayError(SQLSMALLINT STR_HandleType, 
  28.    SQLHSTMT STR_InputHandle);
  29. int STR_Display_Result(SQLHSTMT EXDR_InputHandle);
  30. /** 
  31.  * Test:
  32.  * -#Test to request a commit or a rollback operation for
  33.  *   all active transactions associated with a specific
  34.  *   environment or connection handle
  35.  *
  36.  * @return Zero, if test succeeded
  37.  */
  38. int SQLTransactTest()
  39. {
  40.   SQLRETURN   STR_ret;
  41.   ndbout << endl << "Start SQLTransact Testing" << endl;
  42.   //************************************
  43.   //** Allocate An Environment Handle **
  44.   //************************************
  45.   STR_ret = SQLAllocHandle(SQL_HANDLE_ENV, 
  46.    SQL_NULL_HANDLE, 
  47.    &STR_henv);
  48.   
  49.   if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
  50.     ndbout << "Allocated an environment Handle!" << endl;
  51.   
  52.   //*********************************************
  53.   //** Set the ODBC application Version to 3.x **
  54.   //*********************************************
  55.   STR_ret = SQLSetEnvAttr(STR_henv, 
  56.   SQL_ATTR_ODBC_VERSION, 
  57.   (SQLPOINTER) SQL_OV_ODBC3,
  58.   SQL_IS_UINTEGER);
  59.   
  60.   if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
  61.     ndbout << "Set the ODBC application Version to 3.x!" << endl;
  62.   //**********************************
  63.   //** Allocate A Connection Handle **
  64.   //**********************************
  65.   STR_ret = SQLAllocHandle(SQL_HANDLE_DBC, 
  66.    STR_henv, 
  67.    &STR_hdbc);
  68.   if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
  69.     ndbout << "Allocated a connection Handle!" << endl;
  70.   
  71.   // *******************
  72.   // ** Connect to DB **
  73.   // *******************
  74.   STR_ret = SQLConnect(STR_hdbc, 
  75.        (SQLCHAR *) connectString(), 
  76.        SQL_NTS, 
  77.        (SQLCHAR *) "", 
  78.        SQL_NTS, 
  79.        (SQLCHAR *) "", 
  80.        SQL_NTS);
  81.   
  82.   if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
  83.     ndbout << "Connected to DB : OK!" << endl;
  84.   else 
  85.     {  
  86.       ndbout << "Failure to Connect DB!" << endl;
  87.       return NDBT_FAILED;
  88.     }
  89.   //*******************************
  90.   //** Allocate statement handle **
  91.   //*******************************
  92.   
  93.   STR_ret = SQLAllocHandle(SQL_HANDLE_STMT, 
  94.    STR_hdbc, 
  95.    &STR_hstmt); 
  96.   if(STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO) 
  97.     ndbout << "Allocated a statement handle!" << endl;
  98.   //********************************
  99.   //** Turn Manual-Commit Mode On **
  100.   //********************************
  101.   STR_ret = SQLSetConnectOption(STR_hdbc, 
  102. SQL_AUTOCOMMIT, 
  103. (UDWORD) SQL_AUTOCOMMIT_OFF);
  104.   //**********************************************
  105.   //** Prepare and Execute a prepared statement **
  106.   //**********************************************
  107.   STR_ret = SQLExecDirect(STR_hstmt, 
  108.   (SQLCHAR*)"SELECT * FROM Customers", 
  109.   SQL_NTS);
  110.   if (STR_ret == SQL_INVALID_HANDLE)
  111.   {   
  112.     ndbout << "Handle Type is SQL_HANDLE_STMT, but SQL_INVALID_HANDLE" 
  113.    << endl;
  114.     ndbout << "still appeared. Please check program" << endl;
  115.   }
  116.   if (STR_ret == SQL_ERROR || STR_ret == SQL_SUCCESS_WITH_INFO)
  117.     Transact_DisplayError(SQL_HANDLE_STMT, STR_hstmt);
  118.   //*************************
  119.   //** Display the results **
  120.   //*************************
  121.   STR_Display_Result(STR_hstmt);
  122.   //****************************
  123.   //** Commit the transaction **
  124.   //****************************
  125.   STR_ret = SQLTransact(STR_henv, 
  126. STR_hdbc, 
  127. SQL_COMMIT);
  128.   //****************
  129.   // Free Handles **
  130.   //****************
  131.   SQLDisconnect(STR_hdbc);
  132.   SQLFreeHandle(SQL_HANDLE_STMT, STR_hstmt);
  133.   SQLFreeHandle(SQL_HANDLE_DBC, STR_hdbc);
  134.   SQLFreeHandle(SQL_HANDLE_ENV, STR_henv);
  135.   return NDBT_OK;
  136.  }
  137. void Transact_DisplayError(SQLSMALLINT STR_HandleType, 
  138.      SQLHSTMT STR_InputHandle)
  139. {
  140.      SQLCHAR STR_Sqlstate[5];
  141.      SQLINTEGER   STR_NativeError;
  142.      SQLSMALLINT  STR_i, STR_MsgLen;
  143.      SQLCHAR      STR_Msg[STR_MESSAGE_LENGTH];
  144.      SQLRETURN    SQLSTATEs;
  145.      STR_i = 1;
  146.      ndbout << "-------------------------------------------------" << endl;
  147.      ndbout << "Error diagnostics:" << endl;
  148.   
  149.      while ((SQLSTATEs = SQLGetDiagRec(STR_HandleType, 
  150.        STR_InputHandle, 
  151.        STR_i, 
  152.        STR_Sqlstate, 
  153.        &STR_NativeError, 
  154.        STR_Msg, 
  155.        sizeof(STR_Msg), 
  156.        &STR_MsgLen)) 
  157.     != SQL_NO_DATA)                   {
  158.      ndbout << "the HandleType is:" << STR_HandleType << endl;
  159.      ndbout << "the InputHandle is :" << (long)STR_InputHandle << endl;
  160.      ndbout << "the STR_Msg is: " << (char *) STR_Msg << endl;
  161.      ndbout << "the output state is:" << (char *)STR_Sqlstate << endl; 
  162.      STR_i ++;
  163.      //     break;
  164.                                                        }
  165.      ndbout << "-------------------------------------------------" << endl;
  166. }
  167. int STR_Display_Result(SQLHSTMT STR_InputHandle)
  168. {
  169.   SQLRETURN   STR_retcode;
  170.   unsigned long  STR_CustID;
  171.   SQLCHAR     STR_Name[STR_NAME_LEN], STR_Phone[STR_PHONE_LEN];
  172.   SQLCHAR     STR_Address[STR_ADDRESS_LEN];
  173.   //*********************
  174.   //** Bind columns  1 **
  175.   //*********************
  176.   STR_retcode =SQLBindCol(STR_InputHandle, 
  177.   1, 
  178.   SQL_C_ULONG, 
  179.   &STR_CustID, 
  180.   sizeof(STR_CustID),
  181.   NULL);
  182.       if (STR_retcode == SQL_ERROR)
  183.   ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
  184.   Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
  185.   return NDBT_FAILED;
  186. }
  187.   //*********************
  188.   //** Bind columns  2 **
  189.   //*********************
  190.   STR_retcode =SQLBindCol(STR_InputHandle, 
  191.   2, 
  192.   SQL_C_CHAR, 
  193.   &STR_Name, 
  194.   STR_NAME_LEN,
  195.   NULL);
  196.       if (STR_retcode == SQL_ERROR)
  197.   ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
  198.   Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
  199.   return NDBT_FAILED;
  200. }
  201.   //*********************
  202.   //** Bind columns 3  **
  203.   //*********************
  204.       STR_retcode = SQLBindCol(STR_InputHandle, 
  205.        3,
  206.        SQL_C_CHAR, 
  207.        &STR_Address, 
  208.        STR_ADDRESS_LEN, 
  209.        NULL);
  210.       if (STR_retcode == SQL_ERROR)
  211.   ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
  212.   Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
  213.   return NDBT_FAILED;
  214. }
  215.   //*********************
  216.   //** Bind columns 4  **
  217.   //*********************
  218.       STR_retcode = SQLBindCol(STR_InputHandle, 
  219.        4, 
  220.        SQL_C_CHAR, 
  221.        &STR_Phone, 
  222.        STR_PHONE_LEN,
  223.        NULL);
  224.       if (STR_retcode == SQL_ERROR)
  225.   ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
  226.   Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
  227.   return NDBT_FAILED;
  228. }
  229.   //*****************************************
  230.   //* Fetch and print each row of data. On **
  231.   //* an error, display a message and exit **
  232.   //*****************************************
  233.  
  234.   if (STR_retcode != SQL_ERROR)
  235.   STR_retcode = SQLFetch(STR_InputHandle);
  236.   ndbout << endl << "STR_retcode = SQLFetch(STR_InputHandle) = " 
  237.  << STR_retcode << endl;
  238.   if (STR_retcode == SQL_ERROR)
  239.     { 
  240.       ndbout << "Executing SQLFetch, SQL_ERROR happened!" << endl;
  241.       Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
  242.       return NDBT_FAILED;
  243.     }
  244.   else if (STR_retcode == SQL_SUCCESS_WITH_INFO) 
  245.     {
  246.     ndbout << "CustID = " << (int)STR_CustID << endl;
  247.     ndbout << "Name = " << (char *)STR_Name << endl;
  248.     ndbout << "Address = " << (char *)STR_Address << endl;
  249.     ndbout << "Phone = " << (char *)STR_Phone << endl; 
  250.     Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
  251.     }
  252.   else
  253.    {
  254.     ndbout << "CustID = " << (int)STR_CustID << endl;
  255.     ndbout << "Name = " << (char *)STR_Name << endl;
  256.     ndbout << "Address = " << (char *)STR_Address << endl;
  257.     ndbout << "Phone = " << (char *)STR_Phone << endl;     
  258.    }
  259.  return 0;
  260. }