root/trunk/src/mysql_mgmt.c @ 32

Revision 2, 6.5 KB (checked in by andreu, 17 years ago)

First RENETCOL CVS Integration

  • Property svn:eol-style set to native
Line 
1/*
2 * File: mysql_mgmt.c
3 *
4 * Authors: ANDREU François-Xavier
5 *
6 * Copyright (C) 2005 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include "mysql_mgmt.h"
27
28/*
29 * get_dbpass
30 */
31char *get_dbpass()
32{
33  char * pass;
34  pass = getpass("DB password : ");
35  return pass;
36}
37
38
39/*
40 * init_connection
41 */
42MYSQL *init_connection ()
43{
44  MYSQL *my_connection;
45
46  if (!(my_connection = mysql_init (NULL)))
47    {                           /*FIXME: free after*/
48      fprintf (stderr, "ERROR IN mysql_init\n");
49      exit (1);
50    }
51  if (!mysql_real_connect (my_connection, DB_HOST, DB_USER, get_dbpass(),
52                           DB_NAME, 0, NULL, 0))
53    {
54      fprintf (stderr, "Failed to connect to database: ERROR: %s\n",
55               mysql_error (my_connection));
56      exit (1);
57    }
58  fprintf (stderr, "infos: %s\n", mysql_get_host_info (my_connection));
59  return my_connection;
60}
61
62/*
63 * close_connection
64 */
65void close_connection (MYSQL * myc)
66{
67  mysql_close (myc);
68}
69
70
71/*
72 * get_info_bd
73 *
74 */
75void
76get_info_bd (MYSQL * myc)
77{
78  MYSQL *my_connection = myc;
79  MYSQL_RES *my_res;
80  MYSQL_ROW my_row;
81  unsigned int num_fields, i;
82
83  if (!(my_res = mysql_list_tables (my_connection, NULL)))
84    {
85      fprintf (stderr, "Failed to list tables: ERROR: %s\n",
86               mysql_error (my_connection));
87      exit (1);
88    }
89  num_fields = mysql_num_fields (my_res);
90  while ((my_row = mysql_fetch_row (my_res)))
91    {
92      unsigned long *lengths;
93      lengths = mysql_fetch_lengths (my_res);
94      for (i = 0; i < num_fields; i++)
95        {
96          fprintf (stderr, " %s ", my_row[i]);
97        }
98      fprintf (stderr, "\n");
99    }
100  mysql_free_result (my_res);
101
102  if (mysql_real_query
103      (my_connection, REQ_OUTPUT_TYPE, strlen (REQ_OUTPUT_TYPE)))
104    {
105      fprintf (stderr, "Failed to query OUTPUT_TYPE table: ERROR: %s\n",
106               mysql_error (my_connection));
107      exit (1);
108    }
109  my_res = mysql_store_result (my_connection);
110  if (my_res)
111    {
112      fprintf (stderr, "RESULT: %d rows\n",
113               (int)mysql_affected_rows (my_connection));
114      num_fields = mysql_num_fields (my_res);
115      while ((my_row = mysql_fetch_row (my_res)))
116        {
117          unsigned long *lengths;
118          lengths = mysql_fetch_lengths (my_res);
119          for (i=0; i<(num_fields); i++) {
120            fprintf (stderr, " %s ", my_row[i]);
121          }
122          fprintf (stderr, "\n");
123        }
124    }
125  else
126    {
127      if (mysql_field_count (my_connection) == 0)
128        {
129          /*query does not return data*/
130          fprintf (stderr, "IT WAS NOT a SELECT\n");
131        }
132      else
133        {
134          fprintf (stderr, "RESULT EMPTY, ERROR: %s\n",
135                   mysql_error (my_connection));
136        }
137    }
138  mysql_free_result (my_res);
139}
140
141/*
142 * get_rules
143 *
144 */
145
146p_rules get_rules(MYSQL * myc, p_rules pr)
147{
148  MYSQL *my_connection = myc;
149  MYSQL_RES *my_res;
150  MYSQL_ROW my_row;
151  unsigned int num_fields;
152  struct tm     tm;
153 
154  memset (& tm, 0, sizeof (struct tm));
155  if (mysql_real_query
156      (my_connection, REQ_FILTRES_TABLE, strlen (REQ_FILTRES_TABLE)))
157    {
158      fprintf (stderr, "Failed to query FILTRES table: ERROR: %s\n",
159               mysql_error (my_connection));
160      exit (1);
161    }
162  my_res = mysql_store_result (my_connection);
163  if (my_res)
164    {
165      fprintf (stderr, "RESULT: %d rows\n",
166               mysql_affected_rows (my_connection));
167      num_fields = mysql_num_fields (my_res);
168      while ((my_row = mysql_fetch_row (my_res)))
169        {
170        }
171    }
172  else
173    {
174      if (mysql_field_count (my_connection) == 0)
175        {
176          /*query does not return data*/
177          fprintf (stderr, "IT WAS NOT a SELECT\n");
178        }
179      else
180        {
181          fprintf (stderr, "RESULT EMPTY, ERROR: %s\n",
182                   mysql_error (my_connection));
183        }
184    }
185  mysql_free_result (my_res);
186  return pr;
187}
188
189
190/*
191 * get_fields_to_record
192 *
193 */
194void
195get_fields_to_record(MYSQL * myc, unsigned short idr)
196{
197  MYSQL *my_connection = myc;
198  MYSQL_RES *my_res;
199  MYSQL_ROW my_row;
200  unsigned int num_fields, i;
201 
202  if (mysql_real_query
203      (my_connection, REQ_FIELDS_TO_RECORD, strlen (REQ_FIELDS_TO_RECORD)))
204    {
205      fprintf (stderr, "Failed to query FIELDS_TO_RECORD table: ERROR: %s\n",
206               mysql_error (my_connection));
207      exit (1);
208    }
209  my_res = mysql_store_result (my_connection);
210  if (my_res)
211    {
212      fprintf (stderr, "RESULT: %d rows\n",
213               mysql_affected_rows (my_connection));
214      num_fields = mysql_num_fields (my_res);
215      while ((my_row = mysql_fetch_row (my_res)))
216        {
217          unsigned long *lengths;
218          lengths = mysql_fetch_lengths (my_res);
219          for (i=0; i<(num_fields); i++) {
220            fprintf (stderr, " %s ", my_row[i]);
221          }
222          fprintf (stderr, "\n");
223        }
224    }
225  else
226    {
227      if (mysql_field_count (my_connection) == 0)
228        {
229          /*query does not return data*/
230          fprintf (stderr, "IT WAS NOT a SELECT\n");
231        }
232      else
233        {
234          fprintf (stderr, "RESULT EMPTY, ERROR: %s\n",
235                   mysql_error (my_connection));
236        }
237    }
238  mysql_free_result (my_res);
239}
240
241
242/*
243 * get_output
244 *
245 */
246void
247get_output(MYSQL * myc, unsigned short ido, p_rules rl)
248{
249  MYSQL *my_connection = myc;
250  MYSQL_RES *my_res;
251  MYSQL_ROW my_row;
252  unsigned int num_fields, i;
253 
254  if (mysql_real_query
255      (my_connection, REQ_OUTPUT_TABLE, strlen (REQ_OUTPUT_TABLE)))
256    {
257      fprintf (stderr, "Failed to query OUTPUT_TABLE table: ERROR: %s\n",
258               mysql_error (my_connection));
259      exit (1);
260    }
261  my_res = mysql_store_result (my_connection);
262  if (my_res)
263    {
264      fprintf (stderr, "RESULT: %d rows\n",
265               mysql_affected_rows (my_connection));
266      num_fields = mysql_num_fields (my_res);
267      while ((my_row = mysql_fetch_row (my_res)))
268        {
269          unsigned long *lengths;
270          lengths = mysql_fetch_lengths (my_res);
271          for (i=0; i<(num_fields); i++) {
272            fprintf (stderr, " %s ", my_row[i]);
273          }
274          fprintf (stderr, "\n");
275        }
276    }
277  else
278    {
279      if (mysql_field_count (my_connection) == 0)
280        {
281          /*query does not return data*/
282          fprintf (stderr, "IT WAS NOT a SELECT\n");
283        }
284      else
285        {
286          fprintf (stderr, "RESULT EMPTY, ERROR: %s\n",
287                   mysql_error (my_connection));
288        }
289    }
290  mysql_free_result (my_res);
291}
Note: See TracBrowser for help on using the browser.