Skip to content
Permalink
Browse files

Centralize some code for checking validity of column number in curren…

…t row of TDBQuery. Fixes a crash after canceling new row insertion, through ValidateControls(). Related to #788.
  • Loading branch information...
ansgarbecker committed Nov 1, 2019
1 parent b2ecff5 commit 3b77da0c0c6a95ac5f481e11724f227618a7a9b5
Showing with 22 additions and 11 deletions.
  1. +16 −7 source/dbconnection.pas
  2. +6 −4 source/main.pas
@@ -596,6 +596,7 @@ TDBQuery = class(TComponent)
FDBObject: TDBObject;
FFormatSettings: TFormatSettings;
procedure SetRecNo(Value: Int64); virtual; abstract;
function ColumnExists(Column: Integer): Boolean;
procedure SetColumnOrgNames(Value: TStringList);
procedure SetDBObject(Value: TDBObject);
procedure CreateUpdateRow;
@@ -6249,13 +6250,24 @@ function TDBQuery.ColumnCount: Integer;
end;


function TDBQuery.ColumnExists(Column: Integer): Boolean;
begin
// Check if given column exists in current row
// Prevents crash when cancelling new row insertion
Result := FConnection.Active and (Column > -1) and (Column < ColumnCount);
if Result and FEditingPrepared and Assigned(FCurrentUpdateRow) then begin
Result := FCurrentUpdateRow.Count > Column;
end;
end;


function TMySQLQuery.GetColBinData(Column: Integer; var baData: TBytes): Boolean;
var
AnsiStr: AnsiString;
begin
Result := False;

if (Column > -1) and (Column < ColumnCount) then begin
if ColumnExists(Column) then begin
if FEditingPrepared and Assigned(FCurrentUpdateRow) then begin
// Row was edited and only valid in a TRowData
AnsiStr := AnsiString(FCurrentUpdateRow[Column].NewText);
@@ -6294,7 +6306,7 @@ function TMySQLQuery.Col(Column: Integer; IgnoreErrors: Boolean=False): String;
c: Char;
Field: PMYSQL_FIELD;
begin
if (Column > -1) and (Column < ColumnCount) then begin
if ColumnExists(Column) then begin
if FEditingPrepared and Assigned(FCurrentUpdateRow) then begin
// Row was edited and only valid in a TRowData
Result := FCurrentUpdateRow[Column].NewText;
@@ -6334,10 +6346,7 @@ function TMySQLQuery.Col(Column: Integer; IgnoreErrors: Boolean=False): String;

function TAdoDBQuery.Col(Column: Integer; IgnoreErrors: Boolean=False): String;
begin
// Catch broken connection
if not FConnection.Active then begin
Result := '';
end else if (Column > -1) and (Column < ColumnCount) then begin
if ColumnExists(Column) then begin
if FEditingPrepared and Assigned(FCurrentUpdateRow) then begin
Result := FCurrentUpdateRow[Column].NewText;
end else begin
@@ -6369,7 +6378,7 @@ function TPGQuery.Col(Column: Integer; IgnoreErrors: Boolean=False): String;
var
AnsiStr: AnsiString;
begin
if (Column > -1) and (Column < ColumnCount) then begin
if ColumnExists(Column) then begin
if FEditingPrepared and Assigned(FCurrentUpdateRow) then begin
Result := FCurrentUpdateRow[Column].NewText;
end else begin
@@ -5755,6 +5755,7 @@ procedure TMainForm.ValidateControls(Sender: TObject);
inSynMemo, inSynMemoEditable: Boolean;
Results: TDBQuery;
RowNum: PInt64;
CellText: String;
begin
// When adding some new TAction here, be sure to apply this procedure to its OnUpdate event

@@ -5763,14 +5764,17 @@ procedure TMainForm.ValidateControls(Sender: TObject);
Results := nil;
GridHasChanges := False;
EnableTimestamp := False;
CellText := '';
if HasConnection and Assigned(Grid) then begin
Results := GridResult(Grid);
if (Results<>nil) and Assigned(Grid.FocusedNode) then begin
RowNum := Grid.GetNodeData(Grid.FocusedNode);
Results.RecNo := RowNum^;
GridHasChanges := Results.Modified or Results.Inserted;
if Grid.FocusedColumn > NoColumn then
if Grid.FocusedColumn > NoColumn then begin
EnableTimestamp := Results.DataType(Grid.FocusedColumn).Category in [dtcInteger, dtcReal];
CellText := Results.Col(Grid.FocusedColumn, True);
end;
end;
end;
inDataTab := PageControlMain.ActivePage = tabData;
@@ -5790,9 +5794,7 @@ procedure TMainForm.ValidateControls(Sender: TObject);
actDataSaveBlobToFile.Enabled := HasConnection and inDataOrQueryTabNotEmpty and Assigned(Grid.FocusedNode);
actGridEditFunction.Enabled := HasConnection and inDataOrQueryTabNotEmpty and Assigned(Grid.FocusedNode);
actDataPreview.Enabled := HasConnection and inDataOrQueryTabNotEmpty and Assigned(Grid.FocusedNode);
actDataOpenUrl.Enabled := HasConnection and inDataOrQueryTabNotEmpty and Assigned(Grid.FocusedNode)
and (Results <> nil) // see issue #759
and ExecRegExpr('^https?://[^\s]+$', Grid.Text[Grid.FocusedNode, Grid.FocusedColumn]);
actDataOpenUrl.Enabled := ExecRegExpr('^https?://[^\s]+$', CellText);
actUnixTimestampColumn.Enabled := HasConnection and inDataTab and EnableTimestamp;
actUnixTimestampColumn.Checked := inDataTab and HandleUnixTimestampColumn(Grid, Grid.FocusedColumn);
actPreviousResult.Enabled := HasConnection and inDataOrQueryTabNotEmpty;

0 comments on commit 3b77da0

Please sign in to comment.
You can’t perform that action at this time.