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 SQLColAttributeTest3.cpp
19  */
20 
21 #include <common.hpp>
22 using namespace std;
23 
24 #define MAXIMUM_MESSAGE_LENGTH_Test3 200
25 #define BufferLengthTest3 156
26 
27 SQLHSTMT    ColAtt_hstmtTest3;
28 SQLHSTMT    ColAtt_hdbcTest3;
29 SQLHENV     ColAtt_henvTest3;
30 SQLHDESC    ColAtt_hdescTest3;
31 
32 SQLCHAR     TypeName[18];
33 SQLSMALLINT TypeNameLen;
34 
35 SQLRETURN ColAtt_retTest3;
36 
37 void ColAtt_DisplayErrorTest3(SQLSMALLINT ColAttTest3_HandleType,
38 			      SQLHSTMT ColAttTest3_InputHandle);
39 
40 /**
41  * Test returning descriptor information
42  *
43  * Test:
44  * -# Print out column name without executing statement
45  *
46  * @return Zero, if test succeeded
47  */
48 
SQLColAttributeTest3()49 int SQLColAttributeTest3()
50 {
51   ndbout << endl << "Start SQLColAttribute Testing3" << endl;
52 
53   SQLCHAR SQLStmt [120];
54 
55   //********************************************************************
56   //** Test 3:                                                        **
57   //**                                                                **
58   //** Prepare a statement without executing the statement            **
59   //** We want to print out the Type Name of each column in the table **
60   //** Customers                                                      **
61   //**                                                                **
62   //** Intended result: Only display column name, but there is no new **
63   //**          row in table Customers                                **
64   //********************************************************************
65 
66   //************************************
67   //** Allocate An Environment Handle **
68   //************************************
69   ColAtt_retTest3 = SQLAllocHandle(SQL_HANDLE_ENV,
70 				   SQL_NULL_HANDLE,
71 				   &ColAtt_henvTest3);
72 
73   if (ColAtt_retTest3 == SQL_SUCCESS || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
74     ndbout << "Allocated an environment Handle!" << endl;
75 
76   //*********************************************
77   //** Set the ODBC application Version to 3.x **
78   //*********************************************
79   ColAtt_retTest3 = SQLSetEnvAttr(ColAtt_henvTest3,
80 				  SQL_ATTR_ODBC_VERSION,
81 				  (SQLPOINTER) SQL_OV_ODBC3,
82 				  SQL_IS_UINTEGER);
83 
84   if (ColAtt_retTest3 == SQL_SUCCESS || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
85     ndbout << "Set the ODBC application Version to 3.x!" << endl;
86 
87   //**********************************
88   //** Allocate A Connection Handle **
89   //**********************************
90 
91   ColAtt_retTest3 = SQLAllocHandle(SQL_HANDLE_DBC,
92 				   ColAtt_henvTest3,
93 				   &ColAtt_hdbcTest3);
94 
95   if (ColAtt_retTest3 == SQL_SUCCESS || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
96     ndbout << "Allocated a connection Handle!" << endl;
97 
98   // *******************
99   // ** Connect to DB **
100   // *******************
101   ColAtt_retTest3 = SQLConnect(ColAtt_hdbcTest3,
102 			       (SQLCHAR *) connectString(),
103 			       SQL_NTS,
104 			       (SQLCHAR *) "",
105 			       SQL_NTS,
106 			       (SQLCHAR *) "",
107 			       SQL_NTS);
108 
109   if (ColAtt_retTest3 == SQL_SUCCESS || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
110     ndbout << "Connected to DB : OK!" << endl;
111   else
112     {
113       ndbout << "Failure to Connect DB!" << endl;
114       return NDBT_FAILED;
115     }
116 
117   //*******************************
118   //** Allocate statement handle **
119   //*******************************
120 
121   ColAtt_retTest3 = SQLAllocHandle(SQL_HANDLE_STMT,
122 				   ColAtt_hdbcTest3,
123 				   &ColAtt_hstmtTest3);
124   if(ColAtt_retTest3 == SQL_SUCCESS || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
125     ndbout << "Allocated a statement handle!" << endl;
126 
127   //************************
128   //** Define a statement **
129   //************************
130 
131   /*
132   strcpy((char *) SQLStmt,
133 	 "DELETE FROM Customers WHERE CustID = 6");
134    */
135 
136     strcpy((char *) SQLStmt,
137     "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES (6, 'Jan', 'LM vag 8', '969696')");
138 
139     /*
140     strcpy((char *) SQLStmt,
141     "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES (?, ?, ?, ?)");
142     */
143 
144   //*****************************
145   //** Prepare  SQL statement  **
146   //*****************************
147   ColAtt_retTest3 = SQLPrepare(ColAtt_hstmtTest3,
148 			  SQLStmt,
149 			  SQL_NTS);
150 
151   if (ColAtt_retTest3 == SQL_SUCCESS || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
152     {
153       //************************************
154       //** Display the name of column one **
155       //************************************
156       ColAtt_retTest3 = SQLColAttribute(ColAtt_hstmtTest3,
157 					1,
158 					SQL_COLUMN_TYPE_NAME,
159 					TypeName,
160 					sizeof(TypeName),
161 					&TypeNameLen,
162 					NULL);
163 
164       if (ColAtt_retTest3 == SQL_ERROR || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
165 	{
166 	  ndbout << endl << "ColAtt_retTest3 = " << ColAtt_retTest3 << endl;
167 	  ndbout << endl << "Name of column 1 is:"
168 		 << (char *)TypeName <<endl;
169 	  ColAtt_DisplayErrorTest3(SQL_HANDLE_STMT, ColAtt_hstmtTest3);
170 	}
171 
172       //************************************
173       //** Display the name of column two **
174       //************************************
175       ColAtt_retTest3 = SQLColAttribute(ColAtt_hstmtTest3,
176 					2,
177 					SQL_DESC_BASE_COLUMN_NAME,
178 					TypeName,
179 					sizeof(TypeName),
180 					&TypeNameLen,
181 					NULL);
182 
183       if (ColAtt_retTest3 == SQL_ERROR || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
184 	{
185 	  ndbout << endl << "ColAtt_retTest3 = " << ColAtt_retTest3 << endl;
186 	  ndbout << endl << "Name of column 2 is:"
187 		 << (char *)TypeName <<endl;
188 	  ColAtt_DisplayErrorTest3(SQL_HANDLE_STMT, ColAtt_hstmtTest3);
189 	}
190 
191       //***************************************
192       //**  Display the name of column three **
193       //***************************************
194       ColAtt_retTest3 = SQLColAttribute(ColAtt_hstmtTest3,
195 					3,
196 					SQL_DESC_BASE_COLUMN_NAME,
197 					TypeName,
198 					sizeof(TypeName),
199 					&TypeNameLen,
200 					NULL);
201 
202       if (ColAtt_retTest3 == SQL_ERROR || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
203 	{
204 	  ndbout << "ColAtt_retTest3 = " << ColAtt_retTest3 << endl;
205 	  ndbout << endl << "Name of column 3 is:"
206 		 << (char *)TypeName <<endl;
207 	  ColAtt_DisplayErrorTest3(SQL_HANDLE_STMT, ColAtt_hstmtTest3);
208         }
209 
210       //**************************************
211       //**  Display the name of column four **
212       //**************************************
213       ColAtt_retTest3 = SQLColAttribute(ColAtt_hstmtTest3,
214 					4,
215 					SQL_DESC_BASE_COLUMN_NAME,
216 					TypeName,
217 					sizeof(TypeName),
218 					&TypeNameLen,
219 					NULL);
220 
221       if (ColAtt_retTest3 == SQL_ERROR || ColAtt_retTest3 == SQL_SUCCESS_WITH_INFO)
222 	{
223 	  ndbout << "ColAtt_retTest3 = " << ColAtt_retTest3 << endl;
224 	  ndbout << endl << "Name of column 4 is:"
225 		 << (char *)TypeName <<endl;
226 	  ColAtt_DisplayErrorTest3(SQL_HANDLE_STMT, ColAtt_hstmtTest3);
227 	}
228 
229     }
230 
231   // *********************************
232   // ** Disconnect and Free Handles **
233   // *********************************
234   SQLDisconnect(ColAtt_hdbcTest3);
235   SQLFreeHandle(SQL_HANDLE_STMT, ColAtt_hstmtTest3);
236   SQLFreeHandle(SQL_HANDLE_DBC, ColAtt_hdbcTest3);
237   SQLFreeHandle(SQL_HANDLE_ENV, ColAtt_henvTest3);
238 
239   return NDBT_OK;
240 }
241 
ColAtt_DisplayErrorTest3(SQLSMALLINT ColAttTest3_HandleType,SQLHSTMT ColAttTest3_InputHandle)242 void ColAtt_DisplayErrorTest3(SQLSMALLINT ColAttTest3_HandleType,
243 			      SQLHSTMT ColAttTest3_InputHandle)
244 {
245   SQLSMALLINT ColAtt_i = 1;
246   SQLRETURN ColAtt_SQLSTATEs;
247   SQLCHAR ColAtt_Sqlstate[5];
248   SQLCHAR ColAtt_Msg[MAXIMUM_MESSAGE_LENGTH_Test3];
249   SQLSMALLINT ColAtt_MsgLen;
250   SQLINTEGER  ColAtt_NativeError;
251 
252   ndbout << "-------------------------------------------------" << endl;
253   ndbout << "Error diagnostics:" << endl;
254 
255   while ((ColAtt_SQLSTATEs = SQLGetDiagRec(ColAttTest3_HandleType,
256 					   ColAttTest3_InputHandle,
257 					   ColAtt_i,
258 					   ColAtt_Sqlstate,
259 					   &ColAtt_NativeError,
260 					   ColAtt_Msg,
261 					   sizeof(ColAtt_Msg),
262 					   &ColAtt_MsgLen))
263 	 != SQL_NO_DATA)
264     {
265 
266       ndbout << "the HandleType is:" << ColAttTest3_HandleType << endl;
267       ndbout << "the InputHandle is :" << (long)ColAttTest3_InputHandle << endl;
268       ndbout << "the ColAtt_Msg is: " << (char *) ColAtt_Msg << endl;
269       ndbout << "the output state is:" << (char *)ColAtt_Sqlstate << endl;
270 
271       ColAtt_i ++;
272       break;
273     }
274   ndbout << "-------------------------------------------------" << endl;
275 }
276