From 57077cd32badad800c2841ae3df7601b521819f8 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Mon, 6 Jun 2022 10:15:28 +0200 Subject: [PATCH] Add standalone insert_columns function (#76) This allows for multi-row INSERT OR IGNORE --- include/sqlpp11/insert_value_list.h | 7 +++++++ tests/core/serialize/CustomQuery.cpp | 9 ++++++++- tests/core/usage/CustomQuery.cpp | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/sqlpp11/insert_value_list.h b/include/sqlpp11/insert_value_list.h index 70934309..2a2f4a07 100644 --- a/include/sqlpp11/insert_value_list.h +++ b/include/sqlpp11/insert_value_list.h @@ -679,6 +679,13 @@ namespace sqlpp { return statement_t().dynamic_set(assignments...); } + + template + auto insert_columns(Columns... cols) + -> decltype(statement_t().columns(cols...)) + { + return statement_t().columns(cols...); + } } // namespace sqlpp #endif diff --git a/tests/core/serialize/CustomQuery.cpp b/tests/core/serialize/CustomQuery.cpp index f86a859d..5b2c5819 100644 --- a/tests/core/serialize/CustomQuery.cpp +++ b/tests/core/serialize/CustomQuery.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019, Roland Bock + * Copyright (c) 2016-2022, Roland Bock * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -80,5 +80,12 @@ int CustomQuery(int, char*[]) "SELECT 17 AS omega FROM tab_foo " "WHERE (NOT EXISTS(SELECT tab_foo.omega FROM tab_foo WHERE (tab_foo.omega=17)))"); + // A multi-row "insert or ignore" + auto batch = insert_columns(bar.beta, bar.gamma); + batch.values.add(bar.beta = "sample", bar.gamma = true); + batch.values.add(bar.beta = "ample", bar.gamma = false); + compare(__LINE__, custom_query(sqlpp::insert(), sqlpp::verbatim(" OR IGNORE"), into(bar), batch), + "INSERT OR IGNORE INTO tab_bar (beta,gamma) VALUES ('sample',1),('ample',0)"); + return 0; } diff --git a/tests/core/usage/CustomQuery.cpp b/tests/core/usage/CustomQuery.cpp index 35d2bc7f..1331ae5c 100644 --- a/tests/core/usage/CustomQuery.cpp +++ b/tests/core/usage/CustomQuery.cpp @@ -89,6 +89,12 @@ int CustomQuery(int, char*[]) db(custom_query(sqlpp::insert(), sqlpp::verbatim(" OR IGNORE"), into(t), insert_set(t.beta = "sample", t.gamma = true))); + // Create a custom mulit-row "insert or ignore" + auto batch = insert_columns(t.beta, t.gamma); + batch.values.add(t.beta = "sample", t.gamma = true); + batch.values.add(t.beta = "ample", t.gamma = false); + db(custom_query(sqlpp::insert(), sqlpp::verbatim(" OR IGNORE"), into(t), batch)); + // Create a MYSQL style custom "insert on duplicate update" db(custom_query(sqlpp::insert_into(t).set(t.beta = "sample", t.gamma = true), on_duplicate_key_update(db, t.beta = "sample")(db, t.gamma = false).get()));