tests/sq/test_sq.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- CO_get_query_logging
- CO_get_query_logfile
- main
1 /***************************************
2 $Revision: 1.2 $
3
4 Example code: Unit test driver for mysql_driver.c
5
6 ******************/ /******************
7 Modification History:
8 ottrey (07/04/1999) Created.
9 ******************/ /******************
10 Copyright (c) 1999 RIPE NCC
11
12 All Rights Reserved
13
14 Permission to use, copy, modify, and distribute this software and its
15 documentation for any purpose and without fee is hereby granted,
16 provided that the above copyright notice appear in all copies and that
17 both that copyright notice and this permission notice appear in
18 supporting documentation, and that the name of the author not be
19 used in advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
23 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
24 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
25 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
26 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 ***************************************/
29 #include "../test_.c" /* unit test template */
30 #include "mysql_driver.h"
31
32 #define TEST1 "1 SQ_info_to_string() function"
33 #define TEST2 "2 SQ_result_to_string() function"
34 #define TEST3 "3 SQ_get_column_count() function"
35 #define TEST4 "4 SQ_get_column_label() function"
36 #define TEST5 "5 SQ_result_to_string() & SQ_info_to_string() functions"
37
38 #define HOST "rowan.ripe.net"
39 #define DATABASE_PORT 3306
40 #define DATABASE "RIPE"
41 #define USER "dbint"
42 #define PASSWD "reimp"
43
44 /*
45 #define HOST "rowan.ripe.net"
46 #define DATABASE_PORT 3306
47 #define DATABASE "test"
48 #define USER "dbint"
49 #define PASSWD "reimp"
50 */
51
52 int Query_logging=0;
53 char *Query_logfile="stdout";
54
55 int CO_get_query_logging() {
/* [<][>][^][v][top][bottom][index][help] */
56 return Query_logging;
57 } /* CO_get_query_logging() */
58
59 char *CO_get_query_logfile() {
/* [<][>][^][v][top][bottom][index][help] */
60 return Query_logfile;
61 } /* CO_get_query_logfile() */
62
63 int main(int argc, char **argv) {
/* [<][>][^][v][top][bottom][index][help] */
64 FILE *infile;
65
66 char input[STR_L];
67
68 SQ_connection_t *con;
69 SQ_result_set_t *result;
70 unsigned int i;
71 unsigned int j;
72 unsigned int no_cols;
73 unsigned int no_rows;
74
75 char *str;
76 int integer;
77
78 /* Default to get the input from stdin */
79 infile = stdin;
80
81 /* Get the options from the command line */
82 get_options(argc, argv);
83
84 /* Make connection */
85 con = SQ_get_connection(HOST, DATABASE_PORT, DATABASE, USER, PASSWD);
86 /*
87 con = SQ_connection_init();
88 con = SQ_connection_get(con, HOST, DATABASE_PORT, DATABASE, USER, PASSWD);
89 */
90
91 /* TEST1 */
92 if (Test[1] == 1) {
93 print_title(TEST1);
94
95 /* Get some info */
96 str = SQ_info_to_string(con);
97 if (Short != 1) {
98 printf("%s\n", str);
99 }
100 free(str);
101 } /* TEST1 */
102
103 /* TEST2 */
104 if (Test[2] == 1) {
105 print_title(TEST2);
106
107 if (Infile_name != NULL) {
108 infile = fopen(Infile_name, "r");
109
110 if (infile == NULL) {
111 perror("Couldn't load infile");
112 return -1;
113 }
114 }
115
116 while (fgets(input, STR_L, infile)) {
117 /* strip the \n */
118 input[strlen(input)-1] = '\0';
119
120 if (Verbose == 1) {
121 printf("input=%s\n", input);
122 }
123
124 result = SQ_execute_query(con, input);
125
126 if (SQ_errno(con) == 0) {
127 if (result != NULL) {
128 str = SQ_result_to_string(result);
129 if (str != NULL) {
130 if (Short != 1) {
131 printf(str);
132 }
133 free(str);
134 }
135 }
136 /* Add information about the last query */
137 /* XXX This isn't working! Grrrr... */
138 if (Short != 1) {
139 /*
140 printf("%s\n", mysql_info(con));
141 */
142 printf("%s\n", "XXX Results from mysql_info(con) is meant to go here. But it's not working!");
143 }
144 }
145 else {
146 fprintf(stderr, "ERROR %d: %s\n", SQ_errno(con), SQ_error(con));
147 }
148
149 SQ_free_result(result);
150 }
151
152 close(infile);
153 } /* TEST2 */
154
155 /* TEST3 */
156 if (Test[3] == 1) {
157 print_title(TEST3);
158
159 if (Infile_name != NULL) {
160 infile = fopen(Infile_name, "r");
161
162 if (infile == NULL) {
163 perror("Couldn't load infile");
164 return -1;
165 }
166 }
167
168 while (fgets(input, STR_L, infile)) {
169 /* strip the \n */
170 input[strlen(input)-1] = '\0';
171
172 if (Verbose == 1) {
173 printf("input=%s\n", input);
174 }
175
176 result = SQ_execute_query(con, input);
177
178 if (result != NULL) {
179 if (Short != 1) {
180 printf("column count=%d\n", SQ_get_column_count(result));
181 }
182 }
183 else {
184 fprintf(stderr, "ERROR %d: %s\n", SQ_errno(con), SQ_error(con));
185 }
186
187 SQ_free_result(result);
188 }
189
190 close(infile);
191 } /* TEST3 */
192
193 /* TEST4 */
194 if (Test[4] == 1) {
195 print_title(TEST4);
196
197 if (Infile_name != NULL) {
198 infile = fopen(Infile_name, "r");
199
200 if (infile == NULL) {
201 perror("Couldn't load infile");
202 return -1;
203 }
204 }
205
206 while (fgets(input, STR_L, infile)) {
207 /* strip the \n */
208 strtok(input, "\n");
209 /*
210 input[strlen(input)-1] = '\0';
211 */
212
213 if (Verbose == 1) {
214 printf("input=%s\n", input);
215 }
216
217 result = SQ_execute_query(con, input);
218
219 if (result == NULL) {
220 fprintf(stderr, "ERROR %d: %s\n", SQ_errno(con), SQ_error(con));
221 continue;
222 }
223
224 for (i=0; i < SQ_get_column_count(result); i++) {
225 str = SQ_get_column_label(result, i);
226 if (Short != 1) {
227 if (str != NULL) {
228 printf("%s\t", str);
229 }
230 else {
231 printf("%s\t", "NuLL");
232 }
233 }
234 free(str);
235 }
236 if (Short != 1) {
237 printf("\n");
238 }
239
240 SQ_free_result(result);
241 }
242
243 close(infile);
244 } /* TEST4 */
245
246 /* TEST5 */
247 if (Test[5] == 1) {
248 print_title(TEST5);
249
250 if (Infile_name != NULL) {
251 infile = fopen(Infile_name, "r");
252
253 if (infile == NULL) {
254 perror("Couldn't load infile");
255 return -1;
256 }
257 }
258
259 while (fgets(input, STR_L, infile)) {
260 /* strip the \n */
261 input[strlen(input)-1] = '\0';
262
263 if (Verbose == 1) {
264 printf("input=%s\n", input);
265 }
266
267 result = SQ_execute_query(con, input);
268
269 if (result != NULL) {
270 str = SQ_result_to_string(result);
271 if (Short != 1) {
272 printf(str);
273 }
274 free(str);
275 }
276 else {
277 fprintf(stderr, "ERROR %d: %s\n", SQ_errno(con), SQ_error(con));
278 }
279
280 SQ_free_result(result);
281
282 /* Get some info */
283 str = SQ_info_to_string(con);
284 if (Short != 1) {
285 printf(str);
286 }
287 free(str);
288
289 }
290
291 close(infile);
292 } /* TEST5 */
293
294 /* Close connection */
295 SQ_close_connection(con);
296
297 return 0;
298
299 } /* main() */
300