1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 * Copyright (C) 1998-1999 Brian Bruns
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19 #include "common.h"
20 #include <replacements.h>
21
22 #include <common/test_assert.h>
23
24 static char *
varchar_as_string(TDSSOCKET * tds,int col_idx)25 varchar_as_string(TDSSOCKET * tds, int col_idx)
26 {
27 static char result[256];
28 const void *value = tds->res_info->columns[col_idx]->column_data;
29
30 strlcpy(result, (const char *) value, sizeof(result));
31 return result;
32 }
33
34
35 int
main(int argc,char ** argv)36 main(int argc, char **argv)
37 {
38 TDSLOGIN *login;
39 TDSSOCKET *tds;
40 int verbose = 0;
41 int rc, i;
42
43 int result_type;
44 int rows_returned = 0;
45
46 const char *len200 =
47 "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
48 char *long_query = (char*) malloc(5000);
49
50 strcpy(long_query, "SELECT name FROM #longquerytest WHERE (");
51 for (i = 0; i < 20; ++i)
52 sprintf(strchr(long_query, 0), "name = '%c%s' OR ", 'A'+i, len200);
53 strcat(long_query, "name = 'correct')");
54
55 fprintf(stdout, "%s: Test large (>4096 bytes) queries\n", __FILE__);
56 rc = try_tds_login(&login, &tds, __FILE__, verbose);
57 if (rc != TDS_SUCCESS) {
58 fprintf(stderr, "try_tds_login() failed\n");
59 return 1;
60 }
61
62 /* do not check error here, if TABLE is not create this give error */
63 rc = run_query(tds, "DROP TABLE #longquerytest");
64 rc = run_query(tds, "CREATE TABLE #longquerytest (name varchar(255))");
65 if (rc != TDS_SUCCESS) {
66 return 1;
67 }
68 rc = run_query(tds, "INSERT #longquerytest (name) VALUES ('incorrect')");
69 if (rc != TDS_SUCCESS) {
70 return 1;
71 }
72 rc = run_query(tds, "INSERT #longquerytest (name) VALUES ('correct')");
73 if (rc != TDS_SUCCESS) {
74 return 1;
75 }
76
77 /*
78 * The heart of the test
79 */
80 if (verbose) {
81 fprintf(stdout, "block size %d\n", tds->conn->env.block_size);
82 }
83 rc = tds_submit_query(tds, long_query);
84 while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROWFMT|TDS_RETURN_ROW)) == TDS_SUCCESS) {
85 switch (result_type) {
86 case TDS_ROWFMT_RESULT:
87 if (tds->res_info->columns[0]->column_type != SYBVARCHAR) {
88 fprintf(stderr, "Wrong column_type in %s\n", __FILE__);
89 return 1;
90 }
91 break;
92 case TDS_ROW_RESULT:
93 ++rows_returned;
94 if (verbose) {
95 printf("col 0 is %s\n", varchar_as_string(tds, 0));
96 }
97 break;
98 default:
99 break;
100 }
101 }
102 if (rc == TDS_FAIL) {
103 fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for long query\n");
104 return 1;
105 } else if (rc != TDS_NO_MORE_RESULTS) {
106 fprintf(stderr, "tds_process_tokens() unexpected return\n");
107 }
108
109 if (rows_returned != 1) {
110 fprintf(stderr, "%d rows returned, 1 expected\n", rows_returned);
111 return 1;
112 }
113
114 /* do not check error here, if TABLE is not create this give error */
115 rc = run_query(tds, "DROP TABLE #longquerytest");
116
117 try_tds_logout(login, tds, verbose);
118 free(long_query);
119 return 0;
120 }
121