~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to pgadmin/include/db/pgQueryThread.h

  • Committer: Package Import Robot
  • Author(s): Christoph Berg
  • Date: 2013-09-10 16:16:38 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20130910161638-wwup1q553ylww7dr
Tags: 1.18.0-1
* New upstream release.
* Don't install /usr/bin/png2c anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
4
//
5
 
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
 
5
// Copyright (C) 2002 - 2013, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// pgQueryThread.h - PostgreSQL threaded query class header
12
12
#ifndef PGQUERYTHREAD_H
13
13
#define PGQUERYTHREAD_H
14
14
 
 
15
#include "wx/wx.h"
 
16
#include "wx/event.h"
 
17
#include "db/pgConn.h"
 
18
 
 
19
// Forward declaration
15
20
class pgSet;
 
21
class pgQueryThread;
 
22
class pgBatchQuery;
 
23
 
 
24
// Support for the IN & INOUT parameters type
 
25
class pgParam : public wxObject
 
26
{
 
27
public:
 
28
        enum
 
29
        {
 
30
            PG_PARAM_IN       = 1,
 
31
            PG_PARAM_OUT      = 2,
 
32
            PG_PARAM_INOUT    = 3,
 
33
            PG_PARAM_VARIADIC = 4,
 
34
            PG_PARAM_TABLE    = 5
 
35
        };
 
36
 
 
37
        // Any data
 
38
        //
 
39
        // This constructor won't call the functions - 'htonl' or 'htons'
 
40
        // It will be the responsibility of the caller to take care
 
41
        // to call 'htonl' or 'htons' depending on requirements.
 
42
        //
 
43
        // NOTE:
 
44
        // This data will be owned by pgParam object and will be released.
 
45
        pgParam(Oid _type, void *_val, int _len, short mode = PG_PARAM_IN);
 
46
 
 
47
        // wxString data
 
48
        pgParam(Oid _type, wxString *_val, wxMBConv *_conv = NULL,
 
49
                short mode = PG_PARAM_IN);
 
50
 
 
51
        ~pgParam();
 
52
 
 
53
        // Returns 0 for text type and 1 for otherwise
 
54
        int   GetFormat();
 
55
 
 
56
        Oid   GetType()
 
57
        {
 
58
                return m_type;
 
59
        }
 
60
        short GetMode()
 
61
        {
 
62
                return m_mode;
 
63
        }
 
64
 
 
65
protected:
 
66
        Oid    m_type;
 
67
        void  *m_val;
 
68
        int    m_len;
 
69
        short  m_format;
 
70
        // Modes are required by EnterpriseDB's callable statement
 
71
        short  m_mode;
 
72
 
 
73
        // Do not allow copy construction and shadow-copy
 
74
        // to avoid ownership
 
75
        // Force to use an pointer
 
76
        pgParam(const pgParam &)
 
77
        {
 
78
                wxASSERT(0);
 
79
        }
 
80
        pgParam &operator= (const pgParam &)
 
81
        {
 
82
                wxASSERT(0);
 
83
        }
 
84
 
 
85
        friend class pgConn;
 
86
        friend class pgQueryThread;
 
87
};
 
88
 
 
89
WX_DEFINE_ARRAY_PTR(pgParam *, pgParamsArray);
 
90
 
 
91
class pgBatchQuery : public wxObject
 
