~vadim-tk/percona-server/flushing-algo

« back to all changes in this revision

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

  • Committer: root
  • Date: 2011-10-29 01:34:40 UTC
  • Revision ID: root@hppro1.office.percona.com-20111029013440-qhnf4jk8kdjcf4e0
Initial import

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 SQLprepareTest.cpp
 
18
 */
 
19
#include <common.hpp>
 
20
#define pare_SQL_MAXIMUM_MESSAGE_LENGTH 200
 
21
 
 
22
using namespace std;
 
23
 
 
24
SQLHDBC     pare_hdbc;
 
25
SQLHSTMT    pare_hstmt;
 
26
SQLHENV     pare_henv;
 
27
SQLHDESC    pare_hdesc;
 
28
SQLRETURN   pare_retcode, pare_SQLSTATEs;
 
29
 
 
30
SQLCHAR pare_Sqlstate[5];
 
31
 
 
32
SQLINTEGER    pare_NativeError;
 
33
SQLSMALLINT   pare_i, pare_MsgLen;
 
34
SQLCHAR   pare_Msg[pare_SQL_MAXIMUM_MESSAGE_LENGTH];
 
35
       
 
36
void Prepare_DisplayError(SQLSMALLINT pare_HandleType, 
 
37
                          SQLHSTMT pare_InputHandle);
 
38
 
 
39
/** 
 
40
 * Test to prepare a statement with different handles
 
41
 *
 
42
 * -# Input correct hstmt handle
 
43
 * -# Input incorrect henv handle
 
44
 * -# Input incorrect hdbc handle
 
45
 * -# Input incorrect handle hdesc
 
46
 *
 
47
 * @return Zero, if test succeeded
 
48
 */
 
49
int SQLPrepareTest()
 
