1 /*
2  * Purpose: Test retrieving data and attempting to initiate a new query with results pending
3  * Functions: dbnextrow dbresults dbsqlexec dbgetchar
4  */
5 
6 #include "common.h"
7 #include <ctype.h>
8 
9 #include <common/test_assert.h>
10 
11 int
main(int argc,char ** argv)12 main(int argc, char **argv)
13 {
14 	const int rows_to_add = 50;
15 	LOGINREC *login;
16 	DBPROCESS *dbproc;
17 	int i, expected_error;
18 	char *s, teststr[1024];
19 	DBINT testint;
20 	int failed = 0;
21 
22 	set_malloc_options();
23 
24 	read_login_info(argc, argv);
25 
26 	fprintf(stdout, "Starting %s\n", argv[0]);
27 
28 	dbinit();
29 
30 	dberrhandle(syb_err_handler);
31 	dbmsghandle(syb_msg_handler);
32 
33 	fprintf(stdout, "About to logon\n");
34 
35 	login = dblogin();
36 	DBSETLPWD(login, PASSWORD);
37 	DBSETLUSER(login, USER);
38 	DBSETLAPP(login, "t0004");
39 
40 	fprintf(stdout, "About to open\n");
41 
42 	dbproc = dbopen(login, SERVER);
43 	if (strlen(DATABASE))
44 		dbuse(dbproc, DATABASE);
45 	dbloginfree(login);
46 
47 	fprintf(stdout, "creating table\n");
48 	sql_cmd(dbproc);
49 	dbsqlexec(dbproc);
50 	while (dbresults(dbproc) != NO_MORE_RESULTS) {
51 		/* nop */
52 	}
53 
54 	fprintf(stdout, "insert\n");
55 	for (i = 1; i < rows_to_add; i++) {
56 		sql_cmd(dbproc);
57 		dbsqlexec(dbproc);
58 		while (dbresults(dbproc) != NO_MORE_RESULTS) {
59 			/* nop */
60 		}
61 	}
62 
63 	sql_cmd(dbproc); /* select */
64 	dbsqlexec(dbproc);
65 
66 	if (dbresults(dbproc) != SUCCEED) {
67 		fprintf(stdout, "Was expecting a result set.");
68 		exit(1);
69 	}
70 
71 	for (i = 1; i <= dbnumcols(dbproc); i++)
72 		printf("col %d is %s\n", i, dbcolname(dbproc, i));
73 
74 	dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
75 	dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
76 
77 	for (i = 1; i <= 24; i++) {
78 	char expected[1024];
79 
80 		sprintf(expected, "row %04d", i);
81 
82 		if (i % 5 == 0) {
83 			dbclrbuf(dbproc, 5);
84 		}
85 
86 
87 		testint = -1;
88 		strcpy(teststr, "bogus");
89 
90 		if (REG_ROW != dbnextrow(dbproc)) {
91 			fprintf(stderr, "Failed.  Expected a row\n");
92 			exit(1);
93 		}
94 		if (testint != i) {
95 			fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
96 			abort();
97 		}
98 		if (0 != strncmp(teststr, expected, strlen(expected))) {
99 			fprintf(stdout, "Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
100 			abort();
101 		}
102 		printf("Read a row of data -> %d %s\n", (int) testint, teststr);
103 	}
104 
105 
106 
107 	fprintf(stdout, "second select\n");
108 	fprintf(stdout, "testing dbgetchar...\n");
109 	for (i=0; (s = dbgetchar(dbproc, i)) != NULL; i++) {
110 		putchar(*s);
111 		if (!(isprint((unsigned char)*s) || iscntrl((unsigned char)*s))) {
112 			fprintf(stderr, "%s:%d: dbgetchar failed with %x at position %d\n", __FILE__, __LINE__, *s, i);
113 			failed = 1;
114 			break;
115 		}
116 	}
117 	fprintf(stdout, "<== end of command buffer\n");
118 
119 	if (SUCCEED != sql_cmd(dbproc)) {
120 		fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
121 		failed = 1;
122 	}
123 	fprintf(stdout, "About to exec for the second time.  Should fail\n");
124 	expected_error = 20019;
125 	dbsetuserdata(dbproc, (BYTE*) &expected_error);
126 	if (FAIL != dbsqlexec(dbproc)) {
127 		fprintf(stderr, "%s:%d: dbsqlexec should have failed but didn't\n", __FILE__, __LINE__);
128 		failed = 1;
129 	}
130 	dbexit();
131 
132 	fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
133 	return failed ? 1 : 0;
134 }
135