Add standalone insert_columns function (#76)

This allows for multi-row INSERT OR IGNORE
This commit is contained in:
Roland Bock
2022-06-06 10:15:28 +02:00
parent 256429730a
commit 57077cd32b
3 changed files with 21 additions and 1 deletions
+7
View File
@@ -679,6 +679,13 @@ namespace sqlpp
{
return statement_t<Database, no_insert_value_list_t>().dynamic_set(assignments...);
}
template <typename... Columns>
auto insert_columns(Columns... cols)
-> decltype(statement_t<void, no_insert_value_list_t>().columns(cols...))
{
return statement_t<void, no_insert_value_list_t>().columns(cols...);
}
} // namespace sqlpp
#endif
+8 -1
View File
@@ -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;
}
+6
View File
@@ -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()));