From 5b214a3d75e6de94b5fb46d736010c2b44d483fe Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Tue, 18 Jun 2024 16:53:09 +0200 Subject: [PATCH] Issue #1965: show warnings from some internal queries as well, and when running an SQL file --- source/apphelpers.pas | 10 +--------- source/dbconnection.pas | 27 +++++++++++++++++++++++++++ source/main.pas | 1 + source/table_editor.pas | 5 ++++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/source/apphelpers.pas b/source/apphelpers.pas index 1cb7fc94..f848bcc7 100644 --- a/source/apphelpers.pas +++ b/source/apphelpers.pas @@ -3160,15 +3160,7 @@ begin end; FConnection.LockedByThread := nil; Synchronize(procedure begin MainForm.AfterQueryExecution(Self); end); - // Show warnings and notes in log panel - if FConnection.WarningCount > 0 then begin - Warnings := FConnection.GetResults('SHOW WARNINGS'); - while not Warnings.Eof do begin - FConnection.Log(lcError, Warnings.Col('Level') + ': ('+Warnings.Col('Code')+') ' + Warnings.Col('Message')); - Warnings.Next; - end; - Warnings.Free; - end; + FConnection.ShowWarnings; // Check if FAborted is set by the main thread, to avoid proceeding the loop in case // FStopOnErrors is set to false if FAborted or ErrorAborted then diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 4b839538..c2c7849f 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -592,6 +592,7 @@ type property RowsFound: Int64 read FRowsFound; property RowsAffected: Int64 read FRowsAffected; property WarningCount: Cardinal read FWarningCount; + procedure ShowWarnings; virtual; property LastQueryDuration: Cardinal read FLastQueryDuration; property LastQueryNetworkDuration: Cardinal read FLastQueryNetworkDuration; property IsUnicode: Boolean read FIsUnicode; @@ -670,6 +671,7 @@ type function MaxAllowedPacket: Int64; override; function GetTableColumns(Table: TDBObject): TTableColumnList; override; function GetTableKeys(Table: TDBObject): TTableKeyList; override; + procedure ShowWarnings; override; end; TAdoRawResults = Array of _RecordSet; @@ -4778,6 +4780,27 @@ begin end; +procedure TDBConnection.ShowWarnings; +begin + // Do nothing by default. SHOW WARNINGS is MySQL only. +end; + + +procedure TMySQLConnection.ShowWarnings; +var + Warnings: TDBQuery; +begin + if WarningCount > 0 then begin + Warnings := GetResults('SHOW WARNINGS'); + while not Warnings.Eof do begin + Log(lcError, Warnings.Col('Level') + ': ('+Warnings.Col('Code')+') ' + Warnings.Col('Message')); + Warnings.Next; + end; + Warnings.Free; + end; +end; + + function TDBConnection.GetAllDatabases: TStringList; begin // Get user passed delimited list @@ -9439,6 +9462,7 @@ begin if not IsVirtual then begin sql := GridQuery('DELETE', 'FROM ' + QuotedDbAndTableName + ' WHERE ' + GetWhereClause); Connection.Query(sql); + Connection.ShowWarnings; if Connection.RowsAffected = 0 then raise EDbError.Create(FormatNumber(Connection.RowsAffected)+' rows deleted when that should have been 1.'); end; @@ -9557,6 +9581,7 @@ begin sql := sql + ' FROM '+QuotedDbAndTableName+' WHERE '+GetWhereClause; sql := GridQuery('SELECT', sql); Data := Connection.GetResults(sql); + Connection.ShowWarnings; Result := Data.RecordCount = 1; if Result then begin if not Assigned(FCurrentUpdateRow) then @@ -9680,6 +9705,7 @@ begin if RowModified then try if Row.Inserted then begin Connection.Query('INSERT INTO '+QuotedDbAndTableName+' ('+sqlInsertColumns+') VALUES ('+sqlInsertValues+')'); + Connection.ShowWarnings; for i:=0 to ColumnCount-1 do begin ColAttr := ColAttributes(i); if Assigned(ColAttr) and (ColAttr.DefaultType = cdtAutoInc) then begin @@ -9694,6 +9720,7 @@ begin sqlUpdate := QuotedDbAndTableName+' SET '+sqlUpdate+' WHERE '+GetWhereClause; sqlUpdate := GridQuery('UPDATE', sqlUpdate); Connection.Query(sqlUpdate); + Connection.ShowWarnings; if Connection.RowsAffected = 0 then begin raise EDbError.Create(FormatNumber(Connection.RowsAffected)+' rows updated when that should have been 1.'); Result := False; diff --git a/source/main.pas b/source/main.pas index 745d16cd..4be69214 100644 --- a/source/main.pas +++ b/source/main.pas @@ -4183,6 +4183,7 @@ begin try Conn.Query(Queries[i].SQL, False, lcScript); RowsAffected := RowsAffected + Conn.RowsAffected; + Conn.ShowWarnings; except on E:Exception do begin if actQueryStopOnErrors.Checked then diff --git a/source/table_editor.pas b/source/table_editor.pas index e96aa61d..e01045c1 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -479,12 +479,15 @@ begin else Batch := ComposeAlterStatement; try - for Query in Batch do + for Query in Batch do begin DBObject.Connection.Query(Query.SQL); + DBObject.Connection.ShowWarnings; + end; // Rename table if ObjectExists and (editName.Text <> DBObject.Name) then begin Rename := DBObject.Connection.GetSQLSpecifity(spRenameTable, [DBObject.QuotedName, DBObject.Connection.QuoteIdent(editName.Text)]); DBObject.Connection.Query(Rename); + DBObject.Connection.ShowWarnings; end; tabALTERcode.TabVisible := ObjectExists; if chkCharsetConvert.Checked then begin