~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to storage/ndb/test/odbc/client/SQLGetDataTest.cpp

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 /**
 
17
 * @file SQLGetDataTest.cpp
 
18
 */
 
19
 
 
20
#include <common.hpp>
 
21
using namespace std;
 
22
 
 
23
#define GD_MESSAGE_LENGTH 200
 
24
 
 
25
SQLHSTMT     GD_hstmt;
 
26
SQLHENV      GD_henv;
 
27
SQLHDBC      GD_hdbc;
 
28
SQLHDESC     GD_hdesc;
 
29
 
 
30
void GetData_DisplayError(SQLSMALLINT GD_HandleType, SQLHSTMT GD_InputHandle);
 
31
 
 
32
/** 
 
33
 * Test to retrieve data for a single unbound column
 
34
 * in the current row of a result data set
 
35
 *
 
36
 * Tests:
 
37
 * -# Test1 There is no fetched rowset associated with S
 
38
 * -# Test2 column number is less than zero
 
39
 * -# Test3 fetched rowset is empty
 
40
 * @return Zero, if test succeeded
 
41
 */
 
42
 
 
43
int SQLGetDataTest()
 
44
{
 
45
  SQLRETURN    retcode;
 
46
  SQLCHAR      ColumnName;
 
47
  SQLINTEGER   CustID;
 
48
  //  SQLCHAR      Name, Address, Phone;
 
49
  SQLCHAR SQLStmt [120];
 
50
  SQLCHAR SQLStmt1 [120];
 
51
 
 
52
  //************************************
 
53
  //** Allocate An Environment Handle **
 
54
  //************************************
 
55
  retcode = SQLAllocHandle(SQL_HANDLE_ENV, 
 
56
                           SQL_NULL_HANDLE, 
 
57
                           &GD_henv);
 
58
  
 
59
  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
60
    ndbout << "Allocated an environment Handle!" << endl;
 
61
 
 
62
  //*********************************************
 
63
  //** Set the ODBC application Version to 3.x **
 
64
  //*********************************************
 
65
  retcode = SQLSetEnvAttr(GD_henv,
 
66
                          SQL_ATTR_ODBC_VERSION, 
 
67
                          (SQLPOINTER) SQL_OV_ODBC3, 
 
68
                          SQL_IS_UINTEGER);
 
69
  
 
70
  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
71
    ndbout << "Set the ODBC application Version to 3.X!" << endl;
 
72
 
 
73
  //**********************************
 
74
  //** Allocate A Connection Handle **
 
75
  //**********************************
 
76
 
 
77
  retcode = SQLAllocHandle(SQL_HANDLE_DBC, 
 
78
                           GD_henv, 
 
79
                           &GD_hdbc);
 
80
 
 
81
  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
82
    ndbout << "Allocated a connection Handle!" << endl;
 
83
 
 
84
  // *******************
 
85
  // ** Connect to DB **
 
86
  // *******************
 
87
  retcode = SQLConnect(GD_hdbc, 
 
88
                       (SQLCHAR *) connectString(), 
 
89
                       SQL_NTS, 
 
90
                       (SQLCHAR *) "", 
 
91
                       SQL_NTS, 
 
92
                       (SQLCHAR *) "", 
 
93
                       SQL_NTS);
 
94
  
 
95
  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
96
    ndbout << "Connected to DB : OK!" << endl;
 
97
  else 
 
98
    {  
 
99
      ndbout << "Failure to Connect DB!" << endl;
 
100
      return NDBT_FAILED;
 
101
    }
 
102
 
 
103
  //*******************************
 
104
  //** Allocate statement handle **
 
105
  //*******************************
 
106
  
 
107
  retcode = SQLAllocHandle(SQL_HANDLE_STMT, 
 
108
                           GD_hdbc, 
 
109
                           &GD_hstmt); 
 
110
  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
 
111
    ndbout << "Allocated a statement handle!" << endl;
 
112
 
 
113
  //*****************************
 
114
  //** Define SELECT statement **
 
115
  //*****************************
 
116
 
 
117
  strcpy((char *) SQLStmt, "SELECT * FROM Customers");
 
118
 
 
119
 
 
120
  //***********************************
 
121
  //** Prepare SELECT SQL statement  **
 
122
  //***********************************
 
123
 
 
124
  retcode = SQLPrepare(GD_hstmt, 
 
125
                       SQLStmt, 
 
126
                       SQL_NTS);
 
127
  ndbout << endl << "Preparing SELECT, retcode = SQLprepare()= " 
 
128
         << retcode << endl;
 
129
 
 
130
  //*********************************
 
131
  //** Execute prepared statement  **
 
132
  //*********************************
 
133
 
 
134
  //  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
 
135
  //{
 
136
 
 
137
  retcode = SQLExecute(GD_hstmt);
 
138
 
 
139
  ndbout << "Exexuting SELECT, retcode = SQLExecute()= " 
 
140
         << retcode << endl;
 
141
 
 
142
  //  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
 
143
  //    {
 
144
 
 
145
      //*****************************************************************
 
146
      //** Test1                                                       **
 
147
      //** There is no fetched rowset associated with S(SQL-statement) **
 
148
      //*****************************************************************
 
149
      
 
150
      retcode = SQLGetData(GD_hstmt, 
 
151
                           1, 
 
152
                           SQL_C_SLONG, 
 
153
                           &CustID,
 
154
                           sizeof(CustID), 
 
155
                           NULL);
 
156
      ndbout << "retcode = SQLGetData()= " << retcode << endl;      
 
157
 
 
158
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
159
        {
 
160
          ndbout << endl << "Test 1:" << endl;
 
161
          ndbout << "There is no fetched rowset associated with SQL" 
 
162
                 << " statement. But system reported SUCCESS or" 
 
163
                 << " SUCCESS_WITH_INFO. Please check the function!" << endl;
 
164
          GetData_DisplayError(SQL_HANDLE_STMT, GD_hstmt);
 
165
        }
 
166
      else if (retcode == SQL_ERROR)
 
167
        {
 
168
          ndbout << endl << "Test 1:" << endl;
 
169
          ndbout << "There is no fetched rowset associated with SQL" 
 
170
                 << " statement. The system reported ERROR " 
 
171
                 << " The function is OK!" << endl;
 
172
        }      
 
173
      else 
 
174
        ndbout << endl;
 
175
 
 
176
      //*******************************
 
177
      //** Fetch Data from database  **
 
178
      //*******************************
 
179
 
 
180
      retcode = SQLFetch(GD_hstmt);
 
181
 
 
182
      ndbout << endl 
 
183
             << "Fetching after Executing SELECT, retcode = SQLFetch()= " 
 
184
             << retcode << endl;  
 
185
 
 
186
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
187
        {
 
188
          
 
189
          //**************************************
 
190
          //** Test2                            **
 
191
          //** column number is less than zero  **
 
192
          //**************************************
 
193
          
 
194
          retcode = SQLGetData(GD_hstmt, 
 
195
                               0, 
 
196
                               SQL_C_ULONG, 
 
197
                               &CustID, 
 
198
                               sizeof(CustID), 
 
199
                               NULL);
 
200
          
 
201
          if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
202
            {
 
203
              ndbout << "Test 2:" <<"Column number is less than zero" 
 
204
                     << " The system reported SUCCESS or SUCCESS_WITH_INFO." 
 
205
                     << " Check the function, please!" <<endl;
 
206
              GetData_DisplayError(SQL_HANDLE_STMT, GD_hstmt);
 
207
            }    
 
208
          else if (retcode == SQL_ERROR)
 
209
            {
 
210
              ndbout << "Test 2:" << "Column number is less than zero." 
 
211
                     << " The system reported SQL_ERROR." 
 
212
                     << " The function is OK!" << endl; 
 
213
            }
 
214
          else
 
215
            ndbout << endl;
 
216
        }
 
217
      //    } 
 
218
    
 
219
      //  } 
 
220
 
 
221
  //*****************************
 
222
  //** Define DELETE statement **
 
223
  //*****************************
 
224
 
 
225
      //  strcpy((char *) SQLStmt1, "DELETE FROM Customers");
 
226
  strcpy((char *) SQLStmt1, "DELETE FROM Customers WHERE CustID = 568 AND Name = 'Hans  Peter'");
 
227
 
 
228
  //***********************************
 
229
  //** Prepare DELETE SQL statement  **
 
230
  //***********************************
 
231
 
 
232
  retcode = SQLPrepare(GD_hstmt, 
 
233
                       SQLStmt1, 
 
234
                       SQL_NTS);
 
235
  ndbout << endl << "Preparing DELETE, retcode = SQLPrepare()= " 
 
236
         << retcode << endl;
 
237
 
 
238
  //****************************************
 
239
  //** Execute prepared DELETE statement **
 
240
  //****************************************
 
241
 
 
242
  //  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
 
243
  //    {
 
244
 
 
245
     retcode = SQLExecute(GD_hstmt);
 
246
 
 
247
     ndbout << "Executing DELETE, retcode = SQLExecute()= " 
 
248
            << retcode << endl;
 
249
      
 
250
      //      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
 
251
      //        {
 
252
          
 
253
          retcode = SQLFetch(GD_hstmt);
 
254
 
 
255
          ndbout << "Fetching after Executing DELETE, retcode = SQLExecute()= " 
 
256
                 << retcode << endl;
 
257
 
 
258
          if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
 
259
            {
 
260
              
 
261
              //******************************************************
 
262
              //** Test3                                            **
 
263
              //** If the fetched rowset associated with            **
 
264
              //** Statement is empty, condition is raised: NO DATA **
 
265
              //** We can delete all rows in table Customers for    **
 
266
              //** this case                                        **
 
267
              //******************************************************
 
268
              
 
269
              retcode = SQLGetData(GD_hstmt, 
 
270
                                   1, 
 
271
                                   SQL_C_ULONG, 
 
272
                                   &CustID, 
 
273
                                   sizeof(CustID), 
 
274
                                   NULL);
 
275
              
 
276
              if (retcode == SQL_ERROR)
 
277
                {
 
278
                  ndbout << "Test 3:" << endl;
 
279
                  ndbout << "The fetched rowset associated" 
 
280
                         << "with Statementhandle is empty. The system" 
 
281
                         << " reported SQL_ERROR. Check the function!" << endl;
 
282
                  GetData_DisplayError(SQL_HANDLE_STMT, GD_hstmt);        
 
283
                }
 
284
              else if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
285
                {
 
286
                  ndbout << "Test 3:" << endl;
 
287
                  ndbout << "The fetched rowset associated" 
 
288
                         << "with Statementhandle is empty. The system" 
 
289
                         << " reported SUCCESS. Check the function!" << endl;
 
290
                  GetData_DisplayError(SQL_HANDLE_STMT, GD_hstmt);        
 
291
                }
 
292
              else if (retcode == 100)
 
293
                {
 
294
                  ndbout << "Test 3:" << endl;
 
295
                  ndbout << "The fetched rowset associated" 
 
296
                         << "with Statementhandle is empty. The system" 
 
297
                         << " reported SQL_NO_DATA. The function is OK!" << endl;
 
298
                }
 
299
            } 
 
300
          else if (retcode == SQL_ERROR)
 
301
            { 
 
302
              ndbout << "Test 3 falied!" << endl;
 
303
              GetData_DisplayError(SQL_HANDLE_STMT, GD_hstmt);
 
304
            }
 
305
          else 
 
306
            ndbout << " " << endl;
 
307
 
 
308
          //    }      
 
309
          //    }
 
310
 
 
311
  // *********************************
 
312
  // ** Disconnect and Free Handles **
 
313
  // *********************************  
 
314
  SQLDisconnect(GD_hdbc);
 
315
  SQLFreeHandle(SQL_HANDLE_STMT, GD_hstmt);
 
316
  SQLFreeHandle(SQL_HANDLE_DBC, GD_hdbc);
 
317
  SQLFreeHandle(SQL_HANDLE_ENV, GD_henv);
 
318
  
 
319
  return NDBT_OK;
 
320
}
 
