Logo Search packages:      
Sourcecode: webissues version File versions  Download package

tableiterators.h

/**************************************************************************
* Generic, native, relational database
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007 WebIssues Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#ifndef TABLEITERATORS_H
#define TABLEITERATORS_H

#include "iteratorcore.h"
#include "tableindexes.h"

namespace RDB
{

/**
* General iterator for all rows in an index.
*
* Use this iterator to iterate over all rows of a table of an unknown type.
*
* @see IndexIterator
*/
00028 class IndexIteratorBase
{
public:
    /**
    * Constructor.
    */
00034     IndexIteratorBase( UniqueIndexBase* index ) : m_core( index->data() ) { }

    /**
    * Constructor.
    */
00039     IndexIteratorBase( ForeignIndexBase* index ) : m_core( index->data() ) { }

    /**
    * Destructor.
    */
00044     ~IndexIteratorBase() { }

public:
    /**
    * Advance to the next row.
    *
    * @return @c true if successful or @c false if there are no more rows.
    */
00052     bool next() { return m_core.next(); }

    /**
    * Return the current row.
    */
00057     Row* get() const { return m_core.get(); }

protected:
    IndexIteratorCore m_core;
};

/**
* Type-safe iterator for all rows in an index.
*
* Use this iterator to iterate over all rows of a table.
*
* @code
* IndexIterator<MyRow> it( table.index() );
* while ( it.next() ) {
*     MyRow* row = it.get();
*     // ...
* }
* @endcode
*
* @see IndexConstIterator
*/
template<class ROW>
00079 class IndexIterator : public IndexIteratorBase
{
public:
    /**
    * Constructor.
    */
00085     IndexIterator( UniqueIndex<ROW>* index ) : IndexIteratorBase( index ) { }

    /**
    * Constructor.
    */
00090     IndexIterator( ForeignIndex<ROW>* index ) : IndexIteratorBase( index ) { }

    /**
    * Destructor.
    */
00095     ~IndexIterator() { }

public:
00098     ROW* get() const { return (ROW*)m_core.get(); }
};

/**
* Iterator for all rows in a table.
*
* This is a convenience subclass of IndexIterator for initializing it with a table.
*/
template<class ROW>
00107 class TableIterator : public IndexIterator<ROW>
{
public:
    /**
    * Constructor.
    */
    template<class TABLE>
00114     TableIterator( TABLE* table ) : IndexIterator<ROW>( table->index() ) { }

    /**
    * Destructor.
    */
00119     ~TableIterator() { }
};

/**
* General constant iterator for all rows in an index.
*
* Use this iterator to iterate over all rows of a constant table of an unknown type.
*
* @see IndexConstIterator
*/
00129 class IndexConstIteratorBase
{
public:
    /**
    * Constructor.
    */
00135     IndexConstIteratorBase( const UniqueIndexBase* index ) : m_core( index->data() ) { }

    /**
    * Constructor.
    */
00140     IndexConstIteratorBase( const ForeignIndexBase* index ) : m_core( index->data() ) { }

    /**
    * Destructor.
    */
00145     ~IndexConstIteratorBase() { }

public:
    /**
    * Advance to the next row.
    *
    * @return @c true if successful or @c false if there are no more rows.
    */
00153     bool next() { return m_core.next(); }

    /**
    * Return the current row.
    */
00158     const Row* get() const { return m_core.get(); }

protected:
    IndexIteratorCore m_core;
};

/**
* Type-safe iterator for all rows in an index.
*
* Use this iterator to iterate over all rows of a constant table.
*
* @code
* IndexConstIterator<MyRow> it( table.index() );
* while ( it.next() ) {
*     const MyRow* row = it.get();
*     // ...
* }
* @endcode
*
* @see IndexIterator
*/
template<class ROW>
00180 class IndexConstIterator : public IndexConstIteratorBase
{
public:
    /**
    * Constructor.
    */
00186     IndexConstIterator( const UniqueIndex<ROW>* index ) : IndexConstIteratorBase( index ) { }

    /**
    * Constructor.
    */
00191     IndexConstIterator( const ForeignIndex<ROW>* index ) : IndexConstIteratorBase( index ) { }

    /**
    * Destructor.
    */
00196     ~IndexConstIterator() { }

public:
00199     const ROW* get() const { return (const ROW*)m_core.get(); }
};

