mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-16 03:09:36 +02:00
* Support for -- comments, speedup, made some methods protected/virtual so behaviour is more customizable
git-svn-id: trunk@27949 -
This commit is contained in:
parent
15bf176bc0
commit
7cd6b3cea6
@ -47,8 +47,10 @@ type
|
|||||||
FCommentsInSQL: Boolean;
|
FCommentsInSQL: Boolean;
|
||||||
FTerminator: AnsiString;
|
FTerminator: AnsiString;
|
||||||
FSQL: TStrings;
|
FSQL: TStrings;
|
||||||
|
FCurrentStripped,
|
||||||
FCurrentStatement: TStrings;
|
FCurrentStatement: TStrings;
|
||||||
FDirectives: TStrings;
|
FDirectives: TStrings;
|
||||||
|
FComment,
|
||||||
FEmitLine: Boolean;
|
FEmitLine: Boolean;
|
||||||
procedure SetDefines(const Value: TStrings);
|
procedure SetDefines(const Value: TStrings);
|
||||||
function FindNextSeparator(sep: array of string): AnsiString;
|
function FindNextSeparator(sep: array of string): AnsiString;
|
||||||
@ -57,15 +59,16 @@ type
|
|||||||
procedure SetSQL(value: TStrings);
|
procedure SetSQL(value: TStrings);
|
||||||
procedure SQLChange(Sender: TObject);
|
procedure SQLChange(Sender: TObject);
|
||||||
function GetLine: Integer;
|
function GetLine: Integer;
|
||||||
Function ProcessConditional(Directive : String; Param : String) : Boolean; virtual;
|
|
||||||
function NextStatement: AnsiString;
|
|
||||||
procedure ProcessStatement;
|
|
||||||
function Available: Boolean;
|
|
||||||
procedure InternalStatement (Statement: TStrings; var StopExecution: Boolean);
|
|
||||||
procedure InternalDirective (Directive, Argument: String; var StopExecution: Boolean);
|
|
||||||
// Runs commit. If ComitRetaining, use CommitRetraining if possible, else stop/starttransaction
|
|
||||||
procedure InternalCommit(CommitRetaining: boolean=true);
|
|
||||||
protected
|
protected
|
||||||
|
procedure ClearStatement; virtual;
|
||||||
|
procedure InternalStatement (Statement: TStrings; var StopExecution: Boolean); virtual;
|
||||||
|
procedure InternalDirective (Directive, Argument: String; var StopExecution: Boolean); virtual;
|
||||||
|
// Runs commit. If ComitRetaining, use CommitRetraining if possible, else stop/starttransaction
|
||||||
|
procedure InternalCommit(CommitRetaining: boolean=true); virtual;
|
||||||
|
Function ProcessConditional(Directive : String; Param : String) : Boolean; virtual;
|
||||||
|
function NextStatement: AnsiString; virtual;
|
||||||
|
procedure ProcessStatement; virtual;
|
||||||
|
function Available: Boolean; virtual;
|
||||||
procedure DefaultDirectives; virtual;
|
procedure DefaultDirectives; virtual;
|
||||||
procedure ExecuteStatement (Statement: TStrings; var StopExecution: Boolean); virtual; abstract;
|
procedure ExecuteStatement (Statement: TStrings; var StopExecution: Boolean); virtual; abstract;
|
||||||
procedure ExecuteDirective (Directive, Argument: String; var StopExecution: Boolean); virtual; abstract;
|
procedure ExecuteDirective (Directive, Argument: String; var StopExecution: Boolean); virtual; abstract;
|
||||||
@ -179,21 +182,6 @@ begin
|
|||||||
Result:=Trim(Result);
|
Result:=Trim(Result);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function DeleteComments(SQL_Text: AnsiString; ATerminator: AnsiString = ';'): AnsiString;
|
|
||||||
|
|
||||||
begin
|
|
||||||
With TCustomSQLScript.Create (Nil) do
|
|
||||||
try
|
|
||||||
Terminator:=ATerminator;
|
|
||||||
Script.Add(SQL_Text);
|
|
||||||
Script.Add(Terminator);
|
|
||||||
CommentsInSQL:=False;
|
|
||||||
Result:=ConvertWhiteSpace(NextStatement);
|
|
||||||
finally
|
|
||||||
Free;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ ---------------------------------------------------------------------
|
{ ---------------------------------------------------------------------
|
||||||
TSQLScript
|
TSQLScript
|
||||||
---------------------------------------------------------------------}
|
---------------------------------------------------------------------}
|
||||||
@ -238,12 +226,20 @@ end;
|
|||||||
|
|
||||||
procedure TCustomSQLScript.AddToStatement(value: AnsiString; ForceNewLine : Boolean);
|
procedure TCustomSQLScript.AddToStatement(value: AnsiString; ForceNewLine : Boolean);
|
||||||
|
|
||||||
|
Procedure DA(L : TStrings);
|
||||||
|
|
||||||
|
begin
|
||||||
|
With L do
|
||||||
|
if ForceNewLine or (Count=0) then
|
||||||
|
Add(value)
|
||||||
|
else
|
||||||
|
Strings[Count-1]:=Strings[Count-1] + value;
|
||||||
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
With FCurrentStatement do
|
DA(FCurrentStatement);
|
||||||
if ForceNewLine or (Count=0) then
|
if Not FComment then
|
||||||
Add(value)
|
DA(FCurrentStripped);
|
||||||
else
|
|
||||||
Strings[Count-1]:=Strings[Count-1] + value;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TCustomSQLScript.FindNextSeparator(Sep: array of string): AnsiString;
|
function TCustomSQLScript.FindNextSeparator(Sep: array of string): AnsiString;
|
||||||
@ -365,6 +361,13 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TCustomSQLScript.ClearStatement;
|
||||||
|
|
||||||
|
begin
|
||||||
|
FCurrentStatement.Clear;
|
||||||
|
FCurrentStripped.Clear;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TCustomSQLScript.ProcessStatement;
|
procedure TCustomSQLScript.ProcessStatement;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
@ -375,7 +378,7 @@ Var
|
|||||||
begin
|
begin
|
||||||
if (FCurrentStatement.Count=0) then
|
if (FCurrentStatement.Count=0) then
|
||||||
Exit;
|
Exit;
|
||||||
S:=DeleteComments(FCurrentStatement.Text, Terminator);
|
S:=Trim(FCurrentStripped.Text);
|
||||||
I:=0;
|
I:=0;
|
||||||
Directive:='';
|
Directive:='';
|
||||||
While (i<FDirectives.Count) and (Directive='') do
|
While (i<FDirectives.Count) and (Directive='') do
|
||||||
@ -430,7 +433,7 @@ begin
|
|||||||
DefaultDirectives;
|
DefaultDirectives;
|
||||||
Repeat
|
Repeat
|
||||||
NextStatement();
|
NextStatement();
|
||||||
if Length(Trim(FCurrentStatement.Text))>0 then
|
if Length(Trim(FCurrentStripped.Text))>0 then
|
||||||
ProcessStatement;
|
ProcessStatement;
|
||||||
Until FAborted or Not Available;
|
Until FAborted or Not Available;
|
||||||
end;
|
end;
|
||||||
@ -443,10 +446,10 @@ var
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
terminator_found:=False;
|
terminator_found:=False;
|
||||||
FCurrentStatement.Clear;
|
ClearStatement;
|
||||||
while FLine <= FSQL.Count do
|
while FLine <= FSQL.Count do
|
||||||
begin
|
begin
|
||||||
pnt:=FindNextSeparator([FTerminator, '/*', '"', '''']);
|
pnt:=FindNextSeparator([FTerminator, '/*', '"', '''', '--']);
|
||||||
if (pnt=FTerminator) then
|
if (pnt=FTerminator) then
|
||||||
begin
|
begin
|
||||||
FCol:=FCol + length(pnt);
|
FCol:=FCol + length(pnt);
|
||||||
@ -455,6 +458,7 @@ begin
|
|||||||
end
|
end
|
||||||
else if pnt = '/*' then
|
else if pnt = '/*' then
|
||||||
begin
|
begin
|
||||||
|
FComment:=True;
|
||||||
if FCommentsInSQL then
|
if FCommentsInSQL then
|
||||||
AddToStatement(pnt,false)
|
AddToStatement(pnt,false)
|
||||||
else
|
else
|
||||||
@ -466,6 +470,16 @@ begin
|
|||||||
else
|
else
|
||||||
FEmitLine:=True;
|
FEmitLine:=True;
|
||||||
FCol:=FCol + length(pnt);
|
FCol:=FCol + length(pnt);
|
||||||
|
FComment:=False;
|
||||||
|
end
|
||||||
|
else if pnt = '--' then
|
||||||
|
begin
|
||||||
|
FComment:=True;
|
||||||
|
if FCommentsInSQL then
|
||||||
|
AddToStatement(Copy(FSQL[FLine-1],FCol,Length(FSQL[FLine-1])-FCol+1),True);
|
||||||
|
Inc(Fline);
|
||||||
|
FCol:=0;
|
||||||
|
FComment:=False;
|
||||||
end
|
end
|
||||||
else if pnt = '"' then
|
else if pnt = '"' then
|
||||||
begin
|
begin
|
||||||
@ -485,9 +499,11 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
if not terminator_found then
|
if not terminator_found then
|
||||||
FCurrentStatement.Clear();
|
ClearStatement;
|
||||||
while (FCurrentStatement.Count > 0) and (trim(FCurrentStatement.Strings[0]) = '') do
|
while (FCurrentStatement.Count > 0) and (trim(FCurrentStatement.Strings[0]) = '') do
|
||||||
FCurrentStatement.Delete(0);
|
FCurrentStatement.Delete(0);
|
||||||
|
while (FCurrentStripped.Count > 0) and (trim(FCurrentStripped.Strings[0]) = '') do
|
||||||
|
FCurrentStripped.Delete(0);
|
||||||
Result:=FCurrentStatement.Text;
|
Result:=FCurrentStatement.Text;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -511,6 +527,7 @@ begin
|
|||||||
L.OnChange:=@SQLChange;
|
L.OnChange:=@SQLChange;
|
||||||
FSQL:=L;
|
FSQL:=L;
|
||||||
FDirectives:=TStringList.Create();
|
FDirectives:=TStringList.Create();
|
||||||
|
FCurrentStripped:=TStringList.Create();
|
||||||
FCurrentStatement:=TStringList.Create();
|
FCurrentStatement:=TStringList.Create();
|
||||||
FLine:=1;
|
FLine:=1;
|
||||||
FCol:=1;
|
FCol:=1;
|
||||||
@ -522,7 +539,9 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TCustomSQLScript.Destroy;
|
destructor TCustomSQLScript.Destroy;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
FreeAndNil(FCurrentStripped);
|
||||||
FreeAndNil(FCurrentStatement);
|
FreeAndNil(FCurrentStatement);
|
||||||
FreeAndNil(FSQL);
|
FreeAndNil(FSQL);
|
||||||
FreeAndNil(FDirectives);
|
FreeAndNil(FDirectives);
|
||||||
|
Loading…
Reference in New Issue
Block a user