Wt 3.1.10
Public Member Functions | Protected Member Functions
Wt::Dbo::QueryModel< Result > Class Template Reference

A Wt MVC Model to view/edit query results. More...

#include <Wt/Dbo/QueryModel>

Inheritance diagram for Wt::Dbo::QueryModel< Result >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 QueryModel (WObject *parent=0)
 Creates a new query model.
void setQuery (const Query< Result > &query, bool keepColumns=false)
 Sets the query.
const Query< Result > & query () const
 Returns the query.
int addColumn (const std::string &field, const WString &header, WFlags< ItemFlag > flags=ItemIsSelectable)
 Adds a column.
int addColumn (const std::string &field, WFlags< ItemFlag > flags=ItemIsSelectable)
 Adds a column.
void setColumnFlags (int column, WFlags< ItemFlag > flags)
 Sets column item flags.
WFlags< ItemFlagcolumnFlags (int column) const
 Returns column item flags.
void addAllFieldsAsColumns ()
 Adds all the columns from the field list.
const Result & resultRow (int row) const
 Returns a result row.
Result & resultRow (int row)
 Returns a result row.
void reload ()
 Rereads the data from the database.
void setBatchSize (int count)
 Sets the batch size for fetching results.
int batchSize () const
 Returns the batch size for fetching results.
const std::vector< FieldInfo > & fields () const
 Returns the query field list.
const FieldInfofieldInfo (int column)
 Returns the FieldInfo structure for a column.
const std::string & fieldName (int column)
 Returns the field name for the a column.
virtual int columnCount (const WModelIndex &parent=WModelIndex()) const
 Returns the number of columns.
virtual int rowCount (const WModelIndex &parent=WModelIndex()) const
 Returns the number of rows.
virtual WFlags< ItemFlagflags (const WModelIndex &index) const
 Returns the flags for an item.
virtual boost::any data (const WModelIndex &index, int role=DisplayRole) const
 Returns the data for an item.
virtual bool setData (const WModelIndex &index, const boost::any &value, int role=EditRole)
 Sets data at the given model index.
virtual void sort (int column, SortOrder order=AscendingOrder)
 Sorts the model according to a particular column.
virtual bool insertRows (int row, int count, const WModelIndex &parent=WModelIndex())
 Inserts one or more rows.
virtual bool removeRows (int row, int count, const WModelIndex &parent=WModelIndex())
 Removes one or more rows.
virtual bool setHeaderData (int column, Orientation orientation, const boost::any &value, int role=EditRole)
 Sets header data for a column.
virtual boost::any headerData (int section, Orientation orientation=Horizontal, int role=DisplayRole) const
 Returns header data.

Protected Member Functions

virtual Result createRow ()
 Creates a new row.
virtual void addRow (Result &result)
 Adds a row to the session.
virtual void deleteRow (Result &result)
 Deletes a row from the session.

Detailed Description

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

A Wt MVC Model to view/edit query results.

The model fetches results from the query and presents the data in a table. It supports sorting the underlying SQL query using Query::orderBy().

The default implementation of data() converts Query results to model data using query_result_traits<Result>::getValues(). You may define your own data presentation using the underlying Result by specializing data() and accessing data from resultRow().

You may selectively add fields that you want to display using addColumn(), or you can also add all columns based on the query using addAllFieldsAsColumns().

The model supports editing of the underlying data (even if the underlying query fetches results from multiple tables!). Values in columns that correspond to fields that have been mapped (and are writable) in a Database Object can be edited. The default implementation of setData() uses query_result_traits<Result>::setValue() to manipulate the database object, and thus uses the same write-behind properties as ptr<C>::modify(). To customize editing, you can specialize setData() and use resultRow() to modify the result object directly.

The model supports also inserting rows (only at the end), and removing rows, which are reflected in object additions and removals from the Session.

Editing is directly to the underlying database objects (change, insert and remove). Note that these changes will be flushed to the database whenever a transaction is committed, or before a query is run. The model will not explicitly create a transaction for the modification, but since the model uses a query for reading data, the change may be committed to the database depending on how the model is loading data. Still, this implies that usually inserting a row and setting its data happens within a single SQL "insert" statement.


