Line data Source code
1 : /*
2 : * Purpose: Test retrieving output parameters and return status
3 : * Functions: DBTDS dbnumrets dbresults dbretdata dbretlen dbretname dbrettype dbsqlexec
4 : */
5 :
6 : #include "common.h"
7 : #include <assert.h>
8 :
9 : int
10 8 : main(int argc, char **argv)
11 : {
12 : LOGINREC *login;
13 : DBPROCESS *dbproc;
14 : int i;
15 : char teststr[1024];
16 8 : int erc, failed = 0;
17 8 : char *retname = NULL;
18 8 : int rettype = 0, retlen = 0;
19 :
20 8 : set_malloc_options();
21 :
22 8 : read_login_info(argc, argv);
23 :
24 8 : printf("Starting %s\n", argv[0]);
25 :
26 8 : dbinit();
27 :
28 8 : dberrhandle(syb_err_handler);
29 8 : dbmsghandle(syb_msg_handler);
30 :
31 8 : printf("About to logon\n");
32 :
33 8 : login = dblogin();
34 8 : DBSETLPWD(login, PASSWORD);
35 8 : DBSETLUSER(login, USER);
36 8 : DBSETLAPP(login, "t0022");
37 :
38 8 : printf("About to open\n");
39 :
40 8 : dbproc = dbopen(login, SERVER);
41 8 : if (strlen(DATABASE))
42 8 : dbuse(dbproc, DATABASE);
43 8 : dbloginfree(login);
44 :
45 8 : printf("Dropping proc\n");
46 8 : sql_cmd(dbproc);
47 8 : dbsqlexec(dbproc);
48 8 : while ((erc = dbresults(dbproc)) == SUCCEED) {
49 12 : printf("dbresult succeeded dropping procedure\n");
50 12 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
51 0 : printf("dbnextrow returned spurious rows dropping procedure\n");
52 0 : assert(0); /* dropping a procedure returns no rows */
53 : }
54 12 : assert(erc == NO_MORE_ROWS);
55 : }
56 8 : assert(erc == NO_MORE_RESULTS);
57 :
58 8 : printf("creating proc\n");
59 8 : sql_cmd(dbproc);
60 8 : if (dbsqlexec(dbproc) == FAIL) {
61 0 : printf("Failed to create proc t0022.\n");
62 0 : exit(1);
63 : }
64 16 : while ((erc = dbresults(dbproc)) != NO_MORE_RESULTS) {
65 8 : assert(erc != FAIL);
66 8 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
67 0 : assert(0); /* creating a procedure returns no rows */
68 : }
69 8 : assert(erc == NO_MORE_ROWS);
70 : }
71 :
72 8 : sql_cmd(dbproc);
73 8 : dbsqlexec(dbproc);
74 :
75 8 : while ((erc = dbresults(dbproc)) != NO_MORE_RESULTS) {
76 8 : if (erc == FAIL) {
77 0 : printf("Was expecting a result set.\n");
78 0 : exit(1);
79 : }
80 8 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
81 0 : assert(0); /* procedure returns no rows */
82 : }
83 8 : assert(erc == NO_MORE_ROWS);
84 : }
85 :
86 : #if defined(DBTDS_7_0) && defined(DBTDS_7_1) && defined(DBTDS_7_2) && defined(DBTDS_7_3) \
87 : && defined(DBTDS_7_4) && defined(DBTDS_8_0_)
88 8 : if ((dbnumrets(dbproc) == 0)
89 6 : && ((DBTDS(dbproc) == DBTDS_7_0)
90 6 : || (DBTDS(dbproc) == DBTDS_7_1)
91 2 : || (DBTDS(dbproc) == DBTDS_7_2)
92 2 : || (DBTDS(dbproc) == DBTDS_7_3)
93 0 : || (DBTDS(dbproc) == DBTDS_7_4)
94 0 : || (DBTDS(dbproc) == DBTDS_8_0_))) {
95 6 : printf("WARNING: Received no return parameters from server!\n");
96 6 : printf("WARNING: This is likely due to a bug in Microsoft\n");
97 6 : printf("WARNING: SQL Server 7.0 SP3 and later.\n");
98 6 : printf("WARNING: Please try again using TDS protocol 4.2.\n");
99 6 : dbcmd(dbproc, "drop proc t0022");
100 6 : dbsqlexec(dbproc);
101 6 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
102 : /* nop */
103 : }
104 6 : dbexit();
105 6 : exit(0);
106 : }
107 : #endif
108 2 : for (i = 1; i <= dbnumrets(dbproc); i++) {
109 2 : retname = dbretname(dbproc, i);
110 2 : printf("ret name %d is %s\n", i, retname);
111 2 : rettype = dbrettype(dbproc, i);
112 2 : printf("ret type %d is %d\n", i, rettype);
113 2 : retlen = dbretlen(dbproc, i);
114 2 : printf("ret len %d is %d\n", i, retlen);
115 2 : dbconvert(dbproc, rettype, dbretdata(dbproc, i), retlen, SYBVARCHAR, (BYTE*) teststr, -1);
116 2 : printf("ret data %d is %s\n", i, teststr);
117 : }
118 2 : if ((retname == NULL) || strcmp(retname, "@b")) {
119 0 : printf("Was expecting a retname to be @b.\n");
120 0 : exit(1);
121 : }
122 2 : if (strcmp(teststr, "42")) {
123 0 : printf("Was expecting a retdata to be 42.\n");
124 0 : exit(1);
125 : }
126 2 : if (rettype != SYBINT4) {
127 0 : printf("Was expecting a rettype to be SYBINT4 was %d.\n", rettype);
128 0 : exit(1);
129 : }
130 2 : if (retlen != 4) {
131 0 : printf("Was expecting a retlen to be 4.\n");
132 0 : exit(1);
133 : }
134 :
135 2 : printf("Dropping proc\n");
136 2 : sql_cmd(dbproc);
137 2 : dbsqlexec(dbproc);
138 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
139 : /* nop */
140 : }
141 :
142 : /*
143 : * Chapter 2: test for resultsets containing only a return status
144 : */
145 :
146 2 : printf("Dropping proc t0022a\n");
147 2 : sql_cmd(dbproc);
148 :
149 2 : dbsqlexec(dbproc);
150 :
151 2 : while ((erc = dbresults(dbproc)) == SUCCEED) {
152 4 : printf("dbresult succeeded dropping procedure\n");
153 4 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
154 0 : printf("dbnextrow returned spurious rows dropping procedure\n");
155 0 : assert(0); /* dropping a procedure returns no rows */
156 : }
157 4 : assert(erc == NO_MORE_ROWS);
158 : }
159 2 : assert(erc == NO_MORE_RESULTS);
160 :
161 2 : printf("creating proc t0022a\n");
162 2 : sql_cmd(dbproc);
163 2 : if (dbsqlexec(dbproc) == FAIL) {
164 0 : printf("Failed to create proc t0022a.\n");
165 0 : exit(1);
166 : }
167 4 : while ((erc = dbresults(dbproc)) != NO_MORE_RESULTS) {
168 2 : assert(erc != FAIL);
169 2 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
170 0 : assert(0); /* creating a procedure returns no rows */
171 : }
172 2 : assert(erc == NO_MORE_ROWS);
173 : }
174 :
175 2 : sql_cmd(dbproc);
176 2 : dbsqlexec(dbproc);
177 :
178 4 : for (i=1; (erc = dbresults(dbproc)) != NO_MORE_RESULTS; i++) {
179 : enum {expected_iterations = 2};
180 : DBBOOL fret;
181 : DBINT status;
182 2 : if (erc == FAIL) {
183 0 : printf("t0022a failed for some reason.\n");
184 0 : exit(1);
185 : }
186 2 : printf("procedure returned %srows\n", DBROWS(dbproc)==SUCCEED? "" : "no ");
187 2 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
188 0 : assert(0); /* procedure returns no rows */
189 : }
190 2 : assert(erc == NO_MORE_ROWS);
191 :
192 2 : fret = dbhasretstat(dbproc);
193 2 : printf("procedure has %sreturn status\n", fret==TRUE? "" : "no ");
194 2 : assert(fret == TRUE);
195 :
196 2 : status = dbretstatus(dbproc);
197 2 : printf("return status %d is %d\n", i, (int) status);
198 2 : switch (i) {
199 2 : case 1: assert(status == 17); break;
200 0 : case 2: assert(status == 1024); break;
201 0 : default: assert(i <= expected_iterations);
202 : }
203 :
204 : }
205 :
206 : assert(erc == NO_MORE_RESULTS);
207 :
208 2 : printf("Dropping proc t0022a\n");
209 2 : sql_cmd(dbproc);
210 2 : dbsqlexec(dbproc);
211 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
212 : /* nop */
213 : }
214 :
215 : /* end chapter 2 */
216 :
217 :
218 2 : dbexit();
219 :
220 2 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
221 : return failed ? 1 : 0;
222 : }
|