1 /*
2  * Purpose: Test behaviour of DBCOUNT() for updates and inserts
3  * Functions: DBCOUNT dbbind dbnextrow dbnumcols dbopen dbresults dbsqlexec
4  */
5 
6 #include "common.h"
7 
8 #include <common/test_assert.h>
9 
10 int failed = 0;
11 
12 
13 int
main(int argc,char ** argv)14 main(int argc, char **argv)
15 {
16 	const int rows_to_add = 50;
17 	LOGINREC *login;
18 	DBPROCESS *dbproc;
19 	int i;
20 	char teststr[1024];
21 	DBINT testint;
22 
23 	set_malloc_options();
24 
25 	read_login_info(argc, argv);
26 	fprintf(stdout, "Starting %s\n", argv[0]);
27 
28 	/* Fortify_EnterScope(); */
29 	dbinit();
30 
31 	dberrhandle(syb_err_handler);
32 	dbmsghandle(syb_msg_handler);
33 
34 	fprintf(stdout, "About to logon\n");
35 
36 	login = dblogin();
37 	DBSETLPWD(login, PASSWORD);
38 	DBSETLUSER(login, USER);
39 	DBSETLAPP(login, "t0018");
40 
41 	fprintf(stdout, "About to open\n");
42 
43 	dbproc = dbopen(login, SERVER);
44 	if (strlen(DATABASE))
45 		dbuse(dbproc, DATABASE);
46 	dbloginfree(login);
47 
48 	fprintf(stdout, "creating table\n");
49 	sql_cmd(dbproc);
50 	dbsqlexec(dbproc);
51 	while (dbresults(dbproc) != NO_MORE_RESULTS) {
52 		/* nop */
53 	}
54 
55 	fprintf(stdout, "insert\n");
56 	for (i = 0; i < rows_to_add; i++) {
57 		sql_cmd(dbproc);
58 		dbsqlexec(dbproc);
59 		while (dbresults(dbproc) != NO_MORE_RESULTS) {
60 			/* nop */
61 		}
62 		if (DBCOUNT(dbproc) != 1) {
63 			failed = 1;
64 			fprintf(stdout, "Was expecting a rows affect to be 1.");
65 			exit(1);
66 		}
67 	}
68 
69 	fprintf(stdout, "select\n");
70 	sql_cmd(dbproc);
71 	dbsqlexec(dbproc);
72 
73 	fprintf(stdout, "Checking for an empty result set.\n");
74 	if (dbresults(dbproc) != SUCCEED) {
75 		failed = 1;
76 		fprintf(stdout, "Was expecting a result set.\n");
77 		exit(1);
78 	}
79 
80 	if(DBROWS(dbproc) != FAIL) {
81 		failed = 1;
82 		fprintf(stdout, "Was expecting no rows to be available.\n");
83 		exit(1);
84 	}
85 
86 	fprintf(stdout, "Checking for a result set with content.\n");
87 	if (dbresults(dbproc) != SUCCEED) {
88 		failed = 1;
89 		fprintf(stdout, "Was expecting a result set.");
90 		exit(1);
91 	}
92 
93 	for (i = 1; i <= dbnumcols(dbproc); i++)
94 		printf("col %d is %s\n", i, dbcolname(dbproc, i));
95 
96 	if (SUCCEED != dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint)) {
97 		failed = 1;
98 		fprintf(stderr, "Had problem with bind\n");
99 		abort();
100 	}
101 	if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr)) {
102 		failed = 1;
103 		fprintf(stderr, "Had problem with bind\n");
104 		abort();
105 	}
106 
107 	for (i = 0; i < rows_to_add; i++) {
108 	char expected[1024];
109 
110 		sprintf(expected, "row %03d", i);
111 
112 		if (REG_ROW != dbnextrow(dbproc)) {
113 			failed = 1;
114 			fprintf(stderr, "Failed.  Expected a row\n");
115 			exit(1);
116 		}
117 		if (testint != i) {
118 			failed = 1;
119 			fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
120 			abort();
121 		}
122 		if (0 != strncmp(teststr, expected, strlen(expected))) {
123 			failed = 1;
124 			fprintf(stdout, "Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
125 			abort();
126 		}
127 		printf("Read a row of data -> %d %s\n", (int) testint, teststr);
128 	}
129 
130 	if (dbnextrow(dbproc) != NO_MORE_ROWS) {
131 		failed = 1;
132 		fprintf(stderr, "Was expecting no more rows\n");
133 		exit(1);
134 	}
135 	if (DBCOUNT(dbproc) != rows_to_add) {
136 		failed = 1;
137 		fprintf(stdout, "Was expecting a rows affect to be %d was %d.\n", rows_to_add, DBCOUNT(dbproc));
138 		exit(1);
139 	}
140 
141 	sql_cmd(dbproc);
142 	dbsqlexec(dbproc);
143 	while (dbresults(dbproc) != NO_MORE_RESULTS) {
144 		/* nop */
145 	}
146 	if (DBCOUNT(dbproc) != rows_to_add) {
147 		failed = 1;
148 		fprintf(stdout, "Was expecting a rows affect to be %d was %d.\n", rows_to_add, DBCOUNT(dbproc));
149 		exit(1);
150 	} else {
151 		fprintf(stdout, "Number of rows affected by update = %d.\n", DBCOUNT(dbproc));
152 	}
153 
154 	dbexit();
155 
156 	fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
157 	return failed ? 1 : 0;
158 }
159