提交 85f633cd authored 作者: blu's avatar blu

init

上级 d75b84d9
...@@ -13,50 +13,121 @@ using json = nlohmann::json; ...@@ -13,50 +13,121 @@ using json = nlohmann::json;
namespace DB { namespace DB {
map<string, sqlite3 *> maphdb; map<string, sqlite3 *> maphdb;
map<string, mutex> mapMut; map<string, mutex> mapMut;
static bool bRand = false;
//typedef int (*callback)(void*,int,char**,char**); //typedef int (*callback)(void*,int,char**,char**);
int exec(void *pUserData, const char* fileName, const char* stmt, callback cb){ sqlite3* exec(void *pUserData, const char* fileName, const char* stmt, callback cb){
int ret = 0; int ret = 0;
if(fileName == NULL||strlen(fileName) == 0) { if(fileName == NULL||strlen(fileName) == 0) {
fileName = const_cast<char*>("default.db"); fileName = const_cast<char*>("default.db");
} }
sqlite3 *ppdb = NULL; sqlite3 *pdb = NULL;
try{ try{
ppdb = maphdb.at(string(fileName)); pdb = maphdb.at(string(fileName));
}catch(...){ }catch(...){
ppdb = NULL; pdb = NULL;
} }
mutex &mut = mapMut[string(fileName)]; mutex &mut = mapMut[string(fileName)];
if(ppdb == NULL) { if(pdb == NULL) {
std::lock_guard<std::mutex> lock(mut); std::lock_guard<std::mutex> lock(mut);
if(ppdb == NULL) { if(pdb == NULL) {
ret = sqlite3_open(fileName,&ppdb); ret = sqlite3_open(fileName,&pdb);
if(ret != SQLITE_OK) if(ret != SQLITE_OK)
{ {
spdlog::error("sqlite3_open: {}",sqlite3_errmsg(ppdb)); spdlog::error("sqlite3_open: {}",sqlite3_errmsg(pdb));
exit(1); exit(1);
} }
} }
maphdb[string(fileName)] = ppdb; maphdb[string(fileName)] = pdb;
} }
// //
// sprintf(sql,"create table if not exists address (name text, tel text);"); // sprintf(sql,"create table if not exists address (name text, tel text);");
if(stmt != NULL) { if(stmt != NULL) {
std::lock_guard<std::mutex> lock(mut); std::lock_guard<std::mutex> lock(mut);
ret = sqlite3_exec(ppdb, stmt, cb, pUserData, NULL); ret = sqlite3_exec(pdb, stmt, cb, pUserData, NULL);
if(ret != SQLITE_OK) if(ret != SQLITE_OK)
{ {
spdlog::error("sqlite3_exec: {}",sqlite3_errmsg(ppdb)); spdlog::error("sqlite3_exec: {}",sqlite3_errmsg(pdb));
} }
} }
//sqlite3_close(ppdb); //sqlite3_close(pdb);
return pdb;
}
string genStrRand(int length) {
if(!bRand) {
srand(time(NULL));
bRand = true;
}
static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result;
result.resize(length);
srand(time(NULL));
for (int i = 0; i < length; i++)
result[i] = charset[rand() % charset.length()];
return result;
}
int _getSn(void *pUser, int cc, char **cv, char **cn) {
int ret = SQLITE_OK;
auto v = static_cast<string*>(pUser);
if(cc == 1) {
*v = string(cv[0]);
}else{
if(ret < 1) {
ret = -1;
}else{
ret = 1;
}
}
return ret;
}
int setSn(const char *sn, const char *fileName) {
int ret = 0;
return ret;
}
int setLocalConfig(json config, const char* fileName) {
int ret = 0;
string stmt;
sqlite3* pdb =NULL;
// init tables
stmt = "create table if not exists info(cls text, value text, version text, update datetime, primary key cls);";
pdb = exec(NULL, fileName, stmt.c_str(), NULL);
if(sqlite3_errcode(pdb) != SQLITE_OK) {
spdlog::error("failed to create table info: {}", sqlite3_errmsg(pdb));
return -1;
}
// if sn exist
string sn;
stmt = "select value from info where cls=sn;";
pdb = exec(&sn, fileName, stmt.c_str(), _getSn);
if(sqlite3_errcode(pdb) != SQLITE_OK ||sn.empty()) {
spdlog::error("failed get sn: {}, will create new one", sqlite3_errmsg(pdb));
sn = genStrRand(8);
stmt = "insert into info(cls, value, update) values(sn," + sn + ",'now');";
if(sqlite3_errcode(pdb) != SQLITE_OK) {
spdlog::error("failed insert sn: {}, will create new one", sqlite3_errmsg(pdb));
return -1;
}
}
return ret;
}
int getLocalConfig(json config) {
int ret = 0;
return ret; return ret;
} }
int _getSlices(void *pUser, int cc, char **cv, char **cn) { int _getSlices(void *pUser, int cc, char **cv, char **cn) {
int ret = 0;
auto v = static_cast< vector<int>* >(pUser); auto v = static_cast< vector<int>* >(pUser);
if(cc != v->size()) { if(cc != v->size()) {
return SQLITE_ERROR; return SQLITE_ERROR;
...@@ -65,6 +136,8 @@ namespace DB { ...@@ -65,6 +136,8 @@ namespace DB {
v->at(i) = atoi(cv[0]); v->at(i) = atoi(cv[0]);
} }
} }
return ret;
} }
/* /*
...@@ -137,51 +210,37 @@ namespace DB { ...@@ -137,51 +210,37 @@ namespace DB {
// evml: iid, cid, type, addr, enabled, status // evml: iid, cid, type, addr, enabled, status
// //
// //
int setLocalConfig(json config, const char* fileName) {
int ret;
string stmt;
// init tables
stmt = "create table if not exists info(cls text, value text, version text, update datetime, primary key cls);";
ret = exec(NULL, fileName, stmt.c_str(), NULL);
if(ret != SQLITE_OK) {
return ret;
}
//
}
int getLocalConfig(json config) {
}
int getSlices(void *pUser, int iid, const char *fileName) { int getSlices(void *pUser, int iid, const char *fileName) {
int ret = 0; int ret = 0;
sqlite3 * pdb = NULL;
auto v = static_cast< vector<int>* >(pUser); auto v = static_cast< vector<int>* >(pUser);
string stmt = "select ts from slices where iid="+to_string(iid)+" order by id;"; string stmt = "select ts from slices where iid="+to_string(iid)+" order by id;";
ret = exec(pUser, fileName, stmt.c_str(), _getSlices); pdb = exec(pUser, fileName, stmt.c_str(), _getSlices);
if(ret != SQLITE_OK) { if(sqlite3_errcode(pdb) != SQLITE_OK) {
// create // create
stmt = "create table if not exists slices(id integer, iid integer, ts integer);"; stmt = "create table if not exists slices(id integer, iid integer, ts integer);";
ret = exec(NULL, fileName, stmt.c_str(), NULL); pdb = exec(NULL, fileName, stmt.c_str(), NULL);
if(ret != SQLITE_OK) { if(sqlite3_errcode(pdb) != SQLITE_OK) {
spdlog::error("failed create table slices for evslicer {}", iid); spdlog::error("failed create table slices for evslicer {}", iid);
return ret; return -1;
}else{ }else{
for(int i = 1; i <= v->size(); i ++) { for(int i = 1; i <= v->size(); i ++) {
stmt = "insert into slices(id, iid, ts) values(" + to_string(i) + to_string(iid) + ", 0"; stmt = "insert into slices(id, iid, ts) values(" + to_string(i) + to_string(iid) + ", 0";
ret = exec(NULL, fileName, stmt.c_str(), NULL); pdb = exec(NULL, fileName, stmt.c_str(), NULL);
if(ret != SQLITE_OK) return ret; if(sqlite3_errcode(pdb) != SQLITE_OK) return -2;
v->push_back(0); v->push_back(0);
} }
stmt = "update slices set ts=1 where id = 1 and iid="+to_string(iid) +";"; stmt = "update slices set ts=1 where id = 1 and iid="+to_string(iid) +";";
ret = exec(NULL, stmt.c_str(), NULL, NULL); pdb = exec(NULL, stmt.c_str(), NULL, NULL);
if(ret != SQLITE_OK) return ret; if(sqlite3_errcode(pdb) != SQLITE_OK) return -3;
v->at(0) = 2; v->at(0) = 2;
} }
}else{ }else{
} }
return SQLITE_OK; return ret;
} }
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论