0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Document multi_insert for time_point columns (#367)

This commit is contained in:
Roland Bock 2021-06-13 07:51:20 +02:00
parent cc946f8a32
commit 4178942015
2 changed files with 27 additions and 1 deletions

View File

@ -2,6 +2,8 @@ Haven't found the time to document this in any detail, yet, but this is an examp
```C++ ```C++
db(insert_into(tab).set(tab.gamma = true)); db(insert_into(tab).set(tab.gamma = true));
db(insert_into(tabDateTime)
.set(tabDateTime.colTimePoint = std::chrono::system_clock::now()));
``` ```
This is how you could insert multiple rows at a time: This is how you could insert multiple rows at a time:
@ -16,3 +18,18 @@ multi_insert.values.add(t.gamma = sqlpp::value_or_null(true),
t.delta = sqlpp::value_or_null<sqlpp::integer>(sqlpp::null)); t.delta = sqlpp::value_or_null<sqlpp::integer>(sqlpp::null));
db(multi_insert); db(multi_insert);
``` ```
Note that `add` currently requires precise value types, equal to the respective column's value
type. For instance, time point columns are represented as
`std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>`.
Thus, when using such a column in a `multi_insert`, you might have to cast to the right
time point.
```
auto multi_time_insert = insert_into(tabDateTime).columns(tabDateTime.colTimePoint);
multi_time_insert.values.add(tabDateTime.colTimePoint = std::chrono::time_point_cast<std::chrono::microseconds>(
std::chrono::system_clock::now()));
```
Similar for other data types.

View File

@ -35,6 +35,7 @@ int Insert(int, char*[])
MockDb db = {}; MockDb db = {};
MockDb::_serializer_context_t printer = {}; MockDb::_serializer_context_t printer = {};
const auto t = test::TabBar{}; const auto t = test::TabBar{};
const auto tabDateTime = test::TabDateTime{};
// test::TabFoo f; // test::TabFoo f;
{ {
@ -71,6 +72,14 @@ int Insert(int, char*[])
printer.reset(); printer.reset();
std::cerr << serialize(multi_insert, printer).str() << std::endl; std::cerr << serialize(multi_insert, printer).str() << std::endl;
// Beware, you need exact types for inserted values in multi_insert
insert_into(tabDateTime)
.set(tabDateTime.colTimePoint = std::chrono::system_clock::now());
auto multi_time_insert = insert_into(tabDateTime).columns(tabDateTime.colTimePoint);
multi_time_insert.values.add(tabDateTime.colTimePoint = std::chrono::time_point_cast<std::chrono::microseconds>(
std::chrono::system_clock::now()));
auto i = dynamic_insert_into(db, t).dynamic_set(); auto i = dynamic_insert_into(db, t).dynamic_set();
i.insert_list.add(t.beta = "kirschauflauf"); i.insert_list.add(t.beta = "kirschauflauf");
printer.reset(); printer.reset();