~ubuntu-branches/debian/sid/pgadmin3/sid

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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
// RCS-ID:      $Id: pgConn.h 7819 2009-04-24 11:49:06Z mha $
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
// This software is released under the BSD Licence
//
// pgConn.h - PostgreSQL Connection class
//
//////////////////////////////////////////////////////////////////////////

#ifndef PGCONN_H
#define PGCONN_H

// wxWindows headers
#include <wx/wx.h>

// PostgreSQL headers
#include <libpq-fe.h>

// App headers
#include "pgSet.h"

// status enums
enum 
{
    PGCONN_OK = CONNECTION_OK,
    PGCONN_BAD = CONNECTION_BAD,
    PGCONN_REFUSED,
    PGCONN_DNSERR,
    PGCONN_ABORTED,     // connect user aborted
    PGCONN_BROKEN       // tcp/pipe broken
};

enum 
{
    PGCONN_EMPTY_QUERY = PGRES_EMPTY_QUERY,
    PGCONN_COMMAND_OK = PGRES_COMMAND_OK,
    PGCONN_TUPLES_OK = PGRES_TUPLES_OK,
    PGCONN_COPY_OUT = PGRES_COPY_OUT,
    PGCONN_COPY_IN = PGRES_COPY_IN,
    PGCONN_BAD_RESPONSE = PGRES_BAD_RESPONSE,
    PGCONN_NONFATAL_ERROR = PGRES_NONFATAL_ERROR,
    PGCONN_FATAL_ERROR = PGRES_FATAL_ERROR
};

enum
{
    PGCONN_TXSTATUS_IDLE = PQTRANS_IDLE,
    PGCONN_TXSTATUS_ACTIVE = PQTRANS_ACTIVE,
    PGCONN_TXSTATUS_INTRANS = PQTRANS_INTRANS,
    PGCONN_TXSTATUS_INERROR = PQTRANS_INERROR,
    PGCONN_TXSTATUS_UNKNOWN = PQTRANS_UNKNOWN
};

// Our version of a pgNotify
typedef struct pgNotification {
    wxString name;
    int pid;
    wxString data;
} pgNotification;

// An error record
typedef struct pgError {
    wxString severity;
    wxString sql_state;
    wxString msg_primary;
    wxString msg_detail;
    wxString msg_hint;
    wxString statement_pos;
    wxString internal_pos;
    wxString internal_query;
    wxString context;
    wxString source_file;
    wxString source_line;
    wxString source_function;
    wxString formatted_msg;
} pgError;


class pgConn
{
public:
    pgConn(const wxString& server = wxT(""), const wxString& database = wxT(""), const wxString& username = wxT(""), const wxString& password = wxT(""), int port = 5432, int sslmode=0, OID oid=0);
    ~pgConn();

    bool HasPrivilege(const wxString &objTyp, const wxString &objName, const wxString &priv);
    bool HasFeature(int feature=0);
    bool BackendMinimumVersion(int major, int minor);
    bool BackendMinimumVersion(int major, int minor, int patch);
    bool EdbMinimumVersion(int major, int minor);
    wxString SystemNamespaceRestriction(const wxString &nsp);
    int GetMajorVersion() const { return majorVersion; }
    int GetMinorVersion() const { return minorVersion; }
    bool GetIsEdb();
    bool GetIsGreenplum();
    wxString EncryptPassword(const wxString &user, const wxString &password);
    wxString qtDbString(const wxString& value);
    pgConn *Duplicate();

    static void ExamineLibpqVersion();
    static double GetLibpqVersion() { return libpqVersion; }

    void Close();
    bool ExecuteVoid(const wxString& sql, bool reportError = true);
    wxString ExecuteScalar(const wxString& sql);
    pgSet *ExecuteSet(const wxString& sql);
    wxString GetUser() const { return wxString(PQuser(conn), *conv); }
    wxString GetPassword() const { return wxString(PQpass(conn), *conv); }
    wxString GetHost() const { return dbHost; }
    wxString GetHostName() const { return dbHostName; }
    wxString GetHostAddress() const { return dbHostAddress; }
    wxString GetDbname() const { return dbname; }
    wxString GetName() const;
    bool GetNeedUtfConnectString() { return utfConnectString; }
    int GetPort() const { return atoi(PQport(conn)); };
    wxString GetTTY() const { return wxString(PQtty(conn), *conv); }
    wxString GetOptions() const { return wxString(PQoptions(conn), *conv); }
    int GetSslMode() const { return save_sslmode; }
    wxString GetSslModeName();
    int GetBackendPID() const { return PQbackendPID(conn); }
    int GetStatus() const;
    int GetLastResultStatus() const { return lastResultStatus; }
    bool IsAlive();
    wxString GetLastError() const;
    pgError GetLastResultError() const { return lastResultError; }
    wxString GetVersionString();
    OID GetLastSystemOID() const { return lastSystemOID; }
    OID GetDbOid() const { return dbOid; }
    void RegisterNoticeProcessor(PQnoticeProcessor proc, void *arg);
    wxMBConv *GetConv() { return conv; };

    void LogError(const bool quiet=false);

    bool IsSSLconnected();
    PGconn *connection() { return conn; }
    void Notice(const char *msg);
    pgNotification *GetNotification();
    int GetTxStatus();

    bool TableHasColumn(wxString schemaname, wxString tblname, const wxString& colname);

protected:
    PGconn *conn;
    int lastResultStatus;

    int connStatus;

    void SetLastResultError(PGresult *res, const wxString &msg = wxEmptyString);
    pgError lastResultError;

    wxMBConv *conv;
    bool needColQuoting, utfConnectString;
    wxString dbHost, dbHostName, dbHostAddress, dbname;
    OID lastSystemOID;
    OID dbOid;

    void *noticeArg;
    PQnoticeProcessor noticeProc;
    static double libpqVersion;

    friend class pgQueryThread;

private:
    wxString qtString(const wxString& value);

    bool features[32];
    int minorVersion, majorVersion, patchVersion;
    bool isEdb;
    bool isGreenplum;

    wxString reservedNamespaces;

    wxString save_server, save_database, save_username, save_password;
    int save_port, save_sslmode;
    OID save_oid;
};

#endif