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) && defined(DBTDS_7_4)
87 8 : if ((dbnumrets(dbproc) == 0)
88 6 : && ((DBTDS(dbproc) == DBTDS_7_0)
89 6 : || (DBTDS(dbproc) == DBTDS_7_1)
90 2 : || (DBTDS(dbproc) == DBTDS_7_2)
91 2 : || (DBTDS(dbproc) == DBTDS_7_3)
92 0 : || (DBTDS(dbproc) == DBTDS_7_4))) {
93 6 : printf("WARNING: Received no return parameters from server!\n");
94 6 : printf("WARNING: This is likely due to a bug in Microsoft\n");
95 6 : printf("WARNING: SQL Server 7.0 SP3 and later.\n");
96 6 : printf("WARNING: Please try again using TDS protocol 4.2.\n");
97 6 : dbcmd(dbproc, "drop proc t0022");
98 6 : dbsqlexec(dbproc);
99 6 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
100 : /* nop */
101 : }
102 6 : dbexit();
103 6 : exit(0);
104 : }
105 : #endif
106 2 : for (i = 1; i <= dbnumrets(dbproc); i++) {
107 2 : retname = dbretname(dbproc, i);
108 2 : printf("ret name %d is %s\n", i, retname);
109 2 : rettype = dbrettype(dbproc, i);
110 2 : printf("ret type %d is %d\n", i, rettype);
111 2 : retlen = dbretlen(dbproc, i);
112 2 : printf("ret len %d is %d\n", i, retlen);
113 2 : dbconvert(dbproc, rettype, dbretdata(dbproc, i), retlen, SYBVARCHAR, (BYTE*) teststr, -1);
114 2 : printf("ret data %d is %s\n", i, teststr);
115 : }
116 2 : if ((retname == NULL) || strcmp(retname, "@b")) {
117 0 : printf("Was expecting a retname to be @b.\n");
118 0 : exit(1);
119 : }
120 2 : if (strcmp(teststr, "42")) {
121 0 : printf("Was expecting a retdata to be 42.\n");
122 0 : exit(1);
123 : }
124 2 : if (rettype != SYBINT4) {
125 0 : printf("Was expecting a rettype to be SYBINT4 was %d.\n", rettype);
126 0 : exit(1);
127 : }
128 2 : if (retlen != 4) {
129 0 : printf("Was expecting a retlen to be 4.\n");
130 0 : exit(1);
131 : }
132 :
133 2 : printf("Dropping proc\n");
134 2 : sql_cmd(dbproc);
135 2 : dbsqlexec(dbproc);
136 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
137 : /* nop */
138 : }
139 :
140 : /*
141 : * Chapter 2: test for resultsets containing only a return status
142 : */
143 :
144 2 : printf("Dropping proc t0022a\n");
145 2 : sql_cmd(dbproc);
146 :
147 2 : dbsqlexec(dbproc);
148 :
149 2 : while ((erc = dbresults(dbproc)) == SUCCEED) {
150 4 : printf("dbresult succeeded dropping procedure\n");
151 4 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
152 0 : printf("dbnextrow returned spurious rows dropping procedure\n");
153 0 : assert(0); /* dropping a procedure returns no rows */
154 : }
155 4 : assert(erc == NO_MORE_ROWS);
156 : }
157 2 : assert(erc == NO_MORE_RESULTS);
158 :
159 2 : printf("creating proc t0022a\n");
160 2 : sql_cmd(dbproc);
161 2 : if (dbsqlexec(dbproc) == FAIL) {
162 0 : printf("Failed to create proc t0022a.\n");
163 0 : exit(1);
164 : }
165 4 : while ((erc = dbresults(dbproc)) != NO_MORE_RESULTS) {
166 2 : assert(erc != FAIL);
167 2 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
168 0 : assert(0); /* creating a procedure returns no rows */
169 : }
170 2 : assert(erc == NO_MORE_ROWS);
171 : }
172 :
173 2 : sql_cmd(dbproc);
174 2 : dbsqlexec(dbproc);
175 :
176 4 : for (i=1; (erc = dbresults(dbproc)) != NO_MORE_RESULTS; i++) {
177 : enum {expected_iterations = 2};
178 : DBBOOL fret;
179 : DBINT status;
180 2 : if (erc == FAIL) {
181 0 : printf("t0022a failed for some reason.\n");
182 0 : exit(1);
183 : }
184 2 : printf("procedure returned %srows\n", DBROWS(dbproc)==SUCCEED? "" : "no ");
185 2 : while ((erc = dbnextrow(dbproc)) == SUCCEED) {
186 0 : assert(0); /* procedure returns no rows */
187 : }
188 2 : assert(erc == NO_MORE_ROWS);
189 :
190 2 : fret = dbhasretstat(dbproc);
191 2 : printf("procedure has %sreturn status\n", fret==TRUE? "" : "no ");
192 2 : assert(fret == TRUE);
193 :
194 2 : status = dbretstatus(dbproc);
195 2 : printf("return status %d is %d\n", i, (int) status);
196 2 : switch (i) {
197 2 : case 1: assert(status == 17); break;
198 0 : case 2: assert(status == 1024); break;
199 0 : default: assert(i <= expected_iterations);
200 : }
201 :
202 : }
203 :
204 : assert(erc == NO_MORE_RESULTS);
205 :
206 2 : printf("Dropping proc t0022a\n");
207 2 : sql_cmd(dbproc);
208 2 : dbsqlexec(dbproc);
209 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
210 : /* nop */
211 : }
212 :
213 : /* end chapter 2 */
214 :
215 :
216 2 : dbexit();
217 :
218 2 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
219 : return failed ? 1 : 0;
220 : }
|