home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kompon / d23456 / BKQUERY.ZIP / uEQuery.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-10-02  |  18.8 KB  |  661 lines

  1. unit uEQuery;
  2.  
  3.  
  4. (*
  5.  TEQuery component
  6.  Autor:Jan TUNGLI  Email:jan.tungli@seznam.cz
  7.                    http://www.tungli.host.sk
  8.  
  9.  TEQuery:= Wait Query + Parse Query
  10.  
  11.  PARSE Query SQL syntax  (select type) ;
  12.  Example:
  13.    EQuery1.SQL_Parse(EQuery1.SQL.text);    // inicialization
  14.    (or EQuery1.Parse // self parse)
  15.    S:=EQuery1.SQL_GetSegment('where');    //S = 'where Name>"A"'
  16.    S:=EQuery1.SQL_GetSegment('from');     //S = 'from ANIMALS'
  17.    S:=EQuery1.SQL_GetSegment('order');    //S = ''
  18.    EQuery1.SQL_ChangeSegment('select Name');   //oSQL.text = 'select Name from ANIMALS where Name>"A"'
  19.    EQuery1.SQL_AddToWhere(' Name<"z" ');           //oSQL.text = 'select Name from ANIMALS where (Name>"A") and Name<"z"'
  20.    EQuery1.SQL_DeleteFrom Where(' Name<"z" ');    //oSQL.text = 'select Name from ANIMALS where Name>"A"'
  21.    EQuery1.SQL_ChangeSegment('where');         //oSQL.text = 'select Name from ANIMALS'
  22.    EQuery1.SQL_ChangeSegment('order by Name'); //oSQL.text = 'select Name from ANIMALS order by Name'
  23.    EQuery1.SQL.text:=EQuery1.SQL_Text;  //Query1.SQL.text = 'select Name from ANIMALS order by Name'
  24.    (or EQuery1.SQL.text:=EQuery1.SQL_New;
  25.     or SQL_Set(EQuery1) )
  26.    EQuery1.Open;
  27. *)
  28.  
  29. interface
  30.  
  31. uses
  32.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  33.   Db, DBTables,Clipbrd;
  34.  
  35. type  TSQLParseRec=record A,B:word; W: String; end;
  36.  
  37.   TEQuery = class(TQuery)
  38.   private
  39.     fS:TStringList;
  40.     fSave,fload,fFields,fRef:boolean;
  41.     FRoundDelay : Integer;
  42.     FRoundCount : Integer;
  43.     FErrorLock  : Boolean;
  44.  
  45.     oSyntax:TStringList;
  46.     oSQL:TStringList;
  47.     oWords:array[1..20] of TSQLParseRec;
  48.     oOldSQL:string; // pre parse
  49.     oOldAddToWhere:string;
  50.  
  51.     procedure SetShowDialog(pB:boolean);
  52.     procedure SetSave(pB:boolean);
  53.     procedure SetLoad(pB:boolean);
  54.     procedure SetFields(pB:boolean);
  55.     procedure WriteFields(pMode:byte);
  56.     procedure MyExecute (FuncName : String);
  57.     procedure SetoSQL(pS:TStringList);
  58.     procedure SetoOldSQL(pS:String);
  59.     procedure SetOldWhere(pS:String);
  60.     function ABCopy(pS:String; pA,pB:longint):String;
  61.     { Private declarations }
  62.   protected
  63.     { Protected declarations }
  64.   public
  65.     constructor Create(AOwner: TComponent); override;
  66.     destructor Destroy; override;
  67.       procedure ExecSQL;
  68.       procedure Open;
  69.       procedure First;
  70.       procedure Next;
  71.       procedure Last;
  72.       procedure Prior;
  73.  //     procedure MoveBy(pM:integer);
  74.       { Public declarations }
  75.   published
  76.       property RoundDelay : Integer read  FRoundDelay    //default po 1 sec. skusi vykonat query
  77.                                     write FRoundDelay;
  78.       property RoundCount : Integer read  FRoundCount    //default 10 krat po 1 sec skusi vykonat query
  79.                                     write FRoundCount;
  80.       property ErrorLock  : Boolean read  FErrorLock;
  81.          //ak je true tak tabulku poiziva iny uzivatel (jeho query este nedobehol)
  82.  
  83.     property References:TStringList read fS write fS;
  84.     property Dialog_Fields:boolean read fRef write SetShowDialog;
  85.     property Exec_SaveToClipboard:boolean read fSave write SetSave;
  86.     property Exec_LoadToClipboard:boolean read fLoad write SetLoad;
  87.     property Exec_FieldsToClipboard:boolean read fFields write SetFields;
  88.     property Parse_Syntax:TStringList read oSyntax write oSyntax;
  89.     property SQL_New:TStringList read oSQL write SetoSQL;
  90.     property SQL_Old:string read oOldSQL write SetoOldSQL;
  91.     property SQL_LastAddToWhere : string read oOldAddToWhere write SetOldWhere;
  92.  
  93.     procedure Parse;
  94.     procedure SQL_Parse(pS:String);   //SQL parse
  95.     function  SQL_GetSegment(pKeyWord:string):string; // vrati segment SQL dotazu
  96.     procedure SQL_ChangeSegment(pSegment:string);
  97.     procedure SQL_AddToWhere(pS:String);
  98.     procedure SQL_DeleteFromWhere(pS:String);
  99.     function  SQL_Text:string;
  100.     function  SQL_SelectCount(pQ:Tquery):integer;  // becasue RecordCount is very slowly
  101.     procedure SQL_Set(pQ:TQuery);
  102.     { Published declarations }
  103.   end;
  104.  
  105. (*procedure Register;*)
  106.  
  107. implementation
  108. uses uEQueryForm;
  109. const
  110.   cDBErrorLock = -210;
  111.  
  112. //var cMoveBy:integer;
  113.  
  114. Constructor TEQuery.Create(AOwner: TComponent);
  115. begin
  116.   inherited Create(AOwner);
  117.   fS:=TStringList.Create;
  118.   FRoundDelay := 1000;
  119.   FRoundCount := 5;
  120.   oOldSQL:='';
  121.   oSQL:=TStringList.Create;
  122.   oSyntax:=TStringList.Create;
  123.   with oSyntax do begin
  124.     Clear;
  125.     Add('SELECT');  // SELECT must by first keyword
  126.     Add('FROM');
  127.     Add('WHERE');
  128.     Add('GROUP');
  129.     Add('HAVING');
  130.     Add('UNION');
  131.     //Add('PLAN');      // Interbase
  132.     Add('ORDER');
  133.     Add('COMPUTE');   // for MS_SQL
  134.     Add('FOR');       // MS_SQL: FOR BROWSE or INTERBASE: FOR UPDATE
  135.     Add('INTO');      // for INFORMIX  (into temp)
  136.   end;
  137. End;
  138.  
  139. Destructor TEQuery.Destroy;
  140. begin
  141.   if oSyntax<>nil then begin oSyntax.Free; oSyntax:=nil; end;
  142.   if oSQL<>nil then begin oSQL.Free; oSQL:=nil; end;
  143.   try
  144.     if fS<>nil then begin fS.Free; fS:=nil; end;
  145.   except end;
  146.   Active:=false;
  147.   inherited Destroy;
  148. end;
  149.  
  150. procedure TEQuery.MyExecute (FuncName : String);
  151. var I       : Integer;
  152.     FuncNum : Integer;
  153.     OK      : Boolean;
  154. begin
  155.   FuncName := UpperCase (FuncName);
  156.  
  157.   if FuncName = 'EXECSQL'    then FuncNum := 0
  158.   else if FuncName = 'OPEN'  then FuncNum := 1
  159.   else if FuncName = 'FIRST' then FuncNum := 2
  160.   else if FuncName = 'NEXT'  then FuncNum := 3
  161.   else if FuncName = 'LAST'  then FuncNum := 4
  162.   else if FuncName = 'PRIOR' then FuncNum := 5
  163. //  else if FuncName = 'MOVEBY' then FuncNum := 6
  164.   else FuncNum := -1;
  165.  
  166.   OK := false;
  167.   I  := 1;
  168.  
  169.   while (not OK) and (I < RoundCount) do
  170.     begin
  171.     FErrorLock := false;
  172.     OK         := false;
  173.     try
  174.       case FuncNum of
  175.         0 : inherited ExecSQL;
  176.         1 : inherited Open;
  177.         2 : inherited First;
  178.         3 : inherited Next;
  179.         4 : inherited Last;
  180.         5 : inherited Prior;
  181. //        6 : inherited MoveBy(cMoveBy);
  182.         end; // case
  183.       OK := true;
  184.     except
  185.       on E:EDBEngineError do
  186.         begin
  187.         if E.ErrorCount >= 2 then
  188.           if E.Errors[1].NativeError = cDbErrorLock then
  189.             FErrorLock := true;
  190.         if not FErrorLock then raise;
  191.         end; // on
  192.       else raise;
  193.     end; // try
  194.     Inc(I);
  195.     if not OK then Sleep (RoundDelay);
  196.     end; // while
  197.  
  198.   if not OK then
  199.     begin
  200.     try
  201.       case FuncNum of
  202.         0 : inherited ExecSQL;
  203.         1 : inherited Open;
  204.         2 : inherited First;
  205.         3 : inherited Next;
  206.         4 : inherited Last;
  207.         5 : inherited Prior;
  208. //        6 : inherited MoveBy(cMoveBy);
  209.         end; // case
  210. //      OK := true;
  211.     except
  212.       on E:EDBEngineError do
  213.         begin
  214.         if E.ErrorCount >= 2 then
  215.           if E.Errors[1].NativeError = cDbErrorLock then
  216.             FErrorLock := true;
  217.         raise;
  218.         end; // on
  219.       else raise;
  220.     end; // try
  221.   end; // if
  222. end;
  223.  
  224. procedure TEQuery.SetShowDialog(pB:boolean);
  225. begin
  226.   fRef:=pB;
  227.   try if F_EQRef<>nil then Execute_EQClose; except end;
  228.   if pB then Execute_EQOpen(TQuery(self),fS);
  229.   fRef:=false;
  230. end;
  231.  
  232. procedure TEQuery.SetSave(pB:boolean);
  233. begin
  234.   fSave:=pB;
  235.   try if pB then WriteFields(1);
  236.   finally fSave:=false; end;
  237. end;
  238.  
  239. procedure TEQuery.SetLoad(pB:boolean);
  240. begin
  241.   fLoad:=pB;
  242.   try if pB then WriteFields(2);
  243.   finally fLoad:=false; end;
  244. end;
  245.  
  246. procedure TEQuery.SetFields(pB:boolean);
  247. begin
  248.   fFields:=pB;
  249.   try if pB then WriteFields(0);
  250.   finally fFields:=false; end;
  251. end;
  252.  
  253. procedure TEQuery.WriteFields(pMode:byte);
  254. var mS,mT,mT1:String;
  255.     i,j:integer;
  256.     mB:Boolean;
  257. begin
  258.     mT:='';
  259.     mB:=Active;
  260.     if Active=false then begin
  261.       try Active:=true; except end;
  262.       if Active=false then begin
  263.         ShowMessage(Name+' query - open error');
  264.         exit;
  265.       end;
  266.     end;
  267.     for i:=0 to FieldCount-1 do begin
  268.       mS:='String';
  269.       case Fields[i].DataType of
  270.       ftString: mS:='String';
  271.       ftSmallint, ftInteger, ftWord: mS:='Integer';
  272.       ftFloat:mS:='Float';
  273.       ftDate, ftTime, ftDateTime:mS:='DateTime';
  274.       end;
  275.       mS:='FieldByName('+#39+Fields[i].FieldName+#39+').as'+mS;
  276.       case pMode of
  277.       1: {Save}
  278.         begin
  279.           mS:=mS+':=';
  280.           j:=fS.IndexOfName(Fields[i].FieldName);
  281.           if j>-1 then mT1:=fS.Strings[j]+' ' else mT1:='';
  282.           mT1:=trim(copy(mT1,Pos('=',mT1)+1,255));
  283.           if mT1<>'' then mS:=mS+mT1+'.text';
  284.           mS:=mS+' ;';
  285.         end;
  286.       2: {Load}
  287.         begin
  288.           mS:=':='+mS;
  289.           j:=fS.IndexOfName(Fields[i].FieldName);
  290.           if j>-1 then mT1:=fS.Strings[j]+' ' else mT1:='';
  291.           mT1:=trim(copy(mT1,Pos('=',mT1)+1,255));
  292.           if mT1<>'' then mS:=mT1+'.text'+mS;
  293.           mS:=mS+' ;';
  294.         end;
  295.      end;
  296.       mT:=mT+mS+#13#10;
  297.       Clipboard.AsText:=mT;
  298.     end;
  299.     if not mB then Active:=false;
  300. End;
  301.  
  302. procedure TEQuery.ExecSQL;
  303. begin
  304.   MyExecute ('ExecSQL');
  305. end;
  306.  
  307. //********************************************************
  308.  
  309. procedure TEQuery.Open;
  310. begin
  311.   MyExecute ('Open');
  312. end;
  313.  
  314. //********************************************************
  315.  
  316. procedure TEQuery.First;
  317. begin
  318.   MyExecute ('First');
  319. end;
  320.  
  321. //********************************************************
  322.  
  323. procedure TEQuery.Next;
  324. begin
  325.   MyExecute ('Next');
  326. end;
  327.  
  328. //********************************************************
  329.  
  330. procedure TEQuery.Last;
  331. begin
  332.   MyExecute ('Last');
  333. end;
  334.  
  335. //********************************************************
  336.  
  337. procedure TEQuery.Prior;
  338. begin
  339.   MyExecute ('Prior');
  340. end;
  341.  
  342. (*
  343. procedure TEQuery.MoveBy(pM:integer);
  344. begin
  345.   cMoveBy:=pM;
  346.   MyExecute ('MoveBy');
  347. end;
  348. *)
  349.  
  350. //********************************************************
  351.  
  352. procedure TEQuery.Parse;
  353. begin
  354.   SQL_parse(Self.SQL.text);
  355. end;
  356.  
  357. function TEQuery.SQL_SelectCount(pQ:Tquery):integer;
  358. var mQ:TQuery;
  359. begin
  360.   result:=-1;
  361.   mQ:=Tquery.Create(pQ.owner);
  362.   try
  363.     mQ.dataBaseName:=pQ.DatabaseName;
  364.     SQL_Parse(pQ.SQL.text);
  365.     SQL_ChangeSegment('select Count(*) as xSelCnt');
  366.     mQ.SQL.Clear;
  367.     mQ.SQL.Text:=oSQL.text;
  368.     try
  369.       mQ.Open;
  370.       result:=mQ.FieldByName('xSelCnt').asInteger;
  371.       mQ.Close;
  372.     except end;
  373.   finally
  374.     mQ.Free;
  375.   end;
  376. end;
  377.  
  378. procedure TEQuery.SQL_Parse(pS:String);   //SQL parse
  379. var
  380.     mS:String;
  381.     i,j,k,mA:integer;
  382.     mB,mC:boolean;
  383. begin
  384.   while Pos(#13#10#13#10,pS)>0 do system.delete(pS,Pos(#13#10#13#10,pS),2);
  385.   while Pos(#13#13,pS)>0 do system.delete(pS,Pos(#13#13,pS),1);
  386.   if Trim(pS)='' then begin
  387.     oOldSQL:='';
  388.     for i:=1 to 20 do begin oWords[i].A:=0; oWords[i].B:=0;end;
  389.     exit;
  390.   end;
  391.   if (oOldSQL=pS) and (oSQL.text<>'') then exit;  // netreba vykonat uz bola vykonana
  392.   oSQL.text:=pS;
  393.   oOldSQL:=pS;
  394.   for i:=oSyntax.Count-1 downto 0 do oSyntax.Strings[i]:=TRIM(AnsiUpperCase(oSyntax.Strings[i]));
  395.   for i:=oSyntax.Count-1 downto 0 do if oSyntax.Strings[i]='' then oSyntax.delete(i);
  396.   for i:=0 to oSyntax.Count-1 do oWords[i+1].W:=oSyntax.Strings[i];
  397.   for i:=1 to 20 do begin oWords[i].A:=0; oWords[i].B:=0;end;
  398.   mS:=AnsiUpperCase(pS);
  399.   mA:=0; mB:=false; mC:=false;
  400.   for i:=1 to length(pS)-1 do begin
  401.     if (not (mB or mC)) and (mS[i]='(') then inc(mA);
  402.     if (not (mB or mC)) and (mS[i]=')') then dec(mA);
  403.     if (not mC) and (mS[i]='"') then mB:=not(mB);
  404.     if (not mB) and (mS[i]=#39) then mC:=not(mC);
  405.     if (mA=0) and (not mB) and (not mC) then begin
  406.       if oWords[1].A=0 then begin
  407.         if Length(mS)>=i+length(oWords[1].W) then begin
  408.           if (oWords[1].W=copy(mS,i,length(oWords[1].W))) and (mS[i+length(oWords[1].W)] in [' ',#13]) then oWords[1].A:=i;
  409.         end;
  410.       end else begin
  411.         for j:=2 to oSyntax.Count do begin
  412.           if oWords[j].A=0 then begin
  413.             if length(mS)>=i+length(oWords[j].W) then begin
  414.               if (oWords[j].W=copy(mS,i,length(oWords[j].W))) and (mS[i+length(oWords[j].W)] in [' ',#13]) then begin
  415.                 oWords[j].A:=i;
  416.                 for k:=j-1 downto 1 do if (oWords[k].A>0) and (oWords[k].A<i) and (oWords[k].B=0) then oWords[k].B:=i-1;
  417.                 Break;
  418.               end;
  419.             end;
  420.           end;
  421.         end;
  422.       end;
  423.     end;
  424.   end;
  425.   for k:=oSyntax.Count downto 1 do if (oWords[k].A>0) and (oWords[k].A<length(mS)) and (oWords[k].B=0) then oWords[k].B:=length(pS);
  426. End;
  427.  
  428. Function TEQuery.ABCopy(pS:String; pA,pB:longint):String;
  429. begin
  430.   Result:='';
  431.   if pB<pA then Exit;
  432.   if Length(pS)<=pA then Exit;
  433.   Result:=copy(pS,pA,pB-pA+1);
  434. End;
  435.  
  436. function TEQuery.SQL_GetSegment(pKeyWord:string):string; // vrati segment SQL dotazu
  437.  
  438.   Function Spaces2To1(pS:string):String;
  439.   begin
  440.     while Pos('  ',pS)>0 do System.Delete(pS,Pos('  ',pS),1);
  441.     Result:=pS;
  442.   End;
  443.  
  444.   function Normal(pT:String):String;
  445.   var h:integer;
  446.   begin
  447.     Result:=Spaces2To1(pT);
  448.     h:=Pos(#13#10,pT);
  449.     while h>0 do begin
  450.       system.delete(pT,h,2);
  451.       h:=Pos(#13#10,pT);
  452.     end;
  453.   end;
  454.  
  455.   var i:integer;
  456.       mKey:byte;
  457. begin
  458.   Result:=''; mKey:=0;
  459.   pKeyWord:=Trim(AnsiUpperCase(pKeyWord));
  460.   for i:=1 to oSyntax.Count do begin
  461.     if Trim(oWords[i].W)=pKeyWord then begin mKey:=i;Break; end;
  462.   end;
  463.   if mKey=0 then exit;
  464.   if oWords[mKey].A>0 then begin
  465.     Result:=Trim(ABcopy(oSQL.text,oWords[mKey].A,oWords[mKey].B));
  466.     Result:=Normal(Result);
  467.   end;
  468. end;
  469.  
  470. procedure TEQuery.SQL_ChangeSegment(pSegment:string);
  471.   procedure AddSegment(pKeyWord,pSegment:string);
  472.   var i,mKey:integer;
  473.       mS:String;
  474.   begin
  475.     mKey:=0;
  476.     for i:=0 to oSyntax.Count-1 do begin
  477.       if oSyntax.Strings[i]=pKeyWord then begin mKey:=i; break; end;
  478.     end;
  479.     if mKey=0 then exit;
  480.     for i:=mKey downto 1 do begin
  481.       if (oWords[i].B)>0 then begin
  482.         mS:=oSQL.text;
  483.         system.insert(#13#10+pSegment+#13#10,mS,oWords[i].B+1);
  484.         while Pos(#13#10#13#10,mS)>0 do system.delete(mS,Pos(#13#10#13#10,mS),2);
  485.         oSQL.text:=mS;
  486.         break;
  487.       end;
  488.     end;
  489.   End;
  490. const cD=240;
  491. var i,j,mA,mKey,mI:integer;
  492.     mT,mS,mKeyWord:string;
  493.     mB,mC:boolean;
  494. begin
  495.   if oWords[1].W='' then SQL_Parse(oSQL.text);
  496.   oOldSQL:='';
  497.   pSegment:=Trim(pSegment);
  498.   if (length(pSegment)>cD) then begin
  499.    // rozlamanie pSegment na max. cD dlhe riadky
  500.     mA:=0; mB:=false; mC:=false; mI:=0;
  501.     for i:=1 to length(pSegment) do begin
  502.       inc(mI);
  503.       if (not (mB or mC)) and (pSegment[i]='(') then inc(mA);
  504.       if (not (mB or mC)) and (pSegment[i]=')') then dec(mA);
  505.       if (not mC) and (pSegment[i]='"') then mB:=not(mB);
  506.       if (not mB) and (pSegment[i]=#39) then mC:=not(mC);
  507.       if (mA=0) and (not mB) and (not mC) then begin
  508.         if (mI>5) and ((pSegment[i]=' ') or (pSegment[i]=',')) then begin
  509.           if pSegment[i]=',' then mS:=mS+','+#13#10 else mS:=mS+' '+#13#10;
  510.           mI:=0;
  511.         end else mS:=mS+pSegment[i];
  512.       end else mS:=mS+pSegment[i];
  513.     end;
  514.     // mS je teraz uplne rozlamany
  515.     // ideme viazat riadky do dlzky cD
  516.     j:=Pos(#13#10,mS); mT:=''; pSegment:='';
  517.     if j=0 then j:=length(mS)+1;
  518.     while (j>0) and (Length(mS)>0) do begin
  519.       if length(mT)+Length(copy(mS,1,j-1))<=cD then mT:=mT+copy(mS,1,j-1)
  520.       else begin
  521.         pSegment:=pSegment+mT+#13#10;
  522.         mT:=copy(mS,1,j-1);
  523.       end;
  524.       system.delete(mS,1,j+1);
  525.       j:=Pos(#13#10,mS);
  526.       if j=0 then j:=length(mS)+1;
  527.     end;
  528.     pSegment:=pSegment+mT+#13#10;
  529.   end;
  530.   mKey:=0;
  531.   j:=Pos(' ',pSegment); if j=0 then j:=Pos(#13,pSegment); if j>10 then j:=0;
  532.   if j=0 then mKeyWord:=Trim(AnsiUpperCase(pSegment)) else mKeyWord:=Trim(AnsiUpperCase(copy(pSegment,1,j)));
  533.   for i:=1 to oSyntax.Count do begin
  534.     if Trim(oWords[i].W)=mKeyWord then begin mKey:=i;Break; end;
  535.   end;
  536.   if mKey=0 then exit;  // neexistujuci segment error
  537.   if j=0 then begin   // treba segment vymazat
  538.     if oWords[mKey].A>0 then begin // ak segment exituje tak odstranime
  539.       mS:=oSQL.Text;
  540.       system.delete(mS,oWords[mKey].A,oWords[mKey].B -oWords[mKey].A+1);
  541.       while Pos(#13#10#13#10,mS)>0 do system.delete(mS,Pos(#13#10#13#10,mS),2);
  542.       oSQL.text:=mS;
  543.     end;
  544.   end else begin
  545.     if oWords[mKey].A=0 then AddSegment(mKeyWord,pSegment) // treba segment pridat
  546.     else begin
  547.       mS:=oSQL.Text;   // treba segment prepisat
  548.       if oWords[mKey].A-1>1 then mT:=copy(mS,1,oWords[mKey].A-1) else mT:='';
  549.       mS:=copy(mS,oWords[mKey].B+1,length(oSQL.text));
  550.       if mT<>'' then mT:=mT+#13#10;
  551.       mS:=mT+pSegment+#13#10+mS;
  552.       while Pos(#13#10#13#10,mS)>0 do system.delete(mS,Pos(#13#10#13#10,mS),2);
  553.       oSQL.Text:=mS;
  554.     end;
  555.   end;
  556.   SQL_Parse(oSQL.text);
  557. End;
  558.  
  559. procedure TEQuery.SQL_AddToWhere(pS:String);
  560. var mS:String;
  561.     i:integer;
  562. begin
  563.   oOldAddToWhere:=pS;
  564.   if trim(pS)='' then exit;
  565.   mS:=TrimLeft(SQL_GetSegment('where'));
  566.   if Trim(mS)<>'' then begin
  567.     i:=Pos(' ',mS); if (i=0) or (i>6) then i:=6;
  568.     system.insert('(',mS,i+1);
  569.     mS:=mS+') and '+pS
  570.   end else mS:='where '+ pS;
  571.   SQL_ChangeSegment(mS);
  572. end;
  573.  
  574. procedure TEQuery.SQL_DeleteFromWhere(pS:String);
  575. // tato procedura je urcena iba pre odstanenie casi Where segmentu
  576. //ktora bola pridana AddToWhere procedurou  -  remove AddToWhere
  577. var mS:String;
  578.     i:integer;
  579. begin
  580.   if trim(pS)='' then exit;
  581.   mS:=Trim(SQL_GetSegment('where'));
  582.   system.delete(mS,1,5);
  583.   mS:=trim(mS);
  584.   pS:=Trim(pS);
  585.   i:=Pos(pS,mS); if i=0 then exit;
  586.   system.delete(mS,i,Length(pS));
  587.   mS:=trim(mS);
  588.   if length(mS)>3 then begin
  589.     if AnsiUpperCase(Copy(mS,Length(mS)-2,3))='AND' then begin
  590.       system.delete(mS,Length(mS)-2,3);
  591.       mS:=trim(mS);
  592.       if length(mS)>1 then begin
  593.         if (mS[1]='(') and (mS[length(mS)]=')') then begin
  594.           system.delete(mS,1,1); system.delete(mS,Length(mS),1);
  595.           mS:=trim(mS);
  596.         end;
  597.       end;
  598.     end;
  599.   end;
  600.   mS:=Trim('where '+mS);
  601.   SQL_ChangeSegment(mS);
  602. end;
  603.  
  604. function TEQuery.SQL_Text:string;
  605. begin;
  606.   result:=oSQL.text;
  607. end;
  608.  
  609. procedure TEQuery.SetoSQL(pS:TStringList);
  610. begin
  611.   oSQL.Clear;
  612.   oSQL.text:=pS.text;
  613.   SQL_Parse(pS.text);
  614. end;
  615.  
  616. procedure TEQuery.SetoOldSQL(pS:String);
  617. begin
  618.   // read only
  619.  // su tu iba preto aby bolo ich vidiet v object inspectoru
  620. end;
  621.  
  622. procedure TEQuery.SetOldWhere(pS:String);
  623. begin
  624.  //read only
  625.  // su tu iba preto aby bolo ich vidiet v object inspectoru
  626. end;
  627.  
  628. procedure TEQuery.SQL_Set(pQ:TQuery);
  629. var Q:TQuery;
  630.     a,b:boolean;
  631.     s:string;
  632. begin
  633.   s:=SQL_Text;
  634.   if pQ.SQL.Text=s then exit;
  635.   b:=pQ.ParamCount>0;
  636.   if b then begin
  637.     Q:=TQuery.Create(nil);
  638.     Q.SQL.text:=pQ.sql.text;
  639.     Q.Params:=pQ.Params;
  640.     Q.SQL.text:=pQ.SQL.Text;
  641.   end;
  642.   pQ.DisableControls;
  643.   pQ.close;
  644.   pQ.SQL.Text:=s;
  645.   try
  646.     if b then pQ.Params:=Q.params;
  647.    finally
  648.     pQ.EnableControls;
  649.     if b then Q.Free;
  650.   end;
  651. end;
  652.  
  653. (*
  654. procedure Register;
  655. begin
  656.   RegisterComponents('Edge', [TEQuery]);
  657. end;
  658. *)
  659.  
  660. end.
  661.