00001 #ifndef QUERYTHREAD_H 00002 #define QUERYTHREAD_H 00003 00004 #include "querythreadcommand.h" 00005 00009 struct QueryThreadConnSpecs { 00010 QString driver; 00011 QString dbName; 00012 QString host; 00013 QString user; 00014 QString passwd; 00015 int port; 00016 }; 00017 00018 #include <QMutex> 00019 #include <QQueue> 00020 00033 class QueryQueue { 00034 public: 00035 void enqueue(const QueryThreadCommand &cmd); 00036 QueryThreadCommand dequeue(); 00037 bool contains(const QueryThreadCommand &cmd) const; 00038 00039 private: 00040 void filter(const QueryThreadCommand &cmd); 00041 00042 private: 00043 QMutex mutex; 00044 QQueue<QueryThreadCommand> queue; 00045 }; 00046 00047 extern QSqlDatabase createConnection(const QueryThreadConnSpecs &specs, const char *connName); 00048 extern void connectToDatabase(QSqlDatabase db); 00049 00050 #include <QObject> 00051 00060 class QueryWorker : public QObject { 00061 Q_OBJECT 00062 public: 00063 QueryWorker(QueryThreadConnSpecs &specs, QueryQueue *queryQueue, QObject* parent = 0); 00064 ~QueryWorker(); 00065 QueryThreadCommand currentCmd(); 00066 00067 public slots: 00068 void slotExecute(QueryQueue *queue); 00069 00070 private: 00071 void setCurrentCmd(QueryThreadCommand &cmd); 00072 void releaseCurrentCmd(); 00073 00074 signals: 00075 void querying(bool); 00076 void signal_result(QueryThreadCommand result); 00077 00078 private: 00079 QObject *m_requester; 00080 QueryQueue *m_queryQueue; 00081 QueryThreadCommand m_currentCmd; 00082 QMutex m_currentCmdMutex; 00083 }; 00084 00085 #include <QThread> 00086 #include <QVariant> // Q_DECLARE_METATYPE veronderstelt deze header file 00087 #include "libpq-fe.h" 00088 Q_DECLARE_METATYPE(PGconn*); 00089 00093 class QueryThread : public QThread { 00094 Q_OBJECT 00095 public: 00096 QueryThread(QueryThreadConnSpecs &specs, QueryQueue *queue, QObject *parent = 0); 00097 ~QueryThread(); 00098 00099 void waitToStart(); 00100 QueryThreadCommand currentCmd(); 00101 00102 public slots: 00103 bool cancelQuery(); 00104 void slot_queryQueue_changed(QueryQueue *queue); 00105 00106 signals: 00107 void querying(bool); 00108 void signal_result(QueryThreadCommand result); 00109 void fwdToWorker(QueryQueue *queue); 00110 00111 protected: 00112 void run(); 00113 00114 private: 00115 QueryWorker* worker; 00116 QueryThreadConnSpecs specs; 00117 QueryQueue *m_queryQueue; 00118 }; 00119 00120 #endif // QUERYTHREAD_H