summaryrefslogtreecommitdiff
path: root/storage/ndb/test/odbc/client/SQLFetchTest.cpp
blob: f8dbd9211177d16bff866121ea27d7e6537183fc (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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* 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 SQLFetchTest.cpp
 */

#include <common.hpp>
#define F_MESSAGE_LENGTH 200
using namespace std;

#define F_NAME_LEN 20
#define F_PHONE_LEN 20
#define F_ADDRESS_LEN 20

SQLHSTMT    F_hstmt;
SQLHDESC    F_hdbc;
SQLHENV     F_henv;
SQLHDESC    F_hdesc;

void SQLFetchTest_DisplayError(SQLSMALLINT F_HandleType, 
			       SQLHDESC F_InputHandle);

/** 
 * Test to advance a cursor to the next row of data in a data result set
 * and to retrieve data from any bound columns that exist for that row 
 * into their associated application variables
 *
 * Tests:
 * _# Test1 Execute statements and display the results 
 * -# Test2 There is no executed statement 
 * @return Zero, if test succeeded
 */
int SQLFetchTest()
{
  SQLRETURN retcode;
  SQLCHAR  SQLStmt[120];
  SQLCHAR  SQLStmt1[120];
  SQLCHAR  SQLStmt2[120];
  SQLCHAR  SQLStmt3[120];
  SQLCHAR  SQLStmt4[120];

  SQLCHAR  F_CustID[20];
  SQLCHAR  F_Name[F_NAME_LEN], F_Phone[F_PHONE_LEN];
  SQLCHAR  F_Address[F_ADDRESS_LEN];

  ndbout << "Start SQLFetch Testing!" << endl;

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

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

  retcode = SQLAllocHandle(SQL_HANDLE_DBC, 
			   F_henv, 
			   &F_hdbc);

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

  //*******************************
  //** Allocate statement handle **
  //*******************************
  
  retcode = SQLAllocHandle(SQL_HANDLE_STMT, 
			   F_hdbc, 
			   &F_hstmt); 
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
    ndbout << "Allocated a statement handle!" << endl;
  
  //************************
  //** Define a statement **
  //************************

  /* *** CustID is Integer *** */
  strcpy((char *) SQLStmt1, "CREATE TABLE Customers (CustID Integer, Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID, Name))");

  /* *** the primary key is alone *** */
// strcpy((char *) SQLStmt1, "CREATE TABLE Customers (CustID Integer, Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID))");

   strcpy((char *) SQLStmt2, "INSERT INTO Customers (CustID, Name, Address,Phone) VALUES(188, 'peter','LM Vag8','7190890')");

  /* *** CustID is Float *** */
//  strcpy((char *) SQLStmt1, 
//	 "CREATE TABLE Customers (CustID float, Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID))");
//  strcpy((char *) SQLStmt2, "INSERT INTO Customers (CustID, Name, Address,Phone) VALUES(1.1516, 'peter','LM Vag8','7190890')");

  /* *** CustID is Char *** */
  // strcpy((char *) SQLStmt1, "CREATE TABLE Customers (CustID char(6), Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID))");

  // strcpy((char *) SQLStmt2, "INSERT INTO Customers (CustID, Name, Address,Phone) VALUES('000001', 'peter','LM Vag8','7190890')");

  /* The UPDATE statements */
  //  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = 1.1516");

  //  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = '000001'");

  //  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = 188"); 

  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = 188 AND Name = 'peter'"); 

  // DELETE statements

  //  DELETE all records
  //  strcpy((char *) SQLStmt4, "DELETE FROM Customers");

  //  DELETE One record
  //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 1.1516");
  //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = '000001'");
  //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 188 AND Name = 'peter'");
  //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 188");

  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 188 AND Name = 'peter'");

  //SELECT statements
  strcpy((char *) SQLStmt, "SELECT * FROM Customers");

  //********************************
  //** Prepare  CREATE statements **
  //********************************

  ndbout << ">>>>" << (char*)SQLStmt1 << "<<<<" << endl;
  retcode = SQLPrepare(F_hstmt, 
		       SQLStmt1, 
		       SQL_NTS);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //******************************************************************
  //** There is no executed statement associated with the allocated **
  //** SQL-statement identified by StatementHandle                  **
  //******************************************************************

  //This function is correct after testing. We don't test again.
  /*
    retcode = SQLFetch(F_hstmt);
    ndbout << endl << "retcode = SQLFetch(F_hstmt) = " << retcode << endl;
    if  (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO)
    {
    ndbout << "There is no executed statement associated with" << endl;
    ndbout << "the allocated SQL-statement" << endl;
    SQLFetchTest_DisplayError(SQL_HANDLE_DESC, F_hstmt);
    
    }
  */

  //*******************************
  //** Execute  CREATE statement **
  //*******************************

  retcode = SQLExecute(F_hstmt);

  if (retcode == 0)
    ndbout << endl << "Execute CREATE TABLE Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //********************************
  //** Prepare  INSERT statements **
  //********************************

  ndbout << ">>>>" << (char*)SQLStmt2 << "<<<<" << endl;
  retcode = SQLPrepare(F_hstmt, 
		       SQLStmt2, 
		       SQL_NTS);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  
  //******************************
  //** Execute INSERT statement **
  //******************************
  retcode = SQLExecute(F_hstmt);

  if (retcode == 0)
  ndbout << endl <<"Execute INSERT Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //********************************
  //** Prepare  UPDATE statements **
  //********************************

  ndbout << ">>>>" << (char*)SQLStmt3 << "<<<<" << endl;
  retcode = SQLPrepare(F_hstmt, 
		       SQLStmt3, 
		       SQL_NTS);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //******************************
  //** Execute UPDATE statement **
  //******************************
  retcode = SQLExecute(F_hstmt);

  if (retcode == 0)
  ndbout << endl <<"Execute UPDATE Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //********************************
  //** Prepare  DELETE statements **
  //********************************
  ndbout << ">>>>" << (char*)SQLStmt4 << "<<<<" << endl;
  retcode = SQLPrepare(F_hstmt, 
		       SQLStmt4, 
		       SQL_NTS);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    {
      ndbout << endl << "Preparing DELETE Statement failure!" << endl;
      SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
    }

  //******************************
  //** Execute DELETE statement **
  //******************************

  retcode = SQLExecute(F_hstmt);

  if (retcode == 0)
  ndbout << endl <<"Execute DELETE Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    {
      ndbout << "DELETE Statement executing failure!" << endl;
      SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
    }
  //********************************
  //** Prepare  SELECT statements **
  //********************************
  ndbout << ">>>>" << (char*)SQLStmt << "<<<<" << endl;
  retcode = SQLPrepare(F_hstmt, 
		       SQLStmt, 
		       SQL_NTS);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  /*
  //******************************
  //** Execute SELECT statement **
  //******************************

  retcode = SQLExecute(F_hstmt);

  if (retcode == 0)
  ndbout << endl <<"Execute SELECT Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  */

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

  retcode =SQLBindCol(F_hstmt, 
		      1, 
		      SQL_C_CHAR, 
		      F_CustID, 
		      sizeof(F_CustID),
		      NULL);
  ndbout << endl << "Bind Col1 retcode = " << retcode << " OK!" << endl;

  retcode =SQLBindCol(F_hstmt, 
		      2, 
		      SQL_C_CHAR, 
		      F_Name, 
		      F_NAME_LEN,
		      NULL);

  ndbout << "Bind Col2 retcode = " << retcode << " OK!" << endl;

  retcode = SQLBindCol(F_hstmt, 
		       3,
		       SQL_C_CHAR, 
		       F_Address, 
		       F_ADDRESS_LEN, 
		       NULL);

  ndbout << "Bind Col3 retcode = " << retcode << " OK!" << endl;

  retcode = SQLBindCol(F_hstmt, 
		       4, 
		       SQL_C_CHAR, 
		       F_Phone, 
		       F_PHONE_LEN,
		       NULL);

  ndbout << "Bind Col4 retcode = " << retcode << " OK!" << endl;

  //******************************
  //** Execute SELECT statement **
  //******************************

  retcode = SQLExecute(F_hstmt);

  if (retcode == 0)
  ndbout << endl <<"Execute SELECT Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //***************
  //* Fetch data **
  //***************
  ndbout << endl <<"Executing Fetch SELECT Statement ......" << endl;

  retcode = SQLFetch(F_hstmt);

  if (retcode == 100)
    ndbout << endl <<"Execute Fetch SELECT Statement, But No DATA!" << endl;

  if (retcode == 0)
  ndbout << endl <<"Execute Fetch SELECT Statement OK!" << endl;

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
    SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);

  //*******************
  //* Display result **
  //*******************
  ndbout << endl << "The results is : " << endl;
  ndbout << "CustID = " << (char *)F_CustID << endl;
  ndbout << "Name = " << (char *)F_Name << endl;
  ndbout << "Address = " << (char *)F_Address << endl;
  ndbout << "Phone = " << (char *)F_Phone << endl;


  // *********************************
  // ** Disconnect and Free Handles **
  // *********************************  
  SQLDisconnect(F_hdbc);
  SQLFreeHandle(SQL_HANDLE_STMT, F_hstmt);
  SQLFreeHandle(SQL_HANDLE_DBC, F_hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, F_henv);

  return NDBT_OK;

 }


void SQLFetchTest_DisplayError(SQLSMALLINT F_HandleType, 
			       SQLHSTMT F_InputHandle)
{
  SQLCHAR     Sqlstate[50], Msg[F_MESSAGE_LENGTH];
  SQLRETURN   SQLSTATEs;
  SQLINTEGER  NativeError;
  SQLSMALLINT i, MsgLen;
  Msg[0] = 0;
  i = 1;
  
  ndbout << "-------------------------------------------------" << endl;
  ndbout << "Error diagnostics:" << endl;
  
  while ((SQLSTATEs = SQLGetDiagRec(F_HandleType, 
				    F_InputHandle, 
				    i, 
				    Sqlstate, 
				    &NativeError, 
				    Msg, 
				    sizeof(Msg), 
				    &MsgLen)) 
	 != SQL_NO_DATA)                   
{
    
  ndbout << "the HandleType is:" << F_HandleType << endl;
  ndbout << "the InputHandle is :" <<(long)F_InputHandle << endl;
  ndbout << "the Msg is :" << (char *)Msg << endl;
  ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  
  i ++;
  break;
}
  ndbout << "-------------------------------------------------" << endl;  
}