summaryrefslogtreecommitdiff
path: root/storage/ndb/test/odbc/client/SQLAllocHandleTest_bf.cpp
blob: fc9a9d504babc2c0aef141450bb4cbb9641c2629 (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
/* 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 */

#include sqlcli.h;
#include stdio.h;

#define SQL_MAX_MESSAGE_LENGTH 200;

SQLHDBC     hdbc;
SQLHSTMT    hstmt;
SQLHENV     henv;
SQLHDESC    hdesc;
SQLRETURN   retcode, SQLSTATEs;

SQLCHAR       SqlState[6], SQLStmt[100], Msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER    NativeError;
SQLSMALLINT   i, MsgLen;

struct handle_set
{
SQLHDBC     hdbc_varible;
SQLHSTMT    hstmt_varible;
SQLHENV     henv_varible;
SQLHDESC    hdesc_varible;
INTEGER     strangehandle;
}

static int
check(
    SQLSMALLINT HandleType,
    SQLHANDLE inputhandle,
    SQLHANDLE *outputhandle,
    SQLRETURN wantret,
    char *wantSqlstate)
{
    SQLRETURN ret;
    SQLCHAR Sqlstate[20];

    ret = SQLAllocHandle(handletype, inputhandle, outputhandle);
    if (ret != wantret) {
	// report error
	return -1;
    }
    if (ret == SQL_INVALID_HANDLE) {
	// cannot get diag
	return 0;
    }
    // TODO
    ret = SQLGetDiagRec(HandleType, InputHandle, 1, Sqlstate, &NativeError, Msg, sizeof(Msg), &MsgLen);
    if (strcmp(Sqlstate, wantSqlstate) != 0) {
	// report error;
	return -1;
    }
    return 0;
}

int
Test_SQLAllocHandle()
{
    SQLRETURN ret;
    SQLHENV henv;
    SQLDBC dbc;
    int i;

    // env
    check(SQL_HANDLE_ENV, SQL_NULL_HANDLE, 0, SQL_ERROR, "HY009");
    for (i = 0; i < 1000; i++) {
	if (i != SQL_NULL_HANDLE)
	    check(SQL_HANDLE_ENV, i, &henv, SQL_INVALID_HANDLE, 0);
    }
    if (check(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv, SQL_SUCCESS, "00000") < 0)
	return -1;

    // dbc
    check(SQL_HANDLE_DBC, henv, 0, SQL_ERROR, "HY009");
    for (i = 0; i < 1000; i++) {
	if (i != henv)
	    check(SQL_HANDLE_DBC, i, &dbc, SQL_INVALID_HANDLE, 0);
    }
    if (check(SQL_HANDLE_DBC, henv, &dbc, SQL_SUCCESS, "00000") < 0)
	return -1;

    //??
    check(SQL_HANDLE_ENV, dbc, 0, SQL_ERROR, "HY092");

    // TODO
    // stmt

    return 0;
}


handle_set handlevalue;
 
handlevalue.hdbc_varible = hdbc;
handlevalue.hstmt_varible = hstmt;
handlevalue.henv_varible = henv;
handlevalue.hdesc_varible = hdesc;
handlevalue.stranghandle = 67;

      /*Allocate environment handle */
//retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

while (int j = 0; j++; j < 6) {
   if ( j = 0 ) 
      handle_deal_with(SQL_HANDLE_ENV, SQL_NULL_HANDLE, );
          
   else if ( j = 1 ) 
      handle_deal_with(SQL_HANDLE_ENV, handlevalue.henv_varible, );
                           
   else if ( j = 2 ) 
     handle_deal_with(SQL_HANDLE_ENV, handlevalue.hdbc_varible,  );

   else if ( j = 3 ) 
     handle_deal_with(SQL_HANDLE_ENV, handlevalue.hstmt_varible,  );
           
   else if ( j = 4 )  
     handle_deal_with(SQL_HANDLE_ENV, handlevalue.hdesc_varible,  );
     
  else 
     handle_deal_with(SQL_HANDLE_ENV, handlevalue.stranghandle,  );
        
                              }
                           
 while (int j = 0; j++; j < 6) {
   if ( j = 0 ) 
      handle_deal_with(SQL_HANDLE_DBC, SQL_NULL_HANDLE,  );
          
   else if ( j = 1 ) 
      handle_deal_with(SQL_HANDLE_DBC, handlevalue.henv_varible,  );
                           
   else if ( j = 2 ) 
      handle_deal_with(SQL_HANDLE_DBC, handlevalue.hdbc_varible,  );

   else if ( j = 3 ) 
      handle_deal_with(SQL_HANDLE_DBC, handlevalue.hstmt_varible,  );
           
   else if ( j = 4 )  
      handle_deal_with(SQL_HANDLE_DBC, handlevalue.hdesc_varible,  );
     
  else 
      handle_deal_with(SQL_HANDLE_DBC, handlevalue.stranghandle,  );
         
                              }
 

 while (int j = 0; j++; j < 6) {
   if ( j = 0 ) 
      handle_deal_with(SQL_HANDLE_STMT, SQL_NULL_HANDLE,  );
          
   else if ( j = 1 ) 
      handle_deal_with(SQL_HANDLE_STMT, handlevalue.henv_varible,  );
                           
   else if ( j = 2 ) 
      handle_deal_with(SQL_HANDLE_STMT, handlevalue.hdbc_varible,  );

   else if ( j = 3 ) 
      handle_deal_with(SQL_HANDLE_STMT, handlevalue.hstmt_varible,  );
           
   else if ( j = 4 )  
      handle_deal_with(SQL_HANDLE_STMT, handlevalue.hdesc_varible,  );
     
  else 
      handle_deal_with(SQL_HANDLE_STMT, handlevalue.stranghandle,  );
        
                              }



  while (int j = 0; j++; j < 6) {
   if ( j = 0 ) 
      handle_deal_with(SQL_HANDLE_DESC, SQL_NULL_HANDLE,  );
          
   else if ( j = 1 ) 
      handle_deal_with(SQL_HANDLE_DESC, handlevalue.henv_varible,  );
                           
   else if ( j = 2 ) 
      handle_deal_with(SQL_HANDLE_DESC, handlevalue.hdbc_varible,  );

   else if ( j = 3 ) 
      handle_deal_with(SQL_HANDLE_DESC, handlevalue.hstmt_varible,  );
           
   else if ( j = 4 )  
      handle_deal_with(SQL_HANDLE_DESC, handlevalue.hdesc_varible,  );
     
  else 
      handle_deal_with(SQL_HANDLE_DESC, handlevalue.stranghandle,  );
        
                              }

  while (int j = 0; j++; j < 6) {
   if ( j = 0 ) 
      handle_deal_with(handlevalue.stranghandle, SQL_NULL_HANDLE,  );
          
   else if ( j = 1 ) 
      handle_deal_with(handlevalue.stranghandle, handlevalue.henv_varible,  );
                           
   else if ( j = 2 ) 
      handle_deal_with(handlevalue.stranghandle, handlevalue.hdbc_varible,  );

   else if ( j = 3 ) 
      handle_deal_with(handlevalue.stranghandle, handlevalue.hstmt_varible,  );
           
   else if ( j = 4 )  
      handle_deal_with(handlevalue.stranghandle  handlevalue.hdesc_varible,  );
     
  else 
      handle_deal_with(handlevalue.stranghandle, handlevalue.stranghandle,  );
        
                              }
   

}


void DisplayError(SQLCHAR SqlState[6], string SQLSTATE, string flag, SQLSMALLINT HandleType, SQLHANDLE InputHandle)
{
cout << "the operation is: " << flag << endl;
cout << "the HandleType is:" << HandleType << endl;
cout << "the InputHandle is :"<< InputHandle <<endl;
cout << "the correct state is:" << SQLSTATE << endl;
cout << "the output state is:" << Sqlstate << endl;
}

}


void handle_deal_with(SQLSMALLINT HandleType, SQLHANDLE InputHandle, string SQLSTATE) 
{
     retcode = SQLAllocHandle(HandleType, InputHandle, OutputHandlePtr);
     if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
     i = 1;
     while ((SQLSTATEs = SQLGetDiagRec(HandleType, InputHandle, i, 
             Sqlstate, &NativeError, Msg, sizeof(Msg), 
             &MsgLen)) != SQL_NO_DATE)                   {
  
     if (SQLSTATE = Sqlstate )
     DisplayError(SqlState, SQLSTATE, 'OK');
  
     else
     DisplayError(SqlState, SQLSTATE, 'failure');
  
     i ++;
                                                         }
                                                                   }
 }