rllib  1
Public Member Functions | Private Member Functions | Private Attributes | List of all members
rlBussignalDatabase Class Reference

#include <rlbussignaldatabase.h>

Public Member Functions

 rlBussignalDatabase ()
 
virtual ~rlBussignalDatabase ()
 
int openDatabase (const char *database, const char *table)
 
int writeDatabaseInt (const char *item, int val)
 
int writeDatabaseIntArray (const char *item, int *val, int len)
 
int writeDatabaseFloat (const char *item, float val)
 
int writeDatabaseFloatArray (const char *item, float *val, int len)
 
int writeDatabaseString (const char *item, char *val)
 
int readDatabase (const char *item, char *type, char *value)
 
int closeDatabase ()
 

Private Member Functions

int writeDatabaseString (const char *item)
 
int myquery (const char *query)
 

Private Attributes

void * database
 
void * connection
 
char * databaseName
 
char * tableName
 
char buf [rl_PRINTF_LENGTH]
 
char typebuf [16]
 

Detailed Description

Definition at line 21 of file rlbussignaldatabase.h.

Constructor & Destructor Documentation

◆ rlBussignalDatabase()

rlBussignalDatabase::rlBussignalDatabase ( )

Definition at line 30 of file rlbussignaldatabase.cpp.

31 {
32  databaseName = NULL;
33  tableName = NULL;
34  connection = NULL;
35  if(mysql_is_initialized == 0)
36  {
37  mysql_init(&mysql);
39  }
40 }
static int mysql_is_initialized
static MYSQL mysql

◆ ~rlBussignalDatabase()

rlBussignalDatabase::~rlBussignalDatabase ( )
virtual

Definition at line 42 of file rlbussignaldatabase.cpp.

43 {
44  closeDatabase();
45  if(databaseName != NULL) delete [] databaseName;
46  if(tableName != NULL) delete [] tableName;
47 }

Member Function Documentation

◆ closeDatabase()

int rlBussignalDatabase::closeDatabase ( )

Definition at line 197 of file rlbussignaldatabase.cpp.

198 {
199  mysql_close((MYSQL *) connection);
200  return 0;
201 }

◆ myquery()

int rlBussignalDatabase::myquery ( const char *  query)
private

Definition at line 146 of file rlbussignaldatabase.cpp.

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 }

◆ openDatabase()

int rlBussignalDatabase::openDatabase ( const char *  database,
const char *  table 
)

Definition at line 49 of file rlbussignaldatabase.cpp.

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 }
static MYSQL mysql

◆ readDatabase()

int rlBussignalDatabase::readDatabase ( const char *  item,
char *  type,
char *  value 
)

Definition at line 165 of file rlbussignaldatabase.cpp.

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 }

◆ writeDatabaseFloat()

int rlBussignalDatabase::writeDatabaseFloat ( const char *  item,
float  val 
)

Definition at line 89 of file rlbussignaldatabase.cpp.

90 {
91  sprintf(buf,"%f",val);
92  sprintf(typebuf,"F1");
93  return writeDatabaseString(item);
94 }
char buf[rl_PRINTF_LENGTH]
int writeDatabaseString(const char *item, char *val)

◆ writeDatabaseFloatArray()

int rlBussignalDatabase::writeDatabaseFloatArray ( const char *  item,
float *  val,
int  len 
)

Definition at line 96 of file rlbussignaldatabase.cpp.

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 }
char buf[rl_PRINTF_LENGTH]
int writeDatabaseString(const char *item, char *val)

◆ writeDatabaseInt()

int rlBussignalDatabase::writeDatabaseInt ( const char *  item,
int  val 
)

Definition at line 66 of file rlbussignaldatabase.cpp.

67 {
68  sprintf(buf,"%d",val);
69  sprintf(typebuf,"I1");
70  return writeDatabaseString(item);
71 }
char buf[rl_PRINTF_LENGTH]
int writeDatabaseString(const char *item, char *val)

◆ writeDatabaseIntArray()

int rlBussignalDatabase::writeDatabaseIntArray ( const char *  item,
int *  val,
int  len 
)

Definition at line 73 of file rlbussignaldatabase.cpp.

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 }
char buf[rl_PRINTF_LENGTH]
int writeDatabaseString(const char *item, char *val)

◆ writeDatabaseString() [1/2]

int rlBussignalDatabase::writeDatabaseString ( const char *  item,
char *  val 
)

Definition at line 112 of file rlbussignaldatabase.cpp.

113 {
114  sprintf(typebuf,"S%d",strlen(val));
115  rlstrncpy(buf,val,sizeof(buf)-1);
116  return writeDatabaseString(item);
117 }
char buf[rl_PRINTF_LENGTH]
int writeDatabaseString(const char *item, char *val)
char * rlstrncpy(char *dest, const char *source, int n)
Definition: rlcutil.cpp:169

◆ writeDatabaseString() [2/2]

int rlBussignalDatabase::writeDatabaseString ( const char *  item)
private

Definition at line 119 of file rlbussignaldatabase.cpp.

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 }
char buf[rl_PRINTF_LENGTH]
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int myquery(const char *query)

Member Data Documentation

◆ buf

char rlBussignalDatabase::buf[rl_PRINTF_LENGTH]
private

Definition at line 41 of file rlbussignaldatabase.h.

◆ connection

void* rlBussignalDatabase::connection
private

Definition at line 38 of file rlbussignaldatabase.h.

◆ database

void* rlBussignalDatabase::database
private

Definition at line 37 of file rlbussignaldatabase.h.

◆ databaseName

char* rlBussignalDatabase::databaseName
private

Definition at line 39 of file rlbussignaldatabase.h.

◆ tableName

char* rlBussignalDatabase::tableName
private

Definition at line 40 of file rlbussignaldatabase.h.

◆ typebuf

char rlBussignalDatabase::typebuf[16]
private

Definition at line 42 of file rlbussignaldatabase.h.


The documentation for this class was generated from the following files: