function SQL_Display(sin:String;TSout:TStrings):Integer;
var sout:string;
indent_Stack:TStack;
Indent_Space:Integer;
Execpt_Flag:Boolean;
//------------------------------------------------------------------------
procedure check_Except(sin:string);
begin
if pos(D_Except_KeyWords,sin)<>0 then
begin
Execpt_Flag:=true;
// Dont_Care_Flag:=True;
end;
end;
//------------------------------------------------------------------------
function Recursive_Normalize(var sin:String;var Sout:String):Integer;
var ss:String;
Dont_care_Flag:Boolean;
Found_dont_care_Ends:Boolean;
Bracket_Stack:TStack;
//============================================================================
procedure do_Prefix_Keywords;
begin
Found_dont_care_Ends:=False;
Dont_Care_Flag:=False;
if sout=''
then Sout:=Sout+LPad(ss,Indent_Space+K_Indent_Space,#32)+#32
else Sout:=Sout+#13+#10+LPad(ss,Indent_Space+K_Indent_Space,#32)+#32;
end;
//============================================================================
procedure Do_Active_Dont_Care;
begin
Dont_Care_Flag:=True;
Found_dont_care_Ends:=False;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space+K_Indent_Space,#32)+#32;
end;
//============================================================================
procedure Do_DontCare_begs;
begin
if (Dont_Care_Flag) and (not Found_dont_care_Ends) and (not Execpt_Flag) then
begin
Found_dont_care_Ends:=True;
//
if not indent_Stack.Push(IntToStr(K_Indent_Space)) then
begin
result:=EV_ERR_Stack_Push_Error;
exit;
end;
//
Indent_Space:=Indent_Space+K_Indent_Space;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space,#32)+#32;
result:=Recursive_Normalize(sin,sout);
if result<>0 then
begin
sout:='';
exit;
end;
end
else
if (not Found_dont_care_Ends) and (Execpt_Flag) then
begin
Found_dont_care_Ends:=True;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space,#32)+#13+#10+LPad(#32,K_Field_Indent_Space,#32);
end
else
begin
Sout:=Sout+ss+' ';
if not Bracket_Stack.Push(ss) then
begin
result:=EV_ERR_Stack_Push_Error;
exit;
end;
end;
end;
//============================================================================
procedure Do_DontCare_ends;
var temp:String;
begin
if Bracket_Stack.Stack_Index>=1 then
begin
if not Bracket_Stack.POP(temp) then
begin
result:=EV_ERR_Stack_POP_Error;
exit;
end;
Sout:=Sout+ss+#32;
end
else
begin
Found_dont_care_Ends:=False;
Dont_Care_Flag:=True;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space,#32)+#32;
//
if not Execpt_Flag then
begin
if not indent_Stack.POP(temp) then
begin
result:=EV_ERR_Stack_POP_Error;
exit;
end;
Indent_Space:=Indent_Space-strToInt(temp);
end;
end;
end;
//============================================================================
procedure Do_Posfix_Keywords;
begin
if (Dont_Care_Flag) or (Bracket_Stack.Stack_Index>=1) then
begin
Sout:=Sout+ss+#32;
end
else
begin
Sout:=Sout+ss+#13+#10+LPad(#32,Indent_Space+K_Field_Indent_Space,#32);
end;
// end;
end;
//============================================================================
procedure Do_None;
begin
Sout:=Sout+ss+' ';
end;
//============================================================================
procedure Initial;
begin
Found_dont_care_Ends:=False;
Dont_care_Flag:=False;
//
Bracket_Stack:=TStack.Create;
Bracket_Stack.Initial;
end;
//============================================================================
begin
Initial;
result:=0;
//
while (sin <>'') and (result=0) do
begin
ss:=StringTok(sin,' ');
if InStringCheck(ss,D_Prefix_Keywords,K_Separator)<>0 then
begin
do_Prefix_Keywords;
end
else
if InStringCheck(ss,D_Active_Dont_Care,K_Separator)<>0 then
begin
Do_Active_Dont_Care;
end
else
if InStringCheck(ss,D_DontCare_begs,K_Separator)<>0 then
begin
Do_DontCare_begs;
end
else
if InStringCheck(ss,D_DontCare_ends,K_Separator)<>0 then
begin
Do_DontCare_ends;
end
else
if InStringCheck(ss,D_Posfix_Keywords,K_Separator)<>0 then
begin
Do_Posfix_Keywords;
end
else
Do_None;
end;
end;
//============================================================================
begin
result:=SQL_Normalize(sin);
//
if result<>0
then exit; indent_Stack:=TStack.Create;
indent_Stack.Initial;
TSout.Clear;
//
Execpt_Flag:=False;
Indent_Space:=0;
check_Except(sin);
//
result:=Recursive_Normalize(sin,Sout);
TSout.add(Sout);
end;
var sout:string;
indent_Stack:TStack;
Indent_Space:Integer;
Execpt_Flag:Boolean;
//------------------------------------------------------------------------
procedure check_Except(sin:string);
begin
if pos(D_Except_KeyWords,sin)<>0 then
begin
Execpt_Flag:=true;
// Dont_Care_Flag:=True;
end;
end;
//------------------------------------------------------------------------
function Recursive_Normalize(var sin:String;var Sout:String):Integer;
var ss:String;
Dont_care_Flag:Boolean;
Found_dont_care_Ends:Boolean;
Bracket_Stack:TStack;
//============================================================================
procedure do_Prefix_Keywords;
begin
Found_dont_care_Ends:=False;
Dont_Care_Flag:=False;
if sout=''
then Sout:=Sout+LPad(ss,Indent_Space+K_Indent_Space,#32)+#32
else Sout:=Sout+#13+#10+LPad(ss,Indent_Space+K_Indent_Space,#32)+#32;
end;
//============================================================================
procedure Do_Active_Dont_Care;
begin
Dont_Care_Flag:=True;
Found_dont_care_Ends:=False;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space+K_Indent_Space,#32)+#32;
end;
//============================================================================
procedure Do_DontCare_begs;
begin
if (Dont_Care_Flag) and (not Found_dont_care_Ends) and (not Execpt_Flag) then
begin
Found_dont_care_Ends:=True;
//
if not indent_Stack.Push(IntToStr(K_Indent_Space)) then
begin
result:=EV_ERR_Stack_Push_Error;
exit;
end;
//
Indent_Space:=Indent_Space+K_Indent_Space;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space,#32)+#32;
result:=Recursive_Normalize(sin,sout);
if result<>0 then
begin
sout:='';
exit;
end;
end
else
if (not Found_dont_care_Ends) and (Execpt_Flag) then
begin
Found_dont_care_Ends:=True;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space,#32)+#13+#10+LPad(#32,K_Field_Indent_Space,#32);
end
else
begin
Sout:=Sout+ss+' ';
if not Bracket_Stack.Push(ss) then
begin
result:=EV_ERR_Stack_Push_Error;
exit;
end;
end;
end;
//============================================================================
procedure Do_DontCare_ends;
var temp:String;
begin
if Bracket_Stack.Stack_Index>=1 then
begin
if not Bracket_Stack.POP(temp) then
begin
result:=EV_ERR_Stack_POP_Error;
exit;
end;
Sout:=Sout+ss+#32;
end
else
begin
Found_dont_care_Ends:=False;
Dont_Care_Flag:=True;
Sout:=Sout+#13+#10+LPad(ss,Indent_Space,#32)+#32;
//
if not Execpt_Flag then
begin
if not indent_Stack.POP(temp) then
begin
result:=EV_ERR_Stack_POP_Error;
exit;
end;
Indent_Space:=Indent_Space-strToInt(temp);
end;
end;
end;
//============================================================================
procedure Do_Posfix_Keywords;
begin
if (Dont_Care_Flag) or (Bracket_Stack.Stack_Index>=1) then
begin
Sout:=Sout+ss+#32;
end
else
begin
Sout:=Sout+ss+#13+#10+LPad(#32,Indent_Space+K_Field_Indent_Space,#32);
end;
// end;
end;
//============================================================================
procedure Do_None;
begin
Sout:=Sout+ss+' ';
end;
//============================================================================
procedure Initial;
begin
Found_dont_care_Ends:=False;
Dont_care_Flag:=False;
//
Bracket_Stack:=TStack.Create;
Bracket_Stack.Initial;
end;
//============================================================================
begin
Initial;
result:=0;
//
while (sin <>'') and (result=0) do
begin
ss:=StringTok(sin,' ');
if InStringCheck(ss,D_Prefix_Keywords,K_Separator)<>0 then
begin
do_Prefix_Keywords;
end
else
if InStringCheck(ss,D_Active_Dont_Care,K_Separator)<>0 then
begin
Do_Active_Dont_Care;
end
else
if InStringCheck(ss,D_DontCare_begs,K_Separator)<>0 then
begin
Do_DontCare_begs;
end
else
if InStringCheck(ss,D_DontCare_ends,K_Separator)<>0 then
begin
Do_DontCare_ends;
end
else
if InStringCheck(ss,D_Posfix_Keywords,K_Separator)<>0 then
begin
Do_Posfix_Keywords;
end
else
Do_None;
end;
end;
//============================================================================
begin
result:=SQL_Normalize(sin);
//
if result<>0
then exit; indent_Stack:=TStack.Create;
indent_Stack.Initial;
TSout.Clear;
//
Execpt_Flag:=False;
Indent_Space:=0;
check_Except(sin);
//
result:=Recursive_Normalize(sin,Sout);
TSout.add(Sout);
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货