Constructor & Destructor Documentation

template<class Result >
Wt::Dbo::QueryModel< Result >::QueryModel ( WObject parent = 0)

Creates a new query model.

You need to seed the model with a query using setQuery().


Member Function Documentation

template<class Result >
void Wt::Dbo::QueryModel< Result >::addAllFieldsAsColumns ( )

Adds all the columns from the field list.

All fields are added as columns. Fields that are mutable are marked as editable columns.

This is a convenient alternative to selectively adding columns using addColumn().

See also:
fields()
template<class Result >
int Wt::Dbo::QueryModel< Result >::addColumn ( const std::string &  field,
const WString header,
WFlags< ItemFlag flags = ItemIsSelectable 
)

Adds a column.

The field name may be a qualified or unqualified field name. The list of available fields can be inspected using fields().

The header is used as Wt::DisplayRole for the column header data.

For the column items, flags() will returned the given flags. For example, to indicate that a field is editable, you can set the Wt::ItemIsEditable flag.

See also:
fields()
template<class Result >
int Wt::Dbo::QueryModel< Result >::addColumn ( const std::string &  field,
WFlags< ItemFlag flags = ItemIsSelectable 
)

Adds a column.

This is an overloaded function for convenience, which uses the field name as the header value.

See also:
setHeaderData()
template<class Result >
void Wt::Dbo::QueryModel< Result >::addRow ( Result &  result) [protected, virtual]

Adds a row to the session.

This method is called from within insertRows() to add (and save) a new result row to the Dbo session.

The default implementation uses query_result_traits<Result>::add().

template<class Result >
int Wt::Dbo::QueryModel< Result >::batchSize ( ) const

Returns the batch size for fetching results.

See also:
setBatchSize()
template<class Result >
int Wt::Dbo::QueryModel< Result >::columnCount ( const WModelIndex parent = WModelIndex()) const [virtual]

Returns the number of columns.

Returns the number of columns that have been added using addColumn() or addAllFieldsAsColumns().

Since the query model implements a flat table model, this returns 0 when parent is valid.

Implements Wt::WAbstractItemModel.

template<class Result >
WFlags< ItemFlag > Wt::Dbo::QueryModel< Result >::columnFlags ( int  column) const

Returns column item flags.

See also:
setColumnFlags()
template<class Result >
Result Wt::Dbo::QueryModel< Result >::createRow ( ) [protected, virtual]

Creates a new row.

This method is called from within insertRows() to create a new row.

The default implementation uses query_result_traits<Result>::create().

template<class Result >
boost::any Wt::Dbo::QueryModel< Result >::data ( const WModelIndex index,
int  role = DisplayRole 
) const [virtual]

Returns the data for an item.

Returns data of type Wt::DisplayRole or Wt::EditRole based on the field value corresponding to the index. If necessary, this fetches a batch of results from the underlying database.

Implements Wt::WAbstractItemModel.

template<class Result >
void Wt::Dbo::QueryModel< Result >::deleteRow ( Result &  result) [protected, virtual]

Deletes a row from the session.

This method is called from within removeRows() to remove (and delete) a new result row from the Dbo session.

The default implementation uses query_result_traits<Result>::remove().

template<class Result >
const std::vector< FieldInfo > & Wt::Dbo::QueryModel< Result >::fields ( ) const

Returns the query field list.

This returns the field list from the underlying query.

template<class Result >
WFlags< ItemFlag > Wt::Dbo::QueryModel< Result >::flags ( const WModelIndex index) const [virtual]

Returns the flags for an item.

Returns the flags set for the column using setColumnFlags().

Reimplemented from Wt::WAbstractItemModel.

template<class Result >
boost::any Wt::Dbo::QueryModel< Result >::headerData ( int  section,
Orientation  orientation = Horizontal,
int  role = DisplayRole 
) const [virtual]

Returns header data.

See also:
setHeaderData()

Reimplemented from Wt::WAbstractItemModel.