/**
* Iterator for all rows in a table.
*
* This is a convenience subclass of IndexConstIterator for initializing it with a table.
*/
template<class ROW>
00208 class TableConstIterator : public IndexConstIterator<ROW>
{
public:
    /**
    * Constructor.
    */
    template<class TABLE>
00215     TableConstIterator( const TABLE* table ) : IndexConstIterator<ROW>( table->index() ) { }

    /**
    * Destructor.
    */
00220     ~TableConstIterator() { }
};

/**
* General iterator for rows matching a foreign key.
*
* Use this iterator to iterate over rows of a table of an unknown type.
*
* @see ForeignIterator
*/
00230 class ForeignIteratorBase
{
public:
    /**
    * Constructor.
    */
00236     ForeignIteratorBase( ForeignIndexBase* index, int key ) : m_core( index->data(), key ) { }

    /**
    * Destructor.
    */
00241     ~ForeignIteratorBase() { }

public:
    /**
    * Advance to the next row.
    *
    * @return @c true if successful or @c false if there are no more rows.
    */
00249     bool next() { return m_core.next(); }

    /**
    * Return the current row.
    */
00254     Row* get() const { return m_core.get(); }

protected:
    ForeignIteratorCore m_core;
};

/**
* Type-safe iterator for rows matching a foreign key.
*
* Use this iterator to find all rows with the given value of a foreign key in a table.
*
* @code
* ForeignIterator<MyRow> it( table.parentIndex(), 100 );
* while ( it.next() ) {
*     MyRow* row = it.get();
*     // ...
* }
* @endcode
*
* @see ForeignConstIterator
*/
template<class ROW>
00276 class ForeignIterator : public ForeignIteratorBase
{
public:
    /**
    * Constructor.
    */
00282     ForeignIterator( ForeignIndex<ROW>* index, int key ) : ForeignIteratorBase( index, key ) { }

    /**
    * Destructor.
    */
00287     ~ForeignIterator() { }

public:
00290     ROW* get() const { return (ROW*)m_core.get(); }
};

/**
* General const iterator for rows matching a foreign key.
*
* Use this iterator to iterate over rows of a constant table of an unknown type.
*
* @see ForeignConstIterator
*/
00300 class ForeignConstIteratorBase
{
public:
    /**
    * Constructor.
    */
00306     ForeignConstIteratorBase( const ForeignIndexBase* index, int key ) : m_core( index->data(), key ) { }

    /**
    * Destructor.
    */
00311     ~ForeignConstIteratorBase() { }

public:
    /**
    * Advance to the next row.
    *
    * @return @c true if successful or @c false if there are no more rows.
    */
00319     bool next() { return m_core.next(); }

    /**
    * Return the current row.
    */
00324     const Row* get() const { return m_core.get(); }

protected:
    ForeignIteratorCore m_core;
};

/**
* Type-safe const iterator for rows matching a foreign key.
*
* Use this iterator to find all rows with the given value of a foreign key in a constant table.
*
* @code
* ForeignConstIterator<MyRow> it( table.parentIndex(), 100 );
* while ( it.next() ) {
*     const MyRow* row = it.get();
*     // ...
* }
* @endcode
*
* @see ForeignIterator
*/
template<class ROW>
00346 class ForeignConstIterator : public ForeignConstIteratorBase
{
public:
    /**
    * Constructor.
    */
00352     ForeignConstIterator( const ForeignIndex<ROW>* index, int key ) : ForeignConstIteratorBase( index, key ) { }

    /**
    * Destructor.
    */
00357     ~ForeignConstIterator() { }

public:
00360     const ROW* get() const { return (const ROW*)m_core.get(); }
};

}

#endif

Generated by  Doxygen 1.6.0   Back to index