Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_16] [PostgreSQL] Pass client_encoding='UTF-8' in connection string (fixes #41132) #41179

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions src/providers/postgres/qgspostgresconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
QgsDataSourceUri uri( conninfo );
QString expandedConnectionInfo = uri.connectionInfo( true );

auto addDefaultTimeout = []( QString & connectString )
auto addDefaultTimeoutAndClientEncoding = []( QString & connectString )
{
if ( !connectString.contains( QStringLiteral( "connect_timeout=" ) ) )
{
Expand All @@ -247,10 +247,12 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
int timeout = settings.value( QStringLiteral( "PostgreSQL/default_timeout" ), PG_DEFAULT_TIMEOUT, QgsSettings::Providers ).toInt();
connectString += QStringLiteral( " connect_timeout=%1" ).arg( timeout );
}

connectString += QStringLiteral( " client_encoding='UTF-8'" );
};
addDefaultTimeout( expandedConnectionInfo );
addDefaultTimeoutAndClientEncoding( expandedConnectionInfo );

mConn = PQconnectdb( expandedConnectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
mConn = PQconnectdb( expandedConnectionInfo.toUtf8() );

// remove temporary cert/key/CA if they exist
QgsDataSourceUri expandedUri( expandedConnectionInfo );
Expand Down Expand Up @@ -314,8 +316,8 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s

QgsDebugMsgLevel( "Connecting to " + uri.connectionInfo( false ), 2 );
QString connectString = uri.connectionInfo();
addDefaultTimeout( connectString );
mConn = PQconnectdb( connectString.toLocal8Bit() );
addDefaultTimeoutAndClientEncoding( connectString );
mConn = PQconnectdb( connectString.toUtf8() );
}

if ( PQstatus() == CONNECTION_OK )
Expand All @@ -333,22 +335,6 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
return;
}

//set client encoding to Unicode because QString uses UTF-8 anyway
QgsDebugMsgLevel( QStringLiteral( "setting client encoding to UNICODE" ), 2 );
int errcode = PQsetClientEncoding( mConn, QStringLiteral( "UNICODE" ).toLocal8Bit() );
if ( errcode == 0 )
{
QgsDebugMsgLevel( QStringLiteral( "encoding successfully set" ), 2 );
}
else if ( errcode == -1 )
{
QgsMessageLog::logMessage( tr( "error in setting encoding" ), tr( "PostGIS" ) );
}
else
{
QgsMessageLog::logMessage( tr( "undefined return value from encoding setting" ), tr( "PostGIS" ) );
}

QgsDebugMsgLevel( QStringLiteral( "Connection to the database was successful" ), 2 );

deduceEndian();
Expand Down