template<class Result >
bool Wt::Dbo::QueryModel< Result >::insertRows ( int  row,
int  count,
const WModelIndex parent = WModelIndex() 
) [virtual]

Inserts one or more rows.

Row insertions are only supported at the end (row == rowCount()). For each added row, a new result is added to the underlying database.

Reimplemented from Wt::WAbstractItemModel.

template<class Result >
const Query<Result>& Wt::Dbo::QueryModel< Result >::query ( ) const

Returns the query.

See also:
setQuery()
template<class Result >
void Wt::Dbo::QueryModel< Result >::reload ( )

Rereads the data from the database.

This invalidates the current (cached) data and informs views that they should rerender.

The model keeps the following data cached:

template<class Result >
bool Wt::Dbo::QueryModel< Result >::removeRows ( int  row,
int  count,
const WModelIndex parent = WModelIndex() 
) [virtual]

Removes one or more rows.

For each removed row, the result is removed from the underlying database.

Reimplemented from Wt::WAbstractItemModel.

template<class Result >
const Result & Wt::Dbo::QueryModel< Result >::resultRow ( int  row) const

Returns a result row.

This returns the result corresponding to a particular row, and could be used to customize the model behaviour, e.g. by specializing data() for certain columns.

Returns a const reference to an entry in the result cache.

template<class Result >
Result & Wt::Dbo::QueryModel< Result >::resultRow ( int  row)

Returns a result row.

This returns the result corresponding to a particular row, and could be used to customize the model behaviour, e.g. by specializing setData() for certain columns.

Returns a reference to an entry in the result cache.

See also:
resultRow(int row) const
template<class Result >
int Wt::Dbo::QueryModel< Result >::rowCount ( const WModelIndex parent = WModelIndex()) const [virtual]

Returns the number of rows.

Returns the number of rows return from the underlying query.

Since the query model implements a flat table model, this returns 0 when parent is valid.

Implements Wt::WAbstractItemModel.

template<class Result >
void Wt::Dbo::QueryModel< Result >::setBatchSize ( int  count)

Sets the batch size for fetching results.

The model fetches results from the query in batch, and caches these in memory to avoid repetitive querying of the database.

The default batch size is 40.

template<class Result >
void Wt::Dbo::QueryModel< Result >::setColumnFlags ( int  column,
WFlags< ItemFlag flags 
)

Sets column item flags.

For items in column column, flags() will returned the given flags. For example, to indicate that a field is editable, you can set the Wt::ItemIsEditable flag.

template<class Result >
bool Wt::Dbo::QueryModel< Result >::setData ( const WModelIndex index,
const boost::any &  value,
int  role = EditRole 
) [virtual]

Sets data at the given model index.

If role = Wt::EditRole, sets the value for the field corresponding to the index. All other editing is ignored.

See also:
setCachedResult()

Reimplemented from Wt::WAbstractItemModel.

template<class Result >
bool Wt::Dbo::QueryModel< Result >::setHeaderData ( int  column,
Orientation  orientation,
const boost::any &  value,
int  role = EditRole 
) [virtual]

Sets header data for a column.

The model will return this data in headerData(). Only column headers are supported (orientation == Wt::Horizontal).

Reimplemented from Wt::WAbstractItemModel.

template<class Result >
void Wt::Dbo::QueryModel< Result >::setQuery ( const Query< Result > &  query,
bool  keepColumns = false 
)

Sets the query.

The query is used to query the database.

Unless keepColumns is true, this resets the column list, so you will need to (re-)add one or more columns using addColumn().

When keeping the current columns, a LayoutChange rather than a Reset is emitted by the model, allowing views to keep their column geometry as well.

template<class Result >
void Wt::Dbo::QueryModel< Result >::sort ( int  column,
SortOrder  order = AscendingOrder 
) [virtual]

Sorts the model according to a particular column.

This sorts the model by changing the query using Query<BindStrategy>::orderBy().

Reimplemented from Wt::WAbstractItemModel.

 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator

Generated on Wed Jul 27 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.4