50
{
 
51
  SQLCHAR SQLStmt [120];
 
52
  ndbout << endl << "Start SQLPrepare Testing" << endl;
 
53
  ndbout << endl << "Test 1" << endl;  
 
54
  //*********************************
 
55
  //** Test1                       **
 
56
  //** Input correct hstmt handle  **
 
57
  //*********************************
 
58
 
 
59
  //************************************
 
60
  //** Allocate An Environment Handle **
 
61
  //************************************
 
62
  pare_retcode = SQLAllocHandle(SQL_HANDLE_ENV, 
 
63
                                SQL_NULL_HANDLE, 
 
64
                                &pare_henv);
 
65
  
 
66
  if(pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
 
67
    ndbout << "Allocated an environment Handle!" << endl;
 
68
  
 
69
  //*********************************************
 
70
  //** Set the ODBC application Version to 3.x **
 
71
  //*********************************************
 
72
  pare_retcode = SQLSetEnvAttr(pare_henv, 
 
73
                               SQL_ATTR_ODBC_VERSION, 
 
74
                               (SQLPOINTER) SQL_OV_ODBC3, 
 
75
                               SQL_IS_UINTEGER);
 
76
  
 
77
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
 
78
    ndbout << "Set the ODBC application Version to 3.x!" << endl;
 
79
 
 
80
  //**********************************
 
81
  //** Allocate A Connection Handle **
 
82
  //**********************************
 
83
  pare_retcode = SQLAllocHandle(SQL_HANDLE_DBC, pare_henv, &pare_hdbc);
 
84
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
 
85
    ndbout << "Allocated a connection Handle!" << endl;
 
86
  
 
87
  // *******************
 
88
  // ** Connect to DB **
 
89
  // *******************
 
90
  pare_retcode = SQLConnect(pare_hdbc, 
 
91
                            (SQLCHAR *) connectString(), 
 
92
                            SQL_NTS, 
 
93
                            (SQLCHAR *) "", 
 
94
                            SQL_NTS, 
 
95
                            (SQLCHAR *) "", 
 
96
                            SQL_NTS);
 
97
  
 
98
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
 
99
    ndbout << "Connected to DB : OK!" << endl;
 
100
  else 
 
101
    ndbout << "Failure to Connect DB!" << endl;
 
102
  
 
103
  //*******************************
 
104
  //** Allocate statement handle **
 
105
  //*******************************
 
106
  pare_retcode = SQLAllocHandle(SQL_HANDLE_STMT, pare_hdbc, &pare_hstmt); 
 
107
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO) 
 
108
    ndbout << "Allocated a statement handle!" << endl;
 
109
  
 
110
  //************************
 
111
  //** Define a statement **
 
112
  //************************
 
113
 
 
114
   strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(2, 'Hans  Peter', 'LM Vag8', '468719000')");
 
115
 
 
116
  pare_retcode = SQLPrepare(pare_hstmt, 
 
117
                            SQLStmt, 
 
118
                            SQL_NTS);
 
119
  
 
120
  if (pare_retcode == SQL_INVALID_HANDLE)
 
121
    {
 
122
      ndbout << "pare_retcode = " << pare_retcode << endl;
 
123
      ndbout << "HandleType is SQL_HANDLE_STMT, but SQL_INVALID_HANDLE" 
 
124
             << endl;
 
125
      ndbout << "appeared. Please check program!" << endl;
 
126
    }
 
127
  else if (pare_retcode == SQL_ERROR || pare_retcode == SQL_SUCCESS_WITH_INFO)
 
128
    { 
 
129
      Prepare_DisplayError(SQL_HANDLE_STMT, pare_hstmt);
 
130
    } 
 
131
  else 
 
132
    { 
 
133
      //***********************
 
134
      //** Execute statement **
 
135
      //***********************
 
136
      pare_retcode = SQLExecute(pare_hstmt);
 
137
      if (pare_retcode != SQL_SUCCESS) 
 
138
        {
 
139
          ndbout << "pare_retcode = " << pare_retcode << endl;
 
140
          Prepare_DisplayError(SQL_HANDLE_STMT, pare_hstmt);
 
141
        }
 
142
      else
 
143
        ndbout << endl << "Test 1:Input correct HSTMT handle. OK!" << endl;
 
144
    }
 
145
  
 
146
  //*********************************
 
147
  //** Test2                       **
 
148
  //** Input incorrect henv handle **
 
149
  //*********************************
 
150
 
 
151
  strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(3, 'Hans', 'LM8', '51888')");
 
152
 
 
153
  pare_retcode = SQLPrepare(pare_henv, 
 
154
                            SQLStmt, 
 
155
                            SQL_NTS);
 
156
  
 
157
  ndbout << endl << "Test 2" << endl;
 
158
  if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
 
159
    { 
 
160
      FAILURE("Wrong SQL_HANDLE_HENV, but success returned. Check it!");
 
161
    }
 
162
  else if (pare_retcode == SQL_INVALID_HANDLE) 
 
163
   { 
 
164
     ndbout << "Wrong SQL_HANDLE_HENV input and -2 appeared. OK!" << endl ;
 
165
   }
 
166
  else
 
167
    ;
 
168
  /*
 
169
    {
 
170
      ndbout << "Input wrong SQL_HANDLE_ENV, but SQL_SUCCESS_W_I" << endl;
 
171
      ndbout << "and SQL_SUCCESS appeared. Please check program!" << endl;
 
172
      return NDBT_FAILED;
 
173
    }
 
174
  */
 
175
 
 
176
  //*********************************
 
177
  //** Test3                       **
 
178
  //** Input incorrect hdbc handle **
 
179
  //*********************************
 
180
 
 
181
  strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(4, 'HP', 'V�G8', '90888')");
 
182
 
 
183
  pare_retcode = SQLPrepare(pare_hdbc, 
 
184
                            SQLStmt, 
 
185
                            SQL_NTS);
 
186
 
 
187
  ndbout << endl << "Test 3" << endl;
 
188
  if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
 
189
    {
 
190
      FAILURE("Wrong SQL_HANDLE_HDBC, but success returned. Check it!");
 
191
    }
 
192
  else if (pare_retcode == SQL_INVALID_HANDLE)
 
193
    {
 
194
     ndbout << "Wrong SQL_HANDLE_HDBC input and -2 appeared. OK!" << endl ;
 
195
    }
 
196
  else
 
197
    ;
 
198
 
 
199
    /*
 
200
    {
 
201
      ndbout << "Input wrong statement handle SQL_HANDLE_DBC" << endl;
 
202
      ndbout << "but SQL_SUCCESS_WITH_INFO" << endl;
 
203
      ndbout << "and SQL_SUCCESS still appeared. Please check program" << endl;
 
204
      return NDBT_FAILED;
 
205
    }
 
206
 
 
207
   */
 
208
  //**********************************
 
209
  //** Test4                        **
 
210
  //** Input incorrect handle hdesc **
 
211
  //**********************************
 
212
 
 
213
  strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(5, 'Richard', 'V�G8', '56888')");
 
214
 
 
215
  pare_retcode = SQLPrepare(pare_hdesc, 
 
216
                            SQLStmt, 
 
217
                            SQL_NTS);
 
218
 
 
219
  ndbout << endl << "Test 4" << endl;
 
220
  if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
 
221
    {
 
222
      FAILURE("Wrong SQL_HANDLE_DESC, but success returned");
 
223
    }
 
224
  else if (pare_retcode == SQL_INVALID_HANDLE)
 
225
    {
 
226
     ndbout << "Wrong SQL_HANDLE_DESC input and -2 appeared. OK!" << endl ;
 
227
    }
 
228
  else 
 
229
    ndbout << endl;
 
230
 
 
231
    /*
 
232
    {
 
233
      ndbout << "TEST FAILURE: Input wrong SQL_HANDLE_DESC, " 
 
234
             << "but SQL_SUCCESS_WITH_INFO or SQL_SUCCESS was returned." 
 
235
             << endl;
 
236
      return NDBT_FAILED;
 
237
    }
 
238
   */
 
239
 
 
240
  //****************
 
241
  // Free Handles **
 
242
  //****************
 
243
  SQLDisconnect(pare_hdbc);
 
244
  SQLFreeHandle(SQL_HANDLE_STMT, pare_hstmt);
 
245
  SQLFreeHandle(SQL_HANDLE_DBC, pare_hdbc);
 
246
  SQLFreeHandle(SQL_HANDLE_ENV, pare_henv);
 
247
  
 
248
  return NDBT_OK;
 
249
  
 
250
}
 
