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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* Copyright (C) 2003 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
* @file SQLExecDirectTest.cpp
*/
#include <common.hpp>
#define EXD_MESSAGE_LENGTH 200
#define EXD_NAME_LEN 10
#define EXD_PHONE_LEN 10
#define EXD_ADDRESS_LEN 10
using namespace std;
SQLHDBC EXD_hdbc;
SQLHSTMT EXD_hstmt;
SQLHENV EXD_henv;
SQLHDESC EXD_hdesc;
SQLRETURN EXD_ret, SQLSTATEs;
void ExecDirect_DisplayError(SQLSMALLINT EXD_HandleType,
SQLHSTMT EXD_InputHandle);
int EXD_Display_Result(SQLHSTMT EXDR_InputHandle);
/**
* Test to execute a prepared ststement
*
* -# Normal case: Prepare and Execute a prepared statement
* -# Prepare and Execute an empty statement
* -# Prepare and Execute a statement with wrong henv handle
* -# Prepare and Execute a statement with wrong hdbc handle
* -# Prepare and Execute a statement with wrong hdesc handle
* @return Zero, if test succeeded
*/
int SQLExecDirectTest()
{
ndbout << endl << "Start ExecDirect Testing" << endl;
//************************************
//** Allocate An Environment Handle **
//************************************
EXD_ret = SQLAllocHandle(SQL_HANDLE_ENV,
SQL_NULL_HANDLE,
&EXD_henv);
if (EXD_ret == SQL_SUCCESS || EXD_ret == SQL_SUCCESS_WITH_INFO)
ndbout << "Allocated an environment Handle!" << endl;
//*********************************************
//** Set the ODBC application Version to 3.x **
//*********************************************
EXD_ret = SQLSetEnvAttr(EXD_henv,
SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3,
SQL_IS_UINTEGER);
if (EXD_ret == SQL_SUCCESS || EXD_ret == SQL_SUCCESS_WITH_INFO)
ndbout << "Set the ODBC application Version to 3.x!" << endl;
//**********************************
//** Allocate A Connection Handle **
//**********************************
EXD_ret = SQLAllocHandle(SQL_HANDLE_DBC,
EXD_henv,
&EXD_hdbc);
if (EXD_ret == SQL_SUCCESS || EXD_ret == SQL_SUCCESS_WITH_INFO)
ndbout << "Allocated a connection Handle!" << endl;
// *******************
// ** Connect to DB **
// *******************
EXD_ret = SQLConnect(EXD_hdbc,
(SQLCHAR *) connectString(),
SQL_NTS,
(SQLCHAR *) "",
SQL_NTS,
(SQLCHAR *) "",
SQL_NTS);
if (EXD_ret == SQL_SUCCESS || EXD_ret == SQL_SUCCESS_WITH_INFO)
ndbout << "Connected to DB : OK!" << endl;
else
{
ndbout << "Failure to Connect DB!" << endl;
return NDBT_FAILED;
}
//*******************************
//** Allocate statement handle **
//*******************************
EXD_ret = SQLAllocHandle(SQL_HANDLE_STMT,
EXD_hdbc,
&EXD_hstmt);
if(EXD_ret == SQL_SUCCESS || EXD_ret == SQL_SUCCESS_WITH_INFO)
ndbout << "Allocated a statement handle!" << endl;
//**********************************************
//** Test1 **
//** Prepare and Execute a prepared statement **
//**********************************************
EXD_ret = SQLExecDirect(EXD_hstmt,
(SQLCHAR*)"SELECT * FROM Customers",
SQL_NTS);
if (EXD_ret == SQL_INVALID_HANDLE)
{
ndbout << "Handle Type is SQL_HANDLE_STMT, but SQL_INVALID_HANDLE" << endl;
ndbout << "still appeared. Please check program" << endl;
}
if (EXD_ret == SQL_ERROR || EXD_ret == SQL_SUCCESS_WITH_INFO)
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXD_hstmt);
//*************************
//** Display the results **
//*************************
EXD_Display_Result(EXD_hstmt);
//*******************************************
//** Test2 **
//** Prepare and Execute an empty statement**
//** in order to see what will happen **
//*******************************************
EXD_ret = SQLExecDirect(EXD_hstmt,
(SQLCHAR*)" ",
SQL_NTS);
if (EXD_ret == SQL_ERROR || EXD_ret == SQL_SUCCESS_WITH_INFO)
{
ndbout << "Prepare and Execute an empty statement," << endl;
ndbout << "The following case happened!" << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXD_hstmt);
}
//***************************************************************
//** Test3 **
//** Prepare and Execute a statement with wrong henv handle **
//** in order to see what will happen **
//***************************************************************
EXD_ret = SQLExecDirect(EXD_henv,
(SQLCHAR*)"SELECT * FROM Customers",
SQL_NTS);
if (EXD_ret == SQL_SUCCESS_WITH_INFO || EXD_ret == SQL_SUCCESS)
{ ndbout << "Handle Type is SQL_HANDLE_HENV, but SQL_INVALID_HANDLE" << endl;
ndbout << "still appeared. Please check programm" << endl;
ExecDirect_DisplayError(SQL_HANDLE_ENV, EXD_henv);
}
//******************************************************************
//** Test4 **
//** Prepare and Execute a statement with wrong hdbc handle **
//** in order to see what will happen **
//******************************************************************
EXD_ret = SQLExecDirect(EXD_hdbc,
(SQLCHAR*)"SELECT * FROM Customers",
SQL_NTS);
if (EXD_ret == SQL_SUCCESS_WITH_INFO || EXD_ret == SQL_SUCCESS)
ExecDirect_DisplayError(SQL_HANDLE_DBC, EXD_hdbc);
//*******************************************************************
//** Test5 **
//** Prepare and Execute a statement with wrong hdesc handle **
//** in order to see what will happen **
//*******************************************************************
EXD_ret = SQLExecDirect(EXD_hdesc,
(SQLCHAR*)"SELECT * FROM Customers",
SQL_NTS);
if (EXD_ret == SQL_SUCCESS_WITH_INFO || EXD_ret == SQL_SUCCESS)
{
ndbout << "Handle Type is SQL_HANDLE_DESC, but SQL_SUCCESS_WITH_INFO" <<endl;
ndbout << "appeared. Please check program" << endl;
ExecDirect_DisplayError(SQL_HANDLE_DESC, EXD_hdesc);
}
// *********************************
// ** Disconnect and Free Handles **
// *********************************
SQLDisconnect(EXD_hdbc);
SQLFreeHandle(SQL_HANDLE_STMT, EXD_hstmt);
SQLFreeHandle(SQL_HANDLE_DBC, EXD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, EXD_henv);
return NDBT_OK;
}
void ExecDirect_DisplayError(SQLSMALLINT EXD_HandleType,
SQLHSTMT EXD_InputHandle)
{
SQLCHAR EXD_Sqlstate[5];
SQLINTEGER EXD_NativeError;
SQLSMALLINT EXD_i, EXD_MsgLen;
SQLCHAR EXD_Msg[EXD_MESSAGE_LENGTH];
SQLRETURN SQLSTATEs;
EXD_i = 1;
ndbout << "-------------------------------------------------" << endl;
ndbout << "Error diagnostics:" << endl;
while ((SQLSTATEs = SQLGetDiagRec(EXD_HandleType,
EXD_InputHandle,
EXD_i,
EXD_Sqlstate,
&EXD_NativeError,
EXD_Msg,
sizeof(EXD_Msg),
&EXD_MsgLen))
!= SQL_NO_DATA) {
ndbout << "the HandleType is:" << EXD_HandleType << endl;
ndbout << "the InputHandle is :" << (long)EXD_InputHandle << endl;
ndbout << "the ColAtt_Msg is: " << (char *) EXD_Msg << endl;
ndbout << "the output state is:" << (char *)EXD_Sqlstate << endl;
EXD_i ++;
// break;
}
ndbout << "-------------------------------------------------" << endl;
}
int EXD_Display_Result(SQLHSTMT EXDR_InputHandle)
{
SQLRETURN EXD_retcode;
unsigned long EXD_CustID;
SQLCHAR EXD_Name[EXD_NAME_LEN], EXD_Phone[EXD_PHONE_LEN];
SQLCHAR EXD_Address[EXD_ADDRESS_LEN];
//*********************
//** Bind columns 1 **
//*********************
EXD_retcode =SQLBindCol(EXDR_InputHandle,
1,
SQL_C_ULONG,
&EXD_CustID,
sizeof(EXD_CustID),
NULL);
if (EXD_retcode == SQL_ERROR)
{
ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXDR_InputHandle);
return NDBT_FAILED;
}
//*********************
//** Bind columns 2 **
//*********************
EXD_retcode =SQLBindCol(EXDR_InputHandle,
2,
SQL_C_CHAR,
&EXD_Name,
EXD_NAME_LEN,
NULL);
if (EXD_retcode == SQL_ERROR)
{
ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXDR_InputHandle);
return NDBT_FAILED;
}
//*********************
//** Bind columns 3 **
//*********************
EXD_retcode = SQLBindCol(EXDR_InputHandle,
3,
SQL_C_CHAR,
&EXD_Address,
EXD_ADDRESS_LEN,
NULL);
if (EXD_retcode == SQL_ERROR)
{
ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXDR_InputHandle);
return NDBT_FAILED;
}
//*********************
//** Bind columns 4 **
//*********************
EXD_retcode = SQLBindCol(EXDR_InputHandle,
4,
SQL_C_CHAR,
&EXD_Phone,
EXD_PHONE_LEN,
NULL);
if (EXD_retcode == SQL_ERROR)
{
ndbout << "Executing SQLBindCol, SQL_ERROR happened!" << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXDR_InputHandle);
return NDBT_FAILED;
}
//*****************************************
//* Fetch and print each row of data. On **
//* an error, display a message and exit **
//*****************************************
if (EXD_retcode != SQL_ERROR)
EXD_retcode = SQLFetch(EXDR_InputHandle);
ndbout << endl << "EXD_retcode = SQLFetch(EXDR_InputHandle) = "
<< EXD_retcode << endl;
if (EXD_retcode == SQL_ERROR)
{
ndbout << "Executing SQLFetch, SQL_ERROR happened!" << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXDR_InputHandle);
return NDBT_FAILED;
}
else if (EXD_retcode == SQL_SUCCESS_WITH_INFO)
{
ndbout << "CustID = " << (int)EXD_CustID << endl;
ndbout << "Name = " << (char *)EXD_Name << endl;
ndbout << "Address = " << (char *)EXD_Address << endl;
ndbout << "Phone = " << (char *)EXD_Phone << endl;
ExecDirect_DisplayError(SQL_HANDLE_STMT, EXDR_InputHandle);
}
else
{
ndbout << "CustID = " << (int)EXD_CustID << endl;
ndbout << "Name = " << (char *)EXD_Name << endl;
ndbout << "Address = " << (char *)EXD_Address << endl;
ndbout << "Phone = " << (char *)EXD_Phone << endl;
}
return 0;
}
|