Skip to content

Commit

Permalink
Refactor ConnectionConfigurator out of PostgresConn. Introduce ConstL…
Browse files Browse the repository at this point in the history
…atin1String,

to create compile time String literals.
  • Loading branch information
Alexey Karandashev committed Jun 13, 2023
1 parent becd5b7 commit dbb2065
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 162 deletions.
6 changes: 6 additions & 0 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -4167,6 +4167,12 @@ void CORE_EXPORT qgsFree( void *ptr ) SIP_SKIP;
#define CONSTLATIN1STRING constexpr QLatin1String
#endif

struct ConstLatin1String : public QLatin1String
{
constexpr ConstLatin1String( const char *const s ) :
QLatin1String( s, static_cast<int>( std::char_traits<char>::length( s ) ) ) {}
};

///@cond PRIVATE
class ScopedIntIncrementor
{
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgscolumntypethread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ email : jef at norbit dot de
***************************************************************************/

#include "qgscolumntypethread.h"
#include "qgspostgresconn.h"
#include "qgspostgresconnpool.h"
#include "qgslogger.h"

Expand Down
190 changes: 190 additions & 0 deletions src/providers/postgres/qgsconnectionconfigurator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/***************************************************************************
qgspostgresconnectionconfigurator.h - connection configurator class for
PostgeSQL based connectors.
-------------------
begin : 2023/06/06
copyright : (C) 2023 by Alexey Karandashev
email : reflectored at pm dot me
***************************************************************************/

/***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSCONNECTIONCONFIGURATOR_H
#define QGSCONNECTIONCONFIGURATOR_H

#include <QString>
#include <QtCore/qstring.h>

#include "qgsdatasourceuri.h"
#include "qgssettings.h"

template <typename T>
class QgsConnectionConfigurator
{
static const QString connections_prefix;
static const QString connection_name_prefix;

public:
static QStringList connectionList()
{
QgsSettings settings;
settings.beginGroup( connections_prefix );
return settings.childGroups();
}

static QString selectedConnection()
{
QgsSettings settings;
return settings.value( connections_prefix + QStringLiteral( "/selected" ) ).toString();
}

static void setSelectedConnection( const QString &name )
{
QgsSettings settings;
return settings.setValue( connections_prefix + QStringLiteral( "/selected" ), name );
}

static QgsDataSourceUri connUri( const QString &connName )
{
QgsDebugMsgLevel( "theConnName = " + connName, 2 );

QgsSettings settings;

QString key = connections_prefix + QStringLiteral( "/%1" ).arg( connName );

QString service = settings.value( key + QLatin1String( "/service" ) ).toString();
QString host = settings.value( key + QLatin1String( "/host" ) ).toString();
QString port = settings.value( key + QLatin1String( "/port" ) ).toString();
if ( port.length() == 0 )
{
port = T::mConnectionTypePort;
}
QString database = settings.value( key + QLatin1String( "/database" ) ).toString();

bool estimatedMetadata = useEstimatedMetadata( connName );
QgsDataSourceUri::SslMode sslmode = settings.enumValue( key + QLatin1String( "/sslmode" ), QgsDataSourceUri::SslPrefer );

QString username;
QString password;
if ( settings.value( key + QLatin1String( "/saveUsername" ) ).toString() == QLatin1String( "true" ) )
{
username = settings.value( key + QLatin1String( "/username" ) ).toString();
}

if ( settings.value( key + QLatin1String( "/savePassword" ) ).toString() == QLatin1String( "true" ) )
{
password = settings.value( key + QLatin1String( "/password" ) ).toString();
}

// Old save setting
if ( settings.contains( key + QLatin1String( "/save" ) ) )
{
username = settings.value( key + QLatin1String( "/username" ) ).toString();

if ( settings.value( key + QLatin1String( "/save" ) ).toString() == QLatin1String( "true" ) )
{
password = settings.value( key + QLatin1String( "/password" ) ).toString();
}
}

QString authcfg = settings.value( key + QLatin1String( "/authcfg" ) ).toString();

QgsDataSourceUri uri;
if ( !service.isEmpty() )
{
uri.setConnection( service, database, username, password, sslmode, authcfg );
}
else
{
uri.setConnection( host, port, database, username, password, sslmode, authcfg );
}
uri.setUseEstimatedMetadata( estimatedMetadata );

return uri;
}


static bool publicSchemaOnly( const QString &connectionName )
{
QgsSettings settings;
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/publicOnly" ), false ).toBool();
}

static bool geometryColumnsOnly( const QString &connectionName )
{
QgsSettings settings;

return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/geometryColumnsOnly" ), false ).toBool();
}

static bool dontResolveType( const QString &connectionName )
{
QgsSettings settings;

return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/dontResolveType" ), false ).toBool();
}

static bool useEstimatedMetadata( const QString &connectionName )
{
QgsSettings settings;

return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/estimatedMetadata" ), false ).toBool();
}

static bool allowGeometrylessTables( const QString &connectionName )
{
QgsSettings settings;
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/allowGeometrylessTables" ), false ).toBool();
}

static bool allowProjectsInDatabase( const QString &connectionName )
{
QgsSettings settings;
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/projectsInDatabase" ), false ).toBool();
}

static void deleteConnection( const QString &connectionName )
{
QgsSettings settings;

QString key = connection_name_prefix .arg( connectionName );
settings.remove( key + QLatin1String( "/service" ) );
settings.remove( key + QLatin1String( "/host" ) );
settings.remove( key + QLatin1String( "/port" ) );
settings.remove( key + QLatin1String( "/database" ) );
settings.remove( key + QLatin1String( "/username" ) );
settings.remove( key + QLatin1String( "/password" ) );
settings.remove( key + QLatin1String( "/sslmode" ) );
settings.remove( key + QLatin1String( "/publicOnly" ) );
settings.remove( key + QLatin1String( "/geometryColumnsOnly" ) );
settings.remove( key + QLatin1String( "/allowGeometrylessTables" ) );
settings.remove( key + QLatin1String( "/estimatedMetadata" ) );
settings.remove( key + QLatin1String( "/saveUsername" ) );
settings.remove( key + QLatin1String( "/savePassword" ) );
settings.remove( key + QLatin1String( "/save" ) );
settings.remove( key + QLatin1String( "/authcfg" ) );
settings.remove( key + QLatin1String( "/keys" ) );
settings.remove( key );
}

static bool allowMetadataInDatabase( const QString &connectionName )
{
QgsSettings settings;
return settings.value( connections_prefix + QStringLiteral( "/%1/metadataInDatabase" ).arg( connectionName ), false ).toBool();
}
};

template <typename T>
const QString QgsConnectionConfigurator<T>::connections_prefix = QStringLiteral( "/%1/connections" ).arg( T::mConnectionTypeName );

template <typename T>
const QString QgsConnectionConfigurator<T>::connection_name_prefix = QStringLiteral( "%1/%2" ).arg( connections_prefix );

#endif
1 change: 1 addition & 0 deletions src/providers/postgres/qgspgsourceselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ email : sherman at mrcc.com
#include "qgslogger.h"
#include "qgsdbfilterproxymodel.h"
#include "qgsapplication.h"
#include "qgspostgresconn.h"
#include "qgspostgresprovider.h"
#include "qgspgnewconnection.h"
#include "qgsmanageconnectionsdialog.h"
Expand Down
149 changes: 1 addition & 148 deletions src/providers/postgres/qgspostgresconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
{
// add default timeout
QgsSettings settings;
int timeout = settings.value( QStringLiteral( "PostgreSQL/default_timeout" ), PG_DEFAULT_TIMEOUT, QgsSettings::Providers ).toInt();
int timeout = settings.value( QStringLiteral( "%1/default_timeout" ).arg( mConnectionTypeName ), PG_DEFAULT_TIMEOUT, QgsSettings::Providers ).toInt();
connectString += QStringLiteral( " connect_timeout=%1" ).arg( timeout );
}

Expand Down Expand Up @@ -2538,153 +2538,6 @@ Qgis::WkbType QgsPostgresConn::wkbTypeFromGeomType( Qgis::GeometryType geomType
return Qgis::WkbType::Unknown;
}

QStringList QgsPostgresConn::connectionList()
{
QgsSettings settings;
settings.beginGroup( QStringLiteral( "PostgreSQL/connections" ) );
return settings.childGroups();
}

QString QgsPostgresConn::selectedConnection()
{
QgsSettings settings;
return settings.value( QStringLiteral( "PostgreSQL/connections/selected" ) ).toString();
}

void QgsPostgresConn::setSelectedConnection( const QString &name )
{
QgsSettings settings;
return settings.setValue( QStringLiteral( "PostgreSQL/connections/selected" ), name );
}

QgsDataSourceUri QgsPostgresConn::connUri( const QString &connName )
{
QgsDebugMsgLevel( "theConnName = " + connName, 2 );

QgsSettings settings;

QString key = "/PostgreSQL/connections/" + connName;

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 = QStringLiteral( "5432" );
}
QString database = settings.value( key + "/database" ).toString();

bool estimatedMetadata = useEstimatedMetadata( connName );
QgsDataSourceUri::SslMode sslmode = settings.enumValue( key + "/sslmode", QgsDataSourceUri::SslPrefer );

QString username;
QString password;
if ( settings.value( key + "/saveUsername" ).toString() == QLatin1String( "true" ) )
{
username = settings.value( key + "/username" ).toString();
}

if ( settings.value( key + "/savePassword" ).toString() == QLatin1String( "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() == QLatin1String( "true" ) )
{
password = settings.value( key + "/password" ).toString();
}
}

QString authcfg = settings.value( key + "/authcfg" ).toString();

QgsDataSourceUri uri;
if ( !service.isEmpty() )
{
uri.setConnection( service, database, username, password, sslmode, authcfg );
}
else
{
uri.setConnection( host, port, database, username, password, sslmode, authcfg );
}
uri.setUseEstimatedMetadata( estimatedMetadata );

return uri;
}

bool QgsPostgresConn::publicSchemaOnly( const QString &connName )
{
QgsSettings settings;
return settings.value( "/PostgreSQL/connections/" + connName + "/publicOnly", false ).toBool();
}

bool QgsPostgresConn::geometryColumnsOnly( const QString &connName )
{
QgsSettings settings;

return settings.value( "/PostgreSQL/connections/" + connName + "/geometryColumnsOnly", false ).toBool();
}

bool QgsPostgresConn::dontResolveType( const QString &connName )
{
QgsSettings settings;

return settings.value( "/PostgreSQL/connections/" + connName + "/dontResolveType", false ).toBool();
}

bool QgsPostgresConn::useEstimatedMetadata( const QString &connName )
{
QgsSettings settings;

return settings.value( "/PostgreSQL/connections/" + connName + "/estimatedMetadata", false ).toBool();
}


bool QgsPostgresConn::allowGeometrylessTables( const QString &connName )
{
QgsSettings settings;
return settings.value( "/PostgreSQL/connections/" + connName + "/allowGeometrylessTables", false ).toBool();
}

bool QgsPostgresConn::allowProjectsInDatabase( const QString &connName )
{
QgsSettings settings;
return settings.value( "/PostgreSQL/connections/" + connName + "/projectsInDatabase", false ).toBool();
}

void QgsPostgresConn::deleteConnection( const QString &connName )
{
QgsSettings settings;

QString key = "/PostgreSQL/connections/" + connName;
settings.remove( key + "/service" );
settings.remove( key + "/host" );
settings.remove( key + "/port" );
settings.remove( key + "/database" );
settings.remove( key + "/username" );
settings.remove( key + "/password" );
settings.remove( key + "/sslmode" );
settings.remove( key + "/publicOnly" );
settings.remove( key + "/geometryColumnsOnly" );
settings.remove( key + "/allowGeometrylessTables" );
settings.remove( key + "/estimatedMetadata" );
settings.remove( key + "/saveUsername" );
settings.remove( key + "/savePassword" );
settings.remove( key + "/save" );
settings.remove( key + "/authcfg" );
settings.remove( key + "/keys" );
settings.remove( key );
}

bool QgsPostgresConn::allowMetadataInDatabase( const QString &connName )
{
QgsSettings settings;
return settings.value( "/PostgreSQL/connections/" + connName + "/metadataInDatabase", false ).toBool();
}

bool QgsPostgresConn::cancel()
{
Expand Down
Loading

0 comments on commit dbb2065

Please sign in to comment.