summaryrefslogtreecommitdiff
path: root/storage/ndb/test/odbc/client/SQLPrepareTest.cpp
blob: f215e33a8f1f9c908ba05fde1e8c4ec4a1dda94b (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
/* 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 SQLprepareTest.cpp
 */
#include <common.hpp>
#define pare_SQL_MAXIMUM_MESSAGE_LENGTH 200

using namespace std;

SQLHDBC     pare_hdbc;
SQLHSTMT    pare_hstmt;
SQLHENV     pare_henv;
SQLHDESC    pare_hdesc;
SQLRETURN   pare_retcode, pare_SQLSTATEs;

SQLCHAR pare_Sqlstate[5];

SQLINTEGER    pare_NativeError;
SQLSMALLINT   pare_i, pare_MsgLen;
SQLCHAR   pare_Msg[pare_SQL_MAXIMUM_MESSAGE_LENGTH];
       
void Prepare_DisplayError(SQLSMALLINT pare_HandleType, 
			  SQLHSTMT pare_InputHandle);

/** 
 * Test to prepare a statement with different handles
 *
 * -# Input correct hstmt handle
 * -# Input incorrect henv handle
 * -# Input incorrect hdbc handle
 * -# Input incorrect handle hdesc
 *
 * @return Zero, if test succeeded
 */
int SQLPrepareTest()
{
  SQLCHAR SQLStmt [120];
  ndbout << endl << "Start SQLPrepare Testing" << endl;
  ndbout << endl << "Test 1" << endl;  
  //*********************************
  //** Test1                       **
  //** Input correct hstmt handle  **
  //*********************************

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

  //**********************************
  //** Allocate A Connection Handle **
  //**********************************
  pare_retcode = SQLAllocHandle(SQL_HANDLE_DBC, pare_henv, &pare_hdbc);
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
    ndbout << "Allocated a connection Handle!" << endl;
  
  // *******************
  // ** Connect to DB **
  // *******************
  pare_retcode = SQLConnect(pare_hdbc, 
			    (SQLCHAR *) connectString(), 
			    SQL_NTS, 
			    (SQLCHAR *) "", 
			    SQL_NTS, 
			    (SQLCHAR *) "", 
			    SQL_NTS);
  
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
    ndbout << "Connected to DB : OK!" << endl;
  else 
    ndbout << "Failure to Connect DB!" << endl;
  
  //*******************************
  //** Allocate statement handle **
  //*******************************
  pare_retcode = SQLAllocHandle(SQL_HANDLE_STMT, pare_hdbc, &pare_hstmt); 
  if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO) 
    ndbout << "Allocated a statement handle!" << endl;
  
  //************************
  //** Define a statement **
  //************************

   strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(2, 'Hans  Peter', 'LM Vag8', '468719000')");

  pare_retcode = SQLPrepare(pare_hstmt, 
			    SQLStmt, 
			    SQL_NTS);
  
  if (pare_retcode == SQL_INVALID_HANDLE)
    {
      ndbout << "pare_retcode = " << pare_retcode << endl;
      ndbout << "HandleType is SQL_HANDLE_STMT, but SQL_INVALID_HANDLE" 
	     << endl;
      ndbout << "appeared. Please check program!" << endl;
    }
  else if (pare_retcode == SQL_ERROR || pare_retcode == SQL_SUCCESS_WITH_INFO)
    { 
      Prepare_DisplayError(SQL_HANDLE_STMT, pare_hstmt);
    } 
  else 
    { 
      //***********************
      //** Execute statement **
      //***********************
      pare_retcode = SQLExecute(pare_hstmt);
      if (pare_retcode != SQL_SUCCESS) 
	{
	  ndbout << "pare_retcode = " << pare_retcode << endl;
	  Prepare_DisplayError(SQL_HANDLE_STMT, pare_hstmt);
	}
      else
	ndbout << endl << "Test 1:Input correct HSTMT handle. OK!" << endl;
    }
  
  //*********************************
  //** Test2                       **
  //** Input incorrect henv handle **
  //*********************************

  strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(3, 'Hans', 'LM8', '51888')");

  pare_retcode = SQLPrepare(pare_henv, 
			    SQLStmt, 
			    SQL_NTS);
  
  ndbout << endl << "Test 2" << endl;
  if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
    { 
      FAILURE("Wrong SQL_HANDLE_HENV, but success returned. Check it!");
    }
  else if (pare_retcode == SQL_INVALID_HANDLE) 
   { 
     ndbout << "Wrong SQL_HANDLE_HENV input and -2 appeared. OK!" << endl ;
   }
  else
    ;
  /*
    {
      ndbout << "Input wrong SQL_HANDLE_ENV, but SQL_SUCCESS_W_I" << endl;
      ndbout << "and SQL_SUCCESS appeared. Please check program!" << endl;
      return NDBT_FAILED;
    }
  */

  //*********************************
  //** Test3                       **
  //** Input incorrect hdbc handle **
  //*********************************

  strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(4, 'HP', 'VÄG8', '90888')");

  pare_retcode = SQLPrepare(pare_hdbc, 
			    SQLStmt, 
			    SQL_NTS);

  ndbout << endl << "Test 3" << endl;
  if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
    {
      FAILURE("Wrong SQL_HANDLE_HDBC, but success returned. Check it!");
    }
  else if (pare_retcode == SQL_INVALID_HANDLE)
    {
     ndbout << "Wrong SQL_HANDLE_HDBC input and -2 appeared. OK!" << endl ;
    }
  else
    ;

    /*
    {
      ndbout << "Input wrong statement handle SQL_HANDLE_DBC" << endl;
      ndbout << "but SQL_SUCCESS_WITH_INFO" << endl;
      ndbout << "and SQL_SUCCESS still appeared. Please check program" << endl;
      return NDBT_FAILED;
    }

   */
  //**********************************
  //** Test4                        **
  //** Input incorrect handle hdesc **
  //**********************************

  strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(5, 'Richard', 'VÄG8', '56888')");

  pare_retcode = SQLPrepare(pare_hdesc, 
			    SQLStmt, 
			    SQL_NTS);

  ndbout << endl << "Test 4" << endl;
  if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
    {
      FAILURE("Wrong SQL_HANDLE_DESC, but success returned");
    }
  else if (pare_retcode == SQL_INVALID_HANDLE)
    {
     ndbout << "Wrong SQL_HANDLE_DESC input and -2 appeared. OK!" << endl ;
    }
  else 
    ndbout << endl;

    /*
    {
      ndbout << "TEST FAILURE: Input wrong SQL_HANDLE_DESC, " 
	     << "but SQL_SUCCESS_WITH_INFO or SQL_SUCCESS was returned." 
	     << endl;
      return NDBT_FAILED;
    }
   */

  //****************
  // Free Handles **
  //****************
  SQLDisconnect(pare_hdbc);
  SQLFreeHandle(SQL_HANDLE_STMT, pare_hstmt);
  SQLFreeHandle(SQL_HANDLE_DBC, pare_hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, pare_henv);
  
  return NDBT_OK;
  
}

void Prepare_DisplayError(SQLSMALLINT pare_HandleType, 
			  SQLHSTMT pare_InputHandle)
{
  SQLSMALLINT pare_i = 1;
  SQLRETURN pare_SQLSTATEs;
  
  ndbout << "-------------------------------------------------" << endl;
  ndbout << "Error diagnostics:" << endl;
  
  while ((pare_SQLSTATEs = SQLGetDiagRec(pare_HandleType, 
					 pare_InputHandle, 
					 pare_i,
					 pare_Sqlstate, 
					 &pare_NativeError, 
					 pare_Msg, 
					 sizeof(pare_Msg), 
					 &pare_MsgLen)
	  ) != SQL_NO_DATA)
    {
      ndbout << "SQLSTATE = " << pare_SQLSTATEs << endl;   
      ndbout << "the HandleType is:" << pare_HandleType << endl;
      ndbout << "the Handle is :" << (long)pare_InputHandle << endl;
      ndbout << "the conn_Msg is: " << (char *) pare_Msg << endl;
      ndbout << "the output state is:" << (char *)pare_Sqlstate << endl; 
      
      pare_i ++;
      break;
    }
  ndbout << "-------------------------------------------------" << endl;
}