From 4178942015cde7941903d9b89b21f2890a485b50 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sun, 13 Jun 2021 07:51:20 +0200 Subject: [PATCH] Document multi_insert for time_point columns (#367) --- docs/Insert.md | 19 ++++++++++++++++++- tests/Insert.cpp | 9 +++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/Insert.md b/docs/Insert.md index ff31b84f..f1375046 100644 --- a/docs/Insert.md +++ b/docs/Insert.md @@ -2,6 +2,8 @@ Haven't found the time to document this in any detail, yet, but this is an examp ```C++ 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: @@ -15,4 +17,19 @@ multi_insert.values.add(t.gamma = sqlpp::value_or_null(true), t.beta = sqlpp::value_or_null("pie"), t.delta = sqlpp::value_or_null(sqlpp::null)); db(multi_insert); -``` \ No newline at end of file +``` + +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`. + +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::system_clock::now())); +``` + +Similar for other data types. diff --git a/tests/Insert.cpp b/tests/Insert.cpp index 34e3a48c..85a0571d 100644 --- a/tests/Insert.cpp +++ b/tests/Insert.cpp @@ -35,6 +35,7 @@ int Insert(int, char*[]) MockDb db = {}; MockDb::_serializer_context_t printer = {}; const auto t = test::TabBar{}; + const auto tabDateTime = test::TabDateTime{}; // test::TabFoo f; { @@ -71,6 +72,14 @@ int Insert(int, char*[]) printer.reset(); 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::system_clock::now())); + auto i = dynamic_insert_into(db, t).dynamic_set(); i.insert_list.add(t.beta = "kirschauflauf"); printer.reset();