1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
* @file SQLprepareTest.cpp
20
#define pare_SQL_MAXIMUM_MESSAGE_LENGTH 200
28
SQLRETURN pare_retcode, pare_SQLSTATEs;
30
SQLCHAR pare_Sqlstate[5];
32
SQLINTEGER pare_NativeError;
33
SQLSMALLINT pare_i, pare_MsgLen;
34
SQLCHAR pare_Msg[pare_SQL_MAXIMUM_MESSAGE_LENGTH];
36
void Prepare_DisplayError(SQLSMALLINT pare_HandleType,
37
SQLHSTMT pare_InputHandle);
40
* Test to prepare a statement with different handles
42
* -# Input correct hstmt handle
43
* -# Input incorrect henv handle
44
* -# Input incorrect hdbc handle
45
* -# Input incorrect handle hdesc
47
* @return Zero, if test succeeded
51
SQLCHAR SQLStmt [120];
52
ndbout << endl << "Start SQLPrepare Testing" << endl;
53
ndbout << endl << "Test 1" << endl;
54
//*********************************
56
//** Input correct hstmt handle **
57
//*********************************
59
//************************************
60
//** Allocate An Environment Handle **
61
//************************************
62
pare_retcode = SQLAllocHandle(SQL_HANDLE_ENV,
66
if(pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
67
ndbout << "Allocated an environment Handle!" << endl;
69
//*********************************************
70
//** Set the ODBC application Version to 3.x **
71
//*********************************************
72
pare_retcode = SQLSetEnvAttr(pare_henv,
73
SQL_ATTR_ODBC_VERSION,
74
(SQLPOINTER) SQL_OV_ODBC3,
77
if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
78
ndbout << "Set the ODBC application Version to 3.x!" << endl;
80
//**********************************
81
//** Allocate A Connection Handle **
82
//**********************************
83
pare_retcode = SQLAllocHandle(SQL_HANDLE_DBC, pare_henv, &pare_hdbc);
84
if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
85
ndbout << "Allocated a connection Handle!" << endl;
87
// *******************
88
// ** Connect to DB **
89
// *******************
90
pare_retcode = SQLConnect(pare_hdbc,
91
(SQLCHAR *) connectString(),
98
if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
99
ndbout << "Connected to DB : OK!" << endl;
101
ndbout << "Failure to Connect DB!" << endl;
103
//*******************************
104
//** Allocate statement handle **
105
//*******************************
106
pare_retcode = SQLAllocHandle(SQL_HANDLE_STMT, pare_hdbc, &pare_hstmt);
107
if (pare_retcode == SQL_SUCCESS || pare_retcode == SQL_SUCCESS_WITH_INFO)
108
ndbout << "Allocated a statement handle!" << endl;
110
//************************
111
//** Define a statement **
112
//************************
114
strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(2, 'Hans Peter', 'LM Vag8', '468719000')");
116
pare_retcode = SQLPrepare(pare_hstmt,
120
if (pare_retcode == SQL_INVALID_HANDLE)
122
ndbout << "pare_retcode = " << pare_retcode << endl;
123
ndbout << "HandleType is SQL_HANDLE_STMT, but SQL_INVALID_HANDLE"
125
ndbout << "appeared. Please check program!" << endl;
127
else if (pare_retcode == SQL_ERROR || pare_retcode == SQL_SUCCESS_WITH_INFO)
129
Prepare_DisplayError(SQL_HANDLE_STMT, pare_hstmt);
133
//***********************
134
//** Execute statement **
135
//***********************
136
pare_retcode = SQLExecute(pare_hstmt);
137
if (pare_retcode != SQL_SUCCESS)
139
ndbout << "pare_retcode = " << pare_retcode << endl;
140
Prepare_DisplayError(SQL_HANDLE_STMT, pare_hstmt);
143
ndbout << endl << "Test 1:Input correct HSTMT handle. OK!" << endl;
146
//*********************************
148
//** Input incorrect henv handle **
149
//*********************************
151
strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(3, 'Hans', 'LM8', '51888')");
153
pare_retcode = SQLPrepare(pare_henv,
157
ndbout << endl << "Test 2" << endl;
158
if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
160
FAILURE("Wrong SQL_HANDLE_HENV, but success returned. Check it!");
162
else if (pare_retcode == SQL_INVALID_HANDLE)
164
ndbout << "Wrong SQL_HANDLE_HENV input and -2 appeared. OK!" << endl ;
170
ndbout << "Input wrong SQL_HANDLE_ENV, but SQL_SUCCESS_W_I" << endl;
171
ndbout << "and SQL_SUCCESS appeared. Please check program!" << endl;
176
//*********************************
178
//** Input incorrect hdbc handle **
179
//*********************************
181
strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(4, 'HP', 'V�G8', '90888')");
183
pare_retcode = SQLPrepare(pare_hdbc,
187
ndbout << endl << "Test 3" << endl;
188
if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
190
FAILURE("Wrong SQL_HANDLE_HDBC, but success returned. Check it!");
192
else if (pare_retcode == SQL_INVALID_HANDLE)
194
ndbout << "Wrong SQL_HANDLE_HDBC input and -2 appeared. OK!" << endl ;
201
ndbout << "Input wrong statement handle SQL_HANDLE_DBC" << endl;
202
ndbout << "but SQL_SUCCESS_WITH_INFO" << endl;
203
ndbout << "and SQL_SUCCESS still appeared. Please check program" << endl;
208
//**********************************
210
//** Input incorrect handle hdesc **
211
//**********************************
213
strcpy( (char *) SQLStmt, "INSERT INTO Customers (CustID, Name, Address, Phone) VALUES(5, 'Richard', 'V�G8', '56888')");
215
pare_retcode = SQLPrepare(pare_hdesc,
219
ndbout << endl << "Test 4" << endl;
220
if (pare_retcode == SQL_SUCCESS_WITH_INFO || pare_retcode == SQL_SUCCESS)
222
FAILURE("Wrong SQL_HANDLE_DESC, but success returned");
224
else if (pare_retcode == SQL_INVALID_HANDLE)
226
ndbout << "Wrong SQL_HANDLE_DESC input and -2 appeared. OK!" << endl ;
233
ndbout << "TEST FAILURE: Input wrong SQL_HANDLE_DESC, "
234
<< "but SQL_SUCCESS_WITH_INFO or SQL_SUCCESS was returned."
243
SQLDisconnect(pare_hdbc);
244
SQLFreeHandle(SQL_HANDLE_STMT, pare_hstmt);
245
SQLFreeHandle(SQL_HANDLE_DBC, pare_hdbc);
246
SQLFreeHandle(SQL_HANDLE_ENV, pare_henv);
252
void Prepare_DisplayError(SQLSMALLINT pare_HandleType,
253
SQLHSTMT pare_InputHandle)
255
SQLSMALLINT pare_i = 1;
256
SQLRETURN pare_SQLSTATEs;
258
ndbout << "-------------------------------------------------" << endl;
259
ndbout << "Error diagnostics:" << endl;
261
while ((pare_SQLSTATEs = SQLGetDiagRec(pare_HandleType,
271
ndbout << "SQLSTATE = " << pare_SQLSTATEs << endl;
272
ndbout << "the HandleType is:" << pare_HandleType << endl;
273
ndbout << "the Handle is :" << (long)pare_InputHandle << endl;
274
ndbout << "the conn_Msg is: " << (char *) pare_Msg << endl;
275
ndbout << "the output state is:" << (char *)pare_Sqlstate << endl;
280
ndbout << "-------------------------------------------------" << endl;