92
{
 
93
public:
 
94
        pgBatchQuery(const wxString &_query, pgParamsArray *_params = NULL,
 
95
                     long _eventId = -1, void *_data = NULL, bool _useCallable = false,
 
96
                     int _resultToRetrieve = 0)
 
97
                : m_query(_query), m_params(_params), m_eventID(_eventId), m_data(_data),
 
98
                  m_useCallable(_useCallable), m_resToRetrieve(_resultToRetrieve),
 
99
                  m_returnCode(-1), m_resultSet(NULL), m_rowsInserted(-1), m_insertedOid(-1)
 
100
        {
 
101
                // Do not honour the empty query string
 
102
                wxASSERT(!_query.IsEmpty());
 
103
        }
 
104
        ~pgBatchQuery();
 
105
 
 
106
        bool            Release();
 
107
 
 
108
        pgSet          *ResultSet()
 
109
        {
 
110
                return m_resultSet;
 
111
        }
 
112
 
 
113
        int             ReturnCode()
 
114
        {
 
115
                return m_returnCode;
 
116
        }
 
117
 
 
118
        const wxString &GetMessage()
 
119
        {
 
120
                return m_message;
 
121
        }
 
122
 
 
123
        long            RowInserted()
 
124
        {
 
125
                return m_rowsInserted;
 
126
        }
 
127
 
 
128
        const wxString &GetErrorMessage();
 
129
 
 
130
protected:
 
131
        wxString           m_query;         // Query
 
132
        pgParamsArray     *m_params;        // parameters
 
133
        long               m_eventID;       // Event ID
 
134
        void              *m_data;          // Data to be send with event
 
135
        bool               m_useCallable;   // Use EnterpriseDB callable statement if possible
 
136
        int                m_resToRetrieve; // Which result to be retrieved
 
137
        int                m_returnCode;    // Return code
 
138
        pgSet             *m_resultSet;     // Result-Set
 
139
        long               m_rowsInserted;  // No of rows inserted
 
140
        Oid                m_insertedOid;   // Inserted Oid
 
141
        wxString           m_message;       // Message generated during query execution
 
142
        pgError            m_err;           // Error
 
143
 
 
144
private:
 
145
        // Do not allow copy construction and '=' operator (shadow copying)
 
146
        // to avoid ownership of parameters and result-set
 
147
        //
 
148
        // This will force this class to be used as an pointer only.
 
149
        pgBatchQuery(const pgBatchQuery &)
 
150
        {
 
151
                wxASSERT(0);
 
152
        }
 
153
        pgBatchQuery &operator= (const pgBatchQuery &)
 
154
        {
 
155
                wxASSERT(0);
 
156
        }
 
157
 
 
158
        friend class pgQueryThread;
 
159
};
 
160
WX_DEFINE_ARRAY_PTR(pgBatchQuery *, pgBatchQueryArray);
16
161
 
