Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 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 : static int discard_result(TDSSOCKET * tds);
22 :
23 : static void
24 0 : fatal_error(const char *msg)
25 : {
26 0 : fprintf(stderr, "%s\n", msg);
27 0 : exit(1);
28 : }
29 :
30 : static void
31 16 : test(TDSSOCKET * tds, TDSDYNAMIC * dyn, TDS_INT n, const char *s)
32 : {
33 : TDSPARAMINFO *params;
34 : TDSCOLUMN *curcol;
35 16 : int len = (int)strlen(s);
36 :
37 16 : tds_free_input_params(dyn);
38 :
39 16 : if (!(params = tds_alloc_param_result(dyn->params)))
40 0 : fatal_error("out of memory!");
41 16 : dyn->params = params;
42 :
43 16 : curcol = params->columns[0];
44 16 : tds_set_param_type(tds->conn, curcol, SYBINT4);
45 :
46 : /* TODO test error */
47 16 : tds_alloc_param_data(curcol);
48 16 : curcol->column_cur_size = sizeof(TDS_INT);
49 16 : memcpy(curcol->column_data, &n, sizeof(n));
50 :
51 16 : if (!(params = tds_alloc_param_result(dyn->params)))
52 0 : fatal_error("out of memory!");
53 16 : dyn->params = params;
54 :
55 16 : curcol = params->columns[1];
56 16 : tds_set_param_type(tds->conn, curcol, SYBVARCHAR);
57 16 : curcol->column_size = 40;
58 16 : curcol->column_cur_size = len;
59 :
60 16 : tds_alloc_param_data(curcol);
61 16 : memcpy(curcol->column_data, s, len);
62 :
63 16 : if (tds_submit_execute(tds, dyn) != TDS_SUCCESS)
64 0 : fatal_error("tds_submit_execute() error");
65 16 : if (discard_result(tds) != TDS_SUCCESS)
66 0 : fatal_error("tds_submit_execute() output error");
67 16 : }
68 :
69 : int
70 8 : main(int argc, char **argv)
71 : {
72 : TDSLOGIN *login;
73 : TDSSOCKET *tds;
74 8 : int verbose = 0;
75 8 : TDSDYNAMIC *dyn = NULL;
76 : int rc;
77 :
78 8 : printf("%s: Test dynamic queries\n", __FILE__);
79 8 : rc = try_tds_login(&login, &tds, __FILE__, verbose);
80 8 : if (rc != TDS_SUCCESS)
81 0 : fatal_error("try_tds_login() failed");
82 :
83 8 : run_query(tds, "DROP TABLE #dynamic1");
84 8 : if (run_query(tds, "CREATE TABLE #dynamic1 (i INT, c VARCHAR(40))") != TDS_SUCCESS)
85 0 : fatal_error("creating table error");
86 :
87 8 : if (tds->cur_dyn)
88 0 : fatal_error("already a dynamic query??");
89 :
90 : /* prepare to insert */
91 8 : if (tds_submit_prepare(tds, "INSERT INTO #dynamic1(i,c) VALUES(?,?)", NULL, &dyn, NULL) != TDS_SUCCESS)
92 0 : fatal_error("tds_submit_prepare() error");
93 8 : if (discard_result(tds) != TDS_SUCCESS)
94 0 : fatal_error("tds_submit_prepare() output error");
95 :
96 8 : if (!dyn)
97 0 : fatal_error("dynamic not present??");
98 :
99 : /* insert one record */
100 8 : test(tds, dyn, 123, "dynamic");
101 :
102 : /* some test */
103 8 : if (run_query(tds, "DECLARE @n INT SELECT @n = COUNT(*) FROM #dynamic1 IF @n <> 1 SELECT 0") != TDS_SUCCESS)
104 0 : fatal_error("checking rows");
105 :
106 8 : if (run_query(tds, "DECLARE @n INT SELECT @n = COUNT(*) FROM #dynamic1 WHERE i = 123 AND c = 'dynamic' IF @n <> 1 SELECT 0")
107 : != TDS_SUCCESS)
108 0 : fatal_error("checking rows 1");
109 :
110 : /* insert one record */
111 8 : test(tds, dyn, 654321, "a longer string");
112 :
113 : /* some test */
114 8 : if (run_query(tds, "DECLARE @n INT SELECT @n = COUNT(*) FROM #dynamic1 IF @n <> 2 SELECT 0") != TDS_SUCCESS)
115 0 : fatal_error("checking rows");
116 :
117 8 : if (run_query(tds, "DECLARE @n INT SELECT @n = COUNT(*) FROM #dynamic1 WHERE i = 123 AND c = 'dynamic' IF @n <> 1 SELECT 0")
118 : != TDS_SUCCESS)
119 0 : fatal_error("checking rows 2");
120 :
121 8 : if (run_query
122 : (tds,
123 : "DECLARE @n INT SELECT @n = COUNT(*) FROM #dynamic1 WHERE i = 654321 AND c = 'a longer string' IF @n <> 1 SELECT 0") !=
124 : TDS_SUCCESS)
125 0 : fatal_error("checking rows 3");
126 :
127 8 : if (run_query(tds, "DROP TABLE #dynamic1") != TDS_SUCCESS)
128 0 : fatal_error("dropping table error");
129 :
130 8 : tds_release_dynamic(&dyn);
131 :
132 8 : try_tds_logout(login, tds, verbose);
133 : return 0;
134 : }
135 :
136 : static int
137 24 : discard_result(TDSSOCKET * tds)
138 : {
139 : int rc;
140 : int result_type;
141 :
142 88 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
143 :
144 40 : switch (result_type) {
145 : case TDS_DONE_RESULT:
146 : case TDS_DONEPROC_RESULT:
147 : case TDS_DONEINPROC_RESULT:
148 : case TDS_DESCRIBE_RESULT:
149 : case TDS_STATUS_RESULT:
150 : case TDS_PARAM_RESULT:
151 : break;
152 0 : default:
153 0 : fprintf(stderr, "Error: query should not return results\n");
154 0 : return TDS_FAIL;
155 : }
156 : }
157 24 : if (rc == TDS_FAIL) {
158 0 : fprintf(stderr, "tds_process_tokens() returned TDS_FAIL\n");
159 0 : return TDS_FAIL;
160 24 : } else if (rc != TDS_NO_MORE_RESULTS) {
161 0 : fprintf(stderr, "tds_process_tokens() unexpected return\n");
162 0 : return TDS_FAIL;
163 : }
164 :
165 : return TDS_SUCCESS;
166 : }
|