rllib  1
rlbussignaldatabase.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlbussignaldatabase.cpp - description
3  -------------------
4  begin : Mon Aug 02 2002
5  copyright : (C) 2002 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #ifdef RLWIN32
21 #include <winsock2.h>
22 #endif
23 #include <mysql.h>
24 #include "rlbussignaldatabase.h"
25 #include "rlcutil.h"
26 
27 static int mysql_is_initialized = 0;
28 static MYSQL mysql;
29 
31 {
32  databaseName = NULL;
33  tableName = NULL;
34  connection = NULL;
35  if(mysql_is_initialized == 0)
36  {
37  mysql_init(&mysql);
39  }
40 }
41 
43 {
44  closeDatabase();
45  if(databaseName != NULL) delete [] databaseName;
46  if(tableName != NULL) delete [] tableName;
47 }
48 
49 int rlBussignalDatabase::openDatabase(const char *database, const char *table)
50 {
51  if(databaseName != NULL) delete [] databaseName;
52  if(tableName != NULL) delete [] tableName;
53  databaseName = new char[strlen(database)+1];
54  strcpy(databaseName,database);
55  tableName = new char[strlen(table)+1];
56  strcpy(tableName,table);
57  connection = (void *) mysql_real_connect(&mysql, "localhost","bususr","buspw",databaseName,0,0,0);
58  if(connection == NULL)
59  {
60  printf((const char *) mysql_error(&mysql));
61  return -1;
62  }
63  return 0;
64 }
65 
66 int rlBussignalDatabase::writeDatabaseInt(const char *item, int val)
67 {
68  sprintf(buf,"%d",val);
69  sprintf(typebuf,"I1");
70  return writeDatabaseString(item);
71 }
72 
73 int rlBussignalDatabase::writeDatabaseIntArray(const char *item, int *val, int len)
74 {
75  int i;
76  char vbuf[80];
77 
78  buf[0] = '\0';
79  for(i=0; i<len; i++)
80  {
81  sprintf(vbuf,"%d,",val[i]);
82  if(strlen(buf)+strlen(vbuf) >= sizeof(buf)-1) return -1;
83  strcat(buf,vbuf);
84  }
85  sprintf(typebuf,"I%d",len);
86  return writeDatabaseString(item);
87 }
88 
89 int rlBussignalDatabase::writeDatabaseFloat(const char *item, float val)
90 {
91  sprintf(buf,"%f",val);
92  sprintf(typebuf,"F1");
93  return writeDatabaseString(item);
94 }
95 
96 int rlBussignalDatabase::writeDatabaseFloatArray(const char *item, float *val, int len)
97 {
98  int i;
99  char vbuf[80];
100 
101  buf[0] = '\0';
102  for(i=0; i<len; i++)
103  {
104  sprintf(vbuf,"%f,",val[i]);
105  if(strlen(buf)+strlen(vbuf) >= sizeof(buf)-1) return -1;
106  strcat(buf,vbuf);
107  }
108  sprintf(typebuf,"F%d",len);
109  return writeDatabaseString(item);
110 }
111 
112 int rlBussignalDatabase::writeDatabaseString(const char *item, char *val)
113 {
114  sprintf(typebuf,"S%d",strlen(val));
115  rlstrncpy(buf,val,sizeof(buf)-1);
116  return writeDatabaseString(item);
117 }
118 
120 {
121  int ret;
122  char sqlbuf[rl_PRINTF_LENGTH];
123 
124  // Try an UPDATE
125  ret = snprintf(sqlbuf,sizeof(sqlbuf)-1,"UPDATE %s SET datatype='%s', datavalue='%s' WHERE name='%s'",tableName,typebuf,buf,item);
126  if(ret < 0)
127  {
128  printf("sqlbuf too small in writeDatabaseString\n");
129  return -1;
130  }
131  ret = myquery(sqlbuf);
132  if(ret > 0) return 0; // success
133 
134  // Try an INSERT
135  ret = snprintf(sqlbuf,sizeof(sqlbuf)-1,"INSERT INTO %s VALUES ('%s','%s','%s')",tableName,item,typebuf,buf);
136  if(ret < 0)
137  {
138  printf("sqlbuf too small in writeDatabaseString\n");
139  return -1;
140  }
141  ret = myquery(sqlbuf);
142  if(ret >= 0) return 0; // success
143  return -1; // failure
144 }
145 
146 int rlBussignalDatabase::myquery(const char *query)
147 {
148  int state,num_rows;
149  MYSQL_RES *result;
150 
151  //printf("query=%s\n",query);
152  state = mysql_query((MYSQL *) connection, query);
153  if(state != 0)
154  {
155  //printf("query failed state=%d query=%s\n",state,query);
156  return -1;
157  }
158  num_rows = mysql_affected_rows((MYSQL *) connection);
159  result = mysql_store_result((MYSQL *) connection);
160  mysql_free_result(result);
161  //printf("num_rows=%d\n",num_rows);
162  return num_rows;
163 }
164 
165 int rlBussignalDatabase::readDatabase(const char *item, char *type, char *value)
166 {
167  char query[1024];
168  int state,num_rows;
169  MYSQL_RES *result;
170  MYSQL_ROW row;
171 
172  *type = *value = '\0';
173  sprintf(query,"select * from bus where name = '%s'",item);
174  //printf("query=%s\n",query);
175  state = mysql_query((MYSQL *) connection, query);
176  if(state != 0)
177  {
178  //printf("query failed state=%d query=%s\n",state,query);
179  return -1;
180  }
181  num_rows = mysql_affected_rows((MYSQL *) connection);
182  result = mysql_store_result((MYSQL *) connection);
183  if(result != NULL)
184  {
185  row = mysql_fetch_row(result);
186  if(row != NULL)
187  {
188  if(row[1] != NULL) strcpy(type ,row[1]);
189  if(row[2] != NULL) strcpy(value,row[2]);
190  }
191  mysql_free_result(result);
192  }
193  //printf("num_rows=%d\n",num_rows);
194  return num_rows;
195 }
196 
198 {
199  mysql_close((MYSQL *) connection);
200  return 0;
201 }
202 
int readDatabase(const char *item, char *type, char *value)
char buf[rl_PRINTF_LENGTH]
int writeDatabaseIntArray(const char *item, int *val, int len)
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int writeDatabaseFloatArray(const char *item, float *val, int len)
int writeDatabaseString(const char *item, char *val)
static int mysql_is_initialized
int myquery(const char *query)
int openDatabase(const char *database, const char *table)
int writeDatabaseFloat(const char *item, float val)
char * rlstrncpy(char *dest, const char *source, int n)
Definition: rlcutil.cpp:169
static MYSQL mysql
int writeDatabaseInt(const char *item, int val)