增加数据库的处理

main
Matthew 2 years ago
parent 73c0ae48a6
commit e315401580

@ -0,0 +1,284 @@
#include "MySQLAdo.h"
#include <assert.h>
#include <cstdlib>
#include <cstring>
#include <time.h>
using namespace std;
#ifdef USING_MYSQL
#ifdef _WIN32
#pragma comment( lib, "mariadbclient.lib" )
#endif
std::vector<IEC_POINT_TABLE> g_iec_points;
std::map<unsigned int, IEC_POINT_TABLE*> g_iec_point_map;
#ifdef USING_MYSQL
bool LoadIecPoints();
#endif
static void show_mysql_error(MYSQL *mysql, const char* name)
{
printf("%s: Error(%d) [%s] \"%s\"", name, mysql_errno(mysql),
mysql_sqlstate(mysql),
mysql_error(mysql));
}
static void show_stmt_error(MYSQL_STMT *stmt, const char* name)
{
printf("%s: Error(%d) [%s] \"%s\"", name, mysql_stmt_errno(stmt),
mysql_stmt_sqlstate(stmt),
mysql_stmt_error(stmt));
}
static void ConvertTimestampToMySQLTime(time_t t, MYSQL_TIME& ts)
{
struct tm *ptm = gmtime(&t);
ts.year = ptm->tm_year + 1900;
ts.month = ptm->tm_mon + 1;
ts.day = ptm->tm_mday;
ts.hour = ptm->tm_hour;
ts.minute = ptm->tm_min;
ts.second = ptm->tm_sec;
ts.time_type = MYSQL_TIMESTAMP_DATETIME;
}
inline void BindNullValue(MYSQL_BIND& bind)
{
bind.buffer = NULL;
bind.is_unsigned = false;
bind.is_null_value = 1;
bind.buffer_type = MYSQL_TYPE_NULL;
bind.buffer_length = 0;
}
inline void BindUByteValue(MYSQL_BIND& bind, const unsigned char& value)
{
bind.buffer = (char *)&value;
bind.is_unsigned = true;
bind.buffer_type = MYSQL_TYPE_TINY;
bind.buffer_length = sizeof(value);
}
inline void BindUShortValue(MYSQL_BIND& bind, const unsigned short& value)
{
bind.buffer = (char *)&value;
bind.is_unsigned = true;
bind.buffer_type = MYSQL_TYPE_SHORT;
bind.buffer_length = sizeof(value);
}
inline void BindUIntValue(MYSQL_BIND& bind, const unsigned int& value)
{
bind.buffer = (char *)&value;
bind.is_unsigned = true;
bind.buffer_type = MYSQL_TYPE_LONG;
bind.buffer_length = sizeof(value);
}
inline void BindULongValue(MYSQL_BIND& bind, const unsigned long long& value)
{
bind.buffer = (char *)&value;
bind.is_unsigned = true;
bind.buffer_type = MYSQL_TYPE_LONGLONG;
bind.buffer_length = sizeof(value);
}
inline void BindFloatValue(MYSQL_BIND& bind, const float& value)
{
bind.buffer = (char *)&value;
bind.buffer_type = isnan(value) ? MYSQL_TYPE_NULL : MYSQL_TYPE_FLOAT;
if (isnan(value))
{
// bind.is_null_value = 1;
}
bind.buffer_length = sizeof(value);
}
inline void BindDoubleValue(MYSQL_BIND& bind, const double& value)
{
bind.buffer = (char *)&value;
bind.buffer_type = isnan(value) ? MYSQL_TYPE_NULL : MYSQL_TYPE_DOUBLE;
if (isnan(value))
{
// bind.is_null_value = 1;
}
bind.buffer_length = sizeof(value);
}
inline void BindValue(MYSQL_BIND& bind, const char* value, unsigned long& len)
{
bind.buffer = (char *)value;
bind.buffer_length = len;
bind.buffer_type = MYSQL_TYPE_STRING;
bind.length = &len;
}
#endif
bool PingDatabase();
#ifdef USING_MYSQL
MYSQL* mysql = NULL;
#endif // USING_MYSQL
bool InitDatabase(int argc, char **argv, const char* host, const char* username, const char* password, const char* database)
{
#ifdef USING_MYSQL
if (mysql_library_init(argc, argv, NULL) != 0)
{
return false;
}
mysql = NULL;
mysql = mysql_init(mysql);
char value = 1;
mysql_options(mysql, MYSQL_OPT_RECONNECT, (char *)&value);
MYSQL* handle = mysql_real_connect(mysql, host, username, password, database,
0, /* port number, 0 for default */
NULL, /* socket file or named pipe name */
CLIENT_FOUND_ROWS /* connection flags */);
if (handle == NULL)
{
show_mysql_error(mysql, "Connect MySQL");
mysql_close(mysql);
mysql = NULL;
return false;
}
mysql_query(mysql, "SET NAMES utf8");
return true;
#else
return true;
#endif // USING_MYSQL
}
bool UninitDatabase()
{
#ifdef USING_MYSQL
if (mysql != NULL) mysql_close(mysql);
mysql_library_end();
#endif
return true;
}
bool PingDatabase()
{
#ifdef USING_MYSQL
if (mysql == NULL)
return false;
return mysql_ping(mysql) == 0;
#else
return false;
#endif // USING_MYSQL
}
#ifdef USING_MYSQL
bool LoadIecPoints()
{
// const char* sql = "SELECT t1.`id`,t1.`site_id`,t1.`sensor_id`,t2.sensor_code,t2.grp_no FROM iec_points AS t1 LEFT JOIN iec_sensors AS t2 t1.sensor_id=t2.`id` WHERE t1.`status`=1 AND t2.`status`=1 ORDER BY t1.site_id,t2.sadr";
const char* sql = "SELECT t1.`id`,t1.`site_id`,t1.`sensor_id`,t2.sensor_code,t2.grp_no FROM niec_points AS t1 LEFT JOIN niec_sensors AS t2 t1.sensor_id=t2.`id` WHERE t1.`status`=1 AND t2.`status`=1 ORDER BY t1.site_id,t2.sadr";
if (mysql_query(mysql, sql))
{
// error
show_mysql_error(mysql, "LoadIecPoints");
return false;
}
MYSQL_RES* res = mysql_store_result(mysql);
if (res == NULL)
{
show_mysql_error(mysql, "LoadIecPoints");
return false;
}
MYSQL_ROW row;
IEC_POINT_TABLE pt;
size_t fieldIdx = 0;
while (row = mysql_fetch_row(res))
{
fieldIdx = 1;
// t1.`id`,t1.`site_id`,t1.`sensor_id`,t2.sensor_code,t2.grp_no
pt.sadr = strtoul(row[fieldIdx++], NULL, 10);
pt.siteId = strtoul(row[fieldIdx++], NULL, 10);
fieldIdx++; // sensor code 17位编码
pt.sensorId = strtoul(row[fieldIdx++], NULL, 10);
pt.sensorGroupNo = strtoul(row[fieldIdx++], NULL, 10);
g_iec_points.push_back(pt);
}
}
#endif
/*
1:
0:
-1:
*/
int GetAIPntMsg(int j, unsigned int *igno, unsigned int* iItemNo, DAY_TIME* sCurTime, float* pfValue, unsigned int *iaddr)
{
#ifdef USING_MYSQL
if (j < 0)
{
return -1;
}
if (j >= g_iec_points.size())
{
return 0;
}
const IEC_POINT_TABLE& pt = g_iec_points[j];
if (PingDatabase())
{
return -1;
}
std::string sql = "SELECT sadr,state,stype,ival,fval,d_time FROM niec_origin_data WHERE sadr=" + std::to_string(pt.sadr) + " ORDER BY `id` DESC LIMIT 1";
if (mysql_query(mysql, sql.c_str()))
{
// error
show_mysql_error(mysql, "GetAIPntMsg");
return -1;
}
MYSQL_RES* res = mysql_store_result(mysql);
if (res == NULL)
{
return -1;
}
int year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0;
MYSQL_ROW row = mysql_fetch_row(res);
if (row)
{
if (igno != NULL) *igno = pt.sensorGroupNo;
if (iItemNo != NULL) *iItemNo = pt.itemNo;
if (iaddr != NULL) *iaddr = pt.sadr;
if (pfValue != NULL) *pfValue = atof(row[4]);
if (sCurTime != NULL && row[5] != NULL)
{
sscanf(row[5], "%04d-%02d-%2d %02d:%02d:%02d", &year, &mon, &day, &hour, &min, &sec);
sCurTime->Year = year;
sCurTime->Month = mon;
sCurTime->Day = day;
sCurTime->Hour = hour;
sCurTime->Min = min;
sCurTime->Sec = sec;
sCurTime->mSec = 0;
}
}
mysql_free_result(res);
return row != NULL ? 1 : -1;
#endif
return 0;
}

