This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
DBHomework/bs_util.h
2017-12-28 18:18:44 +08:00

24 lines
1.1 KiB
C++

#pragma once
#include "Request.h"
#include "Response.h"
#include "Session.h"
#include "json.hpp"
#include "jsonfail.h"
#include "Util.h"
using json=nlohmann::json;
/// General Json Failure Function.
void jsonfail_func(json& j,int errcode,const std::string& detail="");
/// Get Permission of User
/// If failed to get level, error message will be written to Response in JSON style. Then exit(0) is called.
/// If success, returns permission_level.
/// Returns -1 if control flow reaches unknown branch.
int getPermissionLevel(const std::string& Username,Response& res);
std::function<void(MySQLResult&)> SQLParseInt(int& nval);
#define default_jsonfail() auto jsonfail=[&](int errcode,const std::string& dtl=""){jsonfail_func(j,errcode,dtl);}
#define postval(NAME) if(req.post[#NAME].empty()){jsonfail(err_missing_parameter,#NAME);break;}string NAME=req.post[#NAME]
#define startdb() DBInfo db;if(db.readConfig()<0){jsonfail(err_config);break;} MySQLConn conn;if(db.connectProxy(conn)<0){jsonfail(err_connect);break;}if(conn.exec("set names utf8",nullptr)<0){jsonfail(err_sql,"Failed to set names");break;}