2025-03-13 17:59:39 +08:00
|
|
|
|
|
|
|
|
#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()
|
|
|
|
|
{
|
2025-03-14 15:02:37 +08:00
|
|
|
m_db=nullptr;
|
2025-03-13 17:59:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2025-03-14 15:02:37 +08:00
|
|
|
if (m_db){
|
|
|
|
|
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;
|
|
|
|
|
}
|
2025-03-13 17:59:39 +08:00
|
|
|
}else{
|
2025-03-14 15:02:37 +08:00
|
|
|
printf("db_sqlite m_db handle is nullptr error!!! \n");
|
2025-03-13 17:59:39 +08:00
|
|
|
}
|
|
|
|
|
return fRet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void db_sqlite::close(void)
|
|
|
|
|
{
|
|
|
|
|
if (m_db){
|
|
|
|
|
sqlite3_close((sqlite3*)m_db);
|
2025-03-14 15:02:37 +08:00
|
|
|
m_db = nullptr;
|
2025-03-13 17:59:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void db_sqlite::vaccum(void)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool db_sqlite::begin_transation(void)
|
|
|
|
|
{
|
|
|
|
|
bool fRet = true;
|
|
|
|
|
std::string sqlcmd = "BEGIN TRANSACTION; ";
|
2025-03-14 15:02:37 +08:00
|
|
|
if (m_db){
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("db_sqlite m_db handle is nullptr error!!! \n");
|
2025-03-13 17:59:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fRet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void db_sqlite::end_transation(void)
|
|
|
|
|
{
|
|
|
|
|
std::string sqlcmd = "COMMIT TRANSACTION; ";
|
2025-03-14 15:02:37 +08:00
|
|
|
if (m_db){
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("db_sqlite m_db handle is nullptr error!!! \n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool db_sqlite::batch_exec(std::vector<std::string> &sql_list,std::vector<db_result_t> &results)
|
|
|
|
|
{
|
|
|
|
|
bool fRet=false;
|
|
|
|
|
char *errMsg = nullptr;
|
|
|
|
|
if (begin_transation()){
|
|
|
|
|
db_result_t result;
|
|
|
|
|
for(const auto &k:sql_list){
|
|
|
|
|
memset(&result, 0, sizeof(result));
|
|
|
|
|
if (sqlite3_exec((sqlite3 *)m_db, k.c_str(), db_result_callback, &result, &errMsg) != SQLITE_OK) {
|
|
|
|
|
printf("sqlite3_exec fail %s \n",errMsg);
|
|
|
|
|
sqlite3_free(errMsg);
|
|
|
|
|
result.result.status = -1;
|
|
|
|
|
}else{
|
|
|
|
|
result.result.status = 0;
|
|
|
|
|
}
|
|
|
|
|
results.push_back(result);
|
|
|
|
|
fRet = true;
|
|
|
|
|
}
|
2025-03-13 17:59:39 +08:00
|
|
|
}
|
2025-03-14 15:02:37 +08:00
|
|
|
end_transation();
|
|
|
|
|
|
|
|
|
|
return fRet;
|
2025-03-13 17:59:39 +08:00
|
|
|
}
|