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 
21 #include <common/test_assert.h>
22 
23 static char software_version[] = "$Id: t0004.c 487476 2015-12-17 19:48:39Z ucko $";
24 static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
25 
26 static char *
varchar_as_string(TDSSOCKET * tds,int col_idx)27 varchar_as_string(TDSSOCKET * tds, int col_idx)
28 {
29 	static char result[256];
30 	const void *value = tds->res_info->columns[col_idx]->column_data;
31 
32 	strncpy(result, (const char *) value, sizeof(result) - 1);
33 	result[sizeof(result) - 1] = '\0';
34 	return result;
35 }
36 
37 
38 int
main(int argc,char ** argv)39 main(int argc, char **argv)
40 {
41 	TDSLOGIN *login;
42 	TDSSOCKET *tds;
43 	int verbose = 0;
44 	int rc, i;
45 
46 	int result_type;
47 	int rows_returned = 0;
48 
49 	const char *len200 =
50 		"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
51 	char *long_query = (char*) malloc(5000);
52 
53 	strcpy(long_query, "SELECT name FROM #longquerytest WHERE (");
54 	for (i = 0; i < 20; ++i)
55 		sprintf(strchr(long_query, 0), "name = '%c%s' OR ", 'A'+i, len200);
56 	strcat(long_query, "name = 'correct')");
57 
58 	fprintf(stdout, "%s: Test large (>4096 bytes) queries\n", __FILE__);
59 	rc = try_tds_login(&login, &tds, __FILE__, verbose);
60 	if (rc != TDS_SUCCESS) {
61 		fprintf(stderr, "try_tds_login() failed\n");
62 		return 1;
63 	}
64 
65 	/* do not check error here, if TABLE is not create this give error */
66 	rc = run_query(tds, "DROP TABLE #longquerytest");
67 	rc = run_query(tds, "CREATE TABLE #longquerytest (name varchar(255))");
68 	if (rc != TDS_SUCCESS) {
69 		return 1;
70 	}
71 	rc = run_query(tds, "INSERT #longquerytest (name) VALUES ('incorrect')");
72 	if (rc != TDS_SUCCESS) {
73 		return 1;
74 	}
75 	rc = run_query(tds, "INSERT #longquerytest (name) VALUES ('correct')");
76 	if (rc != TDS_SUCCESS) {
77 		return 1;
78 	}
79 
80 	/*
81 	 * The heart of the test
82 	 */
83 	if (verbose) {
84 		fprintf(stdout, "block size %d\n", tds->conn->env.block_size);
85 	}
86 	rc = tds_submit_query(tds, long_query);
87 	while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROWFMT|TDS_RETURN_ROW)) == TDS_SUCCESS) {
88 		switch (result_type) {
89 		case TDS_ROWFMT_RESULT:
90 			if (tds->res_info->columns[0]->column_type != SYBVARCHAR) {
91 				fprintf(stderr, "Wrong column_type in %s\n", __FILE__);
92 				return 1;
93 			}
94 			break;
95 		case TDS_ROW_RESULT:
96 			++rows_returned;
97 			if (verbose) {
98 				printf("col 0 is %s\n", varchar_as_string(tds, 0));
99 			}
100 			break;
101 		default:
102 			break;
103 		}
104 	}
105 	if (rc == TDS_FAIL) {
106 		fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for long query\n");
107 		return 1;
108 	} else if (rc != TDS_NO_MORE_RESULTS) {
109 		fprintf(stderr, "tds_process_tokens() unexpected return\n");
110 	}
111 
112 	if (rows_returned != 1) {
113 		fprintf(stderr, "%d rows returned, 1 expected\n", rows_returned);
114 		return 1;
115 	}
116 
117 	/* do not check error here, if TABLE is not create this give error */
118 	rc = run_query(tds, "DROP TABLE #longquerytest");
119 
120 	try_tds_logout(login, tds, verbose);
121 	free(long_query);
122 	return 0;
123 }
124