1 : /*
2 : * Purpose: Test sending and receiving TEXT datatype
3 : * Functions: dbbind dbmoretext dbreadtext dbtxptr dbtxtimestamp dbwritetext
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: t0014.c,v 1.25 2005/06/29 07:21:20 freddy77 Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 :
28 : #define BLOB_BLOCK_SIZE 4096
29 :
30 : int failed = 0;
31 :
32 : char *testargs[] = { "", FREETDS_SRCDIR "/data.bin", "t0014.out" };
33 :
34 : int
35 : main(int argc, char **argv)
36 2 : {
37 2 : const int rows_to_add = 3;
38 : LOGINREC *login;
39 : DBPROCESS *dbproc;
40 : DBPROCESS *blobproc;
41 : int i;
42 : DBINT testint;
43 : FILE *fp;
44 : long result, isiz;
45 : char *blob, *rblob;
46 : unsigned char *textPtr, *timeStamp;
47 : char objname[256];
48 : char sqlCmd[256];
49 : char rbuf[BLOB_BLOCK_SIZE];
50 : long numread;
51 : BOOL readFirstImage;
52 :
53 2 : set_malloc_options();
54 :
55 2 : read_login_info(argc, argv);
56 2 : fprintf(stdout, "Start\n");
57 2 : dbinit();
58 :
59 2 : dberrhandle(syb_err_handler);
60 2 : dbmsghandle(syb_msg_handler);
61 :
62 2 : fprintf(stdout, "About to logon\n");
63 :
64 2 : login = dblogin();
65 2 : DBSETLPWD(login, PASSWORD);
66 2 : DBSETLUSER(login, USER);
67 2 : DBSETLAPP(login, "t0014");
68 :
69 2 : fprintf(stdout, "About to open %s..%s for user '%s'\n", SERVER, DATABASE, USER);
70 :
71 2 : dbproc = dbopen(login, SERVER);
72 2 : blobproc = dbopen(login, SERVER);
73 2 : if (strlen(DATABASE)) {
74 2 : dbuse(dbproc, DATABASE);
75 2 : dbuse(blobproc, DATABASE);
76 : }
77 2 : dbloginfree(login);
78 2 : fprintf(stdout, "After logon\n");
79 :
80 2 : fprintf(stdout, "About to read binary input file\n");
81 :
82 2 : if (argc == 1) {
83 2 : argv = testargs;
84 2 : argc = 3;
85 : }
86 2 : if (argc < 3) {
87 0 : fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
88 0 : return 1;
89 : }
90 :
91 2 : if ((fp = fopen(argv[1], "rb")) == NULL) {
92 0 : fprintf(stderr, "Cannot open input file: %s\n", argv[1]);
93 0 : return 2;
94 : }
95 2 : result = fseek(fp, 0, SEEK_END);
96 2 : isiz = ftell(fp);
97 2 : result = fseek(fp, 0, SEEK_SET);
98 :
99 2 : blob = (char *) malloc(isiz);
100 2 : fread((void *) blob, isiz, 1, fp);
101 2 : fclose(fp);
102 :
103 : /* FIXME this test seem to not work using temporary tables (sybase?)... */
104 2 : fprintf(stdout, "Dropping table\n");
105 2 : dbcmd(dbproc, "drop table dblib0014");
106 2 : dbsqlexec(dbproc);
107 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
108 : /* nop */
109 : }
110 :
111 2 : fprintf(stdout, "creating table\n");
112 2 : dbcmd(dbproc, "create table dblib0014 (i int not null, PigTure image not null)");
113 2 : dbsqlexec(dbproc);
114 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
115 : /* nop */
116 : }
117 :
118 :
119 2 : fprintf(stdout, "insert\n");
120 8 : for (i = 0; i < rows_to_add; i++) {
121 : char cmd[1024];
122 :
123 6 : sprintf(cmd, "insert into dblib0014 values (%d, '')", i);
124 6 : fprintf(stdout, "%s\n", cmd);
125 6 : dbcmd(dbproc, cmd);
126 6 : dbsqlexec(dbproc);
127 12 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
128 : /* nop */
129 : }
130 : }
131 :
132 8 : for (i = 0; i < rows_to_add; i++) {
133 6 : sprintf(sqlCmd, "SELECT PigTure FROM dblib0014 WHERE i = %d", i);
134 6 : dbcmd(dbproc, sqlCmd);
135 6 : dbsqlexec(dbproc);
136 6 : if (dbresults(dbproc) != SUCCEED) {
137 0 : fprintf(stderr, "Error inserting blob\n");
138 0 : return 4;
139 : }
140 :
141 18 : while ((result = dbnextrow(dbproc)) != NO_MORE_ROWS) {
142 6 : result = REG_ROW;
143 6 : result = DBTXPLEN;
144 6 : strcpy(objname, "dblib0014.PigTure");
145 6 : textPtr = dbtxptr(dbproc, 1);
146 6 : timeStamp = dbtxtimestamp(dbproc, 1);
147 :
148 : /*
149 : * Use #ifdef if you want to test dbmoretext mode (needed for 16-bit apps)
150 : * Use #ifndef for big buffer version (32-bit)
151 : */
152 : #if 1
153 : /* DBWRITE_OK_FOR_OVER_4K */
154 6 : if (dbwritetext(blobproc, objname, textPtr, DBTXPLEN, timeStamp, TRUE, isiz, (BYTE*) blob) != SUCCEED)
155 0 : return 5;
156 : #else
157 : if (dbwritetext(blobproc, objname, textPtr, DBTXPLEN, timeStamp, TRUE, isiz, NULL) != SUCCEED)
158 : return 15;
159 : dbsqlok(blobproc);
160 : dbresults(blobproc);
161 :
162 : numtowrite = 0;
163 : /* Send the update value in chunks. */
164 : for (numwritten = 0; numwritten < isiz; numwritten += numtowrite) {
165 : numtowrite = (isiz - numwritten);
166 : if (numtowrite > BLOB_BLOCK_SIZE)
167 : numtowrite = BLOB_BLOCK_SIZE;
168 : dbmoretext(blobproc, (DBINT) numtowrite, blob + numwritten);
169 : }
170 : dbsqlok(blobproc);
171 : while (dbresults(blobproc) != NO_MORE_RESULTS);
172 :
173 : #endif
174 : }
175 : }
176 :
177 2 : fprintf(stdout, "select\n");
178 :
179 2 : dbcmd(dbproc, "select * from dblib0014 order by i");
180 2 : dbsqlexec(dbproc);
181 :
182 2 : if (dbresults(dbproc) != SUCCEED) {
183 0 : failed = 1;
184 0 : fprintf(stdout, "Was expecting a result set.");
185 0 : exit(1);
186 : }
187 :
188 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
189 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
190 : }
191 :
192 2 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
193 0 : failed = 1;
194 0 : fprintf(stderr, "Had problem with bind\n");
195 0 : abort();
196 : }
197 :
198 8 : for (i = 0; i < rows_to_add; i++) {
199 : char expected[1024];
200 :
201 6 : sprintf(expected, "row %03d", i);
202 :
203 6 : if (REG_ROW != dbnextrow(dbproc)) {
204 0 : failed = 1;
205 0 : fprintf(stderr, "Failed. Expected a row\n");
206 0 : exit(1);
207 : }
208 6 : if (testint != i) {
209 0 : failed = 1;
210 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
211 0 : abort();
212 : }
213 :
214 : /* get the image */
215 6 : strcpy(sqlCmd, "SET TEXTSIZE 2147483647");
216 6 : dbcmd(blobproc, sqlCmd);
217 6 : dbsqlexec(blobproc);
218 :
219 6 : if (dbresults(blobproc) != SUCCEED) {
220 0 : dbcancel(blobproc);
221 0 : return 16;
222 : }
223 :
224 6 : sprintf(sqlCmd, "SELECT PigTure FROM dblib0014 WHERE i = %d", i);
225 6 : dbcmd(blobproc, sqlCmd);
226 6 : dbsqlexec(blobproc);
227 6 : if (dbresults(blobproc) != SUCCEED) {
228 0 : fprintf(stderr, "Error extracting blob\n");
229 0 : return 6;
230 : }
231 :
232 6 : numread = 0;
233 6 : rblob = NULL;
234 6 : readFirstImage = FALSE;
235 108 : while ((result = dbreadtext(blobproc, rbuf, BLOB_BLOCK_SIZE)) != NO_MORE_ROWS) {
236 96 : if (result == 0) { /* this indicates end of row */
237 6 : readFirstImage = TRUE;
238 : } else {
239 90 : rblob = (char*) realloc(rblob, result + numread);
240 90 : memcpy((void *) (rblob + numread), (void *) rbuf, result);
241 90 : numread += result;
242 : }
243 : }
244 :
245 6 : if (i == 0) {
246 2 : printf("Saving first blob data row to file: %s\n", argv[2]);
247 2 : if ((fp = fopen(argv[2], "wb")) == NULL) {
248 0 : fprintf(stderr, "Unable to open output file: %s\n", argv[2]);
249 0 : return 3;
250 : }
251 :
252 2 : result = fwrite((void *) rblob, numread, 1, fp);
253 2 : fclose(fp);
254 : }
255 :
256 6 : printf("Read blob data row %d --> %s %ld byte comparison\n",
257 : (int) testint, (memcmp(blob, rblob, numread)) ? "failed" : "PASSED", numread);
258 6 : free(rblob);
259 : }
260 :
261 2 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
262 0 : failed = 1;
263 0 : fprintf(stderr, "Was expecting no more rows\n");
264 0 : exit(1);
265 : }
266 :
267 2 : free(blob);
268 :
269 2 : fprintf(stdout, "Dropping table\n");
270 2 : dbcmd(dbproc, "drop table dblib0014");
271 2 : dbsqlexec(dbproc);
272 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
273 : /* nop */
274 : }
275 :
276 2 : dbexit();
277 :
278 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
279 2 : return failed ? 1 : 0;
280 : }
|