Wt::Dbo::collection< C > Class Template Reference
[Database Objects library (Dbo)]

An STL container for iterating query results. More...

#include <Wt/Dbo/collection>

List of all members.

Public Types

typedef C value_type
 Value type.

Public Member Functions

 collection ()
 Default constructor.
 ~collection ()
 Destructor.
iterator begin ()
 Returns an iterator to the begin of the collection.
iterator end ()
 Returns an iterator to the end of the collection.
const_iterator begin () const
 Returns a const iterator to the begin of the collection.
const_iterator end () const
 Returns a const iterator to the end of the collection.
size_type size () const
 Returns the size.
void insert (C c)
 Inserts an object.
void erase (C c)
 Removes an object.
Sessionsession () const
 Returns the session to which this collection is bound.

Classes

class  const_iterator
 Const Iterator. More...
class  iterator
 Iterator. More...


Detailed Description

template<class C>
class Wt::Dbo::collection< C >

An STL container for iterating query results.

This is an STL-compatible container that is backed by an SQL query for fetching data. Its iterators implement the InputIterator requirements, which mean that you can only iterate its results once.

The container is read only, unless it is being used as a member of a dbo to manage a Many-to-Many relation. In that case, you may also insert() and erase() may also be used.

The collection should be used only for storing ptr<C> values: collection< ptr<C> >.

You will typically iterate the container results for local processing, or copy the results into a standard STL container for extended processing. The reason is that the container uses a non-reentrant sql statement: only one collection, which is backed by the same SQL statement may be used at once per session. Thus, the following will fail:

 void iterateChildren(Wt::Dbo::ptr<Comment> comment)
 {
     typedef Wt::Dbo::collection<Wt::Dbo::ptr<Comment> > Comments;
     Comments children = comment->children;

     for (Comments::const_iterator i = children.begin(); i != children.end(); ++i) {
        std::cerr << "Comment: " << i->text << std::endl;
        iterateChildren(*i); // Illegal since will result in nested use of the same query.
     }
 }

If you cannot gaurantee that during its iteration the same query will be reused, you should copy the results in a standard container. Note that this is no big overhead since dbo pointers are lightweight.

 void iterateChildren(Wt::Dbo::ptr<Comment> comment)
 {
     typedef std::vector<Wt::Dbo::ptr<Comment> > Comments;

     Comments children(comment->children.begin(), comment->children.end()); // copy into an STL container, freeing the underlying query for reuse 

     for (Comments::const_iterator i = children.begin(); i != children.end(); ++i) {
        std::cerr << "Comment: " << i->text << std::endl;
        iterateChildren(*i); // Okay now.
     }
 }

Before iterating a collection, the session is flushed. In this way, the collection will reflect any pending dirty changes.


Constructor & Destructor Documentation

template<class C>
Wt::Dbo::collection< C >::collection (  )  [inline]

Default constructor.

Constructs an empty collection that is not bound to a database session or query.


Member Function Documentation

template<class C>
collection< C >::size_type Wt::Dbo::collection< C >::size (  )  const [inline]

Returns the size.

This will execute an SQL count(*) statement to fetch the size of the collection without fetching all results.

template<class C>
void Wt::Dbo::collection< C >::insert ( c  )  [inline]

Inserts an object.

This is only useful for a collection that implements one side of a ManyToMany relation.

template<class C>
void Wt::Dbo::collection< C >::erase ( c  )  [inline]

Removes an object.

This is only useful for a collection that implements one side of a ManyToMany relation.


Generated on Fri Mar 26 17:12:08 2010 for Wt by doxygen 1.5.6