Shadowrun: Awakened 29 September 2011 - Build 871
DbStream.cpp
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 
00003 #include <stdint.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <iostream>
00007 #include <map>
00008 #include <memory>
00009 
00010 #include <cppconn/driver.h>
00011 #include <cppconn/exception.h>
00012 #include <cppconn/resultset.h>
00013 #include <cppconn/statement.h>
00014 #include <cppconn/prepared_statement.h>
00015 
00016 #include "ManagedDictionary.h"
00017 #include "DbConnectionFactory.h"
00018 #include "DbStream.h"
00019 
00020 //http://dev.mysql.com/doc/refman/5.1/en/connector-net-tutorials-stored-routines-statements.html
00021 
00022 namespace SraData
00023 {
00024     //This managed dictionary is set into auto-collection mode, meaning all streams are deleted when the Streams collection is destroyed
00025     //While this will only be called at the end of the program, this should prevent possible resource leakage in the MySQL server
00026     ManagedDictionary<int, DbStream> DbStream::_Streams(true);
00027     int DbStream::_SerialNumber = 1;
00028 
00033     int DbStream::GetStreamId()
00034     {
00035         if(_SerialNumber != InitStreamId)
00036             return _SerialNumber++;
00037         else
00038             return ++_SerialNumber;
00039     }
00040 
00041     //protected instance methods
00042 
00046     bool DbStream::checkHasMoreRows()
00047     {
00048         //normally, next returns true if there is another row
00049         return _result->next();
00050     }
00051 
00055     DbStream* DbStream::StartStream(const std::string& query)
00056     {
00057         //create a new instance
00058         DbStream* newInstance = new DbStream();
00059 
00060         //execute the query
00061         //if it returns true, then there are rows in it and we can proceed with using it
00062         if(newInstance->execute(query))
00063         {
00064             //assign an ID to it, add it ot the collection, and return it
00065             return newInstance;
00066         }
00067         else
00068         {
00069             //if there are no rows, then we must immediately destroy it and return InitStreamId
00070             delete newInstance;
00071             return NULL;
00072         }
00073     }
00074 
00079     DbStream* DbStream::RetrieveStream(int* id)
00080     {
00081         //the ID may not be valid, in that case we must check it
00082         //if it isn't in there, then we reset the stream ID
00083         //and return NULL
00084         DbStream* stream = _Streams.getSafe(*id);
00085         if(stream == NULL)
00086             *id = InitStreamId;
00087 
00088         return stream;
00089     }
00090 
00095     bool DbStream::CheckHasMoreRows(DbStream* stream, int* newId)
00096     {
00097         //if the stream has more rows in it, then we must return true and ensure a valid ID
00098         if(stream->checkHasMoreRows())
00099         {
00100             //if this is the first row and we have more rows, then save it in the 
00101             if(stream->_firstRowFlag)
00102             {
00103                 *newId = GetStreamId();
00104                 stream->_id = *newId;
00105                 _Streams[*newId] = stream;
00106             }
00107 
00108             stream->_firstRowFlag = false;
00109             return true;
00110         }
00111         else
00112         {
00113             _Streams.remove(stream->_id);
00114             delete stream;
00115             return false;
00116         }
00117     }
00118 
00122     DbStream::DbStream() : _firstRowFlag(true)
00123     {
00124         _connection = SraData::DbConnectionFactory::GetSingleton().getConnection();
00125         _stmt.reset(_connection->createStatement());
00126     }
00127 
00132     DbStream::~DbStream()
00133     {
00134         //have to do this for the API
00135         while(_stmt->getMoreResults()) { }
00136         SraData::DbConnectionFactory::GetSingleton().recycleConnection(_connection);
00137     }
00138 
00143     bool DbStream::execute(const std::string& query)
00144     {
00145         _stmt->execute(query);
00146         _result.reset(_stmt->getResultSet());
00147         return _result->next();
00148     }
00149 
00153     int DbStream::getInt(int index)
00154     {
00155         return _result->getInt(index);
00156     }
00157 
00161     bool DbStream::getBool(int index)
00162     {
00163         return _result->getBoolean(index);
00164     }
00165 }

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