@ -0,0 +1,55 @@
#ifndef __MYSQL_ADO_H__
#define __MYSQL_ADO_H__
#include <string>
#include <vector>
#include <map>
#include <cstring>
using namespace std;
#include <mysql/mysql.h>
#include "basetype.h"
#include <map>
#include <vector>
struct IEC_POINT_TABLE
{
unsigned int sadr;
unsigned int siteId;
unsigned int sensorId;
unsigned int sensorGroupNo;
unsigned char stype; // 1: 1:遥信 2:遥测
unsigned int itemNo;
};
extern std::vector<IEC_POINT_TABLE> g_iec_points;
extern std::map<unsigned int, IEC_POINT_TABLE*> g_iec_point_map;
#ifdef USING_MYSQL
static void show_mysql_error(MYSQL *mysql, const char* name);
static void show_stmt_error(MYSQL_STMT *stmt, const char* name);
static void ConvertTimestampToMySQLTime(time_t t, MYSQL_TIME& ts);
inline void BindUByteValue(MYSQL_BIND& bind, const unsigned char& value);
inline void BindUShortValue(MYSQL_BIND& bind, const unsigned short& value);
inline void BindUIntValue(MYSQL_BIND& bind, const unsigned int& value);
inline void BindULongValue(MYSQL_BIND& bind, const unsigned long long& value);
inline void BindFloatValue(MYSQL_BIND& bind, const float& value);
inline void BindDoubleValue(MYSQL_BIND& bind, const double& value);
inline void BindValue(MYSQL_BIND& bind, const char* value, unsigned long& len);
#endif // USING_MYSQL
bool InitDatabase(int argc, char **argv, const char* host, const char* username, const char* password, const char* database);
bool UninitDatabase();
/*
1:
0:
-1:
*/
int GetAIPntMsg(int j, unsigned int *igno, unsigned int* iItemNo, DAY_TIME* sCurTime, float* pfValue, unsigned int *iaddr);
#endif // __MYSQL_ADO_H__

