1 : /*
2 : * Purpose: Test binding and retrieving strings and ints, and cancelling results
3 : * Functions: dbbind dbcancel dbnextrow dbnumcols 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 : static char software_version[] = "$Id: t0015.c,v 1.15 2005/05/23 08:06:25 freddy77 Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 :
28 :
29 :
30 : int failed = 0;
31 :
32 :
33 : int
34 : main(int argc, char **argv)
35 2 : {
36 2 : const int rows_to_add = 50;
37 : LOGINREC *login;
38 : DBPROCESS *dbproc;
39 : int i;
40 : char teststr[1024];
41 : DBINT testint;
42 :
43 2 : set_malloc_options();
44 :
45 2 : read_login_info(argc, argv);
46 2 : fprintf(stdout, "Start\n");
47 2 : add_bread_crumb();
48 :
49 : /* Fortify_EnterScope(); */
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, "t0015");
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 : fprintf(stdout, "creating table\n");
75 2 : dbcmd(dbproc, "create table #dblib0015 (i int not null, s char(10) not null)");
76 2 : dbsqlexec(dbproc);
77 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
78 : /* nop */
79 : }
80 :
81 2 : fprintf(stdout, "insert\n");
82 102 : for (i = 0; i < rows_to_add; i++) {
83 : char cmd[1024];
84 :
85 100 : sprintf(cmd, "insert into #dblib0015 values (%d, 'row %03d')", i, i);
86 100 : fprintf(stdout, "%s\n", cmd);
87 100 : dbcmd(dbproc, cmd);
88 100 : dbsqlexec(dbproc);
89 200 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
90 : /* nop */
91 : }
92 : }
93 :
94 2 : fprintf(stdout, "select\n");
95 2 : dbcmd(dbproc, "select * from #dblib0015 order by i");
96 2 : dbsqlexec(dbproc);
97 2 : add_bread_crumb();
98 :
99 :
100 2 : if (dbresults(dbproc) != SUCCEED) {
101 0 : add_bread_crumb();
102 0 : failed = 1;
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 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
116 0 : failed = 1;
117 0 : fprintf(stderr, "Had problem with bind\n");
118 0 : abort();
119 : }
120 2 : add_bread_crumb();
121 2 : if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr)) {
122 0 : failed = 1;
123 0 : fprintf(stderr, "Had problem with bind\n");
124 0 : abort();
125 : }
126 2 : add_bread_crumb();
127 :
128 2 : add_bread_crumb();
129 :
130 2 : if (REG_ROW != dbnextrow(dbproc)) {
131 0 : failed = 1;
132 0 : fprintf(stderr, "Failed. Expected a row\n");
133 0 : exit(1);
134 : }
135 :
136 2 : dbcancel(dbproc);
137 :
138 2 : fprintf(stdout, "select 2\n");
139 2 : dbcmd(dbproc, "select * from #dblib0015 where i > 25 order by i");
140 2 : dbsqlexec(dbproc);
141 :
142 2 : if (dbresults(dbproc) != SUCCEED) {
143 0 : add_bread_crumb();
144 0 : failed = 1;
145 0 : fprintf(stdout, "Was expecting a result set.");
146 0 : exit(1);
147 : }
148 :
149 2 : add_bread_crumb();
150 2 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
151 0 : failed = 1;
152 0 : fprintf(stderr, "Had problem with bind\n");
153 0 : abort();
154 : }
155 2 : add_bread_crumb();
156 2 : if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr)) {
157 0 : failed = 1;
158 0 : fprintf(stderr, "Had problem with bind\n");
159 0 : abort();
160 : }
161 2 : add_bread_crumb();
162 :
163 50 : for (i = 26; i < rows_to_add; i++) {
164 : char expected[1024];
165 :
166 48 : sprintf(expected, "row %03d", i);
167 :
168 48 : add_bread_crumb();
169 48 : if (REG_ROW != dbnextrow(dbproc)) {
170 0 : failed = 1;
171 0 : fprintf(stderr, "Failed. Expected a row\n");
172 0 : exit(1);
173 : }
174 48 : add_bread_crumb();
175 48 : if (testint != i) {
176 0 : failed = 1;
177 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
178 0 : abort();
179 : }
180 48 : if (0 != strncmp(teststr, expected, strlen(expected))) {
181 0 : failed = 1;
182 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
183 0 : abort();
184 : }
185 48 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
186 : }
187 :
188 2 : add_bread_crumb();
189 2 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
190 0 : failed = 1;
191 0 : fprintf(stderr, "Was expecting no more rows\n");
192 0 : exit(1);
193 : }
194 :
195 2 : add_bread_crumb();
196 2 : dbexit();
197 2 : add_bread_crumb();
198 :
199 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
200 2 : free_bread_crumb();
201 2 : return failed ? 1 : 0;
202 : }
|