This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
/
pgQueryThread.cpp
195 lines (162 loc) · 4.72 KB
/
pgQueryThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
// RCS-ID: $Id$
// Copyright (C) 2002 - 2008, The pgAdmin Development Team
// This software is released under the Artistic Licence
//
// pgQueryThread.cpp - PostgreSQL threaded query class
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
// wxWindows headers
#include <wx/wx.h>
// PostgreSQL headers
#include <libpq-fe.h>
// App headers
#include "db/pgSet.h"
#include "db/pgConn.h"
#include "db/pgQueryThread.h"
#include "utils/sysLogger.h"
static void pgNoticeProcessor(void *arg, const char *message)
{
wxString str(message, wxConvUTF8);
wxLogNotice(wxT("%s"), str.Trim().c_str());
((pgQueryThread*)arg)->appendMessage(str);
}
pgQueryThread::pgQueryThread(pgConn *_conn, const wxString &qry, int _resultToRetrieve, wxWindow *_caller, long _eventId, void *_data)
: wxThread(wxTHREAD_JOINABLE)
{
query = qry;
conn=_conn;
dataSet=0;
result=0;
resultToRetrieve=_resultToRetrieve;
rc=-1;
insertedOid = (OID)-1;
caller=_caller;
eventId=_eventId;
data=_data;
wxLogSql(wxT("Thread query (%s:%d): %s"), conn->GetHost().c_str(), conn->GetPort(), qry.c_str());
conn->RegisterNoticeProcessor(pgNoticeProcessor, this);
if (conn->conn)
PQsetnonblocking(conn->conn, 1);
}
pgQueryThread::~pgQueryThread()
{
conn->RegisterNoticeProcessor(0, 0);
if (dataSet)
delete dataSet;
}
wxString pgQueryThread::GetMessagesAndClear()
{
wxString msg;
{
wxCriticalSectionLocker cs(criticalSection);
msg=messages;
messages.Empty();
}
return msg;
}
void pgQueryThread::appendMessage(const wxString &str)
{
wxCriticalSectionLocker cs(criticalSection);
messages.Append(str);
}
int pgQueryThread::execute()
{
rowsInserted = -1L;
if (!conn->conn)
return(raiseEvent(0));
if (!PQsendQuery(conn->conn, query.mb_str(*conn->conv)))
{
conn->IsAlive();
return(raiseEvent(0));
}
int resultsRetrieved=0;
PGresult *lastResult=0;
while (true)
{
if (TestDestroy())
{
if (rc != -3)
{
if (!PQrequestCancel(conn->conn)) // could not abort; abort failed.
return(raiseEvent(-1));
rc = -3;
}
}
if (!PQconsumeInput(conn->conn))
return(raiseEvent(0));
if (PQisBusy(conn->conn))
{
Yield();
this->Sleep(10);
continue;
}
// If resultToRetrieve is given, the nth result will be returned,
// otherwise the last result set will be returned.
// all others are discarded
PGresult *res=PQgetResult(conn->conn);
if (!res)
break;
resultsRetrieved++;
if (resultsRetrieved == resultToRetrieve)
{
result=res;
insertedOid=PQoidValue(res);
if (insertedOid && insertedOid != (OID)-1)
appendMessage(wxString::Format(_("Query inserted one row with OID %d.\n"), insertedOid));
else
appendMessage(wxString::Format(_("Query result with %d rows will be returned.\n"), PQntuples(result)));
continue;
}
if (lastResult)
{
if (PQntuples(lastResult))
appendMessage(wxString::Format(_("Query result with %d rows discarded.\n"), PQntuples(lastResult)));
PQclear(lastResult);
}
lastResult=res;
}
if (!result)
result = lastResult;
conn->SetLastResultError(result);
appendMessage(wxT("\n"));
rc=PQresultStatus(result);
insertedOid=PQoidValue(result);
if (insertedOid == (OID)-1)
insertedOid=0;
if (rc == PGRES_TUPLES_OK)
{
dataSet = new pgSet(result, conn, *conn->conv, conn->needColQuoting);
dataSet->MoveFirst();
}
else if (rc == PGRES_COMMAND_OK)
{
char *s=PQcmdTuples(result);
if (*s)
rowsInserted = atol(s);
}
else if (rc == PGRES_FATAL_ERROR)
{
appendMessage(conn->GetLastError() + wxT("\n"));
}
return(raiseEvent(1));
}
int pgQueryThread::raiseEvent(int retval)
{
if (caller)
{
wxCommandEvent resultEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
resultEvent.SetClientData(data);
caller->AddPendingEvent(resultEvent);
}
return retval;
}
void *pgQueryThread::Entry()
{
rc=-2;
execute();
return(NULL);
}