1 : /*
2 : * Purpose: Test retrieving data and attempting to initiate a new query with results pending
3 : * Functions: dbnextrow dbresults dbsqlexec
4 : */
5 :
6 : #if HAVE_CONFIG_H
7 : #include <config.h>
8 : #endif /* HAVE_CONFIG_H */
9 :
10 : #include <stdio.h>
11 :
12 : #if HAVE_STDLIB_H
13 : #include <stdlib.h>
14 : #endif /* HAVE_STDLIB_H */
15 :
16 : #if HAVE_STRING_H
17 : #include <string.h>
18 : #endif /* HAVE_STRING_H */
19 :
20 : #include <sqlfront.h>
21 : #include <sqldb.h>
22 :
23 : #include "common.h"
24 :
25 :
26 : static char software_version[] = "$Id: t0004.c,v 1.13 2005/05/23 08:06:25 freddy77 Exp $";
27 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
28 :
29 :
30 :
31 : int
32 : main(int argc, char **argv)
33 2 : {
34 : char cmd[1024];
35 2 : const int rows_to_add = 50;
36 : LOGINREC *login;
37 : DBPROCESS *dbproc;
38 : int i;
39 : char teststr[1024];
40 : DBINT testint;
41 2 : int failed = 0;
42 :
43 2 : set_malloc_options();
44 :
45 2 : read_login_info(argc, argv);
46 :
47 2 : fprintf(stdout, "Start\n");
48 2 : add_bread_crumb();
49 :
50 2 : dbinit();
51 :
52 2 : add_bread_crumb();
53 2 : dberrhandle(syb_err_handler);
54 2 : dbmsghandle(syb_msg_handler);
55 :
56 2 : fprintf(stdout, "About to logon\n");
57 :
58 2 : add_bread_crumb();
59 2 : login = dblogin();
60 2 : DBSETLPWD(login, PASSWORD);
61 2 : DBSETLUSER(login, USER);
62 2 : DBSETLAPP(login, "t0004");
63 :
64 2 : fprintf(stdout, "About to open\n");
65 :
66 2 : add_bread_crumb();
67 2 : dbproc = dbopen(login, SERVER);
68 2 : if (strlen(DATABASE))
69 2 : dbuse(dbproc, DATABASE);
70 2 : add_bread_crumb();
71 2 : dbloginfree(login);
72 2 : add_bread_crumb();
73 :
74 2 : add_bread_crumb();
75 :
76 2 : fprintf(stdout, "creating table\n");
77 2 : dbcmd(dbproc, "create table #dblib0004 (i int not null, s char(10) not null)");
78 2 : dbsqlexec(dbproc);
79 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
80 : /* nop */
81 : }
82 :
83 2 : fprintf(stdout, "insert\n");
84 100 : for (i = 1; i < rows_to_add; i++) {
85 98 : sprintf(cmd, "insert into #dblib0004 values (%d, 'row %04d')", i, i);
86 98 : fprintf(stdout, "%s\n", cmd);
87 98 : dbcmd(dbproc, cmd);
88 98 : dbsqlexec(dbproc);
89 196 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
90 : /* nop */
91 : }
92 : }
93 :
94 2 : sprintf(cmd, "select * from #dblib0004 where i<=25 order by i");
95 2 : fprintf(stdout, "%s\n", cmd);
96 2 : dbcmd(dbproc, cmd);
97 2 : dbsqlexec(dbproc);
98 2 : add_bread_crumb();
99 :
100 :
101 2 : if (dbresults(dbproc) != SUCCEED) {
102 0 : add_bread_crumb();
103 0 : fprintf(stdout, "Was expecting a result set.");
104 0 : exit(1);
105 : }
106 2 : add_bread_crumb();
107 :
108 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
109 4 : add_bread_crumb();
110 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
111 4 : add_bread_crumb();
112 : }
113 :
114 2 : add_bread_crumb();
115 2 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
116 2 : add_bread_crumb();
117 2 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
118 2 : add_bread_crumb();
119 :
120 2 : add_bread_crumb();
121 :
122 50 : for (i = 1; i <= 24; i++) {
123 : char expected[1024];
124 :
125 48 : sprintf(expected, "row %04d", i);
126 :
127 48 : add_bread_crumb();
128 :
129 48 : if (i % 5 == 0) {
130 8 : dbclrbuf(dbproc, 5);
131 : }
132 :
133 :
134 48 : testint = -1;
135 48 : strcpy(teststr, "bogus");
136 :
137 48 : add_bread_crumb();
138 48 : if (REG_ROW != dbnextrow(dbproc)) {
139 0 : fprintf(stderr, "Failed. Expected a row\n");
140 0 : exit(1);
141 : }
142 48 : add_bread_crumb();
143 48 : if (testint != i) {
144 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
145 0 : abort();
146 : }
147 48 : if (0 != strncmp(teststr, expected, strlen(expected))) {
148 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
149 0 : abort();
150 : }
151 48 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
152 : }
153 :
154 :
155 :
156 2 : fprintf(stdout, "second select\n");
157 :
158 2 : sprintf(cmd, "select * from #dblib0004 where i>950 order by i");
159 2 : fprintf(stdout, "%s\n", cmd);
160 2 : if (SUCCEED != dbcmd(dbproc, cmd)) {
161 0 : fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
162 0 : failed = 1;
163 : }
164 2 : fprintf(stdout, "About to exec for the second time. Should fail\n");
165 2 : if (FAIL != dbsqlexec(dbproc)) {
166 0 : fprintf(stderr, "%s:%d: dbsqlexec should have failed but didn't\n", __FILE__, __LINE__);
167 0 : failed = 1;
168 : }
169 2 : add_bread_crumb();
170 2 : dbexit();
171 2 : add_bread_crumb();
172 :
173 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
174 2 : free_bread_crumb();
175 2 : return failed ? 1 : 0;
176 : }
|