Feature #9442
closedWt::Dbo::Query suggestions
100%
Description
Within one of our projects, we created a custom Query
object that's essentially a wrapper for Wt's Query
object with a few extra features that may be nice to have in Wt.
Templated join functions
We created templated (left) join functions so we don't have to write the table name each time, thus reducing the risk of typos
// Here Query<Result> is our custom object
template<typename TableName>
Query<Result>& join(const std::string& alias, const std::string& condition)
{
// query_ = Wt::Dbo::Query
query_.join(session().template tableNameQuoted<TableName>() + " as " + alias + " on " + condition);
return *this;
}
Nested queries
We also added (left) join functions that take a nested query as parameter. Within the project we sometimes have more complicated queries that need information from subqueries. We again opted here for a helper function so we don't have to write the entire subquery string.
template<typename T>
Query<Result>& join(const Query<T>& subQuery, const std::string& alias, const std::string& condition)
{
query_.join("(" + subQuery.dboString() + ") as " + alias + " on " + condition);
for (const Wt::cpp17::any& value : subQuery.bindValues()) {
bindAny(value);
}
return *this;
}
As you can see, here we needed to implement a few helper functions (dboString
and bindAny
) as well to be able to do this. The dboString
is based on the function that Wt internally uses for queries.
bindAny
relies on a few other helper functions as well. If you think this would be nice to have in Wt as well, contact me for more information.
Find alias
We created our own find
function which takes an alias. This is useful for us, as it will allow us to combine our find
function and join
functions. I believe currently in Wt, when you want to use Wt's join functions, you need to construct the Query object with Wt::Dbo::Session::query
, which requires the user to type the entire query string. Our helper function doesn't require us to do that, which again reduces the risk of typos in the database table name.
I see that Wt::Dbo::Session::find
already takes an optional string condition
parameter, so this might be slightly more complicated to have in Wt.
Updated by Roel Standaert almost 3 years ago
- Target version changed from future to 4.7.0
Updated by Korneel Dumon almost 3 years ago
- Status changed from New to InProgress
- Assignee set to Korneel Dumon
Updated by Korneel Dumon almost 3 years ago
- Status changed from InProgress to Review
- Assignee deleted (
Korneel Dumon)
Updated by Roel Standaert over 2 years ago
- Target version changed from 4.7.0 to 4.8.0
Updated by Roel Standaert over 2 years ago
- Status changed from Review to Implemented @Emweb
- % Done changed from 0 to 100
Updated by Roel Standaert over 2 years ago
- Status changed from Implemented @Emweb to Resolved
Updated by Roel Standaert over 2 years ago
- Status changed from Resolved to Closed