Line data Source code
1 : /*
2 : * Purpose: Test bcp functions, specifically some NULL behaviour
3 : * Functions: bcp_bind bcp_done bcp_init bcp_sendrow
4 : */
5 :
6 : #include "common.h"
7 :
8 : #include <assert.h>
9 :
10 : static void
11 0 : doexit(int value)
12 : {
13 0 : dbexit(); /* always call dbexit before returning to OS */
14 0 : exit(value);
15 : }
16 :
17 : static int
18 10 : init(DBPROCESS * dbproc, const char *name)
19 : {
20 : RETCODE rc;
21 :
22 10 : printf("Dropping %s.%s..%s\n", SERVER, DATABASE, name);
23 10 : sql_cmd(dbproc);
24 10 : dbsqlexec(dbproc);
25 34 : while ((rc=dbresults(dbproc)) == SUCCEED)
26 14 : continue;
27 10 : if (rc != NO_MORE_RESULTS)
28 : return 1;
29 :
30 10 : printf("Creating %s.%s..%s\n", SERVER, DATABASE, name);
31 10 : sql_cmd(dbproc);
32 :
33 10 : if (dbsqlexec(dbproc) == FAIL)
34 : return 1;
35 20 : while ((rc=dbresults(dbproc)) == SUCCEED)
36 10 : continue;
37 10 : if (rc != NO_MORE_RESULTS)
38 : return 1;
39 10 : printf("ok\n");
40 10 : return 0;
41 : }
42 :
43 : int
44 10 : main(int argc, char **argv)
45 : {
46 : LOGINREC *login;
47 : DBPROCESS *dbproc;
48 : DBINT msgno;
49 10 : int num_rows, expected = 2;
50 : static const char table_name[] = "bcp_test";
51 :
52 : /* Variables for host file data to be copied to database. */
53 : char s1[11];
54 : char s2[11];
55 : char s3[11];
56 :
57 10 : set_malloc_options();
58 :
59 10 : read_login_info(argc, argv);
60 :
61 10 : printf("Starting %s\n", argv[0]);
62 :
63 10 : dbsetversion(DBVERSION_100);
64 10 : dbinit();
65 :
66 10 : dberrhandle(syb_err_handler);
67 10 : dbmsghandle(syb_msg_handler);
68 :
69 10 : printf("About to logon\n");
70 :
71 10 : login = dblogin();
72 10 : DBSETLPWD(login, PASSWORD);
73 10 : DBSETLUSER(login, USER);
74 10 : DBSETLAPP(login, "bcp2.c unit test");
75 10 : BCP_SETL(login, TRUE);
76 :
77 10 : printf("About to open %s.%s\n", SERVER, DATABASE);
78 :
79 10 : dbproc = dbopen(login, SERVER);
80 10 : if (strlen(DATABASE))
81 10 : dbuse(dbproc, DATABASE);
82 10 : dbloginfree(login);
83 :
84 10 : if (init(dbproc, table_name))
85 0 : doexit(1);
86 :
87 : /* set up and send the bcp */
88 10 : strcpy(s1, "test short");
89 10 : strcpy(s2, " ");
90 10 : strcpy(s3, "");
91 :
92 10 : if (bcp_init(dbproc, "bcp_test", NULL, "bcp.errors", DB_IN) != SUCCEED)
93 0 : doexit(1);
94 :
95 10 : bcp_bind(dbproc, (BYTE *)s1, 0, -1, (BYTE *)"", 1, SYBCHAR, 1);
96 10 : bcp_bind(dbproc, (BYTE *)s2, 0, -1, (BYTE *)"", 1, SYBCHAR, 2);
97 10 : bcp_bind(dbproc, (BYTE *)s3, 0, -1, (BYTE *)"", 1, SYBCHAR, 3);
98 :
99 10 : printf("Sending some rows... \n");
100 10 : if (bcp_sendrow(dbproc) == FAIL) {
101 0 : fprintf(stderr, "send failed\n");
102 0 : doexit(1);
103 : }
104 :
105 10 : strcpy(s2, "x");
106 10 : strcpy(s3, " ");
107 10 : if (bcp_sendrow(dbproc) == FAIL) {
108 0 : fprintf(stderr, "send failed\n");
109 0 : doexit(1);
110 : }
111 :
112 : /* In MSSQL the error is reported during bcp_done but all inserts are ignored */
113 10 : if (DBTDS_5_0 == DBTDS(dbproc)) {
114 2 : msgno = 20073;
115 2 : dbsetuserdata(dbproc, (BYTE*) &msgno);
116 2 : strcpy(s2, "");
117 2 : strcpy(s3, "");
118 2 : if (bcp_sendrow(dbproc) != FAIL) {
119 0 : fprintf(stderr, "send NULL succeeded\n");
120 0 : doexit(1);
121 : }
122 2 : dbsetuserdata(dbproc, NULL);
123 :
124 : /* end bcp. */
125 2 : if (bcp_done(dbproc) != expected) {
126 0 : fprintf(stderr, "Bulk copy unsuccessful.\n");
127 0 : doexit(1);
128 : }
129 : } else {
130 : /* end bcp. */
131 8 : if (bcp_done(dbproc) != expected) {
132 0 : fprintf(stderr, "Bulk copy unsuccessful.\n");
133 0 : doexit(1);
134 : }
135 :
136 : /* another insert with error */
137 8 : if (bcp_init(dbproc, "bcp_test", NULL, "bcp.errors", DB_IN) != SUCCEED)
138 0 : doexit(1);
139 :
140 8 : bcp_bind(dbproc, (BYTE *)s1, 0, -1, (BYTE *)"", 1, SYBCHAR, 1);
141 8 : bcp_bind(dbproc, (BYTE *)s2, 0, -1, (BYTE *)"", 1, SYBCHAR, 2);
142 8 : bcp_bind(dbproc, (BYTE *)s3, 0, -1, (BYTE *)"", 1, SYBCHAR, 3);
143 :
144 8 : strcpy(s2, "");
145 8 : strcpy(s3, "");
146 8 : if (bcp_sendrow(dbproc) == FAIL) {
147 0 : fprintf(stderr, "send failed\n");
148 0 : doexit(1);
149 : }
150 :
151 : /* end bcp. */
152 8 : msgno = 515;
153 8 : dbsetuserdata(dbproc, (BYTE*) &msgno);
154 8 : num_rows = bcp_done(dbproc);
155 8 : if (num_rows != -1) {
156 0 : fprintf(stderr, "Bulk copy successful. %d rows returned\n", num_rows);
157 0 : doexit(1);
158 : }
159 8 : dbsetuserdata(dbproc, NULL);
160 : }
161 :
162 10 : printf("done\n");
163 :
164 : /* check we inserted the expected number of rows row */
165 10 : num_rows = 0;
166 10 : sql_cmd(dbproc);
167 10 : dbsqlexec(dbproc);
168 30 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
169 30 : while (dbnextrow(dbproc) == REG_ROW)
170 20 : num_rows++;
171 : }
172 10 : if (num_rows != expected) {
173 0 : fprintf(stderr, "Expected %d row(s), got %d\n", expected, num_rows);
174 0 : doexit(1);
175 : }
176 :
177 10 : printf("Dropping table %s\n", table_name);
178 10 : sql_cmd(dbproc);
179 10 : dbsqlexec(dbproc);
180 10 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
181 : /* nop */
182 : }
183 10 : dbexit();
184 :
185 10 : printf("%s OK\n", __FILE__);
186 : return 0;
187 : }
|