Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members

FXDatabaseInterface.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  Generic Database Interface                                   *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2002 by Mathew Robertson.       All Rights Reserved.            *
00007 * Copyright (C) 2002 by Giancarlo Formicuccia.  All Rights Reserved.            *
00008 *********************************************************************************
00009 * This library is free software; you can redistribute it and/or                 *
00010 * modify it under the terms of the GNU Lesser General Public                    *
00011 * License as published by the Free Software Foundation; either                  *
00012 * version 2.1 of the License, or (at your option) any later version.            *
00013 *                                                                               *
00014 * This library is distributed in the hope that it will be useful,               *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00017 * Lesser General Public License for more details.                               *
00018 *                                                                               *
00019 * You should have received a copy of the GNU Lesser General Public              *
00020 * License along with this library; if not, write to the Free Software           *
00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00022 *********************************************************************************/
00023 #ifndef FXDATABASEINTERFACE_H
00024 #define FXDATABASEINTERFACE_H
00025 
00026 #ifndef FXOBJECTLIST_H
00027 #include <fox/FXObjectList.h>
00028 using namespace FX;
00029 #endif
00030 #include "FXArray.h"
00031 #include "FXBaseObject.h"
00032 #include "FXExtendedException.h"
00033 #include "FXDatabaseQuery.h"
00034 namespace FXEX {
00035 
00036 /**
00037  * FXDatabaseInterface is the base class for connection handling of a real database
00038  * interface, such as an ODBC interface.  Child classes implement specific handling of
00039  * connecting to a real database, executing the real query, etc.
00040  */
00041 class FXAPI FXDatabaseInterface: public FXBaseObject {
00042   FXDECLARE_ABSTRACT(FXDatabaseInterface)
00043 
00044 protected:
00045   FXString             database;  // name of database
00046   FXString             username;  // user of database connection
00047   FXString             password;  // password to database connection
00048   FXbool              connected;  // connection status
00049   FXDatabaseQueryList queries;    // allocated queries
00050 
00051   void checkStatus(FXbool should_be);
00052 
00053   /// serialisation
00054   FXDatabaseInterface();
00055 
00056   /// ctor
00057   FXDatabaseInterface(FXApp *a,FXObject *tgt=NULL,FXSelector sel=0);
00058   void notifyAllQueries(FXObject *, FXSelector, void *);
00059 
00060 public:
00061   enum {
00062     ID_QRYDETACH  = FXBaseObject::ID_LAST,
00063     ID_LAST
00064     };
00065 
00066   long onQrydetach(FXObject *, FXSelector, void *);
00067 
00068 public:
00069   static void dbThrow(const FXString &msg, FXint e); /* Exception throw */
00070 
00071 public:
00072   /// indicated whether the object is connected to a real database
00073   FXbool isConnected() const { return connected; }
00074 
00075   /// set the name of the database we want to be connected to
00076   void setDatabase(const FXString &name);
00077 
00078   /// get the name of the database we are connected to
00079   FXString getDatabase() const { return database; }
00080 
00081   /// set the name of the user we want to connect to the database as
00082   void setUser(const FXString &name);
00083 
00084   /// get the name of the user that we are connected to the database as
00085   FXString getUser() const { return username; }
00086 
00087   /// set the password for the user
00088   void setPassword(const FXString &passwd);
00089 
00090   /// this returns a list of tables implemented for this database
00091   virtual FXStringList getTables() = '\0';
00092 
00093   /*
00094    * Dataset allocation+append to list
00095    */
00096   virtual FXDatabaseQuery *createQuery() = '\0';
00097 
00098   /* Transaction stuff */
00099   virtual void BeginTrans() = '\0';
00100   virtual void Commit() = '\0';
00101   virtual void Rollback() = '\0';
00102 
00103   /**
00104    * execute a SQL direct query, which doesn't need to return any results,
00105    * such as an INSERT, APPEND, UPDATE
00106    */
00107   virtual void executeDirect(const FXString &command) = '\0';
00108 
00109   /**
00110    * execute a SQL query and return the resulting data set
00111    */
00112   virtual FXDatabaseQuery *execute(const FXString &command);
00113 
00114   /// Stream save and load
00115   virtual void save(FXStream& store) const;
00116   virtual void load(FXStream& store);
00117 
00118   /**
00119    * Connect to database - readOnly is only a hint - the underlying database may not support
00120    * read-only connections
00121    */
00122   virtual void connect(FXbool readOnly = FALSE) = '\0';
00123 
00124   /// Disconnect from database
00125   virtual void disconnect() = '\0';
00126 
00127   /// dtor
00128   virtual ~FXDatabaseInterface();
00129   };
00130 
00131 } // namespace FXEX
00132 #endif // FXDATABASEINTERFACE_H