I have seen this but how to insert a set of custom objects of std::vector into a database using the soci database library
For example, take the c++ class representing a table,
struct one_min_equity {
int64_t id;
std::string timestamp;
double open;
double high;
double low;
double close;
int64_t volume;
int64_t open_interest;
std::string symbol;
double ltq;
};
Corresponding type conversion,
// Type conversion for the database
namespace soci {
/**
* @brief type_conversion for one_min_equity
*/
template <>
struct type_conversion<one_min_equity> {
typedef values base_type;
// convert from database type to your custom type
static void from_base(const values &v, indicator, one_min_equity &p) {
p.id = v.get<int64_t>("id");
p.timestamp = v.get<std::string>("timestamp");
p.open = v.get<double>("open");
p.high = v.get<double>("high");
p.low = v.get<double>("low");
p.close = v.get<double>("close");
p.volume = v.get<int64_t>("volume");
p.open_interest = v.get<int64_t>("open_interest");
p.symbol = v.get<std::string>("symbol");
p.ltq = v.get<double>("ltq");
}
// convert from your custom type to database type
static void to_base(const one_min_equity &p, values &v, indicator &ind) {
v.set("id", p.id);
v.set("timestamp", p.timestamp);
v.set("open", p.open);
v.set("high", p.high);
v.set("low", p.low);
v.set("close", p.close);
v.set("volume", p.volume);
v.set("open_interest", p.open_interest);
v.set("symbol", p.symbol);
v.set("ltq", p.ltq);
ind = i_ok;
}
};
}
This is compiling fine,
void push_one_min_equity_data(const models::one_min_equity &rows) {
// Get the database session from the database module
auto &session = database::get_session();
// Prepare the statement
soci::statement stmt = (session.prepare << R"(
INSERT INTO one_min_equity (
timestamp,
open,
high,
low,
close,
volume,
open_interest,
symbol,
ltq
) VALUES (
:timestamp,
:open,
:high,
:low,
:close,
:volume,
:open_interest,
:symbol,
:ltq
)
)", soci::use(rows));
// Execute the statement
stmt.execute(true);
}
But if I change the rows into vector of rows
void push_one_min_equity_data(const std::vector<models::one_min_equity> &rows)
It doesn't work How to accomplish the bulk insert. I can't insert for each object of vector because it give poor performance. it isn't ?