-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.cpp
119 lines (102 loc) · 2.47 KB
/
sql.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* FDS - Fuse Door System
* sql.cpp
* Contains SQL routines.
*
* Created on: 17-Jun-2009
* Author: matthew
*/
#include <iostream>
#include <stdio.h>
#include "misc.h"
#include <string.h>
#include <mysql/mysql.h>
using namespace std;
void fnSqlLogAccess(string accessNumber, int result, optStruct* opts)
{
MYSQL* objSql;
string strQuery;
objSql = mysql_init(NULL);
//connect to the server
if(!mysql_real_connect(objSql, opts->strSqlServer.c_str(),
opts->strSqlUser.c_str(),
opts->strSqlPassword.c_str(),
opts->strSqlTable.c_str(),
0, NULL, 0))
{
//we didn't connect
string strTemp;
strTemp.assign(mysql_error(objSql));
strTemp.append(" fnSqlLogAccess");
fnLogError(opts->strErrorDir,strTemp );
return;
}
//set up the query string:
strQuery = "insert into tbl_log (ID, AllowedEntry) values ('";
strQuery.append(accessNumber);
strQuery.append("', '");
strQuery.append(fnConvertIntToString(result));
strQuery.append("');");
//do the query
mysql_query(objSql, strQuery.c_str());
//close the connection to the server:
mysql_close(objSql);
}
int fnSqlCheckOnAir(optStruct* opts)
{
MYSQL* objSql; //mysql data object
MYSQL_RES* result; //the results set
MYSQL_ROW resultRow; //the actual restult from the results set.
//initalise the library:
objSql = mysql_init(NULL);
//connect to the server
if(!mysql_real_connect(objSql, opts->strSqlServer.c_str(),
opts->strSqlUser.c_str(),
opts->strSqlPassword.c_str(),
opts->strSqlTable.c_str(),
0, NULL, 0))
{
string strTemp;
strTemp.assign(mysql_error(objSql));
strTemp.append(" fnSqlCheckOnAir");
fnLogError(opts->strErrorDir,strTemp );
return -1;
}
//perform the query:
if(mysql_query(objSql, "SELECT * FROM `tbl_Settings` LIMIT 1;") != 0)
{
string strTemp;
strTemp.assign(mysql_error(objSql));
strTemp.append(" fnSqlCheckOnAir");
fnLogError(opts->strErrorDir,strTemp );
return -1;
}
//we got some data (hopefully).
result = mysql_store_result(objSql);
if(!result)
{
//we didn't get any info, exit.
fnLogError(opts->strErrorDir, "No Result.");
return -1;
}
//get the row:
resultRow = mysql_fetch_row(result);
//free the results set as we don't need it:
mysql_free_result(result);
//close the connection to the database:
mysql_close(objSql);
//check to see if the station is on-air.
if(strcmp(resultRow[0], "0") == 0)
{
return 0;
}
else if (strcmp(resultRow[0], "1") == 0)
{
return 1;
}
else
{
return -1;
//something went wrong.
}
}