1. 增加数据库接口类定义
This commit is contained in:
@@ -33,6 +33,9 @@ add_executable(dcapp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_WriteData.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_Online.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/f-readdata/normalDataDB.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/db_api/db_api.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/db_api/db_sqlite.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/db_api/db_param.cpp
|
||||
${DC_SRC} ${DS_SRC} ${DQ_SRC} ${DS_BIZSRC}
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
#include "db_api.h"
|
||||
|
||||
db_api *find_db_from_afn_id(void* phandle, uint8_t afn, uint32_t id)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 数据数据库接口类
|
||||
*
|
||||
*/
|
||||
#ifndef _DB_API_H_
|
||||
#define _DB_API_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#define MAX_DATALEN 256
|
||||
#define MAX_ROW_NUM 100
|
||||
|
||||
typedef struct data_s{
|
||||
uint32_t index; //存储序号
|
||||
uint16_t pn; //测量量点号
|
||||
uint32_t id; //数据id
|
||||
time_t stime; //数据更新时间
|
||||
time_t dtime; //数据时标
|
||||
uint32_t crc; //数据校验
|
||||
std::string source; //数据源
|
||||
std::string data; //数据内容
|
||||
}data_t;
|
||||
|
||||
typedef struct result_s{
|
||||
int status; //返回结果 0 -- 正常 其他--错误码
|
||||
int rowNum; //返回数据条数
|
||||
}result_t;
|
||||
|
||||
typedef struct db_result_s {
|
||||
result_t result; //返回结果
|
||||
data_t row[MAX_ROW_NUM]; //返回的数据
|
||||
}db_result_t;
|
||||
|
||||
|
||||
|
||||
class db_api{
|
||||
|
||||
public:
|
||||
db_api(){};
|
||||
db_api(const char *path){};
|
||||
~db_api(){};
|
||||
|
||||
virtual void close(void)=0;
|
||||
virtual void vaccum(void)=0;
|
||||
virtual bool begin_transation() = 0;
|
||||
virtual bool exec(std::string sql, db_result_t* results)=0;
|
||||
virtual void end_transation() = 0;
|
||||
};
|
||||
|
||||
typedef struct db_info_s{
|
||||
db_api *db;
|
||||
std::string tablename;
|
||||
}db_info_t;
|
||||
|
||||
/**
|
||||
* @brief 通过操作句柄,afn, id, 找到对应的数据库句柄和表明
|
||||
* @param phandle -- 操作句柄
|
||||
* @param afn -- 南网afn
|
||||
* @param id -- 南网数据id
|
||||
* @param db_info -- 数据库信息指针
|
||||
* @return false -- 未找到数据库信息, true -- 找到数据库信息指针
|
||||
*/
|
||||
bool find_db_from_afn_id(void* phandle, uint8_t afn, uint32_t id, db_info_t *db_info);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
#ifndef _DB_PARAM_H_
|
||||
#define _DB_PARAM_H_
|
||||
|
||||
include "db_sqlite.h"
|
||||
|
||||
class db_param: public db_sqlite{
|
||||
public:
|
||||
db_param();
|
||||
db_param(const char *path);
|
||||
~db_param();
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
#ifndef _DB_RECORD_H_
|
||||
#define _DB_RECORD_H_
|
||||
|
||||
class db_record : public db_sqlite{
|
||||
|
||||
public:
|
||||
db_record();
|
||||
db_record(const char *path);
|
||||
~db_record();
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,132 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "sqlite3.h"
|
||||
#include "db_sqlite.h"
|
||||
|
||||
#define COLUMN_NAME_INDX "index"
|
||||
#define COLUMN_NAME_PN "mtidx"
|
||||
#define COLUMN_NAME_ID "id"
|
||||
#define COLUMN_NAME_STIME "stime"
|
||||
#define COLUMN_NAME_DTIME "dtime"
|
||||
#define COLUMN_NAME_CRC "crc"
|
||||
#define COLUMN_NAME_SOURCE "source"
|
||||
#define COLUMN_NAME_DATA "data"
|
||||
|
||||
static int db_result_callback(void* out_result, int colnum, char** vals, char** names)
|
||||
{
|
||||
int iRet=0;
|
||||
db_result_t *rslt = (db_result_t*)out_result;
|
||||
if (rslt->result.rowNum<MAX_ROW_NUM){
|
||||
for (int i=0; i<colnum; i++){
|
||||
if (strcmp(COLUMN_NAME_INDX, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].index = std::atoi(vals[i]);
|
||||
}else if (strcmp(COLUMN_NAME_PN, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].pn = std::atoi(vals[i]);
|
||||
}else if (strcmp(COLUMN_NAME_ID, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].id = std::atoi(vals[i]);
|
||||
}else if (strcmp(COLUMN_NAME_STIME, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].stime = std::atoi(vals[i]);
|
||||
}else if (strcmp(COLUMN_NAME_DTIME, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].dtime = std::atoi(vals[i]);
|
||||
}else if (strcmp(COLUMN_NAME_CRC, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].crc = std::atoi(vals[i]);
|
||||
}else if (strcmp(COLUMN_NAME_SOURCE, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].source.insert(0, vals[i], strlen(vals[i]));
|
||||
}else if (strcmp(COLUMN_NAME_DATA, names[i])==0){
|
||||
rslt->row[rslt->result.rowNum].data.insert(0, vals[i], strlen(vals[i]));
|
||||
}
|
||||
}
|
||||
rslt->result.rowNum++;
|
||||
iRet = 0;
|
||||
}else{
|
||||
printf("too many row return, max rowNum %d\n", MAX_ROW_NUM);
|
||||
iRet = -1;
|
||||
}
|
||||
return iRet;
|
||||
}
|
||||
|
||||
db_sqlite::db_sqlite()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
db_sqlite::db_sqlite(const char *cfgfile)
|
||||
{
|
||||
m_db = NULL;
|
||||
sqlite3* sqlite=NULL;
|
||||
int iRet=0;
|
||||
bool fRet=false;
|
||||
|
||||
iRet = sqlite3_open(cfgfile, &sqlite);
|
||||
if (iRet==SQLITE_OK)
|
||||
{
|
||||
iRet = sqlite3_busy_timeout(sqlite, 10000);
|
||||
if (iRet!=SQLITE_OK){
|
||||
printf("sqlite3_busy_timeout error iRet=%d, errmg=%s\n", iRet, sqlite3_errmsg(sqlite));
|
||||
sqlite3_close(sqlite);
|
||||
}else{
|
||||
fRet=true;
|
||||
m_db = (void*)sqlite;
|
||||
}
|
||||
|
||||
}else{
|
||||
printf("db_sqlite::open error iRet=%d \n", iRet );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool db_sqlite::exec(std::string sql, db_result_t* results)
|
||||
{
|
||||
bool fRet = false;
|
||||
char *errMsg = nullptr;
|
||||
if (sqlite3_exec((sqlite3 *)m_db, sql.c_str(), db_result_callback, results, &errMsg) != SQLITE_OK) {
|
||||
printf("sqlite3_exec fail %s \n",errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
fRet = false;
|
||||
results->result.status = -1;
|
||||
}else{
|
||||
results->result.status = 0;
|
||||
fRet = true;
|
||||
}
|
||||
return fRet;
|
||||
}
|
||||
|
||||
void db_sqlite::close(void)
|
||||
{
|
||||
if (m_db){
|
||||
sqlite3_close((sqlite3*)m_db);
|
||||
}
|
||||
}
|
||||
|
||||
void db_sqlite::vaccum(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool db_sqlite::begin_transation(void)
|
||||
{
|
||||
bool fRet = true;
|
||||
std::string sqlcmd = "BEGIN TRANSACTION; ";
|
||||
int iRet = sqlite3_exec((sqlite3*)m_db, sqlcmd.c_str(), NULL, 0, NULL);
|
||||
if (iRet != SQLITE_OK)
|
||||
{
|
||||
printf("db_sqlite::begin_transation do sqlite3_exec %s\n", sqlcmd.c_str());
|
||||
printf("db_sqlite::begin_transation do sqlite3_exec error iRet=%d, errmg=%s\n", iRet, sqlite3_errmsg((sqlite3*)m_db));
|
||||
fRet = false;
|
||||
}
|
||||
|
||||
return fRet;
|
||||
}
|
||||
|
||||
|
||||
void db_sqlite::end_transation(void)
|
||||
{
|
||||
std::string sqlcmd = "COMMIT TRANSACTION; ";
|
||||
int iRet = sqlite3_exec((sqlite3*)m_db, sqlcmd.c_str(), NULL, 0, NULL);
|
||||
if (iRet != SQLITE_OK)
|
||||
{
|
||||
printf("db_sqlite::begin_transation do sqlite3_exec %s\n", sqlcmd.c_str());
|
||||
printf("db_sqlite::begin_transation do sqlite3_exec error iRet=%d, errmg=%s\n", iRet, sqlite3_errmsg((sqlite3*)m_db));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
#ifndef _DB_SQLITE_H_
|
||||
#define _DB_SQLITE_H_
|
||||
|
||||
#include "db_api.h"
|
||||
|
||||
class db_sqlite: public db_api{
|
||||
|
||||
public:
|
||||
db_sqlite();
|
||||
db_sqlite(const char *path);
|
||||
~db_sqlite();
|
||||
|
||||
virtual void close(void);
|
||||
virtual void vaccum(void);
|
||||
virtual bool begin_transation() ; //开启事务
|
||||
virtual bool exec(std::string sql, db_result_t* results);
|
||||
virtual void end_transation() ; //结束事务
|
||||
protected:
|
||||
void *m_db;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user