Public Member Functions

Wt::Dbo::Query< Result > Class Template Reference
[Database Objects library (Dbo)]

A database query. More...

#include <Wt/DboQuery>

List of all members.

Public Member Functions

 ~Query ()
 Destructor.
template<typename T >
Query< Result > & bind (const T &value)
 Binds a value to the next positional marker.
Result resultValue () const
 Returns a unique result value.
collection< Result > resultList () const
 Returns a result list.
 operator Result () const
 Returns a unique result value.
 operator collection< Result > () const
 Returns a result list.

Detailed Description

template<class Result>
class Wt::Dbo::Query< Result >

A database query.

A query will be used to fetch objects of class Result.

Simple queries can be done using Session::find(), while more elaborate queries (with arbitrary result types) using Session::query().

You may insert positional holders for parameters using '?', using bind().

The query result may be fetched using resultValue() or resultList().

Usage example:

 typedef dbo::collection<dbo::ptr<Account> > Accounts;

 Query query = session.find<Account>("where balance > ?").bind(100000);
 Accounts accounts = query.resultList();

 for (Accounts::const_iterator i = accounts.begin(); i != accounts.end(); ++i)
   std::cerr << "Name: " << i->name << std::end;

Caveat warning!

A query object should be used transiently: a query can be used only once (one call to resultValue() or resultList()), after which it becomes unusable. You should not try to keep a query object around. This behavior was chosen because in this way, we do not need to store bound arguments and they are immediately passed on to the underlying prepared statement.


Member Function Documentation

template<class Result >
template<typename T >
Query< Result > & Wt::Dbo::Query< Result >::bind ( const T &  value )

Binds a value to the next positional marker.

This binds the value to the next positional marker. When this is the first bind() call, the statement is first prepared.

You should only bind arguments after the query is complete.

template<class Result >
Wt::Dbo::Query< Result >::operator collection< Result > (  ) const

Returns a result list.

This is a convenience conversion operator that calls resultList().

template<class Result >
Wt::Dbo::Query< Result >::operator Result (  ) const

Returns a unique result value.

This is a convenience conversion operator that calls resultValue().

template<class Result >
collection< Result > Wt::Dbo::Query< Result >::resultList (  ) const

Returns a result list.

This returns a collection which is backed by the underlying query. The query is not executed until this collection is traversed.

After a result has been fetched, the query can no longer be used.

template<class Result >
Result Wt::Dbo::Query< Result >::resultValue (  ) const

Returns a unique result value.

You can use this method if you are expecting the query to return at most one result. If the query returns more than one result a NoUniqueResultException is thrown.

After a result has been fetched, the query can no longer be used.


Generated on Sat Dec 4 2010 06:32:36 for Wt by doxygen 1.7.2