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 <freetds/string.h>
22 
23 #include <common/test_assert.h>
24 
25 static char *
value_as_string(TDSSOCKET * tds,int col_idx)26 value_as_string(TDSSOCKET * tds, int col_idx)
27 {
28 	static char result[256];
29 	const int type = tds->res_info->columns[col_idx]->column_type;
30 	const void *value = tds->res_info->columns[col_idx]->column_data;
31 
32 	switch (type) {
33 	case SYBVARCHAR:
34 		strncpy(result, (const char *) value, sizeof(result) - 1);
35 		result[sizeof(result) - 1] = '\0';
36 		break;
37 	case SYBINT4:
38 		sprintf(result, "%d", *(const int *) value);
39 		break;
40 	default:
41 		sprintf(result, "Unexpected column_type %d", type);
42 		break;
43 	}
44 	return result;
45 }				/* value_as_string()  */
46 
47 
48 int
main(int argc,char ** argv)49 main(int argc, char **argv)
50 {
51 	TDSLOGIN *login;
52 	TDSSOCKET *tds;
53 	int verbose = 0;
54 	int num_cols = 2;
55 	TDS_INT result_type;
56 	int rc;
57 	int i, done_flags;
58 
59 	fprintf(stdout, "%s: Test basic submit query, results\n", __FILE__);
60 	rc = try_tds_login(&login, &tds, __FILE__, verbose);
61 	if (rc != TDS_SUCCESS) {
62 		fprintf(stderr, "try_tds_login() failed\n");
63 		return 1;
64 	}
65 
66 	rc = tds_submit_query(tds, "select db_name() dbname, user_name() username");
67 	if (rc != TDS_SUCCESS) {
68 		fprintf(stderr, "tds_submit_query() failed\n");
69 		return 1;
70 	}
71 
72 	while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
73 		switch (result_type) {
74 		case TDS_ROWFMT_RESULT:
75 			if (tds->res_info->num_cols != num_cols) {
76 				fprintf(stderr, "Error:  num_cols != %d in %s\n", num_cols, __FILE__);
77 				return 1;
78 			}
79 			if (tds->res_info->columns[0]->column_type != SYBVARCHAR
80 			    || tds->res_info->columns[1]->column_type != SYBVARCHAR) {
81 				fprintf(stderr, "Wrong column_type in %s\n", __FILE__);
82 				return 1;
83 			}
84 			if (strcmp(tds_dstr_cstr(&tds->res_info->columns[0]->column_name), "dbname")
85 			    || strcmp(tds_dstr_cstr(&tds->res_info->columns[1]->column_name), "username")) {
86 				fprintf(stderr, "Wrong column_name in %s\n", __FILE__);
87 				return 1;
88 			}
89 			break;
90 
91 		case TDS_ROW_RESULT:
92 
93 			while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) {
94 				if (result_type != TDS_ROW_RESULT || result_type != TDS_COMPUTE_RESULT)
95 					break;
96 				if (verbose) {
97 					for (i = 0; i < num_cols; i++) {
98 						printf("col %i is %s\n", i, value_as_string(tds, i));
99 					}
100 				}
101 			}
102 			if (rc != TDS_SUCCESS) {
103 				fprintf(stderr, "tds_process_tokens() unexpected return\n");
104 			}
105 			break;
106 
107 		case TDS_DONE_RESULT:
108 		case TDS_DONEPROC_RESULT:
109 		case TDS_DONEINPROC_RESULT:
110 			if (!(done_flags & TDS_DONE_ERROR))
111 				break;
112 
113 		default:
114 			fprintf(stderr, "tds_process_tokens() unexpected result_type\n");
115 			break;
116 		}
117 	}
118 	if (rc != TDS_NO_MORE_RESULTS) {
119 		fprintf(stderr, "tds_process_tokens() unexpected return\n");
120 	}
121 
122 	try_tds_logout(login, tds, verbose);
123 	return 0;
124 }
125