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