cpp
This commit is contained in:
55
jtlsrv-cpp/src/db/pool.hpp
Normal file
55
jtlsrv-cpp/src/db/pool.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <cstdint>
|
||||
#include <sql.h>
|
||||
#include <sqlext.h>
|
||||
#include <uv.h>
|
||||
|
||||
enum class CellType { Null, Int64, Double, String, Blob };
|
||||
|
||||
struct Cell {
|
||||
CellType type = CellType::Null;
|
||||
int64_t i64 = 0;
|
||||
double dbl = 0.0;
|
||||
std::string str;
|
||||
std::vector<uint8_t> blob;
|
||||
};
|
||||
|
||||
using Row = std::vector<Cell>;
|
||||
using ResultSet = std::vector<Row>;
|
||||
|
||||
enum class ParamType { Int, BigInt, Float, Double, NVarChar, Bit };
|
||||
|
||||
struct Param {
|
||||
ParamType type;
|
||||
std::string str_val;
|
||||
int64_t int_val = 0;
|
||||
double dbl_val = 0.0;
|
||||
};
|
||||
|
||||
class OdbcPool {
|
||||
public:
|
||||
~OdbcPool();
|
||||
int connect();
|
||||
void disconnect();
|
||||
bool execute(const std::string& sql, const std::vector<Param>& params, ResultSet& out);
|
||||
bool execute(const std::string& sql, ResultSet& out);
|
||||
int64_t execute_scalar(const std::string& sql, const std::vector<Param>& params = {}, int64_t fallback = 0);
|
||||
|
||||
private:
|
||||
struct Connection {
|
||||
SQLHDBC hdbc = SQL_NULL_HDBC;
|
||||
SQLHSTMT hstmt = SQL_NULL_HSTMT;
|
||||
bool in_use = false;
|
||||
};
|
||||
SQLHENV henv_ = SQL_NULL_HENV;
|
||||
std::vector<Connection> conns_;
|
||||
std::mutex mutex_;
|
||||
Connection* checkout();
|
||||
void release(Connection* c);
|
||||
};
|
||||
|
||||
OdbcPool& get_pool();
|
||||
Reference in New Issue
Block a user