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