00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #ifndef DBFACADE_H
00022 #define DBFACADE_H
00023 
00024 #include <map>
00025 #include <string>
00026 #include <boost/extension/extension.hpp>
00027 #include <boost/extension/factory.hpp>
00028 #include <boost/extension/type_map.hpp>
00029 #include <boost/shared_ptr.hpp>
00030 
00190 namespace dbfacade
00191 {
00192 
00194         class Exception
00195         {
00196                 public:
00197                         Exception(
00198                                 const std::string& message);
00199                         virtual std::string getMessage() const;
00200 
00201                 private:
00202                         std::string message_;
00203         };
00204 
00207         class UniqueConstraintException
00208                 : public Exception
00209         {
00210                 public:
00211                         UniqueConstraintException(
00212                                 const std::string& message);
00213         };
00214 
00217         class ForeignKeyConstraintException
00218                 : public Exception
00219         {
00220                 public:
00221                         ForeignKeyConstraintException(
00222                                 const std::string& message);
00223         };
00224 
00227         class EmptyResultSetException
00228                 : public Exception
00229         {
00230                 public:
00231                         EmptyResultSetException(
00232                                 const std::string& message);
00233         };
00234 
00236         class ResultRow
00237         {
00238                 public:
00239                         virtual ~ResultRow();
00240 
00246                         virtual bool hasValueAt(
00247                                 const int index) const throw() = 0;
00248 
00256                         virtual bool getBooleanAt(
00257                                 const int index) const = 0;
00258 
00266                         virtual double getDoubleAt(
00267                                 const int index) const = 0;
00268 
00276                         virtual int getIntAt(
00277                                 const int index) const = 0;
00278 
00286                         virtual std::string getStringAt(
00287                                 const int index) const = 0;
00288         };
00289         typedef boost::shared_ptr<ResultRow> RowPtr;
00290 
00292         class Result
00293         {
00294                 public:
00295                         virtual ~Result();
00296 
00298                         virtual RowPtr getNextRow() = 0;
00299         };
00300         typedef boost::shared_ptr<Result> ResultPtr;
00301 
00303         class Database
00304         {
00305                 public:
00306                         virtual ~Database();
00307 
00310                         static boost::shared_ptr<Database> getDatabase(
00311                                 const std::string& identifier);
00312 
00314                         virtual void connect(
00315                                 const std::string& server,
00316                                 const std::string& username,
00317                                 const std::string& password,
00318                                 const std::string& dbname) = 0;
00319 
00321                         virtual void execute(
00322                                 const std::string& statement) = 0;
00323 
00325                         virtual ResultPtr sqlSelect(
00326                                 const std::string& statement,
00327                                 const bool allAtOnce = true) = 0;
00328 
00339                         virtual int sqlInsert(
00340                                 const std::string& statement) = 0;
00341 
00343                         virtual int sqlUpdate(
00344                                 const std::string& statement) = 0;
00345 
00348                         virtual bool getFirstBoolean(
00349                                 const std::string& statement);
00350 
00353                         virtual double getFirstDouble(
00354                                 const std::string& statement);
00355 
00358                         virtual int getFirstInt(
00359                                 const std::string& statement);
00360 
00363                         virtual std::string getFirstString(
00364                                 const std::string& statement);
00365 
00367                         virtual std::string escape(
00368                                 const char* unescaped,
00369                                 const int size) const = 0;
00370         };
00371         typedef boost::shared_ptr<Database> DatabasePtr;
00372 
00373         typedef std::map<
00374                         std::string,
00375                         boost::extensions::factory<dbfacade::Database> >
00376                 DatabaseFactoryMap;
00377 };
00378 
00379 #define REGISTER_PLUGIN(name, class) \
00380 BOOST_EXTENSION_TYPE_MAP_FUNCTION \
00381 { types.get<dbfacade::DatabaseFactoryMap>()[name].set<class>(); }
00382 
00383 #endif
00384