@ -75,6 +75,8 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Program Files\MariaDB 10.11\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<LibraryPath>C:\Program Files\MariaDB 10.11\lib;D:\Workspace\deps\x64\dbg;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
@ -104,14 +106,14 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;OS_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS;DISABLE_OS_HEAP</PreprocessorDefinitions>
<PreprocessorDefinitions>USING_MYSQL;_DEBUG;_CONSOLE;OS_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS;DISABLE_OS_HEAP</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalOptions>/UTF-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Shlwapi.lib;Secur32.lib;Crypt32.lib;zlibd.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -166,6 +168,7 @@
<ClCompile Include="DSFileSystem.cpp" />
<ClCompile Include="Inifile.cpp" />
<ClCompile Include="list_entry.cpp" />
<ClCompile Include="MySQLAdo.cpp" />
<ClCompile Include="netport.cpp" />
<ClCompile Include="os_heap.cpp" />
<ClCompile Include="Profile_Hash.cpp" />
@ -192,6 +195,7 @@
<ClInclude Include="inifile.h" />
<ClInclude Include="ListReport.h" />
<ClInclude Include="list_entry.h" />
<ClInclude Include="MySQLAdo.h" />
<ClInclude Include="netport.h" />
<ClInclude Include="os_heap.h" />
<ClInclude Include="platform_def.h" />

@ -69,6 +69,9 @@
<ClCompile Include="netport.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="MySQLAdo.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="baohua.h">
@ -155,5 +158,8 @@
<ClInclude Include="netport.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="MySQLAdo.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading…
Cancel
Save