Shadowrun: Awakened 29 September 2011 - Build 871
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes
SraData::DbStream Class Reference

#include <DbStream.h>

List of all members.

Public Member Functions

 DbStream ()
bool getBool (int index)
int getInt (int index)
 ~DbStream ()

Static Public Member Functions

static bool CheckHasMoreRows (DbStream *stream, int *newId)
static DbStreamRetrieveStream (int *id)
static DbStreamStartStream (const std::string &query)

Static Public Attributes

static const int InitStreamId = 0

Protected Member Functions

bool checkHasMoreRows ()
bool execute (const std::string &query)

Static Protected Member Functions

static int GetStreamId ()

Protected Attributes

sql::Connection_connection
bool _firstRowFlag
int _id
std::auto_ptr< sql::ResultSet_result
std::auto_ptr< sql::Statement_stmt

Static Protected Attributes

static int _SerialNumber = 1
static ManagedDictionary< int,
DbStream
_Streams

Detailed Description

This class enables row-by-row streaming of results from the database It features static methods that manage a shared list of active streams This class will probably need thread-safety in the future This class may need some automatic garbage collection capability in the future to prevent leaking

The streaming code is intended to do the following (NOTE: A big part of this behavior depends on the DbStream implementation in SraData): 1) On first call, create a stream instance and call the SPROC 2a) If there no rows, then destroy the stream and immediately return from the function without setting the return values; QueryId will return InitStreamId 2b) If there is one row, then return that single set of values, then destroy the stream and return; QueryId will return InitStreamId 2c) If this is the first call for a query with more than one row, then return that single set of values, then store the DbStream in a static collection and return its unique ID for continuing in subsequent calls 2d) If this is not the first call for a query with more than one rw, then retrieve the existing query using the QueryId, set the current values, then return the current QueryId if there are more rows. If there are no more rows, then return InitStreamId

Definition at line 41 of file DbStream.h.


Constructor & Destructor Documentation

SraData::DbStream::DbStream ( )
SraData::DbStream::~DbStream ( )

Destructor This reclaims all resources; most of them implicitly through the fields that are auto_ptr

Definition at line 132 of file DbStream.cpp.

References _connection, _stmt, Singleton< DbConnectionFactory >::GetSingleton(), and SraData::DbConnectionFactory::recycleConnection().

    {
        //have to do this for the API
        while(_stmt->getMoreResults()) { }
        SraData::DbConnectionFactory::GetSingleton().recycleConnection(_connection);
    }

Member Function Documentation

bool SraData::DbStream::checkHasMoreRows ( ) [protected]

Increments to the next row and returns true if there are more rows; otherwise, returns false

Definition at line 46 of file DbStream.cpp.

References _result.

Referenced by CheckHasMoreRows().

    {
        //normally, next returns true if there is another row
        return _result->next();
    }
bool SraData::DbStream::CheckHasMoreRows ( DbStream stream,
int *  newId 
) [static]

Checks if the given stream has more rows, returning true if it does, otherwise false If this returns false, then it has also cleaned up the resources associated with the stream

Definition at line 95 of file DbStream.cpp.

References _firstRowFlag, _id, _Streams, checkHasMoreRows(), GetStreamId(), Dictionary< Key, Value >::remove(), and stream.

    {
        //if the stream has more rows in it, then we must return true and ensure a valid ID
        if(stream->checkHasMoreRows())
        {
            //if this is the first row and we have more rows, then save it in the 
            if(stream->_firstRowFlag)
            {
                *newId = GetStreamId();
                stream->_id = *newId;
                _Streams[*newId] = stream;
            }

            stream->_firstRowFlag = false;
            return true;
        }
        else
        {
            _Streams.remove(stream->_id);
            delete stream;
            return false;
        }
    }
bool SraData::DbStream::execute ( const std::string &  query) [protected]

Performs the initial query and loads the stream with the result returns true if there are rows to be retrieved; otherwise returns false

Definition at line 143 of file DbStream.cpp.

References _result, and _stmt.

Referenced by StartStream().

    {
        _stmt->execute(query);
        _result.reset(_stmt->getResultSet());
        return _result->next();
    }
bool SraData::DbStream::getBool ( int  index)

Returns bool results from the current row

Definition at line 161 of file DbStream.cpp.

References _result.

    {
        return _result->getBoolean(index);
    }
int SraData::DbStream::getInt ( int  index)

Returns int results from the current row

Definition at line 153 of file DbStream.cpp.

References _result.

    {
        return _result->getInt(index);
    }
int SraData::DbStream::GetStreamId ( ) [static, protected]

Returns a new, unique stream ID NOTE: This will never return the sacred InitStreamId

Definition at line 33 of file DbStream.cpp.

References _SerialNumber, and InitStreamId.

Referenced by CheckHasMoreRows().

    {
        if(_SerialNumber != InitStreamId)
            return _SerialNumber++;
        else
            return ++_SerialNumber;
    }
DbStream * SraData::DbStream::RetrieveStream ( int *  id) [static]

Retrieves a stream from the manager NOTE: If no stream is found, then this returns NULL and sets the ID to InitStreamId

Definition at line 79 of file DbStream.cpp.

References _Streams, Dictionary< Key, Value >::getSafe(), InitStreamId, and stream.

    {
        //the ID may not be valid, in that case we must check it
        //if it isn't in there, then we reset the stream ID
        //and return NULL
        DbStream* stream = _Streams.getSafe(*id);
        if(stream == NULL)
            *id = InitStreamId;

        return stream;
    }
DbStream * SraData::DbStream::StartStream ( const std::string &  query) [static]

Creates a new stream instance, executing the given query and returns its new ID

Definition at line 55 of file DbStream.cpp.

References DbStream(), and execute().

    {
        //create a new instance
        DbStream* newInstance = new DbStream();

        //execute the query
        //if it returns true, then there are rows in it and we can proceed with using it
        if(newInstance->execute(query))
        {
            //assign an ID to it, add it ot the collection, and return it
            return newInstance;
        }
        else
        {
            //if there are no rows, then we must immediately destroy it and return InitStreamId
            delete newInstance;
            return NULL;
        }
    }

Member Data Documentation

Definition at line 45 of file DbStream.h.

Referenced by DbStream(), and ~DbStream().

Definition at line 49 of file DbStream.h.

Referenced by CheckHasMoreRows().

int SraData::DbStream::_id [protected]

Definition at line 48 of file DbStream.h.

Referenced by CheckHasMoreRows().

std::auto_ptr<sql::ResultSet> SraData::DbStream::_result [protected]

Definition at line 47 of file DbStream.h.

Referenced by checkHasMoreRows(), execute(), getBool(), and getInt().

int SraData::DbStream::_SerialNumber = 1 [static, protected]

Definition at line 53 of file DbStream.h.

Referenced by GetStreamId().

std::auto_ptr<sql::Statement> SraData::DbStream::_stmt [protected]

Definition at line 46 of file DbStream.h.

Referenced by DbStream(), execute(), and ~DbStream().

Definition at line 52 of file DbStream.h.

Referenced by CheckHasMoreRows(), and RetrieveStream().

const int SraData::DbStream::InitStreamId = 0 [static]

All initial calls to streams should prime the ID with this value When a stream is out of rows, the DbStream will also set its queryID to this value

Definition at line 81 of file DbStream.h.

Referenced by GetStreamId(), main(), and RetrieveStream().


The documentation for this class was generated from the following files:

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net