Line data Source code
1 : /*
2 : * Purpose: Test NULL behavior in order to fix problems with PHP and NULLs
3 : * PHP use dbdata to get data
4 : */
5 :
6 : #include "common.h"
7 :
8 : #if HAVE_UNISTD_H
9 : #include <unistd.h>
10 : #endif /* HAVE_UNISTD_H */
11 :
12 : #ifndef DBNTWIN32
13 :
14 : static DBPROCESS *dbproc = NULL;
15 : static int failed = 0;
16 :
17 : static int
18 4 : ignore_msg_handler(DBPROCESS * dbproc TDS_UNUSED, DBINT msgno TDS_UNUSED, int state TDS_UNUSED, int severity TDS_UNUSED,
19 : char *text TDS_UNUSED, char *server TDS_UNUSED, char *proc TDS_UNUSED, int line TDS_UNUSED)
20 : {
21 4 : return 0;
22 : }
23 :
24 : static int
25 4 : ignore_err_handler(DBPROCESS * dbproc TDS_UNUSED, int severity TDS_UNUSED, int dberr TDS_UNUSED,
26 : int oserr TDS_UNUSED, char *dberrstr TDS_UNUSED, char *oserrstr TDS_UNUSED)
27 : {
28 4 : return INT_CANCEL;
29 : }
30 :
31 : static void
32 304 : query(const char *query)
33 : {
34 304 : printf("query: %s\n", query);
35 304 : dbcmd(dbproc, (char *) query);
36 304 : dbsqlexec(dbproc);
37 304 : while (dbresults(dbproc) == SUCCEED) {
38 : /* nop */
39 : }
40 304 : }
41 :
42 : static void
43 200 : test0(int n, int expected)
44 : {
45 : static const char sql[] = "select c from #null where n = %d";
46 :
47 200 : printf(sql, n);
48 200 : printf(" ... ");
49 :
50 200 : dbfcmd(dbproc, sql, n);
51 :
52 200 : dbsqlexec(dbproc);
53 :
54 200 : if (dbresults(dbproc) != SUCCEED || dbnextrow(dbproc) != REG_ROW) {
55 0 : printf("\nExpected a row.\n");
56 0 : failed = 1;
57 0 : dbcancel(dbproc);
58 0 : return;
59 : }
60 :
61 200 : printf("got %p and length %d\n", dbdata(dbproc, 1), dbdatlen(dbproc, 1));
62 :
63 200 : if (dbdatlen(dbproc, 1) != (expected < 0? 0 : expected)) {
64 0 : fprintf(stderr, "Error: n=%d: dbdatlen returned %d, expected %d\n",
65 : n, dbdatlen(dbproc, 1), expected < 0? 0 : expected);
66 0 : dbcancel(dbproc);
67 0 : failed = 1;
68 : }
69 :
70 200 : if (dbdata(dbproc, 1) != NULL && expected < 0) {
71 0 : fprintf(stderr, "Error: n=%d: dbdata returned %p, expected NULL and length %d\n",
72 : n, dbdata(dbproc, 1), expected);
73 0 : dbcancel(dbproc);
74 0 : failed = 1;
75 : }
76 :
77 200 : if (dbdata(dbproc, 1) == NULL && expected > 0) {
78 0 : fprintf(stderr, "Error: n=%d: dbdata returned %p, expected non-NULL and length %d\n",
79 : n, dbdata(dbproc, 1), expected);
80 0 : dbcancel(dbproc);
81 0 : failed = 1;
82 : }
83 :
84 200 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
85 0 : fprintf(stderr, "Error: Only one row expected (cancelling remaining results)\n");
86 0 : dbcancel(dbproc);
87 0 : failed = 1;
88 : }
89 :
90 :
91 :
92 200 : while (dbresults(dbproc) == SUCCEED) {
93 : /* nop */
94 : }
95 : }
96 :
97 :
98 : static void
99 54 : test(const char *type, int give_err)
100 : {
101 : RETCODE ret;
102 :
103 54 : query("if object_id('#null') is not NULL drop table #null");
104 :
105 54 : dberrhandle(ignore_err_handler);
106 54 : dbmsghandle(ignore_msg_handler);
107 :
108 54 : printf("create table #null (n int, c %s NULL)\n", type);
109 54 : dbfcmd(dbproc, "create table #null (n int, c %s NULL)", type);
110 54 : dbsqlexec(dbproc);
111 :
112 54 : ret = dbresults(dbproc);
113 :
114 54 : dberrhandle(syb_err_handler);
115 54 : dbmsghandle(syb_msg_handler);
116 :
117 54 : if (ret != SUCCEED) {
118 4 : dbcancel(dbproc);
119 4 : if (!give_err)
120 : return;
121 0 : printf("Was expecting a result set.\n");
122 0 : failed = 1;
123 0 : return;
124 : }
125 :
126 50 : query("insert into #null values(1, '')");
127 50 : query("insert into #null values(2, NULL)");
128 50 : query("insert into #null values(3, ' ')");
129 50 : query("insert into #null values(4, 'a')");
130 :
131 50 : test0(1, DBTDS_5_0 < DBTDS(dbproc)? 0 : 1);
132 50 : test0(2, DBTDS_5_0 < DBTDS(dbproc)? -1 : 0);
133 50 : test0(3, 1);
134 50 : test0(4, 1);
135 :
136 50 : query("drop table #null");
137 : }
138 :
139 : int
140 10 : main(int argc, char **argv)
141 : {
142 : LOGINREC *login;
143 :
144 10 : read_login_info(argc, argv);
145 :
146 10 : printf("Starting %s\n", argv[0]);
147 :
148 10 : dbinit();
149 :
150 10 : dberrhandle(syb_err_handler);
151 10 : dbmsghandle(syb_msg_handler);
152 :
153 10 : printf("About to logon\n");
154 :
155 10 : login = dblogin();
156 10 : DBSETLPWD(login, PASSWORD);
157 10 : DBSETLUSER(login, USER);
158 10 : DBSETLAPP(login, "null");
159 :
160 10 : printf("About to open \"%s\"\n", SERVER);
161 :
162 10 : dbproc = dbopen(login, SERVER);
163 10 : if (!dbproc) {
164 0 : fprintf(stderr, "Unable to connect to %s\n", SERVER);
165 0 : return 1;
166 : }
167 :
168 10 : dbloginfree(login);
169 :
170 10 : if (strlen(DATABASE))
171 10 : dbuse(dbproc, DATABASE);
172 :
173 10 : test("VARCHAR(10)", 1);
174 10 : test("TEXT", 1);
175 :
176 10 : test("NVARCHAR(10)", 0);
177 10 : if (DBTDS_5_0 < DBTDS(dbproc)) {
178 8 : test("NTEXT", 0);
179 8 : test("VARCHAR(MAX)", 0);
180 8 : test("NVARCHAR(MAX)", 0);
181 : }
182 :
183 10 : dbexit();
184 :
185 10 : return failed ? 1 : 0;
186 : }
187 : #else
188 : int main(void)
189 : {
190 : fprintf(stderr, "Not supported by MS DBLib\n");
191 : return 0;
192 : }
193 : #endif
194 :
|