1 ////////////////////////////////////////////////////////////////////////// 2 // 3 // pgAdmin III - PostgreSQL Tools 4 // 5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team 6 // This software is released under the PostgreSQL Licence 7 // 8 // pgConn.h - PostgreSQL Connection class 9 // 10 ////////////////////////////////////////////////////////////////////////// 11 12 #ifndef PGCONN_H 13 #define PGCONN_H 14 15 // wxWindows headers 16 #include <wx/wx.h> 17 18 // PostgreSQL headers 19 #include <libpq-fe.h> 20 21 // App headers 22 #include "pgSet.h" 23 24 // status enums 25 enum 26 { 27 PGCONN_OK = CONNECTION_OK, 28 PGCONN_BAD = CONNECTION_BAD, 29 PGCONN_REFUSED, 30 PGCONN_DNSERR, 31 PGCONN_ABORTED, // connect user aborted 32 PGCONN_BROKEN, // tcp/pipe broken 33 PGCONN_SSHTUNNEL_ERROR 34 }; 35 36 enum 37 { 38 PGCONN_EMPTY_QUERY = PGRES_EMPTY_QUERY, 39 PGCONN_COMMAND_OK = PGRES_COMMAND_OK, 40 PGCONN_TUPLES_OK = PGRES_TUPLES_OK, 41 PGCONN_COPY_OUT = PGRES_COPY_OUT, 42 PGCONN_COPY_IN = PGRES_COPY_IN, 43 PGCONN_BAD_RESPONSE = PGRES_BAD_RESPONSE, 44 PGCONN_NONFATAL_ERROR = PGRES_NONFATAL_ERROR, 45 PGCONN_FATAL_ERROR = PGRES_FATAL_ERROR 46 }; 47 48 enum 49 { 50 PGCONN_TXSTATUS_IDLE = PQTRANS_IDLE, 51 PGCONN_TXSTATUS_ACTIVE = PQTRANS_ACTIVE, 52 PGCONN_TXSTATUS_INTRANS = PQTRANS_INTRANS, 53 PGCONN_TXSTATUS_INERROR = PQTRANS_INERROR, 54 PGCONN_TXSTATUS_UNKNOWN = PQTRANS_UNKNOWN 55 }; 56 57 // Our version of a pgNotify 58 typedef struct pgNotification 59 { 60 wxString name; 61 int pid; 62 wxString data; 63 } pgNotification; 64 65 // An error record 66 typedef struct pgError 67 { 68 wxString severity; 69 wxString sql_state; 70 wxString msg_primary; 71 wxString msg_detail; 72 wxString msg_hint; 73 wxString statement_pos; 74 wxString internal_pos; 75 wxString internal_query; 76 wxString context; 77 wxString source_file; 78 wxString source_line; 79 wxString source_function; 80 wxString formatted_msg; 81 82 void SetError(PGresult *_res = NULL, wxMBConv *_conv = NULL); 83 } pgError; 84 85 class pgConn 86 { 87 public: 88 pgConn(const wxString &server = wxT(""), const wxString &service = wxT(""), const wxString &hostaddr = wxT(""), 89 const wxString &database = wxT(""), const wxString &username = wxT(""), const wxString &password = wxT(""), 90 int port = 5432, const wxString &rolename = wxT(""), int sslmode = 0, OID oid = 0, 91 const wxString &applicationname = wxT("pgAdmin"), 92 const wxString &sslcert = wxT(""), const wxString &sslkey = wxT(""), const wxString &sslrootcert = wxT(""), const wxString &sslcrl = wxT(""), 93 const bool sslcompression = true); 94 ~pgConn(); 95 96 bool IsSuperuser(); 97 bool HasPrivilege(const wxString &objTyp, const wxString &objName, const wxString &priv); 98 bool HasFeature(int feature = 0, bool forceCheck = false); 99 bool BackendMinimumVersion(int major, int minor); 100 bool BackendMinimumVersion(int major, int minor, int patch); 101 bool EdbMinimumVersion(int major, int minor); 102 wxString SystemNamespaceRestriction(const wxString &nsp); GetMajorVersion()103 int GetMajorVersion() const 104 { 105 return majorVersion; 106 } GetMinorVersion()107 int GetMinorVersion() const 108 { 109 return minorVersion; 110 } 111 bool GetIsEdb(); 112 bool GetIsGreenplum(); 113 bool GetIsHawq(); 114 wxString EncryptPassword(const wxString &user, const wxString &password); 115 wxString qtDbString(const wxString &value); 116 pgConn *Duplicate(const wxString &_appName = wxT("")); 117 118 static void ExamineLibpqVersion(); GetLibpqVersion()119 static double GetLibpqVersion() 120 { 121 return libpqVersion; 122 } 123 IsValidServerEncoding(int encid)124 static bool IsValidServerEncoding(int encid) 125 { 126 return pg_valid_server_encoding_id(encid) == 0 ? false : true; 127 } 128 129 void Close(); 130 bool Reconnect(); 131 bool ExecuteVoid(const wxString &sql, bool reportError = true); 132 wxString ExecuteScalar(const wxString &sql, bool reportError = true); 133 pgSet *ExecuteSet(const wxString &sql, bool reportError = true); 134 void CancelExecution(void); 135 GetHostAddr()136 wxString GetHostAddr() const 137 { 138 return save_hostaddr; 139 } GetService()140 wxString GetService() const 141 { 142 return save_service; 143 } GetUser()144 wxString GetUser() const 145 { 146 return conn ? wxString(PQuser(conn), *conv) : wxT(""); 147 } GetPassword()148 wxString GetPassword() const 149 { 150 return conn ? wxString(PQpass(conn), *conv) : wxT(""); 151 } GetRole()152 wxString GetRole() const 153 { 154 return dbRole; 155 } GetHost()156 wxString GetHost() const 157 { 158 return dbHost; 159 } GetHostName()160 wxString GetHostName() const 161 { 162 return dbHostName; 163 } GetDbname()164 wxString GetDbname() const 165 { 166 return save_database; 167 } GetApplicationName()168 wxString GetApplicationName() const 169 { 170 return save_applicationname; 171 } GetSSLCert()172 wxString GetSSLCert() const 173 { 174 return save_sslcert; 175 } GetSSLKey()176 wxString GetSSLKey() const 177 { 178 return save_sslkey; 179 } GetSSLRootCert()180 wxString GetSSLRootCert() const 181 { 182 return save_sslrootcert; 183 } GetSSLCrl()184 wxString GetSSLCrl() const 185 { 186 return save_sslcrl; 187 } GetSSLCompression()188 bool GetSSLCompression() const 189 { 190 return save_sslcompression; 191 } 192 wxString GetName() const; GetNeedUtfConnectString()193 bool GetNeedUtfConnectString() 194 { 195 return utfConnectString; 196 } GetPort()197 int GetPort() const 198 { 199 return conn ? atoi(PQport(conn)) : 0; 200 }; GetTTY()201 wxString GetTTY() const 202 { 203 return conn ? wxString(PQtty(conn), *conv) : wxT(""); 204 } GetOptions()205 wxString GetOptions() const 206 { 207 return conn ? wxString(PQoptions(conn), *conv) : wxT(""); 208 } GetSslMode()209 int GetSslMode() const 210 { 211 return save_sslmode; 212 } 213 wxString GetSslModeName(); GetBackendPID()214 int GetBackendPID() const 215 { 216 return conn ? PQbackendPID(conn) : 0; 217 } 218 int GetStatus() const; GetLastResultStatus()219 int GetLastResultStatus() const 220 { 221 return lastResultStatus; 222 } 223 bool IsAlive(); 224 wxString GetLastError() const; GetLastResultError()225 pgError GetLastResultError() const 226 { 227 return lastResultError; 228 } 229 wxString GetVersionString(); GetLastSystemOID()230 OID GetLastSystemOID() const 231 { 232 return lastSystemOID; 233 } GetDbOid()234 OID GetDbOid() const 235 { 236 return dbOid; 237 } 238 void RegisterNoticeProcessor(PQnoticeProcessor proc, void *arg); GetConv()239 wxMBConv *GetConv() 240 { 241 return conv; 242 }; 243 244 void LogError(const bool quiet = false); 245 246 bool IsSSLconnected(); connection()247 PGconn *connection() 248 { 249 return conn; 250 } 251 void Notice(const char *msg); 252 pgNotification *GetNotification(); 253 int GetTxStatus(); 254 255 void Reset(); 256 257 bool StartCopy(const wxString query); 258 bool PutCopyData(const char *data, long count); 259 bool EndPutCopy(const wxString errormsg); 260 bool GetCopyFinalStatus(void); 261 262 bool TableHasColumn(wxString schemaname, wxString tblname, const wxString &colname); 263 264 protected: 265 PGconn *conn; 266 PGcancel *m_cancelConn; 267 wxMutex m_cancelConnMutex; 268 int lastResultStatus; 269 270 int connStatus; 271 272 void SetLastResultError(PGresult *res, const wxString &msg = wxEmptyString); 273 void SetConnCancel(void); 274 void ResetConnCancel(void); 275 pgError lastResultError; 276 277 wxMBConv *conv; 278 bool needColQuoting, utfConnectString; 279 wxString dbRole, dbHost, dbHostName; 280 OID lastSystemOID; 281 OID dbOid; 282 283 void *noticeArg; 284 PQnoticeProcessor noticeProc; 285 static double libpqVersion; 286 287 friend class pgQueryThread; 288 289 private: 290 bool DoConnect(); 291 bool Initialize(); 292 293 wxString qtString(const wxString &value); 294 295 bool features[32]; 296 int minorVersion, majorVersion, patchVersion; 297 bool isEdb; 298 bool isGreenplum; 299 bool isHawq; 300 301 wxString reservedNamespaces; 302 wxString connstr; 303 304 wxString save_server, save_service, save_hostaddr, save_database, save_username, save_password, save_rolename, save_applicationname; 305 wxString save_sslcert, save_sslkey, save_sslrootcert, save_sslcrl; 306 int save_port, save_sslmode; 307 bool save_sslcompression; 308 OID save_oid; 309 }; 310 311 #endif 312 313 314