Fix offset portion in LIMIT clause, at least for MySQL.

This commit is contained in:
Ansgar Becker
2011-03-30 05:37:52 +00:00
parent a6d2271988
commit fb20c8e75f
2 changed files with 12 additions and 8 deletions

View File

@ -483,7 +483,7 @@ type
procedure ParseRoutineStructure(CreateCode: String; Parameters: TRoutineParamList; procedure ParseRoutineStructure(CreateCode: String; Parameters: TRoutineParamList;
var Deterministic: Boolean; var Definer, Returns, DataAccess, Security, Comment, Body: String); var Deterministic: Boolean; var Definer, Returns, DataAccess, Security, Comment, Body: String);
function GetDatatypeByName(Datatype: String): TDBDatatype; function GetDatatypeByName(Datatype: String): TDBDatatype;
function ApplyLimitClause(QueryType, QueryBody: String; Limit: Cardinal): String; function ApplyLimitClause(QueryType, QueryBody: String; Limit, Offset: Cardinal): String;
property SessionName: String read FSessionName write FSessionName; property SessionName: String read FSessionName write FSessionName;
property Parameters: TConnectionParameters read FParameters write FParameters; property Parameters: TConnectionParameters read FParameters write FParameters;
property ThreadId: Cardinal read GetThreadId; property ThreadId: Cardinal read GetThreadId;
@ -2973,14 +2973,18 @@ begin
end; end;
function TDBConnection.ApplyLimitClause(QueryType, QueryBody: String; Limit: Cardinal): String; function TDBConnection.ApplyLimitClause(QueryType, QueryBody: String; Limit, Offset: Cardinal): String;
begin begin
Result := QueryType + ' '; Result := QueryType + ' ';
if IsMSSQL then if IsMSSQL then
Result := Result + 'TOP('+IntToStr(Limit)+') '; Result := Result + 'TOP('+IntToStr(Limit)+') ';
Result := Result + QueryBody; Result := Result + QueryBody;
if IsMySQL then if IsMySQL then begin
Result := Result + ' LIMIT '+IntToStr(Limit); Result := Result + ' LIMIT ';
if Offset > 0 then
Result := Result + IntToStr(Offset) + ', ';
Result := Result + IntToStr(Limit);
end;
end; end;
@ -3582,7 +3586,7 @@ begin
PrepareEditing; PrepareEditing;
IsVirtual := Assigned(FCurrentUpdateRow) and FCurrentUpdateRow.Inserted; IsVirtual := Assigned(FCurrentUpdateRow) and FCurrentUpdateRow.Inserted;
if not IsVirtual then begin if not IsVirtual then begin
sql := Connection.ApplyLimitClause('DELETE', 'FROM ' + QuotedDbAndTableName + ' WHERE ' + GetWhereClause, 1); sql := Connection.ApplyLimitClause('DELETE', 'FROM ' + QuotedDbAndTableName + ' WHERE ' + GetWhereClause, 1, 0);
Connection.Query(sql); Connection.Query(sql);
end; end;
if Assigned(FCurrentUpdateRow) then begin if Assigned(FCurrentUpdateRow) then begin
@ -3692,7 +3696,7 @@ begin
sql := sql + Connection.QuoteIdent(FColumnOrgNames[i]); sql := sql + Connection.QuoteIdent(FColumnOrgNames[i]);
end; end;
sql := sql + ' FROM '+QuotedDbAndTableName+' WHERE '+GetWhereClause; sql := sql + ' FROM '+QuotedDbAndTableName+' WHERE '+GetWhereClause;
sql := Connection.ApplyLimitClause('SELECT', sql, 1); sql := Connection.ApplyLimitClause('SELECT', sql, 1, 0);
Data := Connection.GetResults(sql); Data := Connection.GetResults(sql);
Result := Data.RecordCount = 1; Result := Data.RecordCount = 1;
if Result then begin if Result then begin
@ -3787,7 +3791,7 @@ begin
end; end;
end else begin end else begin
sqlUpdate := QuotedDbAndTableName+' SET '+sqlUpdate+' WHERE '+GetWhereClause; sqlUpdate := QuotedDbAndTableName+' SET '+sqlUpdate+' WHERE '+GetWhereClause;
sqlUpdate := Connection.ApplyLimitClause('UPDATE', sqlUpdate, 1); sqlUpdate := Connection.ApplyLimitClause('UPDATE', sqlUpdate, 1, 0);
Connection.Query(sqlUpdate); Connection.Query(sqlUpdate);
end; end;
// Reset modification flags // Reset modification flags

View File

@ -4039,7 +4039,7 @@ begin
Offset := DataGridResult.RecordCount Offset := DataGridResult.RecordCount
else else
Offset := 0; Offset := 0;
Select := DBObj.Connection.ApplyLimitClause('SELECT', Select, DatagridWantedRowCount-Offset); Select := DBObj.Connection.ApplyLimitClause('SELECT', Select, DatagridWantedRowCount-Offset, Offset);
try try
ShowStatusMsg('Fetching rows ...'); ShowStatusMsg('Fetching rows ...');