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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL 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
PGCONN_SSHTUNNEL_ERROR
};
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;
void SetError(PGresult *_res = NULL, wxMBConv *_conv = NULL);
} pgError;
class pgConn
{
public:
pgConn(const wxString &server = wxT(""), const wxString &service = wxT(""), const wxString &hostaddr = wxT(""),
const wxString &database = wxT(""), const wxString &username = wxT(""), const wxString &password = wxT(""),
int port = 5432, const wxString &rolename = wxT(""), int sslmode = 0, OID oid = 0,
const wxString &applicationname = wxT("pgAdmin"),
const wxString &sslcert = wxT(""), const wxString &sslkey = wxT(""), const wxString &sslrootcert = wxT(""), const wxString &sslcrl = wxT(""),
const bool sslcompression = true);
~pgConn();
bool IsSuperuser();
bool HasPrivilege(const wxString &objTyp, const wxString &objName, const wxString &priv);
bool HasFeature(int feature = 0, bool forceCheck = false);
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(const wxString &_appName = wxT(""));
static void ExamineLibpqVersion();
static double GetLibpqVersion()
{
return libpqVersion;
}
static bool IsValidServerEncoding(int encid)
{
return pg_valid_server_encoding_id(encid) == 0 ? false : true;
}
void Close();
bool Reconnect();
bool ExecuteVoid(const wxString &sql, bool reportError = true);
wxString ExecuteScalar(const wxString &sql, bool reportError = true);
pgSet *ExecuteSet(const wxString &sql, bool reportError = true);
void CancelExecution(void);
wxString GetHostAddr() const
{
return save_hostaddr;
}
wxString GetService() const
{
return save_service;
}
wxString GetUser() const
{
return wxString(PQuser(conn), *conv);
}
wxString GetPassword() const
{
return wxString(PQpass(conn), *conv);
}
wxString GetRole() const
{
return dbRole;
}
wxString GetHost() const
{
return dbHost;
}
wxString GetHostName() const
{
return dbHostName;
}
wxString GetDbname() const
{
return save_database;
}
wxString GetApplicationName() const
{
return save_applicationname;
}
wxString GetSSLCert() const
{
return save_sslcert;
}
wxString GetSSLKey() const
{
return save_sslkey;
}
wxString GetSSLRootCert() const
{
return save_sslrootcert;
}
wxString GetSSLCrl() const
{
return save_sslcrl;
}
bool GetSSLCompression() const
{
return save_sslcompression;
}
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();
void Reset();
bool StartCopy(const wxString query);
bool PutCopyData(const char *data, long count);
bool EndPutCopy(const wxString errormsg);
bool GetCopyFinalStatus(void);
bool TableHasColumn(wxString schemaname, wxString tblname, const wxString &colname);
protected:
PGconn *conn;
PGcancel *m_cancelConn;
wxMutex m_cancelConnMutex;
int lastResultStatus;
int connStatus;
void SetLastResultError(PGresult *res, const wxString &msg = wxEmptyString);
void SetConnCancel(void);
void ResetConnCancel(void);
pgError lastResultError;
wxMBConv *conv;
bool needColQuoting, utfConnectString;
wxString dbRole, dbHost, dbHostName;
OID lastSystemOID;
OID dbOid;
void *noticeArg;
PQnoticeProcessor noticeProc;
static double libpqVersion;
friend class pgQueryThread;
private:
bool DoConnect();
bool Initialize();
wxString qtString(const wxString &value);
bool features[32];
int minorVersion, majorVersion, patchVersion;
bool isEdb;
bool isGreenplum;
wxString reservedNamespaces;
wxString connstr;
wxString save_server, save_service, save_hostaddr, save_database, save_username, save_password, save_rolename, save_applicationname;
wxString save_sslcert, save_sslkey, save_sslrootcert, save_sslcrl;
int save_port, save_sslmode;
bool save_sslcompression;
OID save_oid;
};
#endif
|