17
162
class pgQueryThread : public wxThread
18
163
{
19
164
public:
20
 
        pgQueryThread(pgConn *_conn, const wxString &qry, int resultToRetrieve = -1, wxWindow *_caller = 0, long eventId = 0, void *_data = 0);
 
165
        // For running a single query (Used by few components)
 
166
        pgQueryThread(pgConn *_conn, const wxString &qry, int resultToRetrieve = -1,
 
167
                      wxWindow *_caller = 0, long eventId = 0, void *_data = 0);
 
168
 
 
169
        // Support for multiple queries support
 
170
        pgQueryThread(pgConn *_conn, wxEvtHandler *_caller = NULL,
 
171
                      PQnoticeProcessor _processor = NULL, void *_noticeHandler = NULL);
 
172
 
21
173
        ~pgQueryThread();
22
174
 
 
175
        bool HasMultipleQueriesSupport()
 
176
        {
 
177
                return m_multiQueries;
 
178
        }
 
179
 
 
180
        bool SupportCallableStatement()
 
181
        {
 
182
                return m_useCallable;
 
183
        }
 
184
 
 
185
        void SetEventOnCancellation(bool eventOnCancelled);
 
186
 
 
187
        void AddQuery(
 
188
            const wxString &_qry, pgParamsArray *_params = NULL,
 
189
            long _eventId = 0, void *_data = NULL, bool _useCallable = false,
 
190
            int _resultToRetrieve = -1);
 
191
 
23
192
        virtual void *Entry();
24
 
        bool DataValid() const
25
 
        {
26
 
                return dataSet != NULL;
27
 
        }
28
 
        pgSet *DataSet()
29
 
        {
30
 
                return dataSet;
31
 
        }
32
 
        int ReturnCode() const
33
 
        {
34
 
                return rc;
35
 
        }
36
 
        long RowsInserted() const
37
 
        {
38
 
                return rowsInserted;
39
 
        }
40
 
        OID InsertedOid() const
41
 
        {
42
 
                return insertedOid;
43
 
        }
44
 
        wxString GetMessagesAndClear();
45
 
        void appendMessage(const wxString &str);
 
193
        bool DataValid(int _idx = -1) const
 
194
        {
 
195
                if (_idx == -1)
 
196
                        _idx = m_currIndex;
 
197
                return (_idx >= 0 && _idx > m_currIndex ? false : (m_queries[_idx]->m_resultSet != NULL));
 
198
        }
 
199
 
 
200
        pgConn *GetConn()
 
201
        {
 
202
                return m_conn;
 
203
        }
 
204
 
 
205
        pgSet *DataSet(int _idx = -1)
 
206
        {
 
207
                if (_idx == -1)
 
208
                        _idx = m_currIndex;
 
209
                return (_idx >= 0 && _idx > m_currIndex ? NULL : m_queries[_idx]->m_resultSet);
 
210
        }
 
211
 
 
212
        int ReturnCode(int _idx = -1) const
 
213
        {
 
214
                if (_idx == -1)
 
215
                        _idx = m_currIndex;
 
216
                return (_idx >= 0 && _idx > m_currIndex ? -1 : m_queries[_idx]->m_returnCode);
 
217
        }
 
218
 
 
219
        long RowsInserted(int _idx = -1) const
 
220
        {
 
221
                if (_idx == -1)
 
222
                        _idx = m_currIndex;
 
223
                return (_idx >= 0 && _idx > m_currIndex ? -1L : m_queries[_idx]->m_rowsInserted);
 
224
        }
 
225
 
 
226
        Oid InsertedOid(int _idx = -1) const
 
227
        {
 
228
                if (_idx == -1)
 
229
                        _idx = m_currIndex;
 
230
                return (_idx >= 0 && _idx > m_currIndex ? -1L : m_queries[_idx]->m_insertedOid);
 
231
        }
 
232
 
 
233
        inline void CancelExecution()
 
234
        {
 
235
                m_cancelled = true;
 
236
        }
 
237
 
 
238
        inline size_t GetNumberQueries()
 
239
        {
 
240
                return m_queries.GetCount();
 
241
        }
 
242
 
 
243
        size_t QueriesExecuted()
 
244
        {
 
245
                return m_currIndex + 1;
 
246
        }
 
247
 
 
248
        wxString GetMessagesAndClear(int _idx = -1);
 
249
        void AppendMessage(const wxString &_str);
 
250
 
 
251
        int DeleteReleasedQueries();
 
252
 
 
253
        pgError GetResultError(int idx = -1);
46
254
 
47
255
private:
48
 
        int rc;
49
 
        int resultToRetrieve;
50
 
        long rowsInserted;
51
 
        OID insertedOid;
52
 
 
53
 
        wxString query;
54
 
        pgConn *conn;
55
 
        PGresult *result;
56
 
        wxString messages;
57
 
        pgSet *dataSet;
58
 
        wxCriticalSection criticalSection;
59
 
 
60
 
        void *data;
61
 
        wxWindow *caller;
62
 
        long eventId;
63
 
 
64
 
        int execute();
65
 
        int raiseEvent(int retval = 0);
66
 
 
67
 
        void appendMessageRaw(const wxString &str);
 
256
        int Execute();
 
257
        int RaiseEvent(int _retval = 0);
 
258
 
 
259
        // Queries to be exectued
 
260
        pgBatchQueryArray  m_queries;
 
261
        // Current running query index
 
262
        int                m_currIndex;
 
263
        // Connection object
 
264
        pgConn            *m_conn;
 
265
        // Execution cancelled?
 
266
        bool               m_cancelled;
 
267
        // Raise events even when cancelled the execution
 
268
        bool               m_eventOnCancellation;
 
269
        // Does this thread support multiple queries
 
270
        bool               m_multiQueries;
 
271
        // Use EDB callable statement (if available and require)
 
272
        bool               m_useCallable;
 
273
        // Is executing a query
 
274
        bool               m_executing;
 
275
        // Queries are being accessed at this time
 
276
        wxMutex            m_queriesLock;
 
277
        // When one thread is accesing messages, other should not be able to access it
 
278
        wxCriticalSection  m_criticalSection;
 
279
        // Event Handler
 
280
        wxEvtHandler      *m_caller;
 
281
        // Database server notice-processor
 
282
        PQnoticeProcessor  m_processor;
 
283
        // Notice Handler
 
284
        void              *m_noticeHandler;
 
285
 
68
286
};
69
287
 
70
288
#endif