321
 
 
322
void GetData_DisplayError(SQLSMALLINT GD_HandleType, SQLHSTMT GD_InputHandle)
 
323
{
 
324
  
 
325
  SQLSMALLINT  i, MsgLen;
 
326
  SQLRETURN    SQLSTATEs;
 
327
  SQLCHAR      Sqlstate[5], Msg[GD_MESSAGE_LENGTH];
 
328
  SQLINTEGER   NativeError;
 
329
  i = 1;
 
330
  
 
331
  ndbout << "-------------------------------------------------" << endl;
 
332
  ndbout << "Error diagnostics:" << endl;
 
333
  
 
334
  while ((SQLSTATEs = SQLGetDiagRec(GD_HandleType, 
 
335
                                    GD_InputHandle, 
 
336
                                    i, 
 
337
                                    Sqlstate, 
 
338
                                    &NativeError, 
 
339
                                    Msg, 
 
340
                                    sizeof(Msg), 
 
341
                                    &MsgLen)) 
 
342
         != SQL_NO_DATA)  
 
343
    {
 
344
      
 
345
      ndbout << "the HandleType is:" << GD_HandleType << endl;
 
346
      ndbout << "the InputHandle is :" << (long)GD_InputHandle << endl;
 
347
      ndbout << "Phone = " << (char *)Msg << endl;
 
348
      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
 
349
      
 
350
      i ++;
 
351
      break;
 
352
    }
 
353
  ndbout << "-------------------------------------------------" << endl;
 
354
}
 
355
 
 
356
 
 
357