批量执行SQL

代码:

unit DHibernateBatchSQL;

interface

uses
   Classes, SysUtils, db, ADODB;

type
   TOnException = procedure(Sender: TObject; E: Exception) of object;
   TOnFinishOne = procedure(Sender: TObject; FinishedSQL: string) of object;
   TDHibernateBatchSQL = class(TComponent)
   private
     FConnection: TADOConnection;
     FBatchSQL: TStringList;
     FBeforeExecute: TNotifyEvent;
     FAfterExecute: TNotifyEvent;
     FOnException: TOnException;
     FOnFinishOne: TOnFinishOne;
     FAbout: string;
     function GetBatchSQL: TStrings;
     procedure SetBatchSQL(const Value: TStrings);
   protected
     FSQLList: TStringList;
     procedure ExtractBatchSQLToSQLList;
   public
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
     procedure Execute;
   published
     property About: string read FAbout write FAbout;
     property Connection: TADOConnection read FConnection write FConnection;
     property BatchSQL: TStrings read GetBatchSQL write SetBatchSQL;
     property BeforeExecute: TNotifyEvent read FBeforeExecute write FBeforeExecute;
     property AfterExecute: TNotifyEvent read FAfterExecute write FAfterExecute;
     property OnException: TOnException read FOnException write FOnException;
     property OnFinishOne: TOnFinishOne read FOnFinishOne write FOnFinishOne;
   end;

implementation

{ TDHibernateBatchSQL }

constructor TDHibernateBatchSQL.Create(AOwner: TComponent);
begin
   inherited Create(AOwner);
   FConnection := nil;
   FBatchSQL := TStringList.Create;
   FSQLList := TStringList.Create;
end;

destructor TDHibernateBatchSQL.Destroy;
begin
   FConnection := nil;
   FBatchSQL.Free;
   FSQLList.Free;
   inherited Destroy;
end;

procedure TDHibernateBatchSQL.Execute;
var
   i                  : Integer;
   hql                : string;
begin
   if FConnection = nil then
     raise Exception.Create('No ADOConnection found!');
   if not FConnection.Connected then
     FConnection.Open();
   if Assigned(BeforeExecute) then
     BeforeExecute(Self);
   ExtractBatchSQLToSQLList;
   //
   with TADOQuery.Create(nil) do
   begin
     // todo: exceute the sql
     for i := 0 to FSQLList.Count - 1 do
     begin
       hql := FSQLList[i];
       // check whether select, insert, update, delete included.
       if Pos('select', hql) > 0 then
       begin
         // do select
         Close;
         SQL.Text := hql;
         try
           Open;
           if Assigned(OnFinishOne) then
             OnFinishOne(self, hql);
         except
           on E: Exception do
             if Assigned(OnException) then
               OnException(Self, E);
         end;
       end
       else
         if (Pos('insert', hql) > 0) or (pos('update', hql) > 0) or (Pos('delete', hql) > 0) then
         begin
           // do insert, update, delete
           Close;
           sql.Text := hql;
           try
             ExecSQL;
             if Assigned(OnFinishOne) then
               OnFinishOne(self, hql);
           except
             on E: Exception do
               if Assigned(OnException) then
                 OnException(Self, E);
           end;
         end
         else
         begin
           // not matched! exception throws.
           if Assigned(OnException) then
             OnException(self, exception.Create('SQL not contains select, insert, update or delete.'));
         end;
     end;
     close;
     Free;
   end;
   if Assigned(AfterExecute) then
     AfterExecute(Self);
end;

procedure TDHibernateBatchSQL.ExtractBatchSQLToSQLList;
var
   str                : string;
begin
   FSQLList.Clear;
   str := FBatchSQL.Text;
   // remove the CR_LF
   str := StringReplace(str, #13#10, EmptyStr, [rfReplaceAll, rfIgnoreCase]);
   // split the string to list
   FSQLList.Delimiter := '|';
   FSQLList.DelimitedText := str;
end;

function TDHibernateBatchSQL.GetBatchSQL: TStrings;
begin
   Result := FBatchSQL;
end;

procedure TDHibernateBatchSQL.SetBatchSQL(const Value: TStrings);
begin
   FBatchSQL.Assign(Value);
end;

end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值