/
qgspostgresconnection.cpp
132 lines (104 loc) · 4.17 KB
/
qgspostgresconnection.cpp
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
/***************************************************************************
qgspostgresconnection.cpp - PostgresSQL/PostGIS connection
-------------------
begin : 3 June 2011
copyright : (C) 2011 by Giuseppe Sucameli
email : brush.tyler at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgspostgresconnection.h"
#include <qgslogger.h>
#include "qgspostgresprovider.h"
#include "qgsproviderregistry.h"
#include "qgsdatasourceuri.h"
#include <QSettings>
QStringList QgsPostgresConnection::connectionList()
{
QSettings settings;
settings.beginGroup( "/PostgreSQL/connections" );
return settings.childGroups();
}
QString QgsPostgresConnection::selectedConnection()
{
QSettings settings;
return settings.value( "/PostgreSQL/connections/selected" ).toString();
}
void QgsPostgresConnection::setSelectedConnection( QString name )
{
QSettings settings;
return settings.setValue( "/PostgreSQL/connections/selected", name );
}
QgsPostgresConnection::QgsPostgresConnection( QString theConnName ) :
mConnName( theConnName )
{
QgsDebugMsg( "theConnName = " + theConnName );
QSettings settings;
QString key = "/PostgreSQL/connections/" + mConnName;
QString service = settings.value( key + "/service" ).toString();
QString host = settings.value( key + "/host" ).toString();
QString port = settings.value( key + "/port" ).toString();
if ( port.length() == 0 )
{
port = "5432";
}
QString database = settings.value( key + "/database" ).toString();
//bool publicSchemaOnly = settings.value( key + "/publicOnly", false ).toBool();
//bool geometryColumnsOnly = settings.value( key + "/geometrycolumnsOnly", false ).toBool();
//bool allowGeometrylessTables = settings.value( key + "/allowGeometrylessTables", false ).toBool();
bool useEstimatedMetadata = settings.value( key + "/estimatedMetadata", false ).toBool();
int sslmode = settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt();
QString username;
QString password;
if ( settings.value( key + "/saveUsername" ).toString() == "true" )
{
username = settings.value( key + "/username" ).toString();
}
if ( settings.value( key + "/savePassword" ).toString() == "true" )
{
password = settings.value( key + "/password" ).toString();
}
// Old save setting
if ( settings.contains( key + "/save" ) )
{
username = settings.value( key + "/username" ).toString();
if ( settings.value( key + "/save" ).toString() == "true" )
{
password = settings.value( key + "/password" ).toString();
}
}
QgsDataSourceURI uri;
if ( !service.isEmpty() )
{
uri.setConnection( service, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode );
}
else
{
uri.setConnection( host, port, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode );
}
uri.setUseEstimatedMetadata( useEstimatedMetadata );
mConnectionInfo = uri.connectionInfo();
QgsDebugMsg( QString( "Connection info: '%1'." ).arg( mConnectionInfo ) );
}
QgsPostgresConnection::~QgsPostgresConnection()
{
}
QString QgsPostgresConnection::connectionInfo( )
{
return mConnectionInfo;
}
QgsPostgresProvider * QgsPostgresConnection::provider( )
{
// TODO: Create and bind to data provider
// load the server data provider plugin
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QgsPostgresProvider *postgresProvider =
( QgsPostgresProvider* ) pReg->provider( "postgres", mConnectionInfo );
return postgresProvider;
}