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