Line data Source code
1 : /*
2 : * Purpose: dbnull behavior
3 : */
4 :
5 : #include "common.h"
6 : #include <freetds/replacements.h>
7 :
8 : static int failed = 0;
9 : static DBPROCESS *dbproc = NULL;
10 :
11 : static void
12 80 : char_test(const char *null, int bindlen, const char *expected)
13 : {
14 : char db_c[16];
15 : RETCODE ret;
16 :
17 80 : if (null) {
18 56 : fprintf(stderr, "\tdbsetnull(CHARBIND, %u, '%s').\n", (unsigned int) strlen(null), null);
19 56 : ret = dbsetnull(dbproc, CHARBIND, strlen(null), (BYTE *) null);
20 56 : if (ret != SUCCEED) {
21 0 : fprintf(stderr, "dbsetnull returned error %d\n", (int) ret);
22 0 : failed = 1;
23 : }
24 : }
25 :
26 80 : memset(db_c, '_', sizeof(db_c));
27 80 : strcpy(db_c, "123456");
28 80 : dbcmd(dbproc, "select convert(char(20), null)");
29 :
30 80 : dbsqlexec(dbproc);
31 :
32 80 : if (dbresults(dbproc) != SUCCEED) {
33 0 : fprintf(stderr, "Was expecting a row.\n");
34 0 : failed = 1;
35 0 : dbcancel(dbproc);
36 : }
37 :
38 80 : fprintf(stderr, "dbbind(CHARBIND, bindlen= %d).\n", bindlen);
39 80 : dbbind(dbproc, 1, CHARBIND, bindlen, (BYTE *) &db_c);
40 80 : db_c[sizeof(db_c)-1] = 0;
41 80 : printf("buffer before/after dbnextrow: '%s'/", db_c);
42 :
43 80 : if (dbnextrow(dbproc) != REG_ROW) {
44 0 : fprintf(stderr, "Was expecting a row.\n");
45 0 : failed = 1;
46 0 : dbcancel(dbproc);
47 : }
48 80 : db_c[sizeof(db_c)-1] = 0;
49 80 : printf("'%s'\n", db_c);
50 :
51 80 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
52 0 : fprintf(stderr, "Only one row expected\n");
53 0 : dbcancel(dbproc);
54 0 : failed = 1;
55 : }
56 :
57 80 : while (dbresults(dbproc) == SUCCEED) {
58 : /* nop */
59 : }
60 :
61 80 : if (strcmp(db_c, expected) != 0) {
62 0 : fprintf(stderr, "Invalid NULL '%s' returned expected '%s' (%s:%d)\n", db_c, expected, basename(__FILE__), __LINE__);
63 0 : failed = 1;
64 : }
65 80 : }
66 :
67 : int
68 8 : main(int argc, char **argv)
69 : {
70 : LOGINREC *login;
71 : DBINT db_i;
72 : RETCODE ret;
73 :
74 8 : read_login_info(argc, argv);
75 :
76 8 : printf("Starting %s\n", argv[0]);
77 8 : dbinit();
78 :
79 8 : dberrhandle(syb_err_handler);
80 8 : dbmsghandle(syb_msg_handler);
81 :
82 8 : login = dblogin();
83 8 : DBSETLPWD(login, PASSWORD);
84 8 : DBSETLUSER(login, USER);
85 8 : DBSETLAPP(login, "setnull");
86 :
87 :
88 8 : printf("About to open %s.%s\n", SERVER, DATABASE);
89 :
90 8 : dbproc = dbopen(login, SERVER);
91 8 : if (strlen(DATABASE))
92 8 : dbuse(dbproc, DATABASE);
93 8 : dbloginfree(login);
94 :
95 : /* try to set an int */
96 8 : db_i = 0xdeadbeef;
97 8 : ret = dbsetnull(dbproc, INTBIND, 0, (BYTE *) &db_i);
98 8 : if (ret != SUCCEED) {
99 0 : fprintf(stderr, "dbsetnull returned error %d\n", (int) ret);
100 0 : failed = 1;
101 : }
102 :
103 8 : ret = dbsetnull(dbproc, INTBIND, 1, (BYTE *) &db_i);
104 8 : if (ret != SUCCEED) {
105 0 : fprintf(stderr, "dbsetnull returned error %d\n", (int) ret);
106 0 : failed = 1;
107 : }
108 :
109 : /* check result */
110 8 : db_i = 0;
111 8 : dbcmd(dbproc, "select convert(int, null)");
112 :
113 8 : dbsqlexec(dbproc);
114 :
115 8 : if (dbresults(dbproc) != SUCCEED) {
116 0 : fprintf(stderr, "Was expecting a row.\n");
117 0 : failed = 1;
118 0 : dbcancel(dbproc);
119 : }
120 :
121 8 : dbbind(dbproc, 1, INTBIND, 0, (BYTE *) &db_i);
122 8 : printf("db_i = %ld\n", (long int) db_i);
123 :
124 8 : if (dbnextrow(dbproc) != REG_ROW) {
125 0 : fprintf(stderr, "Was expecting a row.\n");
126 0 : failed = 1;
127 0 : dbcancel(dbproc);
128 : }
129 8 : printf("db_i = %ld\n", (long int) db_i);
130 :
131 8 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
132 0 : fprintf(stderr, "Only one row expected\n");
133 0 : dbcancel(dbproc);
134 0 : failed = 1;
135 : }
136 :
137 8 : while (dbresults(dbproc) == SUCCEED) {
138 : /* nop */
139 : }
140 :
141 8 : if (db_i != 0xdeadbeef) {
142 0 : fprintf(stderr, "Invalid NULL %ld returned (%s:%d)\n", (long int) db_i, basename(__FILE__), __LINE__);
143 0 : failed = 1;
144 : }
145 :
146 : /* try if dbset null consider length */
147 256 : for (db_i = 1; db_i > 0; db_i <<= 1) {
148 248 : printf("db_i = %ld\n", (long int) db_i);
149 248 : ret = dbsetnull(dbproc, INTBIND, db_i, (BYTE *) &db_i);
150 248 : if (ret != SUCCEED) {
151 0 : fprintf(stderr, "dbsetnull returned error %d for bindlen %ld\n", (int) ret, (long int) db_i);
152 0 : failed = 1;
153 0 : break;
154 : }
155 : }
156 :
157 8 : char_test(NULL, -1, "123456");
158 8 : char_test(NULL, 0, "123456");
159 8 : char_test(NULL, 2, " 3456");
160 :
161 8 : char_test("foo", -1, "foo456");
162 8 : char_test("foo", 0, "foo456");
163 : /* char_test("foo", 2, ""); */
164 8 : char_test("foo", 5, "foo 6");
165 :
166 8 : char_test("foo ", -1, "foo 56");
167 8 : char_test("foo ", 0, "foo 56");
168 : /* char_test("foo ", 2, ""); */
169 8 : char_test("foo ", 5, "foo 6");
170 :
171 8 : char_test("foo ", 10, "foo _____");
172 :
173 8 : printf("dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
174 8 : dbexit();
175 :
176 8 : return failed ? 1 : 0;
177 : }
|