Project

General

Profile

Feature #9442

Wt::Dbo::Query suggestions

Added by Yoika Ghysens about 1 month ago. Updated about 1 month ago.

Status:
New
Priority:
Normal
Assignee:
-
Target version:
Start date:
12/03/2021
Due date:
% Done:

0%

Estimated time:

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.

#1

Updated by Yoika Ghysens about 1 month ago

  • Target version set to future
#2

Updated by Roel Standaert about 1 month ago

  • Target version changed from future to 4.7.0

Also available in: Atom PDF