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

« back to all changes in this revision

Viewing changes to pgadmin/db/pgConn.cpp

  • Committer: Package Import Robot
  • Author(s): Christoph Berg
  • Date: 2013-09-10 16:16:38 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20130910161638-wwup1q553ylww7dr
Tags: 1.18.0-1
* New upstream release.
* Don't install /usr/bin/png2c anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
4
//
5
 
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
 
5
// Copyright (C) 2002 - 2013, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// pgConn.cpp - PostgreSQL Connection class
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)
56
56
{
57
57
        wxString msg;
58
58
 
324
324
void pgConn::Close()
325
325
{
326
326
        if (conn)
 
327
        {
 
328
                CancelExecution();
327
329
                PQfinish(conn);
 
330
        }
328
331
        conn = 0;
329
332
        connStatus = PGCONN_BAD;
330
333
}
350
353
}
351
354
 
352
355
 
353
 
pgConn *pgConn::Duplicate()
 
356
pgConn *pgConn::Duplicate(const wxString &_appName)
354
357
{
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);
 
363
 
 
364
        // Save the version and features information from the existing connection
 
365
        res->majorVersion = majorVersion;
 
366
        res->minorVersion = minorVersion;
 
367
        res->patchVersion = patchVersion;
 
368
        res->isEdb = isEdb;
 
369
        res->isGreenplum = isGreenplum;
 
370
        res->reservedNamespaces = reservedNamespaces;
 
371
 
 
372
        for (size_t index = FEATURE_INITIALIZED; index < FEATURE_LAST; index++)
 
373
                res->features[index] = features[index];
 
374
 
 
375
        return res;
358
376
}
359
377
 
360
378
 
634
652
        return str;
635
653
}
636
654
 
637
 
#ifdef SSL
 
