summaryrefslogtreecommitdiff
path: root/storage/ndb/test/odbc/client/SQLGetFunctionsTest.cpp
blob: e9432e64eb883863da2a90616234419d70fa1154 (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
/* 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

 /**
 * @file SQLGetFunctionsTest.cpp
 */


#include <common.hpp>
#define GF_MESSAGE_LENGTH 200

using namespace std;

SQLHDBC     GF_hdbc;
SQLHSTMT    GF_hstmt;
SQLHENV     GF_henv;
       
void SQLGetFunctions_DisplayError(SQLSMALLINT GF_HandleType, 
				  SQLHDBC GF_InputHandle);

/** 
 * Test whether a specific ODBC API function is supported by
 * the driver an application is currently connected to.
 *
 * In this test program, we can change ODBC function values in order to
 * know different which fuction is supported by ODBC drivers
 * Tests:
 * -# Test1 There is no established SQL-connection
 * -# Test2 ConnectionHandle does not identify an allocated SQL-connection 
 * -# Test3 The value of FunctionId is not in table 27
 * -# Test4 Normal case test
 * @return Zero, if test succeeded
 */

int SQLGetFunctionsTest()
{
  SQLUSMALLINT TableExists, Supported;
  SQLCHAR SQLStmt [120];
  SQLRETURN retcode;

  ndbout << endl << "Start SQLGetFunctions Testing" << endl;

  //**********************************************************
  //** Test 1                                               **
  //** If there is no established SQL-connection associated **
  //** with allocated SQL-connection                        **
  //**********************************************************

  retcode = SQLGetFunctions(GF_hdbc, SQL_API_SQLTABLES, &TableExists);
  if (retcode == -2)
{
  ndbout << endl << "Test 1" << endl;
  ndbout << "retcode = " << retcode << endl;
  ndbout << endl << "There is no established SQL-connection" << endl;
  ndbout << "associated with allocated SQL_connection" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}
  else if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
  ndbout << endl << "Test 1" << endl;
  ndbout << "retcode = " << retcode << endl;
  ndbout << endl << "There is no established SQL-connection" << endl;
  ndbout << "associated with allocated SQL_connection" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}
  else
{
  ndbout << endl << "Test 1" << endl;
  ndbout << "retcode = " << retcode << endl;
  ndbout << endl << "There is no established SQL-connection" << endl;
  ndbout << "associated with allocated SQL_connection" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}

  //************************************
  //** Allocate An Environment Handle **
  //************************************
  retcode = SQLAllocHandle(SQL_HANDLE_ENV, 
			   SQL_NULL_HANDLE, 
			   &GF_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(GF_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, 
			   GF_henv, 
			   &GF_hdbc);

  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    ndbout << "Allocated a connection Handle!" << endl;

  // *******************
  // ** Connect to DB **
  // *******************
  retcode = SQLConnect(GF_hdbc, 
		       (SQLCHAR*) connectString(), 
		       SQL_NTS, 
		       (SQLCHAR*) "", 
		       SQL_NTS,
		       (SQLCHAR*) "", 
		       SQL_NTS);

  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
{

    //*************************************************************
    //** Test 2                                                  **
    //** If ConnectionHandle does not identify an allocated      **
    //** SQL-connection, then an exception condition is raised   **
    //*************************************************************
    retcode = SQLGetFunctions(GF_hdbc, SQL_API_SQLTABLES, &TableExists);
    if (retcode == -2)
{
  ndbout << endl << "Test 2" << endl;
  ndbout << "retcode = " << retcode << endl;
  ndbout << "If ConnectionHandle does not identify an allocated" << endl;
  ndbout << "SQL-connection,an exception condition is raised" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}
  else if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
  ndbout << endl << "Test 2" << endl;
  ndbout << "retcode = " << retcode << endl;
  ndbout << "If ConnectionHandle does not identify an allocated" << endl;
  ndbout << "SQL-connection,an exception condition is raised" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}
   else
{
  ndbout << endl << "Test 2 :" << endl;
  ndbout << "retcode = " << retcode << endl;
  ndbout << "If ConnectionHandle does not identify an allocated" << endl;
  ndbout << "SQL-connection,an exception condition is raised" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}

    //*************************************************************
    //** Test 3                                                  **
    //** If the value of FunctionId is not in table 27, "Codes   **
    //** used to identify SQL/CLI routines"                      **
    //*************************************************************
    
 retcode = SQLGetFunctions(GF_hdbc, 88888, &TableExists);
 ndbout<< "TableExists = " << TableExists << endl;
 if (retcode == -2)
{
  ndbout << "retcode = " << retcode << endl;
  ndbout << "Test 3 : The value of FunctionId is not in table 27" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}
 else if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO)
{
  ndbout << "retcode = " << retcode << endl;
  ndbout << "Test 3 : The value of FunctionId is not in table 27" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}
 else
{
  ndbout << "retcode = " << retcode << endl;
  ndbout << "Test 3 : The value of FunctionId is not in table 27" << endl;
  SQLGetFunctions_DisplayError(SQL_HANDLE_STMT, GF_hstmt);
}

  //******************
  //** Test 4       **
  //** Normal case  **
  //******************
  ndbout << "Test 4:" << endl;
  retcode = SQLGetFunctions(GF_hdbc, SQL_API_SQLBROWSECONNECT, &Supported);
  ndbout << "retcode = " << retcode << endl;
  if (Supported == TRUE)
{
  ndbout << "Supported = " << Supported << endl;
  ndbout << "SQLBrowseConnect is supported by the current data source" 
	 << endl;
}
  else
{
  ndbout << "Supported = " << Supported << endl;
  ndbout << "SQLBrowseConnect isn't supported by the current data source" 
	 << endl;
}


  //******************
  //** Test 5       **
  //** Normal case  **
  //******************
  ndbout << endl << "Test 5:" << endl;
  retcode = SQLGetFunctions(GF_hdbc, SQL_API_SQLFETCH, &Supported);
  ndbout << "retcode = " << retcode << endl;
  if (Supported == TRUE)
{
  ndbout << "Supported = " << Supported << endl;
  ndbout << "SQLFETCH is supported by the current data source" << endl;
}
  else
{
  ndbout << "Supported = " << Supported << endl;
  ndbout << "SQLFETCH isn't supported by the current data source" << endl;
}

}

  // *********************************
  // ** Disconnect and Free Handles **
  // *********************************  
  SQLDisconnect(GF_hdbc);
  SQLFreeHandle(SQL_HANDLE_STMT, GF_hstmt);
  SQLFreeHandle(SQL_HANDLE_DBC, GF_hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, GF_henv);

 return NDBT_OK;

}


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