1 : /*
2 : * Purpose: Test buffering
3 : * Functions: dbclrbuf dbgetrow dbsetopt
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 :
27 : static char software_version[] = "$Id: t0003.c,v 1.11 2005/05/23 08:06:25 freddy77 Exp $";
28 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
29 :
30 :
31 :
32 : int failed = 0;
33 :
34 :
35 : int
36 : main(int argc, char **argv)
37 2 : {
38 : RETCODE rc;
39 2 : const int rows_to_add = 50;
40 : LOGINREC *login;
41 : DBPROCESS *dbproc;
42 : int i;
43 : char teststr[1024];
44 : DBINT testint;
45 :
46 2 : set_malloc_options();
47 :
48 2 : read_login_info(argc, argv);
49 :
50 2 : fprintf(stdout, "Start\n");
51 2 : add_bread_crumb();
52 :
53 : /* Fortify_EnterScope(); */
54 2 : dbinit();
55 :
56 2 : add_bread_crumb();
57 2 : dberrhandle(syb_err_handler);
58 2 : dbmsghandle(syb_msg_handler);
59 :
60 2 : fprintf(stdout, "About to logon\n");
61 :
62 2 : add_bread_crumb();
63 2 : login = dblogin();
64 2 : DBSETLPWD(login, PASSWORD);
65 2 : DBSETLUSER(login, USER);
66 2 : DBSETLAPP(login, "t0003");
67 2 : DBSETLHOST(login, "ntbox.dntis.ro");
68 :
69 2 : fprintf(stdout, "About to open\n");
70 :
71 2 : add_bread_crumb();
72 2 : dbproc = dbopen(login, SERVER);
73 2 : if (strlen(DATABASE))
74 2 : dbuse(dbproc, DATABASE);
75 2 : add_bread_crumb();
76 2 : dbloginfree(login);
77 2 : add_bread_crumb();
78 :
79 2 : dbsetopt(dbproc, DBBUFFER, "100", 0);
80 2 : add_bread_crumb();
81 :
82 2 : fprintf(stdout, "creating table\n");
83 2 : dbcmd(dbproc, "create table #dblib0003 (i int not null, s char(10) not null)");
84 2 : dbsqlexec(dbproc);
85 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
86 : /* nop */
87 : }
88 :
89 2 : fprintf(stdout, "insert\n");
90 100 : for (i = 1; i < rows_to_add; i++) {
91 : char cmd[1024];
92 :
93 98 : sprintf(cmd, "insert into #dblib0003 values (%d, 'row %03d')", i, i);
94 98 : dbcmd(dbproc, cmd);
95 98 : dbsqlexec(dbproc);
96 196 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
97 : /* nop */
98 : }
99 : }
100 :
101 2 : fprintf(stdout, "select\n");
102 2 : dbcmd(dbproc, "select * from #dblib0003 order by i");
103 2 : dbsqlexec(dbproc);
104 2 : add_bread_crumb();
105 :
106 :
107 2 : if (dbresults(dbproc) != SUCCEED) {
108 0 : failed = 1;
109 0 : add_bread_crumb();
110 0 : fprintf(stdout, "Was expecting a result set.");
111 0 : exit(1);
112 : }
113 2 : add_bread_crumb();
114 :
115 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
116 4 : add_bread_crumb();
117 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
118 4 : add_bread_crumb();
119 : }
120 :
121 2 : add_bread_crumb();
122 2 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
123 2 : add_bread_crumb();
124 2 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
125 2 : add_bread_crumb();
126 :
127 2 : add_bread_crumb();
128 :
129 100 : for (i = 1; i < rows_to_add; i++) {
130 : char expected[1024];
131 :
132 98 : sprintf(expected, "row %03d", i);
133 :
134 98 : add_bread_crumb();
135 :
136 98 : if (i % 100 == 0) {
137 0 : dbclrbuf(dbproc, 100);
138 : }
139 :
140 98 : add_bread_crumb();
141 98 : if (REG_ROW != dbnextrow(dbproc)) {
142 0 : failed = 1;
143 0 : fprintf(stderr, "Failed. Expected a row\n");
144 0 : exit(1);
145 : }
146 98 : add_bread_crumb();
147 98 : if (testint != i) {
148 0 : failed = 1;
149 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
150 0 : abort();
151 : }
152 98 : if (0 != strncmp(teststr, expected, strlen(expected))) {
153 0 : failed = 1;
154 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
155 0 : abort();
156 : }
157 98 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
158 : }
159 :
160 :
161 2 : add_bread_crumb();
162 2 : dbclrbuf(dbproc, 1);
163 2 : rc = dbnextrow(dbproc);
164 2 : if (rc != NO_MORE_ROWS) {
165 0 : failed = 1;
166 0 : fprintf(stderr, "Was expecting no more rows. (rc=%d)\n", rc);
167 0 : exit(1);
168 : }
169 :
170 2 : add_bread_crumb();
171 2 : dbexit();
172 2 : add_bread_crumb();
173 :
174 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
175 2 : free_bread_crumb();
176 2 : return failed ? 1 : 0;
177 : }
|