summaryrefslogtreecommitdiff
path: root/storage/ndb/test/odbc/client/SQLTransactTest.cpp
blob: 427765f131b7b152a2933b406bcf9f886c81a483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Copyright (c) 2003, 2005 MySQL AB
   Use is subject to license terms

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */

 /**
 * @file SQLTransactTest.cpp
 */
#include <common.hpp>
#define STR_MESSAGE_LENGTH 200
#define STR_NAME_LEN 20
#define STR_PHONE_LEN 20
#define STR_ADDRESS_LEN 20
using namespace std;

SQLHDBC     STR_hdbc;
SQLHSTMT    STR_hstmt;
SQLHENV     STR_henv;
SQLHDESC    STR_hdesc;
       
void Transact_DisplayError(SQLSMALLINT STR_HandleType, 
			   SQLHSTMT STR_InputHandle);

int STR_Display_Result(SQLHSTMT EXDR_InputHandle);

/** 
 * Test:
 * -#Test to request a commit or a rollback operation for
 *   all active transactions associated with a specific
 *   environment or connection handle
 *
 * @return Zero, if test succeeded
 */

int SQLTransactTest()
{
  SQLRETURN   STR_ret;

  ndbout << endl << "Start SQLTransact Testing" << endl;

  //************************************
  //** Allocate An Environment Handle **
  //************************************
  STR_ret = SQLAllocHandle(SQL_HANDLE_ENV, 
			   SQL_NULL_HANDLE, 
			   &STR_henv);
  
  if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
    ndbout << "Allocated an environment Handle!" << endl;
  
  //*********************************************
  //** Set the ODBC application Version to 3.x **
  //*********************************************
  STR_ret = SQLSetEnvAttr(STR_henv, 
			  SQL_ATTR_ODBC_VERSION, 
			  (SQLPOINTER) SQL_OV_ODBC3,
			  SQL_IS_UINTEGER);
  
  if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
    ndbout << "Set the ODBC application Version to 3.x!" << endl;

  //**********************************
  //** Allocate A Connection Handle **
  //**********************************

  STR_ret = SQLAllocHandle(SQL_HANDLE_DBC, 
			   STR_henv, 
			   &STR_hdbc);

  if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
    ndbout << "Allocated a connection Handle!" << endl;
  
  // *******************
  // ** Connect to DB **
  // *******************
  STR_ret = SQLConnect(STR_hdbc, 
		       (SQLCHAR *) connectString(), 
		       SQL_NTS, 
		       (SQLCHAR *) "", 
		       SQL_NTS, 
		       (SQLCHAR *) "", 
		       SQL_NTS);
  
  if (STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO)
    ndbout << "Connected to DB : OK!" << endl;
  else 
    {  
      ndbout << "Failure to Connect DB!" << endl;
      return NDBT_FAILED;
    }

  //*******************************
  //** Allocate statement handle **
  //*******************************
  
  STR_ret = SQLAllocHandle(SQL_HANDLE_STMT, 
			   STR_hdbc, 
			   &STR_hstmt); 
  if(STR_ret == SQL_SUCCESS || STR_ret == SQL_SUCCESS_WITH_INFO) 
    ndbout << "Allocated a statement handle!" << endl;

  //********************************
  //** Turn Manual-Commit Mode On **
  //********************************
  STR_ret = SQLSetConnectOption(STR_hdbc, 
				SQL_AUTOCOMMIT, 
				(UDWORD) SQL_AUTOCOMMIT_OFF);

  //**********************************************
  //** Prepare and Execute a prepared statement **
  //**********************************************
  STR_ret = SQLExecDirect(STR_hstmt, 
			  (SQLCHAR*)"SELECT * FROM Customers", 
			  SQL_NTS);

  if (STR_ret == SQL_INVALID_HANDLE)
  {   
    ndbout << "Handle Type is SQL_HANDLE_STMT, but SQL_INVALID_HANDLE" 
	   << endl;
    ndbout << "still appeared. Please check program" << endl;
  }

  if (STR_ret == SQL_ERROR || STR_ret == SQL_SUCCESS_WITH_INFO)
    Transact_DisplayError(SQL_HANDLE_STMT, STR_hstmt);

  //*************************
  //** Display the results **
  //*************************

  STR_Display_Result(STR_hstmt);

  //****************************
  //** Commit the transaction **
  //****************************
  STR_ret = SQLTransact(STR_henv, 
			STR_hdbc, 
			SQL_COMMIT);

  //****************
  // Free Handles **
  //****************
  SQLDisconnect(STR_hdbc);
  SQLFreeHandle(SQL_HANDLE_STMT, STR_hstmt);
  SQLFreeHandle(SQL_HANDLE_DBC, STR_hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, STR_henv);

  return NDBT_OK;

 }

