1 /* Copyright (c) 2003, 2005 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 /**
18  * @file SQLColAttributeTest.cpp
19  */
20 
21 #include <common.hpp>
22 using namespace std;
23 
24 #define MAXIMUM_MESSAGE_LENGTH_Test 200
25 #define BufferLengthTest 156
26 
27 SQLHSTMT    ColAtt_hstmt;
28 SQLHSTMT    ColAtt_hdbc;
29 SQLHENV     ColAtt_henv;
30 SQLHDESC    ColAtt_hdesc;
31 
32 SQLCHAR        CharacterAttributePtr;
33 SQLINTEGER     NumericAttributePtr;
34 SQLSMALLINT    StringLengthPtr;
35 
36 SQLRETURN ColAtt_ret;
37 
38 void ColAtt_DisplayError(SQLSMALLINT ColAtt_HandleType,
39 			 SQLHSTMT ColAtt_InputHandle);
40 
41 /**
42  * Test returning descriptor information
43  *
44  * Tests:
45  * -# Call SQLColAttribute, without preceeding SQLPrepare
46  * -# ???
47  *
48  * @return Zero, if test succeeded
49  */
SQLColAttributeTest()50 int SQLColAttributeTest()
51 {
52   ndbout << endl << "Start SQLColAttribute Testing" << endl;
53 
54   SQLCHAR SQLStmt [120];
55 
56   /********************************************************************
57    ** Test 1:                                                        **
58    **                                                                **
59    ** Checks to execute SQLColAttribute, when there is no            **
60    ** prepared or executed statement associated with StatementHandle **
61    **                                                                **
62    ** Intended result:  SQL_ERROR ???                                **
63    ********************************************************************/
64   ColAtt_ret = SQLColAttribute(ColAtt_hstmt,
65 			       1,
66 			       SQL_DESC_AUTO_UNIQUE_VALUE,
67 			       &CharacterAttributePtr,
68 			       BufferLengthTest,
69 			       &StringLengthPtr,
70 			       &NumericAttributePtr);
71 
72   if (ColAtt_ret == SQL_ERROR)
73     {
74       ndbout << "ColAtt_ret = " << ColAtt_ret << endl;
75       ndbout << endl << "There is no prepared or executed" << endl
76 	     << " statement associated with StatementHandle" << endl;
77       ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
78     }
79   else if (ColAtt_ret == SQL_SUCCESS_WITH_INFO)
80     {
81       ndbout << "ColAtt_ret = " << ColAtt_ret << endl;
82       ndbout << endl << "There is no prepared or executed" << endl
83 	     << " statement associated with StatementHandle" << endl;
84       ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
85     }
86   else if (ColAtt_ret == SQL_SUCCESS)
87     {
88       ndbout << "ColAtt_ret = " << ColAtt_ret << endl;
89       ndbout << endl << "There is no prepared or executed" << endl
90 	     << " statement associated with StatementHandle" << endl;
91       ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
92     }
93   else if (ColAtt_ret == -2)
94     {
95       ndbout << "ColAtt_ret = " << ColAtt_ret << endl;
96       ndbout << endl << "There is no prepared or executed" << endl
97 	     << " statement associated with StatementHandle" << endl;
98       ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
99     }
100   else
101     {
102       ndbout << "ColAtt_ret = " << ColAtt_ret << endl;
103       ndbout << endl << "There is no prepared or executed" << endl
104 	     << " statement associated with StatementHandle" << endl;
105       ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
106     }
107 
108   //*******************************************************************
109   //** Test 2:                                                       **
110   //**                                                               **
111   //** hstmt                                                         **
112   //** Execute a statement to retrieve rows from the Customers table **
113   //** We can create the table and insert rows into Mysql            **
114   //**                                                               **
115   //** Intended result: ???                                          **
116   //*******************************************************************
117 
118   //************************************
119   //** Allocate An Environment Handle **
120   //************************************
121   ColAtt_ret = SQLAllocHandle(SQL_HANDLE_ENV,
122 			      SQL_NULL_HANDLE,
123 			      &ColAtt_henv);
124 
125   if (ColAtt_ret == SQL_SUCCESS || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
126     ndbout << "Allocated an environment Handle!" << endl;
127 
128   //*********************************************
129   //** Set the ODBC application Version to 2.x **
130   //*********************************************
131   ColAtt_ret = SQLSetEnvAttr(ColAtt_henv,
132 			     SQL_ATTR_ODBC_VERSION,
133 			     (SQLPOINTER) SQL_OV_ODBC2,
134 			     SQL_IS_UINTEGER);
135 
136   if (ColAtt_ret == SQL_SUCCESS || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
137     ndbout << "Set the ODBC application Version to 2.x!" << endl;
138 
139   //**********************************
140   //** Allocate A Connection Handle **
141   //**********************************
142 
143   ColAtt_ret = SQLAllocHandle(SQL_HANDLE_DBC,
144 			      ColAtt_henv,
145 			      &ColAtt_hdbc);
146 
147   if (ColAtt_ret == SQL_SUCCESS || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
148     ndbout << "Allocated a connection Handle!" << endl;
149 
150   // *******************
151   // ** Connect to DB **
152   // *******************
153   ColAtt_ret = SQLConnect(ColAtt_hdbc,
154 			  (SQLCHAR *) connectString(),
155 			  SQL_NTS,
156 			  (SQLCHAR *) "",
157 			  SQL_NTS,
158 			  (SQLCHAR *) "",
159 			  SQL_NTS);
160 
161   if (ColAtt_ret == SQL_SUCCESS || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
162     ndbout << "Connected to DB : OK!" << endl;
163   else
164     {
165       ndbout << "Failure to Connect DB!" << endl;
166       return NDBT_FAILED;
167     }
168   //*******************************
169   //** Allocate statement handle **
170   //*******************************
171 
172   ColAtt_ret = SQLAllocHandle(SQL_HANDLE_STMT,
173 			      ColAtt_hdbc,
174 			      &ColAtt_hstmt);
175   if(ColAtt_ret == SQL_SUCCESS || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
176     ndbout << "Allocated a statement handle!" << endl;
177 
178   //************************
179   //** Define a statement **
180   //************************
181 
182   /*
183   strcpy((char *) SQLStmt,
184 	 "DELETE FROM Customers WHERE CustID = 6");
185    */
186 
187     strcpy((char *) SQLStmt,
188     "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES (6, 'Jan', 'LM vag 8', '969696')");
189 
190     /*
191     strcpy((char *) SQLStmt,
192     "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES (?, ?, ?, ?)");
193     */
194 
195   //********************************
196   //** Prepare  SQL statement     **
197   //********************************
198   ColAtt_ret = SQLPrepare(ColAtt_hstmt,
199 			  SQLStmt,
200 			  SQL_NTS);
201 
202   if (ColAtt_ret == SQL_SUCCESS || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
203     {
204       //**************************************************************
205       //** FieldIdentifer is not one of the code valuess in Table 20,
206       //** "Codes used for descriptor fields"
207       //**************************************************************
208       ColAtt_ret = SQLColAttribute(ColAtt_hstmt,
209 				   2,
210 				   9999,
211 				   &CharacterAttributePtr,
212 				   BufferLengthTest,
213 				   &StringLengthPtr,
214 				   &NumericAttributePtr);
215 
216       if (ColAtt_ret == SQL_ERROR || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
217 	{
218 	  ndbout << endl << "FieldIdentifer is not one of the" << endl;
219 	  ndbout << "code valuess in Table 20, Codes used for" << endl;
220 	  ndbout << "descriptor fields" <<endl;
221 	  ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
222 	}
223 
224       //****************************************************************
225       //** Let TYPE is 'ITEM' in Table 20, ColumnNumber is less than one
226       //****************************************************************
227       ColAtt_ret = SQLColAttribute(ColAtt_hstmt,
228 				   -1,
229 				   SQL_DESC_BASE_COLUMN_NAME,
230 				   &CharacterAttributePtr,
231 				   BufferLengthTest,
232 				   &StringLengthPtr,
233 				   &NumericAttributePtr);
234 
235       if (ColAtt_ret == SQL_ERROR || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
236 	{
237 	  ndbout << "Let TYPE is 'ITEM' in Table 20,ColumnNumber"
238 		 << "is less than one" << endl;
239 	  ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
240 	}
241 
242       //*********************************************************
243       //** Let TYPE is 'ITEM' in Table 20, FieldIdentifer is zero
244       //*********************************************************
245       ColAtt_ret = SQLColAttribute(ColAtt_hstmt,
246 				   1018,
247 				   0,
248 				   &CharacterAttributePtr,
249 				   BufferLengthTest,
250 				   &StringLengthPtr,
251 				   &NumericAttributePtr);
252 
253       if (ColAtt_ret == SQL_ERROR || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
254 	{
255 	  ndbout << "Let TYPE is 'ITEM' in Table 20, FieldIdentifer"
256 		 << " is zero"  <<endl;
257 	  ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
258         }
259 
260       //**********************************************************
261       //** Let TYPE is 'ITEM' in Table 20, ColumnNumber is greater
262       //** than TOP_LEVEL_COUNT(1044)
263       //*********************************************************
264       ColAtt_ret = SQLColAttribute(ColAtt_hstmt,
265 				   1045,
266 				   SQL_DESC_BASE_COLUMN_NAME,
267 				   &CharacterAttributePtr,
268 				   BufferLengthTest,
269 				   &StringLengthPtr,
270 				   &NumericAttributePtr);
271 
272       if (ColAtt_ret == SQL_ERROR || ColAtt_ret == SQL_SUCCESS_WITH_INFO)
273 	{
274 	  ndbout << "Let TYPE is 'ITEM' in Table 20, ColumnNumber" << endl
275 		 << "is greater than TOP_LEVEL_COUNT(1044)" << endl;
276 	  ColAtt_DisplayError(SQL_HANDLE_STMT, ColAtt_hstmt);
277 	}
278 
279     }
280 
281   // *********************************
282   // ** Disconnect and Free Handles **
283   // *********************************
284   SQLDisconnect(ColAtt_hdbc);
285   SQLFreeHandle(SQL_HANDLE_STMT, ColAtt_hstmt);
286   SQLFreeHandle(SQL_HANDLE_DBC, ColAtt_hdbc);
287   SQLFreeHandle(SQL_HANDLE_ENV, ColAtt_henv);
288 
289   return NDBT_OK;
290 }
291 
ColAtt_DisplayError(SQLSMALLINT ColAtt_HandleType,SQLHSTMT ColAtt_InputHandle)292 void ColAtt_DisplayError(SQLSMALLINT ColAtt_HandleType,
293 			 SQLHSTMT ColAtt_InputHandle)
294 {
295   SQLSMALLINT ColAtt_i = 1;
296   SQLRETURN ColAtt_SQLSTATEs;
297   SQLCHAR ColAtt_Sqlstate[5];
298   SQLCHAR ColAtt_Msg[MAXIMUM_MESSAGE_LENGTH_Test];
299   SQLSMALLINT ColAtt_MsgLen;
300   SQLINTEGER  ColAtt_NativeError;
301 
302   ndbout << "-------------------------------------------------" << endl;
303   ndbout << "Error diagnostics:" << endl;
304 
305   while ((ColAtt_SQLSTATEs = SQLGetDiagRec(ColAtt_HandleType,
306 					   ColAtt_InputHandle,
307 					   ColAtt_i,
308 					   ColAtt_Sqlstate,
309 					   &ColAtt_NativeError,
310 					   ColAtt_Msg,
311 					   sizeof(ColAtt_Msg),
312 					   &ColAtt_MsgLen))
313 	 != SQL_NO_DATA)
314     {
315 
316       ndbout << "the HandleType is:" << ColAtt_HandleType << endl;
317       ndbout << "the InputHandle is :" << (long)ColAtt_InputHandle << endl;
318       ndbout << "the ColAtt_Msg is: " << (char *) ColAtt_Msg << endl;
319       ndbout << "the output state is:" << (char *)ColAtt_Sqlstate << endl;
320 
321       ColAtt_i ++;
322       break;
323     }
324   ndbout << "-------------------------------------------------" << endl;
325 }
326 
327 
328 
329