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

« back to all changes in this revision

Viewing changes to pgadmin/debugger/dbgPgConn.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2011-06-07 23:03:54 UTC
  • mfrom: (1.3.1 upstream) (13 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110607230354-3td4j9y71u4ahcvj
Tags: 1.14.0~beta1-1
* New upstream development release, adding Build-Depends on
  postgresql-server-dev-all >= 117~.
* Add Build-Depends on quilt, (un)patch to debian/rules and patch for fixing
  the include for kwlist.h in pgadmin/db/keywords.c.
* Add pg_config --includedir-server output to CPPFLAGS.
* Remove unrecognized configure options: --with-wx-config,
  --with-pgsql-include, --enable-gtk2, --enable-unicode.
* Clean up manually the files that are left behind after the broken
  distclean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//////////////////////////////////////////////////////////////////////////
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
 
// 
5
 
// Copyright (C) 2002 - 2010, The pgAdmin Development Team
 
4
//
 
5
// Copyright (C) 2002 - 2011, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
 
// dbgPgConn.cpp - debugger 
 
8
// dbgPgConn.cpp - debugger
9
9
//
10
10
//////////////////////////////////////////////////////////////////////////
11
11
 
38
38
// dbgPgConn constructors
39
39
//
40
40
//    A dbgPgConn object encapsulates the connection to a PostgreSQL server.  This
41
 
//  class is a wrapper around a Pgconn that provides a convenient constructor 
42
 
//  and a few member functions.  
 
41
//  class is a wrapper around a Pgconn that provides a convenient constructor
 
42
//  and a few member functions.
43
43
//
44
44
//    The constructor creates a new thread and connects to the specified server
45
45
 
46
46
dbgPgConn::dbgPgConn(frmDebugger *frame, const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, const wxString &applicationname)
47
 
   : m_frame(frame)
 
47
        : m_frame(frame)
48
48
{
49
 
    Init( server, database, userName, password, port, sslmode, applicationname, true );
 
49
        Init( server, database, userName, password, port, sslmode, applicationname, true );
50
50
}
51
51
 
52
 
dbgPgConn::dbgPgConn(frmDebugger *frame, const dbgConnProp & props, bool startThread )
53
 
   : m_frame(frame)
 
52
dbgPgConn::dbgPgConn(frmDebugger *frame, const dbgConnProp &props, bool startThread )
 
53
        : m_frame(frame)
54
54
{
55
 
    Init(  props.m_host, props.m_database, props.m_userName, props.m_password, props.m_port, props.m_sslMode, props.m_applicationName, startThread );
 
55
        Init(  props.m_host, props.m_database, props.m_userName, props.m_password, props.m_port, props.m_sslMode, props.m_applicationName, startThread );
56
56
}
57
57
 
58
58
void dbgPgConn::Init( const wxString &server, const wxString &database, const wxString &username, const wxString &password, const wxString &port, int sslmode, const wxString &applicationname, bool startThread )
59
59
{
60
 
    bool utfConnectString = false;
61
 
    bool libcConnectString = false;
62
 
 
63
 
    m_pgConn       = NULL;
64
 
    m_majorVersion = 0;
65
 
    m_minorVersion = 0;
66
 
    m_debuggerApiVersion = DEBUGGER_UNKNOWN_API;
67
 
    m_isEdb = false;
68
 
 
69
 
    if( startThread )
70
 
        m_workerThread = new dbgPgThread( *this );
71
 
    else
72
 
        m_workerThread = NULL;
73
 
 
74
 
    wxString    msg;
75
 
    wxString    delimiter;
76
 
 
77
 
    // To keep the user interface thread responsive while we're waiting for the 
78
 
    // PostgreSQL server, we create a separate thread to interact with the 
79
 
    // database - the worker thread sleeps until the GUI thread signals that it
80
 
    // has some work to do.  When the result set arrives from the server, the 
81
 
    // worker thread creates a DBResult event and posts it to the GUI thread's
82
 
    // event queue.
83
 
 
84
 
    if( m_workerThread )
85
 
    {
86
 
        m_workerThread->Create();
87
 
        m_workerThread->Run();
88
 
    }
89
 
 
90
 
    // Figure out the hostname/IP address
91
 
    struct hostent *host;
92
 
    in_addr_t addr;
93
 
    wxString hostip, hostname;
 
60
        bool utfConnectString = false;
 
61
        bool libcConnectString = false;
 
62
 
 
63
        m_pgConn       = NULL;
 
64
        m_majorVersion = 0;
 
65
        m_minorVersion = 0;
 
66
        m_debuggerApiVersion = DEBUGGER_UNKNOWN_API;
 
67
        m_isEdb = false;
 
68
 
 
69
        if( startThread )
 
70
                m_workerThread = new dbgPgThread( *this );
 
71
        else
 
72
                m_workerThread = NULL;
 
73
 
 
74
        wxString        msg;
 
75
        wxString        delimiter;
 
76
 
 
77
        // To keep the user interface thread responsive while we're waiting for the
 
78
        // PostgreSQL server, we create a separate thread to interact with the
 
79
        // database - the worker thread sleeps until the GUI thread signals that it
 
80
        // has some work to do.  When the result set arrives from the server, the
 
81
        // worker thread creates a DBResult event and posts it to the GUI thread's
 
82
        // event queue.
 
83
 
 
84
        if( m_workerThread )
 
85
        {
 
86
                m_workerThread->Create();
 
87
                m_workerThread->Run();
 
88
        }
 
89
 
 
90
        // Figure out the hostname/IP address
 
91
        struct hostent *host;
 
92
        in_addr_t addr;
 
93
        wxString hostip, hostname;
94
94
 
95
95
#ifdef __WXMSW__
96
 
    struct in_addr ipaddr;
 
96
        struct in_addr ipaddr;
97
97
#else
98
 
    unsigned long ipaddr;
99
 
#endif
100
 
    
101
 
#ifndef __WXMSW__
102
 
    if (!(server.IsEmpty() || server.StartsWith(wxT("/"))))
103
 
    {
104
 
#endif
105
 
        addr = inet_addr(server.ToAscii());
106
 
        if (addr == INADDR_NONE) // szServer is not an IP address
107
 
        {
108
 
            host = gethostbyname(server.ToAscii());
109
 
            if (host == NULL)
110
 
            {
111
 
                wxLogError(__("Could not resolve hostname %s"), server.c_str());
112
 
                return;
113
 
            }
114
 
 
115
 
            memcpy(&(ipaddr),host->h_addr,host->h_length); 
116
 
            hostip = wxString::FromAscii(inet_ntoa(*((struct in_addr*) host->h_addr_list[0])));
117
 
            hostname = server;
118
 
        }
119
 
        else
120
 
        {
121
 
            hostip = server;
122
 
            hostname = server;
123
 
        }
124
 
#ifndef __WXMSW__
125
 
    }
126
 
    else
127
 
        hostname = server;
128
 
#endif
129
 
 
130
 
    // Build up a connection string
131
 
    wxString connectParams;
132
 
 
133
 
    if(hostname.Length()) 
134
 
    {
135
 
        connectParams.Append(wxT( "host="));
136
 
        connectParams.Append(hostname);
137
 
 
138
 
        msg += delimiter + server; delimiter = _(":");
139
 
    }
140
 
 
141
 
    if (hostip.Length()) 
142
 
    {
143
 
      connectParams.Append(wxT(" hostaddr="));
144
 
      connectParams.Append(hostip);
145
 
    }
146
 
 
147
 
    if( port.Length()) 
148
 
    {
149
 
        connectParams += wxT(" port=");
150
 
        connectParams += port;
151
 
 
152
 
        msg += delimiter + port; delimiter = _(":");
153
 
    }
154
 
 
155
 
 
156
 
    if( database.Length()) 
157
 
    {
158
 
        connectParams.Append(wxT(" dbname="));
159
 
        connectParams.Append(qtConnString(database));
160
 
 
161
 
        msg += delimiter + database; delimiter = _(":");
162
 
    }
163
 
 
164
 
    if(username.Length()) 
165
 
    {
166
 
        connectParams.Append(wxT(" user="));
167
 
        connectParams.Append(username );
168
 
 
169
 
        msg += delimiter + username; delimiter =  _(":");
170
 
    }
171
 
 
172
 
    if(password.Length()) 
173
 
    {
174
 
        connectParams.Append(wxT(" password="));
175
 
        connectParams.Append(password);
176
 
    }
177
 
 
178
 
    switch (sslmode)
179
 
    {
180
 
        case 1: 
181
 
            connectParams.Append(wxT(" sslmode=require"));   
182
 
            break;
183
 
 
184
 
        case 2: 
185
 
            connectParams.Append(wxT(" sslmode=prefer"));
186
 
            break;
187
 
 
188
 
        case 3: 
189
 
            connectParams.Append(wxT(" sslmode=allow"));
190
 
            break;
191
 
 
192
 
        case 4: 
193
 
            connectParams.Append(wxT(" sslmode=disable"));
194
 
            break;
 
98
        unsigned long ipaddr;
 
99
#endif
 
100
 
 
101
#ifndef __WXMSW__
 
102
        if (!(server.IsEmpty() || server.StartsWith(wxT("/"))))
 
103
        {
 
104
#endif
 
105
                addr = inet_addr(server.ToAscii());
 
106
                if (addr == INADDR_NONE) // szServer is not an IP address
 
107
                {
 
108
                        host = gethostbyname(server.ToAscii());
 
109
                        if (host == NULL)
 
110
                        {
 
111
                                wxLogError(__("Could not resolve hostname %s"), server.c_str());
 
112
                                return;
 
113
                        }
 
114
 
 
115
                        memcpy(&(ipaddr), host->h_addr, host->h_length);
 
116
                        hostip = wxString::FromAscii(inet_ntoa(*((struct in_addr *) host->h_addr_list[0])));
 
117
                        hostname = server;
 
118
                }
 
119
                else
 
120
                {
 
121
                        hostip = server;
 
122
                        hostname = server;
 
123
                }
 
124
#ifndef __WXMSW__
 
125
        }
 
126
        else
 
127
                hostname = server;
 
128
#endif
 
129
 
 
130
        // Build up a connection string
 
131
        wxString connectParams;
 
132
 
 
133
        if(hostname.Length())
 
134
        {
 
135
                connectParams.Append(wxT( "host="));
 
136
                connectParams.Append(hostname);
 
137
 
 
138
                msg += delimiter + server;
 
139
                delimiter = _(":");
 
140
        }
 
141
 
 
142
        if (hostip.Length())
 
143
        {
 
144
                connectParams.Append(wxT(" hostaddr="));
 
145
                connectParams.Append(hostip);
 
146
        }
 
147
 
 
148
        if(port.Length())
 
149
        {
 
150
                connectParams += wxT(" port=");
 
151
                connectParams += port;
 
152
 
 
153
                msg += delimiter + port;
 
154
                delimiter = _(":");
 
155
        }
 
156
 
 
157
 
 
158
        if(database.Length())
 
159
        {
 
160
                connectParams.Append(wxT(" dbname="));
 
161
                connectParams.Append(qtConnString(database));
 
162
 
 
163
                msg += delimiter + database;
 
164
                delimiter = _(":");
 
165
        }
 
166
 
 
167
        if(username.Length())
 
168
        {
 
169
                connectParams.Append(wxT(" user="));
 
170
                connectParams.Append(username );
 
171
 
 
172
                msg += delimiter + username;
 
173
                delimiter =  _(":");
 
174
        }
 
175
 
 
176
        if(password.Length())
 
177
        {
 
178
                connectParams.Append(wxT(" password="));
 
179
                connectParams.Append(password);
 
180
        }
 
181
 
 
182
        switch (sslmode)
 
183
        {
 
184
                case 1:
 
185
                        connectParams.Append(wxT(" sslmode=require"));
 
186
                        break;
 
187
 
 
188
                case 2:
 
189
                        connectParams.Append(wxT(" sslmode=prefer"));
 
190
                        break;
 
191
 
 
192
                case 3:
 
193
                        connectParams.Append(wxT(" sslmode=allow"));
 
194
                        break;
 
195
 
 
196
                case 4:
 
197
                        connectParams.Append(wxT(" sslmode=disable"));
 
198
                        break;
195
199
 
196
200
                case 5:
197
201
                        connectParams.Append(wxT(" sslmode=verify-ca"));
201
205
                        connectParams.Append(wxT(" sslmode=verify-full"));
202
206
                        break;
203
207
 
204
 
        default:
205
 
            break;
206
 
    }
207
 
    
208
 
    connectParams.Trim(true);
209
 
    connectParams.Trim(false);
 
208
                default:
 
209
                        break;
 
210
        }
 
211
 
 
212
        connectParams.Trim(true);
 
213
        connectParams.Trim(false);
210
214
 
211
215
#ifdef HAVE_CONNINFO_PARSE
212
 
    if (!applicationname.IsEmpty())
213
 
    {
214
 
        // Check connection string with application_name
215
 
        char *errmsg;
216
 
        wxString connectParams_with_applicationname = connectParams + wxT(" application_name='") + applicationname + wxT("'");
217
 
        if (PQconninfoParse(connectParams_with_applicationname.mb_str(wxConvUTF8), &errmsg))
218
 
        {
219
 
            utfConnectString = true;
220
 
            connectParams = connectParams_with_applicationname;
221
 
        }
222
 
        else if (PQconninfoParse(connectParams_with_applicationname.mb_str(wxConvLibc), &errmsg))
223
 
        {
224
 
            libcConnectString = true;
225
 
            connectParams = connectParams_with_applicationname;
226
 
        }
227
 
    }
 
216
        if (!applicationname.IsEmpty())
 
217
        {
 
218
                // Check connection string with application_name
 
219
                char *errmsg;
 
220
                wxString connectParams_with_applicationname = connectParams + wxT(" application_name='") + applicationname + wxT("'");
 
221
                if (PQconninfoParse(connectParams_with_applicationname.mb_str(wxConvUTF8), &errmsg))
 
222
                {
 
223
                        utfConnectString = true;
 
224
                        connectParams = connectParams_with_applicationname;
 
225
                }
 
226
                else if (PQconninfoParse(connectParams_with_applicationname.mb_str(wxConvLibc), &errmsg))
 
227
                {
 
228
                        libcConnectString = true;
 
229
                        connectParams = connectParams_with_applicationname;
 
230
                }
 
231
        }
228
232
#endif
229
 
        
230
 
    m_frame->getStatusBar()->SetStatusText( wxString::Format(_( "Connecting to %s" ), msg.c_str()), 1 );        
231
 
    wxCharBuffer cstrUTF=connectParams.mb_str(wxConvUTF8);
232
 
    wxCharBuffer cstrLibc=connectParams.mb_str(wxConvLibc);
233
 
 
234
 
    if (!libcConnectString)
235
 
        m_pgConn = PQconnectdb(cstrUTF);
236
 
    else
237
 
        m_pgConn = PQconnectdb(cstrLibc);
238
 
 
239
 
    if (PQstatus(m_pgConn) != CONNECTION_OK)
240
 
    {
241
 
        PQfinish(m_pgConn);
242
 
        m_pgConn = PQconnectdb(cstrLibc);
243
 
    }
244
 
 
245
 
    if( PQstatus( m_pgConn ) == CONNECTION_OK )
246
 
    {
247
 
        m_frame->getStatusBar()->SetStatusText( wxString::Format(_( "Connected to %s" ), msg.c_str()), 1 );
248
 
        PQsetClientEncoding( m_pgConn, "UNICODE" );
249
 
    }
250
 
    else
251
 
    {
252
 
        throw( std::runtime_error( PQerrorMessage( m_pgConn )));
253
 
    }
 
233
 
 
234
        m_frame->getStatusBar()->SetStatusText( wxString::Format(_( "Connecting to %s" ), msg.c_str()), 1 );
 
235
        wxCharBuffer cstrUTF = connectParams.mb_str(wxConvUTF8);
 
236
        wxCharBuffer cstrLibc = connectParams.mb_str(wxConvLibc);
 
237
 
 
238
        if (!libcConnectString)
 
239
                m_pgConn = PQconnectdb(cstrUTF);
 
240
        else
 
241
                m_pgConn = PQconnectdb(cstrLibc);
 
242
 
 
243
        if (PQstatus(m_pgConn) != CONNECTION_OK)
 
244
        {
 
245
                PQfinish(m_pgConn);
 
246
                m_pgConn = PQconnectdb(cstrLibc);
 
247
        }
 
248
 
 
249
        if( PQstatus( m_pgConn ) == CONNECTION_OK )
 
250
        {
 
251
                m_frame->getStatusBar()->SetStatusText( wxString::Format(_( "Connected to %s" ), msg.c_str()), 1 );
 
252
                PQsetClientEncoding( m_pgConn, "UNICODE" );
 
253
        }
 
254
        else
 
255
        {
 
256
                throw( std::runtime_error( PQerrorMessage( m_pgConn )));
 
257
        }
254
258
}
255
259
 
256
260
dbgPgConn::~dbgPgConn()
257
261
{
258
 
    Close();
 
262
        Close();
259
263
}
260
264
 
261
265
////////////////////////////////////////////////////////////////////////////////
266
270
 
267
271
bool dbgPgConn::isConnected( void ) const
268
272
{
269
 
    if( m_pgConn && PQstatus( m_pgConn ) == CONNECTION_OK )
270
 
        return( true );
271
 
    else
272
 
        return( false );
 
273
        if( m_pgConn && PQstatus( m_pgConn ) == CONNECTION_OK )
 
274
                return( true );
 
275
        else
 
276
                return( false );
273
277
}
274
278
 
275
279
////////////////////////////////////////////////////////////////////////////////
280
284
 
281
285
const wxString dbgPgConn::getName() const
282
286
{
283
 
    wxString    result = wxString( PQhost( m_pgConn ), wxConvUTF8 );
284
 
 
285
 
    result += wxT( "/" );
286
 
    result.Append( wxString( PQdb( m_pgConn ), wxConvUTF8 ));
287
 
 
288
 
    return( result );
 
287
        wxString        result = wxString( PQhost( m_pgConn ), wxConvUTF8 );
 
288
 
 
289
        result += wxT( "/" );
 
290
        result.Append( wxString( PQdb( m_pgConn ), wxConvUTF8 ));
 
291
 
 
292
        return( result );
289
293
 
290
294
}
291
295
 
296
300
 
297
301
const wxString dbgPgConn::getHost() const
298
302
{
299
 
    return( wxString( PQhost( m_pgConn ), wxConvUTF8 ));
 
303
        return( wxString( PQhost( m_pgConn ), wxConvUTF8 ));
300
304
}
301
305
 
302
306
////////////////////////////////////////////////////////////////////////////////
306
310
 
307
311
const wxString dbgPgConn::getDatabase() const
308
312
{
309
 
    return( wxString( PQdb( m_pgConn ), wxConvUTF8 ));
 
313
        return( wxString( PQdb( m_pgConn ), wxConvUTF8 ));
310
314
}
311
315
 
312
316
////////////////////////////////////////////////////////////////////////////////
314
318
//
315
319
//    Returns the libpq connection handle for this dbgPgConn
316
320
 
317
 
PGconn * dbgPgConn::getConnection()
 
321
PGconn *dbgPgConn::getConnection()
318
322
{
319
 
    return( m_pgConn );
 
323
        return( m_pgConn );
320
324
}
321
325
 
322
326
////////////////////////////////////////////////////////////////////////////////
325
329
//     The GUI thread invokes startCommand() when the user asks us to execute a
326
330
//    command.  We pass off the real work to the worker thread.
327
331
 
328
 
void dbgPgConn::startCommand( const wxString &command, wxEvtHandler * caller, wxEventType eventType, dbgPgParams *params )
 
332
void dbgPgConn::startCommand( const wxString &command, wxEvtHandler *caller, wxEventType eventType, dbgPgParams *params )
329
333
{
330
 
    wxLogSql(wxT("%s"), command.c_str());
 
334
        wxLogSql(wxT("%s"), command.c_str());
331
335
 
332
 
    m_workerThread->startCommand(command, caller, eventType, params);
 
336
        m_workerThread->startCommand(command, caller, eventType, params);
333
337
}
334
338
 
335
 
PGresult * dbgPgConn::waitForCommand( const wxString &command )
 
339
PGresult *dbgPgConn::waitForCommand( const wxString &command )
336
340
{
337
 
    wxLogSql(wxT("%s"), command.c_str());
338
 
 
339
 
    PGresult * result = PQexec( m_pgConn, command.mb_str( wxConvUTF8 ));
340
 
 
341
 
    return( result );
 
341
        wxLogSql(wxT("%s"), command.c_str());
 
342
 
 
343
        PGresult *result = PQexec( m_pgConn, command.mb_str( wxConvUTF8 ));
 
344
 
 
345
        return( result );
342
346
}
343
347
 
344
348
////////////////////////////////////////////////////////////////////////////////
346
350
//
347
351
//     Register a NOTICE handler with the libpq library - libpq will invoke the
348
352
//  given handler whenever a NOTICE arrives on this connection. libpq will
349
 
//    pass 'arg' to the handler. 'handler' is typically a static function and 
350
 
//    'arg' is often a pointer to an object.  That lets you use a regular member 
351
 
//    function as a callback (because 'arg' is mapped into a 'this' pointer by the 
 
353
//    pass 'arg' to the handler. 'handler' is typically a static function and
 
354
//    'arg' is often a pointer to an object.  That lets you use a regular member
 
355
//    function as a callback (because 'arg' is mapped into a 'this' pointer by the
352
356
//     callback function).
353
357
 
354
 
void dbgPgConn::setNoticeHandler( PQnoticeProcessor handler, void * arg )
 
358
void dbgPgConn::setNoticeHandler( PQnoticeProcessor handler, void *arg )
355
359
{
356
 
    PQnoticeProcessor p=NULL;
357
 
    p = PQsetNoticeProcessor( m_pgConn, handler, arg );
 
360
        PQnoticeProcessor p = NULL;
 
361
        p = PQsetNoticeProcessor( m_pgConn, handler, arg );
358
362
}
359
363
 
360
364
void dbgPgConn::Close()
361
365
{
362
 
    // Attempt to cancel any ongoing query
363
 
    Cancel();
364
 
 
365
 
    // Wait a tenth of a second or so for things to sort themselves out.
366
 
    // Otherwise on Windows things can get funky here ...
367
 
    wxMilliSleep(100);
368
 
 
369
 
    if (m_workerThread)
370
 
    {
371
 
        m_workerThread->Die();
372
 
        m_workerThread->Wait();
373
 
 
374
 
        delete m_workerThread;
375
 
        m_workerThread = NULL;
376
 
    }
377
 
 
378
 
    if (m_pgConn)
379
 
        PQfinish(m_pgConn);
380
 
 
381
 
    m_pgConn = NULL;
 
366
        // Attempt to cancel any ongoing query
 
367
        Cancel();
 
368
 
 
369
        // Wait a tenth of a second or so for things to sort themselves out.
 
370
        // Otherwise on Windows things can get funky here ...
 
371
        wxMilliSleep(100);
 
372
 
 
373
        if (m_workerThread)
 
374
        {
 
375
                m_workerThread->Die();
 
376
                m_workerThread->Wait();
 
377
 
 
378
                delete m_workerThread;
 
379
                m_workerThread = NULL;
 
380
        }
 
381
 
 
382
        if (m_pgConn)
 
383
                PQfinish(m_pgConn);
 
384
 
 
385
        m_pgConn = NULL;
382
386
}
383
387
 
384
388
void dbgPgConn::Cancel()
385
389
{
386
 
    // Attempt to cancel any ongoing query
387
 
    if (m_pgConn)
388
 
    {
389
 
        PGcancel *cancel = PQgetCancel(m_pgConn);
390
 
        char errbuf[256];
391
 
        PQcancel(cancel, errbuf, sizeof(errbuf));
392
 
        PQfreeCancel(cancel);
393
 
    }
 
390
        // Attempt to cancel any ongoing query
 
391
        if (m_pgConn)
 
392
        {
 
393
                PGcancel *cancel = PQgetCancel(m_pgConn);
 
394
                char errbuf[256];
 
395
                PQcancel(cancel, errbuf, sizeof(errbuf));
 
396
                PQfreeCancel(cancel);
 
397
        }
394
398
}
395
399
 
396
400
// Check the backend version
397
401
bool dbgPgConn::BackendMinimumVersion(int major, int minor)
398
402
{
399
 
    if (!m_majorVersion)
400
 
    {
401
 
        wxString version=GetVersionString();
402
 
        sscanf(version.ToAscii(), "%*s %d.%d", &m_majorVersion, &m_minorVersion);
403
 
        m_isEdb = version.Upper().Matches(wxT("ENTERPRISEDB*"));
404
 
 
405
 
        // EnterpriseDB 8.3 beta 1 & 2 and possibly later actually have PostgreSQL 8.2 style
406
 
        // catalogs. This is expected to change either before GA, but in the meantime we
407
 
        // need to check the catalogue version in more detail, and if we don't see what looks
408
 
        // like a 8.3 catalog, force the version number back to 8.2. Yuck.
409
 
        if (m_isEdb && m_majorVersion == 8 && m_minorVersion == 3)
410
 
        {
411
 
            PGresult *res;
412
 
            wxString result;
413
 
 
414
 
            res = waitForCommand(wxT( "SELECT count(*) FROM pg_attribute WHERE attname = 'proconfig' AND attrelid = 'pg_proc'::regclass"));
415
 
 
416
 
            if (PQresultStatus(res) == PGRES_TUPLES_OK)
417
 
            {
418
 
                // Retrieve the query result and return it.
419
 
                result=wxString(PQgetvalue(res, 0, 0), wxConvUTF8);
420
 
 
421
 
                // Cleanup & exit
422
 
                PQclear(res);
423
 
            }
424
 
            if (result == wxT("0"))
425
 
                m_minorVersion = 2; 
426
 
        }
427
 
        else
428
 
            m_isGreenplum = version.Upper().Matches(wxT("*GREENPLUM DATABASE*"));
429
 
    }
430
 
 
431
 
    return m_majorVersion > major || (m_majorVersion == major && m_minorVersion >= minor);
 
403
        if (!m_majorVersion)
 
404
        {
 
405
                wxString version = GetVersionString();
 
406
                sscanf(version.ToAscii(), "%*s %d.%d", &m_majorVersion, &m_minorVersion);
 
407
                m_isEdb = version.Upper().Matches(wxT("ENTERPRISEDB*"));
 
408
 
 
409
                // EnterpriseDB 8.3 beta 1 & 2 and possibly later actually have PostgreSQL 8.2 style
 
410
                // catalogs. This is expected to change either before GA, but in the meantime we
 
411
                // need to check the catalogue version in more detail, and if we don't see what looks
 
412
                // like a 8.3 catalog, force the version number back to 8.2. Yuck.
 
413
                if (m_isEdb && m_majorVersion == 8 && m_minorVersion == 3)
 
414
                {
 
415
                        PGresult *res;
 
416
                        wxString result;
 
417
 
 
418
                        res = waitForCommand(wxT( "SELECT count(*) FROM pg_attribute WHERE attname = 'proconfig' AND attrelid = 'pg_proc'::regclass"));
 
419
 
 
420
                        if (PQresultStatus(res) == PGRES_TUPLES_OK)
 
421
                        {
 
422
                                // Retrieve the query result and return it.
 
423
                                result = wxString(PQgetvalue(res, 0, 0), wxConvUTF8);
 
424
 
 
425
                                // Cleanup & exit
 
426
                                PQclear(res);
 
427
                        }
 
428
                        if (result == wxT("0"))
 
429
                                m_minorVersion = 2;
 
430
                }
 
431
                else
 
432
                        m_isGreenplum = version.Upper().Matches(wxT("*GREENPLUM DATABASE*"));
 
433
        }
 
434
 
 
435
        return m_majorVersion > major || (m_majorVersion == major && m_minorVersion >= minor);
432
436
}
433
437
 
434
438
// Check the EDB backend version
435
439
bool dbgPgConn::EdbMinimumVersion(int major, int minor)
436
440
{
437
 
    return BackendMinimumVersion(major, minor) && GetIsEdb();
 
441
        return BackendMinimumVersion(major, minor) && GetIsEdb();
438
442
}
439
443
 
440
444
// Get the debugger API version
441
445
DebuggerApiVersions dbgPgConn::DebuggerApiVersion()
442
446
{
443
 
    if (m_debuggerApiVersion > 0)
444
 
        return m_debuggerApiVersion;
445
 
 
446
 
    // The v1 protocol didn't have pldbg_get_proxy_info()
447
 
    wxString result;
448
 
 
449
 
    PGresult *res = waitForCommand(wxT( "SELECT count(*) FROM pg_proc WHERE proname = 'pldbg_get_proxy_info';"));
450
 
 
451
 
    if (PQresultStatus(res) == PGRES_TUPLES_OK)
452
 
    {
453
 
        // Retrieve the query result and return it.
454
 
        if (wxString(PQgetvalue(res, 0, 0), wxConvUTF8) == wxT("0"))
455
 
        {
456
 
            PQclear(res);
457
 
            m_debuggerApiVersion = DEBUGGER_V1_API;
458
 
            return DEBUGGER_V1_API;
459
 
        }
460
 
 
461
 
        PQclear(res);
462
 
    }
463
 
    else
464
 
    {
465
 
        wxLogError(wxT("%s"), wxString(PQerrorMessage(m_pgConn), wxConvUTF8).c_str());
466
 
        return DEBUGGER_UNKNOWN_API;
467
 
    }
468
 
 
469
 
    // We have pldbg_get_proxy_info, so use it to get the API version
470
 
    res = waitForCommand(wxT( "SELECT proxyapiver FROM pldbg_get_proxy_info();"));
471
 
 
472
 
    if (PQresultStatus(res) == PGRES_TUPLES_OK)
473
 
    {
474
 
        // Retrieve the query result and return it.
475
 
        m_debuggerApiVersion = (DebuggerApiVersions)atoi(wxString(PQgetvalue(res, 0, 0), wxConvUTF8).ToAscii());
476
 
        PQclear(res);
477
 
    }
478
 
    else
479
 
    {
480
 
        wxLogError(wxT("%s"), wxString(PQerrorMessage(m_pgConn), wxConvUTF8).c_str());
481
 
        return DEBUGGER_UNKNOWN_API;
482
 
    }
483
 
 
484
 
    return m_debuggerApiVersion;
 
447
        if (m_debuggerApiVersion > 0)
 
448
                return m_debuggerApiVersion;
 
449
 
 
450
        // The v1 protocol didn't have pldbg_get_proxy_info()
 
451
        wxString result;
 
452
 
 
453
        PGresult *res = waitForCommand(wxT( "SELECT count(*) FROM pg_proc WHERE proname = 'pldbg_get_proxy_info';"));
 
454
 
 
455
        if (PQresultStatus(res) == PGRES_TUPLES_OK)
 
456
        {
 
457
                // Retrieve the query result and return it.
 
458
                if (wxString(PQgetvalue(res, 0, 0), wxConvUTF8) == wxT("0"))
 
459
                {
 
460
                        PQclear(res);
 
461
                        m_debuggerApiVersion = DEBUGGER_V1_API;
 
462
                        return DEBUGGER_V1_API;
 
463
                }
 
464
 
 
465
                PQclear(res);
 
466
        }
 
467
        else
 
468
        {
 
469
                wxLogError(wxT("%s"), wxString(PQerrorMessage(m_pgConn), wxConvUTF8).c_str());
 
470
                return DEBUGGER_UNKNOWN_API;
 
471
        }
 
472
 
 
473
        // We have pldbg_get_proxy_info, so use it to get the API version
 
474
        res = waitForCommand(wxT( "SELECT proxyapiver FROM pldbg_get_proxy_info();"));
 
475
 
 
476
        if (PQresultStatus(res) == PGRES_TUPLES_OK)
 
477
        {
 
478
                // Retrieve the query result and return it.
 
479
                m_debuggerApiVersion = (DebuggerApiVersions)atoi(wxString(PQgetvalue(res, 0, 0), wxConvUTF8).ToAscii());
 
480
                PQclear(res);
 
481
        }
 
482
        else
 
483
        {
 
484
                wxLogError(wxT("%s"), wxString(PQerrorMessage(m_pgConn), wxConvUTF8).c_str());
 
485
                return DEBUGGER_UNKNOWN_API;
 
486
        }
 
487
 
 
488
        return m_debuggerApiVersion;
485
489
}
486
490
 
487
491
 
488
492
wxString dbgPgConn::GetVersionString()
489
493
{
490
 
    PGresult *res;
491
 
    wxString result;
492
 
 
493
 
    res = waitForCommand(wxT( "SELECT version();"));
494
 
 
495
 
    if (PQresultStatus(res) == PGRES_TUPLES_OK)
496
 
    {
497
 
        // Retrieve the query result and return it.
498
 
        result=wxString(PQgetvalue(res, 0, 0), wxConvUTF8);
499
 
 
500
 
        // Cleanup & exit
501
 
        PQclear(res);
502
 
    }
503
 
 
504
 
    return result;
 
494
        PGresult *res;
 
495
        wxString result;
 
496
 
 
497
        res = waitForCommand(wxT( "SELECT version();"));
 
498
 
 
499
        if (PQresultStatus(res) == PGRES_TUPLES_OK)
 
500
        {
 
501
                // Retrieve the query result and return it.
 
502
                result = wxString(PQgetvalue(res, 0, 0), wxConvUTF8);
 
503
 
 
504
                // Cleanup & exit
 
505
                PQclear(res);
 
506
        }
 
507
 
 
508
        return result;
505
509
}
506
510
 
507
511
bool dbgPgConn::GetIsEdb()
508
512
{
509
 
    // to retrieve edb flag
510
 
    BackendMinimumVersion(0,0);
511
 
    return m_isEdb; 
 
513
        // to retrieve edb flag
 
514
        BackendMinimumVersion(0, 0);
 
515
        return m_isEdb;
512
516
}
513
517
 
514
518
bool dbgPgConn::GetIsGreenplum()
515
519
{
516
 
    // to retrieve edb flag
517
 
    BackendMinimumVersion(0,0);
518
 
    return m_isGreenplum; 
 
520
        // to retrieve edb flag
 
521
        BackendMinimumVersion(0, 0);
 
522
        return m_isGreenplum;
519
523
}
520
524
 
521
 
 
522
 
 
 
525
wxString dbgPgConn::qtDbString(const wxString &value)
 
526
{
 
527
        wxString result = value;
 
528
 
 
529
        result.Replace(wxT("\\"), wxT("\\\\"));
 
530
        result.Replace(wxT("'"), wxT("''"));
 
531
        result.Append(wxT("'"));
 
532
 
 
533
        if (BackendMinimumVersion(8, 1))
 
534
        {
 
535
                if (result.Contains(wxT("\\")))
 
536
                        result.Prepend(wxT("E'"));
 
537
                else
 
538
                        result.Prepend(wxT("'"));
 
539
        }
 
540
        else
 
541
                result.Prepend(wxT("'"));
 
542
 
 
543
        return result;
 
544
}
 
 
b'\\ No newline at end of file'