From e315401580a6714a8f5b3109f2d148fa1746bcd7 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 16 Nov 2023 19:42:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MySQLAdo.cpp | 284 ++++++++++++++++++++++++++++++++++++++++ MySQLAdo.h | 55 ++++++++ widgets.vcxproj | 8 +- widgets.vcxproj.filters | 6 + 4 files changed, 351 insertions(+), 2 deletions(-) create mode 100644 MySQLAdo.cpp create mode 100644 MySQLAdo.h diff --git a/MySQLAdo.cpp b/MySQLAdo.cpp new file mode 100644 index 0000000..bb2eeea --- /dev/null +++ b/MySQLAdo.cpp @@ -0,0 +1,284 @@ +#include "MySQLAdo.h" +#include +#include + +#include +#include +using namespace std; + +#ifdef USING_MYSQL +#ifdef _WIN32 +#pragma comment( lib, "mariadbclient.lib" ) +#endif + +std::vector g_iec_points; +std::map 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; +} diff --git a/MySQLAdo.h b/MySQLAdo.h new file mode 100644 index 0000000..62458f4 --- /dev/null +++ b/MySQLAdo.h @@ -0,0 +1,55 @@ +#ifndef __MYSQL_ADO_H__ +#define __MYSQL_ADO_H__ + +#include +#include +#include +#include +using namespace std; + +#include +#include "basetype.h" +#include +#include + +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 g_iec_points; +extern std::map 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__ \ No newline at end of file diff --git a/widgets.vcxproj b/widgets.vcxproj index 0ee6f84..3311fc3 100644 --- a/widgets.vcxproj +++ b/widgets.vcxproj @@ -75,6 +75,8 @@ true + C:\Program Files\MariaDB 10.11\include;$(VC_IncludePath);$(WindowsSDK_IncludePath); + C:\Program Files\MariaDB 10.11\lib;D:\Workspace\deps\x64\dbg;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64 false @@ -104,14 +106,14 @@ Level3 Disabled true - _DEBUG;_CONSOLE;OS_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS;DISABLE_OS_HEAP + USING_MYSQL;_DEBUG;_CONSOLE;OS_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS;DISABLE_OS_HEAP true /UTF-8 %(AdditionalOptions) Console true - 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) + 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) @@ -166,6 +168,7 @@ + @@ -192,6 +195,7 @@ + diff --git a/widgets.vcxproj.filters b/widgets.vcxproj.filters index bbac05f..7ce01a7 100644 --- a/widgets.vcxproj.filters +++ b/widgets.vcxproj.filters @@ -69,6 +69,9 @@ 源文件 + + 源文件 + @@ -155,5 +158,8 @@ 头文件 + + 头文件 + \ No newline at end of file