1 : /*
2 : * Purpose: Test binding strings and ints, attempt 2nd query with results pending.
3 : * Functions: dbbind dbcmd dbnextrow 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 :
26 : static char software_version[] = "$Id: t0007.c,v 1.14 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 : static void
32 : create_tables(DBPROCESS * dbproc, int rows_to_add)
33 2 : {
34 : int i;
35 : char cmd[1024];
36 :
37 :
38 2 : fprintf(stdout, "creating table\n");
39 2 : dbcmd(dbproc, "create table #dblib0007 (i int not null, s char(12) not null)");
40 2 : dbsqlexec(dbproc);
41 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
42 : /* nop */
43 : }
44 :
45 2 : fprintf(stdout, "insert\n");
46 20 : for (i = 1; i < rows_to_add; i++) {
47 18 : sprintf(cmd, "insert into #dblib0007 values (%d, 'row %07d')", i, i);
48 18 : fprintf(stdout, "%s\n", cmd);
49 18 : dbcmd(dbproc, cmd);
50 18 : dbsqlexec(dbproc);
51 36 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
52 : /* nop */
53 : }
54 : }
55 2 : } /* create_tables() */
56 :
57 :
58 : static int
59 : start_query(DBPROCESS * dbproc, const char *cmd)
60 4 : {
61 : int i;
62 :
63 4 : fprintf(stdout, "%s\n", cmd);
64 4 : if (SUCCEED != dbcmd(dbproc, cmd)) {
65 0 : return 0;
66 : }
67 4 : if (SUCCEED != dbsqlexec(dbproc)) {
68 2 : return 0;
69 : }
70 2 : add_bread_crumb();
71 :
72 2 : if (dbresults(dbproc) != SUCCEED) {
73 0 : add_bread_crumb();
74 0 : return 0;
75 : }
76 2 : add_bread_crumb();
77 :
78 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
79 4 : add_bread_crumb();
80 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
81 4 : add_bread_crumb();
82 : }
83 2 : return 1;
84 : } /* start_query() */
85 :
86 : int
87 : main(int argc, char **argv)
88 2 : {
89 : LOGINREC *login;
90 : DBPROCESS *dbproc;
91 : int i;
92 : char teststr[1024];
93 : DBINT testint;
94 2 : int failed = 0;
95 :
96 2 : set_malloc_options();
97 :
98 2 : read_login_info(argc, argv);
99 :
100 2 : fprintf(stdout, "Start\n");
101 2 : add_bread_crumb();
102 :
103 2 : dbinit();
104 :
105 2 : add_bread_crumb();
106 2 : dberrhandle(syb_err_handler);
107 2 : dbmsghandle(syb_msg_handler);
108 :
109 2 : fprintf(stdout, "About to logon\n");
110 :
111 2 : add_bread_crumb();
112 2 : login = dblogin();
113 2 : DBSETLPWD(login, PASSWORD);
114 2 : DBSETLUSER(login, USER);
115 2 : DBSETLAPP(login, "t0007");
116 :
117 2 : fprintf(stdout, "About to open\n");
118 :
119 2 : add_bread_crumb();
120 2 : dbproc = dbopen(login, SERVER);
121 2 : if (strlen(DATABASE))
122 2 : dbuse(dbproc, DATABASE);
123 2 : add_bread_crumb();
124 2 : dbloginfree(login);
125 2 : add_bread_crumb();
126 :
127 2 : add_bread_crumb();
128 :
129 2 : create_tables(dbproc, 10);
130 :
131 2 : if (!start_query(dbproc, "select * from #dblib0007 where i<=5 order by i")) {
132 0 : fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
133 0 : failed = 1;
134 : }
135 :
136 2 : add_bread_crumb();
137 2 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
138 2 : add_bread_crumb();
139 2 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
140 2 : add_bread_crumb();
141 :
142 2 : add_bread_crumb();
143 :
144 6 : for (i = 1; i <= 2; i++) {
145 : char expected[1024];
146 :
147 4 : sprintf(expected, "row %07d", i);
148 :
149 4 : add_bread_crumb();
150 :
151 4 : if (i % 5 == 0) {
152 0 : dbclrbuf(dbproc, 5);
153 : }
154 :
155 4 : testint = -1;
156 4 : strcpy(teststr, "bogus");
157 :
158 4 : add_bread_crumb();
159 4 : if (REG_ROW != dbnextrow(dbproc)) {
160 0 : fprintf(stderr, "Failed. Expected a row\n");
161 0 : abort();
162 : }
163 4 : add_bread_crumb();
164 4 : if (testint != i) {
165 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
166 0 : abort();
167 : }
168 4 : if (0 != strncmp(teststr, expected, strlen(expected))) {
169 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
170 0 : abort();
171 : }
172 4 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
173 : }
174 :
175 :
176 2 : fprintf(stdout, "second select. Should fail.\n");
177 :
178 2 : if (start_query(dbproc, "select * from #dblib0007 where i>=5 order by i")) {
179 0 : fprintf(stderr, "%s:%d: start_query should have failed but didn't\n", __FILE__, __LINE__);
180 0 : failed = 1;
181 : }
182 :
183 2 : add_bread_crumb();
184 2 : dbexit();
185 2 : add_bread_crumb();
186 :
187 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
188 2 : free_bread_crumb();
189 2 : return failed ? 1 : 0;
190 : }
|