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 *value_as_string(TDSSOCKET * tds, int col_idx);
25 
26 int
main(int argc,char ** argv)27 main(int argc, char **argv)
28 {
29 	TDSLOGIN *login;
30 	TDSSOCKET *tds;
31 	int verbose = 0;
32 	int rc;
33 	int i;
34 
35 	int result_type;
36 
37 	const char *len200 =
38 		"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
39 	char large_sql[1000];
40 
41 	fprintf(stdout, "%s: Test large (>512 bytes) replies\n", __FILE__);
42 	rc = try_tds_login(&login, &tds, __FILE__, verbose);
43 	if (rc != TDS_SUCCESS) {
44 		fprintf(stderr, "try_tds_login() failed\n");
45 		return 1;
46 	}
47 
48 	/* do not test error, remove always table */
49 	rc = run_query(tds, "DROP TABLE #test_table");
50 	rc = run_query(tds, "CREATE TABLE #test_table (id int, name varchar(255))");
51 	if (rc != TDS_SUCCESS) {
52 		return 1;
53 	}
54 
55 	sprintf(large_sql, "INSERT #test_table (id, name) VALUES (0, 'A%s')", len200);
56 	rc = run_query(tds, large_sql);
57 	if (rc != TDS_SUCCESS) {
58 		return 1;
59 	}
60 	sprintf(large_sql, "INSERT #test_table (id, name) VALUES (1, 'B%s')", len200);
61 	rc = run_query(tds, large_sql);
62 	if (rc != TDS_SUCCESS) {
63 		return 1;
64 	}
65 	sprintf(large_sql, "INSERT #test_table (id, name) VALUES (2, 'C%s')", len200);
66 	rc = run_query(tds, large_sql);
67 	if (rc != TDS_SUCCESS) {
68 		return 1;
69 	}
70 
71 	/*
72 	 * The heart of the test
73 	 */
74 	rc = tds_submit_query(tds, "SELECT * FROM #test_table");
75 	while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW)) == TDS_SUCCESS) {
76 		switch (result_type) {
77 		case TDS_ROW_RESULT:
78 			for (i = 0; i < tds->res_info->num_cols; i++) {
79 				if (verbose) {
80 					printf("col %i is %s\n", i, value_as_string(tds, i));
81 				}
82 			}
83 			break;
84 		default:
85 			fprintf(stderr, "tds_process_tokens() returned unexpected result\n");
86 			break;
87 		}
88 	}
89 	if (rc == TDS_FAIL) {
90 		fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for SELECT\n");
91 		return 1;
92 	} else if (rc != TDS_NO_MORE_RESULTS) {
93 		fprintf(stderr, "tds_process_tokens() unexpected return\n");
94 	}
95 
96 	/* do not test error, remove always table */
97 	rc = run_query(tds, "DROP TABLE #test_table");
98 
99 	try_tds_logout(login, tds, verbose);
100 	return 0;
101 }
102 
103 static char *
value_as_string(TDSSOCKET * tds,int col_idx)104 value_as_string(TDSSOCKET * tds, int col_idx)
105 {
106 	static char result[256];
107 	const int type = tds->res_info->columns[col_idx]->column_type;
108 	const void *value = tds->res_info->columns[col_idx]->column_data;
109 
110 	switch (type) {
111 	case SYBVARCHAR:
112 		strlcpy(result, (const char *) value, sizeof(result));
113 		break;
114 	case SYBINT4:
115 		sprintf(result, "%d", *(const int *) value);
116 		break;
117 	default:
118 		sprintf(result, "Unexpected column_type %d", type);
119 		break;
120 	}
121 	return result;
122 }
123