52
52
pgConn::pgConn(const wxString &server, const wxString &service, const wxString &hostaddr, const wxString &database, const wxString &username, const wxString &password,
53
53
int port, const wxString &rolename, int sslmode, OID oid, const wxString &applicationname,
54
54
const wxString &sslcert, const wxString &sslkey, const wxString &sslrootcert, const wxString &sslcrl,
55
const bool sslcompression)
55
const bool sslcompression) : m_cancelConn(NULL)
353
pgConn *pgConn::Duplicate()
356
pgConn *pgConn::Duplicate(const wxString &_appName)
355
return new pgConn(wxString(save_server), wxString(save_service), wxString(save_hostaddr), wxString(save_database), wxString(save_username), wxString(save_password),
356
save_port, save_rolename, save_sslmode, save_oid,
357
save_applicationname, save_sslcert, save_sslkey, save_sslrootcert, save_sslcrl, save_sslcompression);
358
pgConn *res = new pgConn(wxString(save_server), wxString(save_service),
359
wxString(save_hostaddr), wxString(save_database), wxString(save_username),
360
wxString(save_password), save_port, save_rolename, save_sslmode, save_oid,
361
_appName.IsEmpty() ? save_applicationname : _appName, save_sslcert, save_sslkey,
362
save_sslrootcert, save_sslcrl, save_sslcompression);
364
// Save the version and features information from the existing connection
365
res->majorVersion = majorVersion;
366
res->minorVersion = minorVersion;
367
res->patchVersion = patchVersion;
369
res->isGreenplum = isGreenplum;
370
res->reservedNamespaces = reservedNamespaces;
372
for (size_t index = FEATURE_INITIALIZED; index < FEATURE_LAST; index++)
373
res->features[index] = features[index];
743
765
// Execute the query and get the status.
744
766
PGresult *qryRes;
745
767
wxLogSql(wxT("Scalar query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str());
746
770
qryRes = PQexec(conn, sql.mb_str(*conv));
747
773
lastResultStatus = PQresultStatus(qryRes);
748
774
SetLastResultError(qryRes);
750
776
// Check for errors
751
777
if (lastResultStatus != PGRES_TUPLES_OK && lastResultStatus != PGRES_COMMAND_OK)
779
LogError(!reportError);
755
781
return wxEmptyString;
778
pgSet *pgConn::ExecuteSet(const wxString &sql)
804
pgSet *pgConn::ExecuteSet(const wxString &sql, bool reportError)
780
806
// Execute the query and get the status.
781
807
if (GetStatus() == PGCONN_OK)
783
809
PGresult *qryRes;
784
810
wxLogSql(wxT("Set query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str());
785
813
qryRes = PQexec(conn, sql.mb_str(*conv));
787
816
lastResultStatus = PQresultStatus(qryRes);
788
817
SetLastResultError(qryRes);
1011
void pgConn::SetConnCancel(void)
1013
wxMutexLocker lock(m_cancelConnMutex);
1014
PGcancel *oldCancelConn = m_cancelConn;
1016
m_cancelConn = NULL;
1018
if (oldCancelConn != NULL)
1019
PQfreeCancel(oldCancelConn);
1024
m_cancelConn = PQgetCancel(conn);
1029
void pgConn::ResetConnCancel(void)
1031
wxMutexLocker lock(m_cancelConnMutex);
1032
PGcancel *oldCancelConn = m_cancelConn;
1034
m_cancelConn = NULL;
1036
if (oldCancelConn != NULL)
1037
PQfreeCancel(oldCancelConn);
1041
void pgConn::CancelExecution(void)
1044
wxMutexLocker lock(m_cancelConnMutex);
1048
PGcancel *cancelConn = m_cancelConn;
1049
m_cancelConn = NULL;
1051
if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
1053
SetLastResultError(NULL, wxT("Cancel request sent"));
1057
SetLastResultError(NULL, wxString::Format(wxT("Could not send cancel request:\n%s"), errbuf));
1059
PQfreeCancel(cancelConn);
971
1064
wxString pgConn::GetVersionString()
973
1066
return ExecuteScalar(wxT("SELECT version();"));
1225
void pgError::SetError(PGresult *_res, wxMBConv *_conv)
1229
_conv = &wxConvLibc;
1233
msg_primary = wxString(PQresultErrorField(_res, PG_DIAG_MESSAGE_PRIMARY), *_conv);
1234
severity = wxString(PQresultErrorField(_res, PG_DIAG_SEVERITY), *_conv);
1235
sql_state = wxString(PQresultErrorField(_res, PG_DIAG_SQLSTATE), *_conv);
1236
msg_detail = wxString(PQresultErrorField(_res, PG_DIAG_MESSAGE_DETAIL), *_conv);
1237
msg_hint = wxString(PQresultErrorField(_res, PG_DIAG_MESSAGE_HINT), *_conv);
1238
statement_pos = wxString(PQresultErrorField(_res, PG_DIAG_STATEMENT_POSITION), *_conv);
1239
internal_pos = wxString(PQresultErrorField(_res, PG_DIAG_INTERNAL_POSITION), *_conv);
1240
internal_query = wxString(PQresultErrorField(_res, PG_DIAG_INTERNAL_QUERY), *_conv);
1241
context = wxString(PQresultErrorField(_res, PG_DIAG_CONTEXT), *_conv);
1242
source_file = wxString(PQresultErrorField(_res, PG_DIAG_SOURCE_FILE), *_conv);
1243
source_line = wxString(PQresultErrorField(_res, PG_DIAG_SOURCE_LINE), *_conv);
1244
source_function = wxString(PQresultErrorField(_res, PG_DIAG_SOURCE_FUNCTION), *_conv);
1248
msg_primary = wxEmptyString;
1249
severity = wxEmptyString;
1250
sql_state = wxEmptyString;
1251
msg_detail = wxEmptyString;
1252
msg_hint = wxEmptyString;
1253
statement_pos = wxEmptyString;
1254
internal_pos = wxEmptyString;
1255
internal_query = wxEmptyString;
1256
context = wxEmptyString;
1257
source_file = wxEmptyString;
1258
source_line = wxEmptyString;
1259
source_function = wxEmptyString;
1264
if (severity != wxEmptyString && msg_primary != wxEmptyString)
1265
errMsg = severity + wxT(": ") + msg_primary;
1266
else if (msg_primary != wxEmptyString)
1267
errMsg = msg_primary;
1269
if (!sql_state.IsEmpty())
1271
if (!errMsg.EndsWith(wxT("\n")))
1272
errMsg += wxT("\n");
1273
errMsg += _("SQL state: ");
1274
errMsg += sql_state;
1277
if (!msg_detail.IsEmpty())
1279
if (!errMsg.EndsWith(wxT("\n")))
1280
errMsg += wxT("\n");
1281
errMsg += _("Detail: ");
1282
errMsg += msg_detail;
1285
if (!msg_hint.IsEmpty())
1287
if (!errMsg.EndsWith(wxT("\n")))
1288
errMsg += wxT("\n");
1289
errMsg += _("Hint: ");
1293
if (!statement_pos.IsEmpty())
1295
if (!errMsg.EndsWith(wxT("\n")))
1296
errMsg += wxT("\n");
1297
errMsg += _("Character: ");
1298
errMsg += statement_pos;
1301
if (!context.IsEmpty())
1303
if (!errMsg.EndsWith(wxT("\n")))
1304
errMsg += wxT("\n");
1305
errMsg += _("Context: ");
1308
formatted_msg = errMsg;