void Transact_DisplayError(SQLSMALLINT STR_HandleType, 
			     SQLHSTMT STR_InputHandle)
{
     SQLCHAR STR_Sqlstate[5];
     SQLINTEGER   STR_NativeError;
     SQLSMALLINT  STR_i, STR_MsgLen;
     SQLCHAR      STR_Msg[STR_MESSAGE_LENGTH];
     SQLRETURN    SQLSTATEs;
     STR_i = 1;

     ndbout << "-------------------------------------------------" << endl;
     ndbout << "Error diagnostics:" << endl;
  
     while ((SQLSTATEs = SQLGetDiagRec(STR_HandleType, 
				       STR_InputHandle, 
				       STR_i, 
				       STR_Sqlstate, 
				       &STR_NativeError, 
				       STR_Msg, 
				       sizeof(STR_Msg), 
				       &STR_MsgLen)) 
	    != SQL_NO_DATA)                   {

     ndbout << "the HandleType is:" << STR_HandleType << endl;
     ndbout << "the InputHandle is :" << (long)STR_InputHandle << endl;
     ndbout << "the STR_Msg is: " << (char *) STR_Msg << endl;
     ndbout << "the output state is:" << (char *)STR_Sqlstate << endl; 

     STR_i ++;
     //     break;
                                                       }
     ndbout << "-------------------------------------------------" << endl;
}

int STR_Display_Result(SQLHSTMT STR_InputHandle)
{
  SQLRETURN   STR_retcode;
  unsigned long  STR_CustID;
  SQLCHAR     STR_Name[STR_NAME_LEN], STR_Phone[STR_PHONE_LEN];
  SQLCHAR     STR_Address[STR_ADDRESS_LEN];

  //*********************
  //** Bind columns  1 **
  //*********************
  STR_retcode =SQLBindCol(STR_InputHandle, 
			  1, 
			  SQL_C_ULONG, 
			  &STR_CustID, 
			  sizeof(STR_CustID),
			  NULL);
      if (STR_retcode == SQL_ERROR)
	{ 
	  ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
	  Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
	  return NDBT_FAILED;
	}

  //*********************
  //** Bind columns  2 **
  //*********************

  STR_retcode =SQLBindCol(STR_InputHandle, 
			  2, 
			  SQL_C_CHAR, 
			  &STR_Name, 
			  STR_NAME_LEN,
			  NULL);
      if (STR_retcode == SQL_ERROR)
	{ 
	  ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
	  Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
	  return NDBT_FAILED;
	}

  //*********************
  //** Bind columns 3  **
  //*********************

      STR_retcode = SQLBindCol(STR_InputHandle, 
			       3,
			       SQL_C_CHAR, 
			       &STR_Address, 
			       STR_ADDRESS_LEN, 
			       NULL);

      if (STR_retcode == SQL_ERROR)
	{ 
	  ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
	  Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
	  return NDBT_FAILED;
	}

  //*********************
  //** Bind columns 4  **
  //*********************

      STR_retcode = SQLBindCol(STR_InputHandle, 
			       4, 
			       SQL_C_CHAR, 
			       &STR_Phone, 
			       STR_PHONE_LEN,
			       NULL);

      if (STR_retcode == SQL_ERROR)
	{ 
	  ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
	  Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
	  return NDBT_FAILED;
	}

  //*****************************************
  //* Fetch and print each row of data. On **
  //* an error, display a message and exit **
  //*****************************************
 
  if (STR_retcode != SQL_ERROR)
  STR_retcode = SQLFetch(STR_InputHandle);

  ndbout << endl << "STR_retcode = SQLFetch(STR_InputHandle) = " 
	 << STR_retcode << endl;

  if (STR_retcode == SQL_ERROR)
    { 
      ndbout << "Executing SQLFetch, SQL_ERROR happened!" << endl;
      Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
      return NDBT_FAILED;
    }
  else if (STR_retcode == SQL_SUCCESS_WITH_INFO) 
    {
    ndbout << "CustID = " << (int)STR_CustID << endl;
    ndbout << "Name = " << (char *)STR_Name << endl;
    ndbout << "Address = " << (char *)STR_Address << endl;
    ndbout << "Phone = " << (char *)STR_Phone << endl; 
    Transact_DisplayError(SQL_HANDLE_STMT, STR_InputHandle);
    }
  else
   {
    ndbout << "CustID = " << (int)STR_CustID << endl;
    ndbout << "Name = " << (char *)STR_Name << endl;
    ndbout << "Address = " << (char *)STR_Address << endl;
    ndbout << "Phone = " << (char *)STR_Phone << endl;     
   }
 return 0;
}