Skip to content

Commit

Permalink
Set character set and collation of MySQL/MariaDB connections only if …
Browse files Browse the repository at this point in the history
…current one is not UTF8/16/32. Closes #287
  • Loading branch information
ansgarbecker committed Apr 22, 2022
1 parent 63a229e commit 67cd7bb
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions source/dbconnection.pas
Expand Up @@ -1986,7 +1986,7 @@ constructor TDBConnection.Create(AOwner: TComponent);
FLastQueryNetworkDuration := 0;
FThreadID := 0;
FLogPrefix := '';
FIsUnicode := False;
FIsUnicode := True;
FIsSSL := False;
FDatabaseCache := TDatabaseCache.Create(True);
FColumnCache := TColumnCache.Create;
Expand Down Expand Up @@ -2434,6 +2434,9 @@ procedure TMySQLConnection.SetActive( Value: Boolean );
Raise;
end;
end;

FIsUnicode := CharacterSet.StartsWith('utf', True);
if not IsUnicode then
try
CharacterSet := 'utf8mb4';
except
Expand All @@ -2445,7 +2448,7 @@ procedure TMySQLConnection.SetActive( Value: Boolean );
Log(lcError, E.Message);
end;
end;
Log(lcInfo, _('Characterset')+': '+GetCharacterSet);
Log(lcInfo, _('Characterset')+': '+CharacterSet);
FConnectionStarted := GetTickCount div 1000;
FServerUptime := -1;
Status := GetResults(GetSQLSpecifity(spGlobalStatus));
Expand Down Expand Up @@ -2579,7 +2582,6 @@ procedure TAdoDBConnection.SetActive(Value: Boolean);
// CharacterSet := 'utf8';
// CurCharset := CharacterSet;
// Log(lcDebug, 'Characterset: '+CurCharset);
FIsUnicode := True;
FAdoHandle.CommandTimeout := Parameters.QueryTimeout;
try
// Gracefully accept failure on MS Azure (SQL Server 11), which does not have a sysprocesses table
Expand Down Expand Up @@ -2735,7 +2737,6 @@ procedure TPgConnection.SetActive(Value: Boolean);
FServerDateTimeOnStartup := GetVar('SELECT ' + GetSQLSpecifity(spFuncNow));
FServerVersionUntouched := GetVar('SELECT VERSION()');
FConnectionStarted := GetTickCount div 1000;
FIsUnicode := True;
Query('SET statement_timeout TO '+IntToStr(Parameters.QueryTimeout*1000));
try
FServerUptime := StrToIntDef(GetVar('SELECT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - pg_postmaster_start_time())::INTEGER'), -1);
Expand Down Expand Up @@ -2788,7 +2789,6 @@ procedure TSQLiteConnection.SetActive(Value: Boolean);

if ConnectResult = SQLITE_OK then begin
FActive := True;
FIsUnicode := True;
FLib.sqlite3_collation_needed(FHandle, Self, SQLite_CollationNeededCallback);
Query('PRAGMA busy_timeout='+(Parameters.QueryTimeout*1000).ToString);
// Override "main" database name with custom one
Expand Down Expand Up @@ -2911,7 +2911,6 @@ procedure TInterbaseConnection.SetActive(Value: Boolean);

if FFDHandle.Connected then begin
FActive := True;
FIsUnicode := True;
//! Query('PRAGMA busy_timeout='+(Parameters.QueryTimeout*1000).ToString);

FServerDateTimeOnStartup := GetVar('SELECT ' + GetSQLSpecifity(spFuncNow));
Expand Down Expand Up @@ -4298,7 +4297,6 @@ function TDBConnection.GetCharacterSet: String;

function TMySQLConnection.GetCharacterSet: String;
begin
Result := inherited;
Result := DecodeAPIString(FLib.mysql_character_set_name(FHandle));
end;

Expand All @@ -4317,11 +4315,12 @@ procedure TMySQLConnection.SetCharacterSet(CharsetName: String);
Return: Integer;
begin
FStatementNum := 0;
Log(lcInfo, 'Changing character set from '+CharacterSet+' to '+CharsetName);
Return := FLib.mysql_set_character_set(FHandle, PAnsiChar(Utf8Encode(CharsetName)));
if Return <> 0 then
raise EDbError.Create(LastErrorMsg)
else
FIsUnicode := Pos('utf8', LowerCase(CharsetName)) = 1;
FIsUnicode := CharsetName.StartsWith('utf', True);
end;


Expand Down

0 comments on commit 67cd7bb

Please sign in to comment.