655
#ifdef PG_SSL
638
656
// we don't define USE_SSL so we don't get ssl.h included
639
657
extern "C"
640
658
{
714
732
        PGresult *qryRes;
715
733
 
716
734
        wxLogSql(wxT("Void query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str());
 
735
 
 
736
        SetConnCancel();
717
737
        qryRes = PQexec(conn, sql.mb_str(*conv));
 
738
        ResetConnCancel();
 
739
 
718
740
        lastResultStatus = PQresultStatus(qryRes);
719
741
        SetLastResultError(qryRes);
720
742
 
734
756
 
735
757
 
736
758
 
737
 
wxString pgConn::ExecuteScalar(const wxString &sql)
 
759
wxString pgConn::ExecuteScalar(const wxString &sql, bool reportError)
738
760
{
739
761
        wxString result;
740
762
 
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());
 
768
 
 
769
                SetConnCancel();
746
770
                qryRes = PQexec(conn, sql.mb_str(*conv));
 
771
                ResetConnCancel();
 
772
 
747
773
                lastResultStatus = PQresultStatus(qryRes);
748
774
                SetLastResultError(qryRes);
749
775
 
750
776
                // Check for errors
751
777
                if (lastResultStatus != PGRES_TUPLES_OK && lastResultStatus != PGRES_COMMAND_OK)
752
778
                {
753
 
                        LogError();
 
779
                        LogError(!reportError);
754
780
                        PQclear(qryRes);
755
781
                        return wxEmptyString;
756
782
                }
775
801
        return result;
776
802
}
777
803
 
778
 
pgSet *pgConn::ExecuteSet(const wxString &sql)
 
804
pgSet *pgConn::ExecuteSet(const wxString &sql, bool reportError)
779
805
{
780
806
        // Execute the query and get the status.
781
807
        if (GetStatus() == PGCONN_OK)
782
808
        {
783
809
                PGresult *qryRes;
784
810
                wxLogSql(wxT("Set query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str());
 
811
 
 
812
                SetConnCancel();
785
813
                qryRes = PQexec(conn, sql.mb_str(*conv));
 
814
                ResetConnCancel();
786
815
 
787
816
                lastResultStatus = PQresultStatus(qryRes);
788
817
                SetLastResultError(qryRes);
792
821
                        pgSet *set = new pgSet(qryRes, this, *conv, needColQuoting);
793
822
                        if (!set)
794
823
                        {
795
 
                                wxLogError(__("Couldn't create a pgSet object!"));
 
824
                                if (reportError)
 
825
                                        wxLogError(_("Couldn't create a pgSet object!"));
 
826
                                else
 
827
                                        wxLogQuietError(_("Couldn't create a pgSet object!"));
796
828
                                PQclear(qryRes);
797
829
                        }
798
830
                        return set;
799
831
                }
800
832
                else
801
833
                {
802
 
                        LogError();
 
834
                        LogError(!reportError);
803
835
                        PQclear(qryRes);
804
836
                }
805
837
        }
924
956
bool pgConn::IsAlive()
925
957
{
926
958
        if (GetStatus() != PGCONN_OK)
 
959
        {
 
960
                if (conn)
 
961
                {
 
962
                        PQfinish(conn);
 
963
                        conn = 0;
 
964
                        connStatus = PGCONN_BROKEN;
 
965
                }
927
966
                return false;
 
967
        }
928
968
 
929
969
        PGresult *qryRes = PQexec(conn, "SELECT 1;");
930
970
        lastResultStatus = PQresultStatus(qryRes);
968
1008
}
969
1009
 
970
1010
 
 
1011
void pgConn::SetConnCancel(void)
 
1012
{
 
1013
        wxMutexLocker  lock(m_cancelConnMutex);
 
1014
        PGcancel      *oldCancelConn = m_cancelConn;
 
1015
 
 
1016
        m_cancelConn = NULL;
 
1017
 
 
1018
        if (oldCancelConn != NULL)
 
1019
                PQfreeCancel(oldCancelConn);
 
1020
 
 
1021
        if (!conn)
 
1022
                return;
 
1023
 
 
1024
        m_cancelConn = PQgetCancel(conn);
 
1025
 
 
1026
}
 
1027
 
 
1028
 
 
1029
void pgConn::ResetConnCancel(void)
 
1030
{
 
1031
        wxMutexLocker  lock(m_cancelConnMutex);
 
1032
        PGcancel      *oldCancelConn = m_cancelConn;
 
1033
 
 
1034
        m_cancelConn = NULL;
 
1035
 
 
1036
        if (oldCancelConn != NULL)
 
1037
                PQfreeCancel(oldCancelConn);
 
1038
}
 
1039
 
 
1040
 
 
1041
void pgConn::CancelExecution(void)
 
1042
{
 
1043
        char           errbuf[256];
 
1044
        wxMutexLocker  lock(m_cancelConnMutex);
 
1045
 
 
1046
        if (m_cancelConn)
 
1047
        {
 
1048
                PGcancel *cancelConn = m_cancelConn;
 
1049
                m_cancelConn = NULL;
 
1050
 
 
1051
                if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
 
1052
                {
 
1053
                        SetLastResultError(NULL, wxT("Cancel request sent"));
 
1054
                }
 
1055
                else
 
1056
                {
 
1057
                        SetLastResultError(NULL, wxString::Format(wxT("Could not send cancel request:\n%s"), errbuf));
 
1058
                }
 
1059
                PQfreeCancel(cancelConn);
 
1060
        }
 
1061
}
 
1062
 
 
1063
 
971
1064
wxString pgConn::GetVersionString()
972
1065
{
973
1066
        return ExecuteScalar(wxT("SELECT version();"));
1129
1222
        return false;
1130
1223
}
1131
1224
 
 
1225
void pgError::SetError(PGresult *_res, wxMBConv *_conv)
 
1226
{
 
1227
        if (!_conv)
 
1228
        {
 
1229
                _conv = &wxConvLibc;
 
1230
        }
 
1231
        if (_res)
 
1232
        {
 
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);
 
1245
        }
 
1246
        else
 
1247
        {
 
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;
 
1260
        }
 
1261
 
 
1262
        wxString errMsg;
 
1263
 
 
1264
        if (severity != wxEmptyString && msg_primary != wxEmptyString)
 
1265
                errMsg = severity + wxT(": ") + msg_primary;
 
1266
        else if (msg_primary != wxEmptyString)
 
1267
                errMsg = msg_primary;
 
1268
 
 
1269
        if (!sql_state.IsEmpty())
 
1270
        {
 
1271
                if (!errMsg.EndsWith(wxT("\n")))
 
1272
                        errMsg += wxT("\n");
 
1273
                errMsg += _("SQL state: ");
 
1274
                errMsg += sql_state;
 
1275
        }
 
1276
 
 
1277
        if (!msg_detail.IsEmpty())
 
1278
        {
 
1279
                if (!errMsg.EndsWith(wxT("\n")))
 
1280
                        errMsg += wxT("\n");
 
1281
                errMsg += _("Detail: ");
 
1282
                errMsg += msg_detail;
 
1283
        }
 
1284
 
 
1285
        if (!msg_hint.IsEmpty())
 
1286
        {
 
1287
                if (!errMsg.EndsWith(wxT("\n")))
 
1288
                        errMsg += wxT("\n");
 
1289
                errMsg += _("Hint: ");
 
1290
                errMsg += msg_hint;
 
1291
        }
 
1292
 
 
1293
        if (!statement_pos.IsEmpty())
 
1294
        {
 
1295
                if (!errMsg.EndsWith(wxT("\n")))
 
1296
                        errMsg += wxT("\n");
 
1297
                errMsg += _("Character: ");
 
1298
                errMsg += statement_pos;
 
1299
        }
 
1300
 
 
1301
        if (!context.IsEmpty())
 
1302
        {
 
1303
                if (!errMsg.EndsWith(wxT("\n")))
 
1304
                        errMsg += wxT("\n");
 
1305
                errMsg += _("Context: ");
 
1306
                errMsg += context;
 
1307
        }
 
1308
        formatted_msg = errMsg;
 
1309
}