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

Allow INSERT INTO from SELECT

This commit is contained in:
rbock 2019-04-11 11:30:25 +02:00
parent b27406a87e
commit c729387ab6
3 changed files with 35 additions and 17 deletions

View File

@ -627,17 +627,17 @@ namespace sqlpp
context << " (";
interpret_tuple(t._columns, ",", context);
context << ")";
context << " VALUES ";
bool first = true;
for (const auto& row : t._insert_values)
{
if (not first)
if (first)
{
context << ',';
context << " VALUES ";
first = false;
}
else
{
first = false;
context << ',';
}
context << '(';
interpret_tuple(row, ",", context);
@ -669,7 +669,10 @@ namespace sqlpp
context << ',';
}
interpret_list(t._dynamic_columns, ',', context);
context << ") VALUES(";
context << ")";
if (sizeof...(Assignments) or not t._dynamic_values.empty())
{
context << " VALUES(";
interpret_tuple(t._values, ",", context);
if (sizeof...(Assignments) and not t._dynamic_values.empty())
{
@ -678,6 +681,7 @@ namespace sqlpp
interpret_list(t._dynamic_values, ',', context);
context << ")";
}
}
return context;
}
};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016, Roland Bock
* Copyright (c) 2016-2019, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -31,14 +31,15 @@
SQLPP_ALIAS_PROVIDER(pragma)
int CustomQuery(int, char* [])
int CustomQuery(int, char*[])
{
const auto foo = test::TabFoo{};
const auto bar = test::TabBar{};
auto db = MockDb{};
// Unconditionally
compare(__LINE__, custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega), from(foo),
compare(__LINE__,
custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega), from(foo),
sqlpp::unconditionally()),
"SELECT DISTINCT tab_foo.omega FROM tab_foo ");
@ -68,5 +69,16 @@ int CustomQuery(int, char* [])
custom_query(sqlpp::verbatim("PRAGMA user_version")).with_result_type_of(select(sqlpp::value(1).as(pragma))),
" PRAGMA user_version");
// An insert from select for postgresql
const auto x = 17;
compare(__LINE__,
custom_query(insert_into(foo).columns(foo.omega),
select(sqlpp::value(x).as(foo.omega))
.from(foo)
.where(not exists(select(foo.omega).from(foo).where(foo.omega == x)))),
"INSERT INTO tab_foo (omega) "
"SELECT 17 AS omega FROM tab_foo "
"WHERE (NOT EXISTS(SELECT tab_foo.omega FROM tab_foo WHERE (tab_foo.omega=17)))");
return 0;
}

View File

@ -66,9 +66,11 @@ int Insert(int, char*[])
multi_insert.values.add(t.gamma = true, t.beta = "cheesecake", t.delta = 1);
multi_insert.values.add(t.gamma = sqlpp::default_value, t.beta = sqlpp::default_value,
t.delta = sqlpp::default_value);
multi_insert.values.add(t.gamma = sqlpp::value_or_null(true),
t.beta = sqlpp::value_or_null("pie"),
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::integer>(sqlpp::null));
printer.reset();
std::cerr << serialize(multi_insert, printer).str() << std::endl;
auto i = dynamic_insert_into(db, t).dynamic_set();
i.insert_list.add(t.beta = "kirschauflauf");
printer.reset();