summaryrefslogtreecommitdiff
path: root/storage/ndb/test/odbc/client/SQLDescribeColTest.cpp
blob: abb31b53c5bfb70c5ba3ac58bdea2f503d489360 (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
/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

 /**
 * @file SQLDescribeColTest.cpp
 */
#include <common.hpp>

using namespace std;

#define DC_Column_NAME_LEN 50
#define DC_MESSAGE_LENGTH 200

SQLHSTMT    DC_hstmt;
SQLHDBC     DC_hdbc;
SQLHENV     DC_henv;
SQLHDESC    DC_hdesc;

void DescribeCol_DisplayError(SQLSMALLINT DC_HandleType, 
			      SQLHSTMT DC_InputHandle);

/** 
 * Test to retrieve  basic result data set metadata information
 * (specifically, column name, SQL data type, column size, decimal
 * precision, and nullability) for a specified column in a result
 * data set
 * -# No prepared or executed statement when executing
 * -# ColumnNumber is less than 1
 * -#  ColumnNumber is greater than the value of the TOP_LEVEL_COUNT field of IRD
 * @return Zero, if test succeeded
 */

int SQLDescribeColTest()
{
  SQLCHAR     SQLStmt [120];
  SQLRETURN   retcode;
  SQLCHAR     ColumnName[DC_Column_NAME_LEN];
  SQLSMALLINT NameLength, DataTypePtr, DecimalDigitsPtr, NullablePtr;
  SQLUINTEGER ColumnSizePtr;

  ndbout << "Start SQLDescribeCol Test " << endl;
  //******************************************************************
  //** Test1                                                        **
  //** There is no prepared or executed statement associated with   **
  //** StatementHandle                                              **
  //******************************************************************

  retcode = SQLDescribeCol(DC_hstmt, 
			   (SQLUSMALLINT)1, 
			   ColumnName, 
			   sizeof(ColumnName), 
			   &NameLength, 
			   &DataTypePtr, 
			   &ColumnSizePtr, 
			   &DecimalDigitsPtr, 
			   &NullablePtr);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO)
    DescribeCol_DisplayError(SQL_HANDLE_STMT, DC_hstmt);


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

  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    ndbout << "Allocated a connection Handle!" << endl;
  
  // *******************
  // ** Connect to DB **
  // *******************
  retcode = SQLConnect(DC_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, 
			   DC_hdbc, 
			   &DC_hstmt); 
  if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
    ndbout << "Allocated a statement handle!" << endl;
  
  //************************
  //** Define a statement **
  //************************

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

  //***********************************************
  //** Prepare and Execute the SQL statement     **
  //***********************************************

  retcode = SQLExecDirect(DC_hstmt, 
			  SQLStmt, 
			  SQL_NTS);

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

  //*********************************
  //** ColumnNumber is from 1 to 4 **
  //*********************************
  ndbout << endl << "ColumnNumber is from 1 to 4" << endl;
  
  for (int ii = 1; ii <= 4; ii++)
 {  
  retcode = SQLDescribeCol(DC_hstmt, 
			   ii, 
			   ColumnName, 
			   sizeof(ColumnName), 
			   &NameLength, 
			   &DataTypePtr, 
			   &ColumnSizePtr, 
			   &DecimalDigitsPtr, 
			   &NullablePtr);

  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    ndbout << "Column Name = " << (char *)ColumnName << endl;

 }

  //*********************************
  //** Test2                       **
  //** ColumnNumber is less than 1 **
  //*********************************
 
  retcode = SQLDescribeCol(DC_hstmt, 
			   (SQLUSMALLINT)-1, 
			   ColumnName, 
			   sizeof(ColumnName), 
			   &NameLength, 
			   &DataTypePtr, 
			   &ColumnSizePtr, 
			   &DecimalDigitsPtr, 
			   &NullablePtr);

  if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO)
    ndbout << endl << "ColumnNumber is less than 1" << endl;
    DescribeCol_DisplayError(SQL_HANDLE_STMT, DC_hstmt);

  //*********************************************************************
  //** Test3                                                           **
  //** ColumnNumber is greater than N(the value of the TOP_LEVEL_COUNT ** 
  //** field of IRD)                                                   **
  //*********************************************************************
  
    ndbout << endl <<"ColumnNumber is greater than N(the value" 
	   << "of the TOP_LEVEL_COUNTfield of IRD)" << endl;

    retcode = SQLDescribeCol(DC_hstmt, 
			     (SQLUSMALLINT)1045, 
			     ColumnName, 
			     sizeof(ColumnName), 
			     &NameLength, 
			     &DataTypePtr, 
			     &ColumnSizePtr, 
			     &DecimalDigitsPtr, 
			     &NullablePtr);

    if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO)
      DescribeCol_DisplayError(SQL_HANDLE_STMT, DC_hstmt);
    
}

  // *********************************
  // ** Disconnect and Free Handles **
  // *********************************  
  SQLDisconnect(DC_hdbc);
  SQLFreeHandle(SQL_HANDLE_STMT, DC_hstmt);
  SQLFreeHandle(SQL_HANDLE_DBC, DC_hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, DC_henv);

 return NDBT_OK;
}

void DescribeCol_DisplayError(SQLSMALLINT DC_HandleType, 
			      SQLHSTMT DC_InputHandle)
{
  SQLCHAR    Sqlstate[5], Msg[DC_MESSAGE_LENGTH];
  SQLINTEGER NativeError;
  SQLSMALLINT DC_i, MsgLen;
  SQLRETURN  SQLSTATEs;

  DC_i = 1;

  while ((SQLSTATEs = SQLGetDiagRec(DC_HandleType, 
				    DC_InputHandle, 
				    DC_i, 
				    Sqlstate, 
				    &NativeError, 
				    Msg, 
				    sizeof(Msg), 
				    &MsgLen)) 
	 != SQL_NO_DATA)                   {

    ndbout << "the HandleType is:" << DC_HandleType << endl;
    ndbout << "the InputHandle is :" << (long)DC_InputHandle << endl;
    ndbout << "the return message is:" << (char *)Msg << endl;
    ndbout << "the output state is:" << (char *)Sqlstate << endl; 
    
    DC_i ++;
    break;
  }

}