1 : /*
2 : * Purpose: Test behaviour of DBCOUNT() for updates and inserts
3 : * Functions: DBCOUNT dbbind dbnextrow dbnumcols dbopen 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: t0018.c,v 1.16 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, "t0018");
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 #dblib0018 (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 #dblib0018 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 100 : if (DBCOUNT(dbproc) != 1) {
93 0 : failed = 1;
94 0 : fprintf(stdout, "Was expecting a rows affect to be 1.");
95 0 : exit(1);
96 : }
97 : }
98 :
99 2 : fprintf(stdout, "select\n");
100 2 : dbcmd(dbproc, "select * into #tmp0 from #dblib0018\nselect * from #tmp0 order by i");
101 2 : dbsqlexec(dbproc);
102 2 : add_bread_crumb();
103 :
104 2 : fprintf(stdout, "Checking for an empty result set.\n");
105 2 : if (dbresults(dbproc) != SUCCEED) {
106 0 : add_bread_crumb();
107 0 : failed = 1;
108 0 : fprintf(stdout, "Was expecting a result set.\n");
109 0 : exit(1);
110 : }
111 2 : add_bread_crumb();
112 :
113 2 : if(DBROWS(dbproc) != FAIL) {
114 0 : add_bread_crumb();
115 0 : failed = 1;
116 0 : fprintf(stdout, "Was expecting no rows to be available.\n");
117 0 : exit(1);
118 : }
119 2 : add_bread_crumb();
120 :
121 2 : fprintf(stdout, "Checking for a result set with content.\n");
122 2 : if (dbresults(dbproc) != SUCCEED) {
123 0 : add_bread_crumb();
124 0 : failed = 1;
125 0 : fprintf(stdout, "Was expecting a result set.");
126 0 : exit(1);
127 : }
128 2 : add_bread_crumb();
129 :
130 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
131 4 : add_bread_crumb();
132 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
133 4 : add_bread_crumb();
134 : }
135 :
136 2 : add_bread_crumb();
137 2 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
138 0 : failed = 1;
139 0 : fprintf(stderr, "Had problem with bind\n");
140 0 : abort();
141 : }
142 2 : add_bread_crumb();
143 2 : if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr)) {
144 0 : failed = 1;
145 0 : fprintf(stderr, "Had problem with bind\n");
146 0 : abort();
147 : }
148 2 : add_bread_crumb();
149 :
150 2 : add_bread_crumb();
151 :
152 102 : for (i = 0; i < rows_to_add; i++) {
153 : char expected[1024];
154 :
155 100 : sprintf(expected, "row %03d", i);
156 :
157 100 : add_bread_crumb();
158 100 : if (REG_ROW != dbnextrow(dbproc)) {
159 0 : failed = 1;
160 0 : fprintf(stderr, "Failed. Expected a row\n");
161 0 : exit(1);
162 : }
163 100 : add_bread_crumb();
164 100 : if (testint != i) {
165 0 : failed = 1;
166 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
167 0 : abort();
168 : }
169 100 : if (0 != strncmp(teststr, expected, strlen(expected))) {
170 0 : failed = 1;
171 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
172 0 : abort();
173 : }
174 100 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
175 : }
176 :
177 :
178 2 : add_bread_crumb();
179 2 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
180 0 : failed = 1;
181 0 : fprintf(stderr, "Was expecting no more rows\n");
182 0 : exit(1);
183 : }
184 2 : if (DBCOUNT(dbproc) != rows_to_add) {
185 0 : failed = 1;
186 0 : fprintf(stdout, "Was expecting a rows affect to be %d was %d.\n", rows_to_add, DBCOUNT(dbproc));
187 0 : exit(1);
188 : }
189 :
190 2 : dbcmd(dbproc, "update #dblib0018 set s = 'row 000'");
191 2 : dbsqlexec(dbproc);
192 2 : add_bread_crumb();
193 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
194 : /* nop */
195 : }
196 2 : if (DBCOUNT(dbproc) != rows_to_add) {
197 0 : failed = 1;
198 0 : fprintf(stdout, "Was expecting a rows affect to be %d was %d.\n", rows_to_add, DBCOUNT(dbproc));
199 0 : exit(1);
200 : } else {
201 2 : fprintf(stdout, "Number of rows affected by update = %d.\n", DBCOUNT(dbproc));
202 : }
203 :
204 2 : add_bread_crumb();
205 2 : dbexit();
206 2 : add_bread_crumb();
207 :
208 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
209 2 : free_bread_crumb();
210 2 : return failed ? 1 : 0;
211 : }
|