Dynamic statements.
Hi,
I'm trying to use the dynamic select statements like dynamic_where and dynamic_limit like that:
auto s = dynamic_select(m_db, all_of(entry)).from(entry).dynamic_where();
s.add_where(true);
but I got the following compilation error:
error: ‘struct sqlpp::statement_t<sqlpp::sqlite3::connection, sqlpp::no_with_t, sqlpp::select_t, sqlpp::no_select_flag_list_t, sqlpp::select_column_list_t<...>, sqlpp::from_t<void, sdn_sql::Entry>, sqlpp::no_extra_tables_t, sqlpp::where_t<sqlpp::sqlite3::connection>, sqlpp::no_group_by_t, sqlpp::no_having_t, sqlpp::no_order_by_t, sqlpp::no_limit_t, sqlpp::no_offset_t, sqlpp::no_union_t>’
has no member named ‘add_where’
My code seems similar to the one on the wiki. I search the code and it does not seem to have the add_where method. Did you change the usage of dynamic select statements?
Best regards, Simon
Ooops! Thanks for the hint! The documentation is outdated (I'll update asap).
Please try
s.where.add(true).
See also tests/Select.cpp
Updated the documentation.
I just found the test/Select.cpp and the solution you just pointed out. :) I also had problems with dynamic_offset and dynamic_limit but solved them using the new syntax:
s.offset.set(start)
s.limit.set(count)
However I still have problems with the order_by statement. My query looks like this:
auto s = dynamic_select(m_db, all_of(sql_entry))
.from(entry)
.order_by(entry.time.desc())
.dynamic_where().dynamic_offset().dynamic_limit();
Any idea on what I've done wrong?
Hum it compiles with the dynamic_order_by, I suppose it is not possible to use static order_by on a dynamic_select?
Thanks for the report. That's a bug, it also affects several other clauses.
Fixed in develop.
Oh great thanks. BTW it would be great to have a simple doc on dynamic insert. I managed to use them but had some trouble to find that I needed to use insert_list.add.
Good point :-)
In that context: It would be great if you could send a short description of how and why you use the dynamic versions of the queries.
Well it's pretty simple. I have a class Entry with some boost::optional data in it. I insert the Entry instances in a table but I want to insert the optional data only if they are present. It looks like that:
class Entry
{
//...
int64_t time;
boost::optional<std::string> data;
}
void insert(const Entry& entry)
{
auto i = dynamic_insert_into(m_db, sql_entry).dynamic_set(
sql_entry.time = entry.time
);
if (entry.data())
{
i.insert_list.add(sql_entry.data = entry.data().get());
}
m_db(i);
}
I also wanted to limit the number of entries in the table. Therefore I wanted to execute a request looking like that:
delete from entry
where id in (
select id from entry
order by entry.time asc
limit (
select case when count(*) - 800 > 0 then count(*) - 800 else 0 end from entry
)
);
However I did not find any IN operator. Is is possible to do something like that with sqlpp11?
(maybe I should have opened a new issue for that...)
Yes, please open an new issue about missing documentation. In short, you can do
tab.x.in(1,2,3)
tab.x.in(select(whatever))
tab.x.in(sqlpp::value_list(a_vector_of_values))
Fixed dynamic statement issues in release 0.35