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

FXDatabaseQuery.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  Database query-result interface                              *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2002 by Mathew Robertson.       All Rights Reserved.            *
00007 * Copyright (C) 2003 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 FXDATABASEQUERY_H
00024 #define FXDATABASEQUERY_H
00025 
00026 #ifndef FXBASEOBJECT_H
00027 #include "FXBaseObject.h"
00028 #endif
00029 namespace FXEX {
00030 class FXDatabaseInterface;
00031 
00032 /**
00033  * Interface to data returned from a database query
00034  */
00035 class FXAPI FXDatabaseQuery : public FXBaseObject {
00036   FXDECLARE_ABSTRACT(FXDatabaseQuery)
00037 
00038 typedef struct {
00039   FXObject *tgt;
00040   FXSelector msg;
00041   FXString fldName;
00042   FXint fldPos;
00043 } FXfldTarget;
00044 
00045 typedef FXArray<FXfldTarget> FXfldTargetList;
00046 
00047 protected:
00048   FXString              query;      // query performed
00049   FXDatabaseInterface   *database;  // points to real database instance
00050   FXDatabaseFieldList   fields;
00051   FXrsState             state;
00052   FXfldTargetList       fldTargets;
00053   FXbool                readOnly;
00054 
00055   /// build a query on a specific database
00056   FXDatabaseQuery(FXDatabaseInterface *dbi, FXObject *tgt=NULL, FXSelector sel=0);
00057 
00058   FXDatabaseQuery();
00059   void checkState(FXrsState should_be) const;
00060   void checkOpen(FXbool open) const;
00061   void checkEditable(FXbool editable) const;
00062   void broadcastMessage(FXdbEvType ev, void *data = NULL);
00063   virtual void notifyMove(FXuint currentPos); /* dispatch dbRefresh to targets */
00064   FXint findFldTarget(FXObject *tgt);
00065   FXbool bindTarget(FXint pos);
00066   FXbool unbindTarget(FXint pos);
00067   void initTarget(FXint tgt);
00068   virtual void deleteFields();
00069 public:
00070   enum {
00071     ID_DISCONNECT = FXBaseObject::ID_LAST,
00072     ID_CONNECT,
00073     ID_DESTROY,
00074     ID_LAST
00075     };
00076 
00077 public:
00078   long onDisconnect(FXObject *, FXSelector, void *);
00079   long onConnect(FXObject *, FXSelector, void *);
00080   long onDestroy(FXObject *, FXSelector, void *);
00081   long onFree(FXObject *, FXSelector, void *);
00082 
00083 public:
00084   /// get database interface
00085   FXDatabaseInterface* getDatabase() const { return database; }
00086 
00087   /// get the last query
00088   FXString getQuery() const { checkOpen(TRUE); return query; }
00089 
00090   /// Add fld targets
00091   void addFldTarget(FXObject *tgt, FXSelector msg, const FXString &fld);
00092   void addFldTarget(FXObject *tgt, FXSelector msg, FXint fld);
00093   void removeFldTarget(FXObject *tgt);
00094   FXrsState getState() const { return state; }
00095   FXbool isOpen() const;
00096   FXbool isEditable() const;
00097   /* WARNING: derivated classes MUST call FXDatabaseQuery::these_methods
00098      if (and only if) the operation is successul.
00099      These functions perform message delivery to various targets. */
00100   virtual void Open(const FXString &query, FXbool readOnly=TRUE);
00101   virtual void Close();
00102   virtual void addNew();
00103   virtual void Edit();
00104   virtual void Delete();
00105 //  virtual void Delete();
00106   virtual void Update();
00107   virtual void CancelUpdate();
00108   virtual void Free(); /* Self-destruction. Don't call ~FXDatabaseQuery directly. */
00109 
00110   /* Remember to call notifyMove() in derivated classes on success */
00111   virtual void Requery() = '\0';
00112   virtual FXbool moveFirst() = '\0';
00113   virtual FXbool moveNext() = '\0';
00114   virtual FXbool movePrevious() = '\0';
00115   virtual FXbool moveLast() = '\0';
00116   virtual FXbool moveTo(FXuint where) = '\0';
00117   virtual FXbool moveOf(FXint displacement) = '\0';
00118 
00119   /// Current position
00120   virtual FXuint currentPos() = '\0';
00121 
00122   /// Estimate record count
00123   virtual FXuint recordCount() = '\0';
00124 
00125   /// Field count
00126   FXint fieldCount();
00127   FXDatabaseField &operator[](FXint pos);
00128 
00129   /// Stream save and load
00130   virtual void save(FXStream& store) const;
00131   virtual void load(FXStream& store);
00132 
00133   /// dtor
00134   virtual ~FXDatabaseQuery();
00135   };
00136 
00137 typedef FXObjectListOf<FXDatabaseQuery> FXDatabaseQueryList;
00138 
00139 }  // namespace FXEX
00140 #endif // FXDATABASEQUERY_H