This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
dbgPgConn.cpp
498 lines (398 loc) · 13.2 KB
/
dbgPgConn.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
// RCS-ID: $Id$
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
// This software is released under the Artistic Licence
//
// dbgPgConn.cpp - debugger
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "debugger/dbgPgConn.h"
#include "utils/sysLogger.h"
#include <stdexcept>
#ifdef __WXMSW__
#include <winsock.h>
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#ifndef INADDR_NONE
#define INADDR_NONE (-1)
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// dbgPgConn constructors
//
// A dbgPgConn object encapsulates the connection to a PostgreSQL server. This
// class is a wrapper around a Pgconn that provides a convenient constructor
// and a few member functions.
//
// The constructor creates a new thread and connects to the specified server
dbgPgConn::dbgPgConn(frmDebugger *frame, const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, int sslverify )
: m_frame(frame)
{
Init( server, database, userName, password, port, sslmode, sslverify, true );
}
dbgPgConn::dbgPgConn(frmDebugger *frame, const dbgConnProp & props, bool startThread )
: m_frame(frame)
{
Init( props.m_host, props.m_database, props.m_userName, props.m_password, props.m_port, props.m_sslMode, props.m_sslVerify, startThread );
}
void dbgPgConn::Init( const wxString &server, const wxString &database, const wxString &username, const wxString &password, const wxString &port, int sslmode, int sslverify, bool startThread )
{
m_pgConn = NULL;
m_majorVersion = 0;
m_minorVersion = 0;
m_debuggerApiVersion = DEBUGGER_UNKNOWN_API;
m_isEdb = false;
if( startThread )
m_workerThread = new dbgPgThread( *this );
else
m_workerThread = NULL;
wxString msg;
wxString delimiter;
// To keep the user interface thread responsive while we're waiting for the
// PostgreSQL server, we create a separate thread to interact with the
// database - the worker thread sleeps until the GUI thread signals that it
// has some work to do. When the result set arrives from the server, the
// worker thread creates a DBResult event and posts it to the GUI thread's
// event queue.
if( m_workerThread )
{
m_workerThread->Create();
m_workerThread->Run();
}
// Figure out the hostname/IP address
struct hostent *host;
unsigned long addr;
wxString hostip, hostname;
#ifdef __WXMSW__
struct in_addr ipaddr;
#else
unsigned long ipaddr;
#endif
#ifndef __WXMSW__
if (!(server.IsEmpty() || server.StartsWith(wxT("/"))))
{
#endif
addr = inet_addr(server.ToAscii());
if (addr == INADDR_NONE) // szServer is not an IP address
{
host = gethostbyname(server.ToAscii());
if (host == NULL)
{
wxLogError(__("Could not resolve hostname %s"), server.c_str());
return;
}
memcpy(&(ipaddr),host->h_addr,host->h_length);
hostip = wxString::FromAscii(inet_ntoa(*((struct in_addr*) host->h_addr_list[0])));
hostname = server;
}
else
{
hostip = server;
hostname = server;
}
#ifndef __WXMSW__
}
else
hostname = server;
#endif
// Build up a connection string
wxString connectParams;
if(hostname.Length())
{
connectParams.Append(wxT( "host="));
connectParams.Append(hostname);
msg += delimiter + server; delimiter = _(":");
}
if (hostip.Length())
{
connectParams.Append(wxT(" hostaddr="));
connectParams.Append(hostip);
}
if( port.Length())
{
connectParams += wxT(" port=");
connectParams += port;
msg += delimiter + port; delimiter = _(":");
}
if( database.Length())
{
connectParams.Append(wxT(" dbname="));
connectParams.Append(qtConnString(database));
msg += delimiter + database; delimiter = _(":");
}
if(username.Length())
{
connectParams.Append(wxT(" user="));
connectParams.Append(username );
msg += delimiter + username; delimiter = _(":");
}
if(password.Length())
{
connectParams.Append(wxT(" password="));
connectParams.Append(password);
}
switch (sslmode)
{
case 1:
connectParams.Append(wxT(" sslmode=require"));
break;
case 2:
connectParams.Append(wxT(" sslmode=prefer"));
break;
case 3:
connectParams.Append(wxT(" sslmode=allow"));
break;
case 4:
connectParams.Append(wxT(" sslmode=disable"));
break;
default:
break;
}
switch (sslverify)
{
case 1:
connectParams.Append(wxT(" sslverify=cn"));
break;
case 2:
connectParams.Append(wxT(" sslverify=cert"));
break;
case 3:
connectParams.Append(wxT(" sslverify=none"));
break;
default:
break;
}
connectParams.Trim(true);
connectParams.Trim(false);
m_frame->getStatusBar()->SetStatusText( wxString::Format(_( "Connecting to %s" ), msg.c_str()), 1 );
m_pgConn = PQconnectdb( connectParams.mb_str(wxConvUTF8));
if( PQstatus( m_pgConn ) == CONNECTION_OK )
{
m_frame->getStatusBar()->SetStatusText( wxString::Format(_( "Connected to %s" ), msg.c_str()), 1 );
PQsetClientEncoding( m_pgConn, "UNICODE" );
}
else
{
throw( std::runtime_error( PQerrorMessage( m_pgConn )));
}
}
dbgPgConn::~dbgPgConn()
{
Close();
}
////////////////////////////////////////////////////////////////////////////////
// isConnected()
//
// Returns true if the connection to the PostgreSQL server is alive and well,
// otherwise, returns false.
bool dbgPgConn::isConnected( void ) const
{
if( m_pgConn && PQstatus( m_pgConn ) == CONNECTION_OK )
return( true );
else
return( false );
}
////////////////////////////////////////////////////////////////////////////////
// getName()
//
// This function returns a user-friendly descriptive name for this connection.
// The result is "hostname/database".
const wxString dbgPgConn::getName() const
{
wxString result = wxString( PQhost( m_pgConn ), wxConvUTF8 );
result += wxT( "/" );
result.Append( wxString( PQdb( m_pgConn ), wxConvUTF8 ));
return( result );
}
////////////////////////////////////////////////////////////////////////////////
// getHost()
//
// Returns the name of the host (or the IP address) that we're connected to.
const wxString dbgPgConn::getHost() const
{
return( wxString( PQhost( m_pgConn ), wxConvUTF8 ));
}
////////////////////////////////////////////////////////////////////////////////
// getDatabase()
//
// Returns the name of the database that we're connected to
const wxString dbgPgConn::getDatabase() const
{
return( wxString( PQdb( m_pgConn ), wxConvUTF8 ));
}
////////////////////////////////////////////////////////////////////////////////
// getConnection()
//
// Returns the libpq connection handle for this dbgPgConn
PGconn * dbgPgConn::getConnection()
{
return( m_pgConn );
}
////////////////////////////////////////////////////////////////////////////////
// startCommand()
//
// The GUI thread invokes startCommand() when the user asks us to execute a
// command. We pass off the real work to the worker thread.
void dbgPgConn::startCommand( const wxString &command, wxEvtHandler * caller, wxEventType eventType, dbgPgParams *params )
{
wxLogSql(wxT("%s"), command.c_str());
m_workerThread->startCommand(command, caller, eventType, params);
}
PGresult * dbgPgConn::waitForCommand( const wxString &command )
{
wxLogSql(wxT("%s"), command.c_str());
PGresult * result = PQexec( m_pgConn, command.mb_str( wxConvUTF8 ));
return( result );
}
////////////////////////////////////////////////////////////////////////////////
// setNoticeHandler()
//
// Register a NOTICE handler with the libpq library - libpq will invoke the
// given handler whenever a NOTICE arrives on this connection. libpq will
// pass 'arg' to the handler. 'handler' is typically a static function and
// 'arg' is often a pointer to an object. That lets you use a regular member
// function as a callback (because 'arg' is mapped into a 'this' pointer by the
// callback function).
void dbgPgConn::setNoticeHandler( PQnoticeProcessor handler, void * arg )
{
PQnoticeProcessor p=NULL;
p = PQsetNoticeProcessor( m_pgConn, handler, arg );
}
void dbgPgConn::Close()
{
// Attempt to cancel any ongoing query
Cancel();
// Wait a tenth of a second or so for things to sort themselves out.
// Otherwise on Windows things can get funky here ...
wxMilliSleep(100);
if (m_workerThread)
{
m_workerThread->Die();
m_workerThread->Wait();
delete m_workerThread;
m_workerThread = NULL;
}
if (m_pgConn)
PQfinish(m_pgConn);
m_pgConn = NULL;
}
void dbgPgConn::Cancel()
{
// Attempt to cancel any ongoing query
if (m_pgConn)
{
PGcancel *cancel = PQgetCancel(m_pgConn);
char errbuf[256];
PQcancel(cancel, errbuf, sizeof(errbuf));
PQfreeCancel(cancel);
}
}
// Check the backend version
bool dbgPgConn::BackendMinimumVersion(int major, int minor)
{
if (!m_majorVersion)
{
wxString version=GetVersionString();
sscanf(version.ToAscii(), "%*s %d.%d", &m_majorVersion, &m_minorVersion);
m_isEdb = version.Upper().Matches(wxT("ENTERPRISEDB*"));
// EnterpriseDB 8.3 beta 1 & 2 and possibly later actually have PostgreSQL 8.2 style
// catalogs. This is expected to change either before GA, but in the meantime we
// need to check the catalogue version in more detail, and if we don't see what looks
// like a 8.3 catalog, force the version number back to 8.2. Yuck.
if (m_isEdb && m_majorVersion == 8 && m_minorVersion == 3)
{
PGresult *res;
wxString result;
res = waitForCommand(wxT( "SELECT count(*) FROM pg_attribute WHERE attname = 'proconfig' AND attrelid = 'pg_proc'::regclass"));
if (PQresultStatus(res) == PGRES_TUPLES_OK)
{
// Retrieve the query result and return it.
result=wxString(PQgetvalue(res, 0, 0), wxConvUTF8);
// Cleanup & exit
PQclear(res);
}
if (result == wxT("0"))
m_minorVersion = 2;
m_isGreenplum = version.Upper().Matches(wxT("*GREENPLUM DATABASE*"));
}
}
return m_majorVersion > major || (m_majorVersion == major && m_minorVersion >= minor);
}
// Check the EDB backend version
bool dbgPgConn::EdbMinimumVersion(int major, int minor)
{
return BackendMinimumVersion(major, minor) && GetIsEdb();
}
// Get the debugger API version
DebuggerApiVersions dbgPgConn::DebuggerApiVersion()
{
if (m_debuggerApiVersion > 0)
return m_debuggerApiVersion;
// The v1 protocol didn't have pldbg_get_proxy_info()
wxString result;
PGresult *res = waitForCommand(wxT( "SELECT count(*) FROM pg_proc WHERE proname = 'pldbg_get_proxy_info';"));
if (PQresultStatus(res) == PGRES_TUPLES_OK)
{
// Retrieve the query result and return it.
if (wxString(PQgetvalue(res, 0, 0), wxConvUTF8) == wxT("0"))
{
PQclear(res);
m_debuggerApiVersion = DEBUGGER_V1_API;
return DEBUGGER_V1_API;
}
PQclear(res);
}
else
{
wxLogError(wxT("%s"), wxString(PQerrorMessage(m_pgConn), wxConvUTF8).c_str());
return DEBUGGER_UNKNOWN_API;
}
// We have pldbg_get_proxy_info, so use it to get the API version
res = waitForCommand(wxT( "SELECT proxyapiver FROM pldbg_get_proxy_info();"));
if (PQresultStatus(res) == PGRES_TUPLES_OK)
{
// Retrieve the query result and return it.
m_debuggerApiVersion = (DebuggerApiVersions)atoi(wxString(PQgetvalue(res, 0, 0), wxConvUTF8).ToAscii());
PQclear(res);
}
else
{
wxLogError(wxT("%s"), wxString(PQerrorMessage(m_pgConn), wxConvUTF8).c_str());
return DEBUGGER_UNKNOWN_API;
}
return m_debuggerApiVersion;
}
wxString dbgPgConn::GetVersionString()
{
PGresult *res;
wxString result;
res = waitForCommand(wxT( "SELECT version();"));
if (PQresultStatus(res) == PGRES_TUPLES_OK)
{
// Retrieve the query result and return it.
result=wxString(PQgetvalue(res, 0, 0), wxConvUTF8);
// Cleanup & exit
PQclear(res);
}
return result;
}
bool dbgPgConn::GetIsEdb()
{
// to retrieve edb flag
BackendMinimumVersion(0,0);
return m_isEdb;
}
bool dbgPgConn::GetIsGreenplum()
{
// to retrieve edb flag
BackendMinimumVersion(0,0);
return m_isGreenplum;
}