251
 
 
252
void Prepare_DisplayError(SQLSMALLINT pare_HandleType, 
 
253
                          SQLHSTMT pare_InputHandle)
 
254
{
 
255
  SQLSMALLINT pare_i = 1;
 
256
  SQLRETURN pare_SQLSTATEs;
 
257
  
 
258
  ndbout << "-------------------------------------------------" << endl;
 
259
  ndbout << "Error diagnostics:" << endl;
 
260
  
 
261
  while ((pare_SQLSTATEs = SQLGetDiagRec(pare_HandleType, 
 
262
                                         pare_InputHandle, 
 
263
                                         pare_i,
 
264
                                         pare_Sqlstate, 
 
265
                                         &pare_NativeError, 
 
266
                                         pare_Msg, 
 
267
                                         sizeof(pare_Msg), 
 
268
                                         &pare_MsgLen)
 
269
          ) != SQL_NO_DATA)
 
270
    {
 
271
      ndbout << "SQLSTATE = " << pare_SQLSTATEs << endl;   
 
272
      ndbout << "the HandleType is:" << pare_HandleType << endl;
 
273
      ndbout << "the Handle is :" << (long)pare_InputHandle << endl;
 
274
      ndbout << "the conn_Msg is: " << (char *) pare_Msg << endl;
 
275
      ndbout << "the output state is:" << (char *)pare_Sqlstate << endl; 
 
276
      
 
277
      pare_i ++;
 
278
      break;
 
279
    }
 
280
  ndbout << "-------------------------------------------------" << endl;
 
281
}
 
282
 
 
283
 
 
284