Advertisement
Guest User

SQL Executer v2 (working)

a guest
Jul 15th, 2014
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.88 KB | None | 0 0
  1. unit SQLExec;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Classes, SysUtils, DB, ADODB;
  7.  
  8. type
  9.   TSQLExec = class;
  10.  
  11.   TSQLExecEvent = procedure(Sender: TSQLExec) of object;
  12.   TSQLExecFailEvent = procedure(Sender: TSQLExec; const EM: String) of object;
  13.   TSQLBlockEvent = procedure(Sender: TSQLExec; const Block, Line: Integer;
  14.     const SQL: String) of object;
  15.   TSQLBlockFailEvent = procedure(Sender: TSQLExec; const Block, Line: Integer;
  16.     const SQL, EM: String) of object;
  17.   TSQLPrintEvent = procedure(Sender: TSQLExec;const Block, Line: Integer;
  18.     const S: String) of object;
  19.  
  20.   TSQLExec = class(TObject)
  21.   private
  22.     FScript: TStringList;
  23.     FDB: TADOConnection;
  24.     FQry: TADOQuery;
  25.     FCurLine: Integer;
  26.     FCurBlock: Integer;
  27.     FOnExecSuccess: TSQLExecEvent;
  28.     FOnExecStart: TSQLExecEvent;
  29.     FOnExecFail: TSQLExecFailEvent;
  30.     FOnBlockSuccess: TSQLBlockEvent;
  31.     FOnBlockStart: TSQLBlockEvent;
  32.     FOnBlockFail: TSQLBlockFailEvent;
  33.     FOnPrint: TSQLPrintEvent;
  34.     procedure Print(const Block, Line: Integer; const S: String);
  35.   public
  36.     constructor Create;
  37.     destructor Destroy; override;
  38.     procedure ExecSQL(const Filename: String; const ConnStr: String;
  39.       const UseDB: String = '');
  40.     property OnExecStart: TSQLExecEvent read FOnExecStart write FOnExecStart;
  41.     property OnExecSuccess: TSQLExecEvent read FOnExecSuccess write FOnExecSuccess;
  42.     property OnExecFail: TSQLExecFailEvent read FOnExecFail write FOnExecFail;
  43.     property OnBlockStart: TSQLBlockEvent read FOnBlockStart write FOnBlockStart;
  44.     property OnBlockSuccess: TSQLBlockEvent read FOnBlockSuccess write FOnBlockSuccess;
  45.     property OnBlockFail: TSQLBlockFailEvent read FOnBlockFail write FOnBlockFail;
  46.     property OnPrint: TSQLPrintEvent read FOnPrint write FOnPrint;
  47.   end;
  48.  
  49. implementation
  50.  
  51. { TSQLExec }
  52.  
  53. constructor TSQLExec.Create;
  54. begin
  55.   FScript:= TStringList.Create;
  56.   FDB:= TADOConnection.Create(nil);
  57.   FDB.LoginPrompt:= False;
  58.   FQry:= TADOQuery.Create(nil);
  59.   FQry.Connection:= FDB;
  60.   FQry.ParamCheck:= False;
  61. end;
  62.  
  63. destructor TSQLExec.Destroy;
  64. begin
  65.   FDB.Connected:= False;
  66.   FQry.Free;
  67.   FDB.Free;
  68.   FScript.Free;
  69.   inherited;
  70. end;
  71.  
  72. procedure TSQLExec.ExecSQL(const Filename, ConnStr, UseDB: String);
  73. var
  74.   S: String;
  75.   X: Integer;
  76.   function ExecBlock: Boolean;
  77.   var
  78.     R: Integer;
  79.   begin
  80.     Inc(FCurBlock);
  81.     if Assigned(FOnBlockStart) then
  82.       FOnBlockStart(Self, FCurBlock, FCurLine, FQry.SQL.Text);
  83.     try
  84.       if FQry.SQL.Count > 0 then begin
  85.         R:= FQry.ExecSQL;
  86.         if Assigned(FOnBlockSuccess) then
  87.           FOnBlockSuccess(Self, FCurBlock, FCurLine, FQry.SQL.Text);
  88.       end;
  89.     except
  90.       on e: exception do begin
  91.         if Assigned(FOnBlockFail) then
  92.           FOnBlockFail(Self, FCurBlock, FCurLine, FQry.SQL.Text, e.Message);
  93.         raise Exception.Create('Block failure: '+e.Message);
  94.       end;
  95.     end;
  96.     FQry.SQL.Clear;
  97.   end;
  98.   procedure DoPrint;
  99.   var
  100.     P: Integer;
  101.   begin
  102.     P:= Pos('''', S);
  103.     Delete(S, 1, P-1);
  104.     P:= Pos('''', S);
  105.     Delete(S, P, Length(S));
  106.     Print(FCurBlock, FCurLine, S);
  107.     //Not sure if this follows the syntax rules...?
  108.   end;
  109. begin
  110.   FCurLine:= 0;
  111.   FCurBlock:= 0;
  112.   FQry.SQL.Clear;
  113.   FDB.ConnectionString:= ConnStr;
  114.   try
  115.     try
  116.       if Assigned(FOnExecStart) then
  117.         FOnExecStart(Self);
  118.       if FileExists(Filename) then begin      
  119.         FScript.LoadFromFile(Filename);
  120.         FDB.Connected:= True;
  121.         FDB.BeginTrans;
  122.         try
  123.           for X := 0 to FScript.Count-1 do begin
  124.             FCurLine:= X;
  125.             S:= FScript[X];
  126.             if (Pos('use ', Trim(S)) = 1) and (UseDB <> '') then begin
  127.               FScript[X]:= 'use '+UseDB;
  128.             end else
  129.             if Pos('print ', Trim(S))=1 then begin
  130.               //This is not working as intended... Necessary to keep?
  131.               DoPrint;
  132.             end else
  133.             if SameText('go', Trim(S)) then begin
  134.               ExecBlock;  //Actual execution
  135.             end else begin
  136.               if Trim(S) <> '' then
  137.                 FQry.SQL.Append(S);
  138.             end;
  139.           end;
  140.           //Execute last bit, in case it didn't end with GO
  141.           ExecBlock;
  142.           FDB.CommitTrans;
  143.           if Assigned(FOnExecSuccess) then
  144.             FOnExecSuccess(Self);
  145.         except
  146.           on e: exception do begin
  147.             FDB.RollbackTrans;
  148.             raise Exception.Create(e.Message);
  149.           end;
  150.         end;
  151.       end else begin
  152.         raise Exception.Create('File does not exist');
  153.       end;
  154.     except
  155.       on e: exception do begin
  156.         FOnExecFail(Self, e.Message);
  157.       end;
  158.     end;
  159.   finally
  160.     FQry.Close;
  161.     FDB.Connected:= False;
  162.   end;
  163. end;
  164.  
  165. procedure TSQLExec.Print(const Block, Line: Integer; const S: String);
  166. begin
  167.   if Assigned(FOnPrint) then
  168.     FOnPrint(Self, Block, Line, S);
  169. end;
  170.  
  171. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement