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