home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 March / Chip_2002-03_cd1.bin / zkuste / delphi / kompon / d5 / ADO.ZIP / src / AdoCommand.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-12-23  |  15.5 KB  |  456 lines

  1. unit AdoCommand;
  2.  
  3. interface   
  4.  
  5. {$I Ado25.inc}
  6.  
  7. uses
  8.   ActiveX, Classes, Graphics, OleServer, StdVCL,  Dialogs,
  9.   AdoConnection, Ado21Int, AdoConsts;
  10.  
  11.  
  12. type
  13.  
  14.   TParameterItems = class;
  15.  
  16.   TCommand = class(TOleServer)
  17.   private
  18.     FIntf:        ICommand;
  19.     FConnection: TConnection;
  20.     FParameterItems: TParameterItems;
  21.     function      GetCommand: ICommand;
  22.     function Get_CommandText: WideString;
  23.     function Get_CommandTimeout: Integer;
  24.     function Get_CommandType: CommandTypeEnum;
  25.     function Get_Parameters: IParameters;
  26.     function Get_Prepared: WordBool;
  27.     function GetActiveConnection: IConnection;
  28.     procedure Set_CommandText(const Value: WideString);
  29.     procedure Set_CommandTimeout(const Value: Integer);
  30.     procedure Set_CommandType(const Value: CommandTypeEnum);
  31.     procedure Set_Prepared(const Value: WordBool);
  32.     procedure SetActiveConnection(const Value: IConnection);
  33.     procedure SetConnection(const Value: TConnection);
  34.     function GetParameterItems: TParameterItems;
  35.     procedure SetParameterItems(const Value: TParameterItems);
  36.     procedure UpdateParameterItems;
  37.     procedure CheckActtiveConnection;
  38.     procedure CheckCommandText;
  39.   protected
  40.     procedure InitServerData; override;
  41.     function  Get_State: Integer;
  42.     procedure SetName(const NewName: TComponentName); override;
  43.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  44.   public
  45.     constructor Create(AOwner: TComponent); override;
  46.     destructor  Destroy; override;
  47.     procedure Connect; override;
  48.     procedure ConnectTo(svrIntf: ICommand);
  49.     procedure Disconnect; override;
  50.     procedure Cancel;
  51.     property  Command: ICommand read GetCommand;
  52.     property State: Integer read Get_State;                
  53.     //function  Execute: IRecordset; overload;
  54.     function  Execute(var RecordsAffected: integer; var Parameters: OleVariant; Options: Integer): IRecordset; overload;
  55.     function  CreateParameter(const Name: WideString; Type_: DataTypeEnum;
  56.                               Direction: ParameterDirectionEnum; Size: Integer; Value: OleVariant): IParameter; 
  57.     property Parameters: IParameters read Get_Parameters;
  58.     //property ActiveConnection: IConnection read GetActiveConnection write SetActiveConnection;
  59.   published
  60.     property CommandText: WideString read Get_CommandText write Set_CommandText;
  61.     property CommandTimeout: Integer read Get_CommandTimeout write Set_CommandTimeout;
  62.     property Prepared: WordBool read Get_Prepared write Set_Prepared;
  63.     property CommandType: CommandTypeEnum read Get_CommandType write Set_CommandType;
  64.  
  65.     //property ParameterItems: TParameterItems read GetParameterItems write SetParameterItems;
  66.     property Connection: TConnection read FConnection write SetConnection;
  67.   end;
  68.  
  69.   TParameterItem = class(TCollectionItem)
  70.   private
  71.     FName: WideString;
  72.     FValue: OleVariant;
  73.     FType_: DataTypeEnum;
  74.     FDirection: ParameterDirectionEnum;
  75.     FPrecision: Byte;
  76.     FNumericScale: Byte;
  77.     FSize: Integer;
  78.     FAttributes: Integer;
  79.     procedure SetType_(const Value: DataTypeEnum);
  80.     procedure SetValue(const Value: OleVariant);
  81.   protected
  82.     function GetDisplayName: string; override;
  83.   public
  84.     constructor Create(Collection: TCollection); override; 
  85.     destructor Destroy; override;
  86.     procedure Assign(Source: TPersistent); override;
  87.   published   
  88.     property Name: WideString read FName write FName;
  89.     property Value: OleVariant read FValue write SetValue;
  90.     property Type_: DataTypeEnum read FType_ write SetType_;
  91.     property Direction: ParameterDirectionEnum read FDirection write FDirection;
  92.     property Precision: Byte read FPrecision write FPrecision;
  93.     property NumericScale: Byte read FNumericScale write FNumericScale;
  94.     property Size: Integer read FSize write FSize;
  95.     property Attributes: Integer read FAttributes write FAttributes;
  96.   end;
  97.  
  98.   TParameterItems = class(TCollection)
  99.   private
  100.     FCommand: TCommand;
  101.     procedure AddParameter(Parameter: IParameter);
  102.     procedure AddParamteters(Parameters: IParameters);
  103.     function GetParametersArray: OleVariant;
  104.     function GetParameters(index: integer): IParameter;
  105.   public
  106.     constructor Create(Command: TCommand);
  107.     function Add: TParameterItem;
  108.     function GetItem(Index: Integer): TParameterItem;
  109.     procedure SetItem(Index: Integer; Value: TParameterItem);
  110.     property Items[Index: Integer]: TParameterItem read GetItem write SetItem; default;
  111.     property ParametersArray: OleVariant read GetParametersArray;
  112.     property Parameters[index: integer]: IParameter read GetParameters; 
  113.   end;
  114.  
  115.  
  116. implementation
  117.  
  118. uses ComObj;
  119.  
  120. procedure TCommand.InitServerData;
  121. const
  122.   CServerData: TServerData = (
  123.     ClassID:   '{00000507-0000-0010-8000-00AA006D2EA4}';
  124.     IntfIID:   '{0000054E-0000-0010-8000-00AA006D2EA4}';
  125.     EventIID:  '';
  126.     LicenseKey: nil;
  127.     Version: 500);
  128. begin
  129.   ServerData := @CServerData;
  130. end;
  131.  
  132. procedure TCommand.Connect;
  133. var
  134.   punk: IUnknown;
  135. begin
  136.   if FIntf = nil then
  137.   begin
  138.     punk := GetServer;
  139.     Fintf:= punk as ICommand;
  140.   end;
  141. end;
  142.  
  143. procedure TCommand.ConnectTo(svrIntf: ICommand);
  144. begin
  145.   Disconnect;
  146.   FIntf := svrIntf;
  147. end;
  148.  
  149. procedure TCommand.DisConnect;
  150. begin
  151.   if Fintf <> nil then
  152.   begin
  153.     FIntf := nil;
  154.   end;
  155. end;
  156.  
  157. function TCommand.GetCommand: ICommand;
  158. begin
  159.   if FIntf = nil then
  160.     Connect;
  161.   Assert(FIntf <> nil, 'Command is NULL. Component is not connected to Server. You must call ''Connect'' or ''ConnectTo'' before this operation');
  162.   Result := FIntf;
  163. end;
  164. //------------------------------------------------------------------------------
  165. constructor TCommand.Create(AOwner: TComponent);
  166. begin
  167.   inherited Create(AOwner);
  168.   FParameterItems:= TParameterItems.Create(self);
  169. end;
  170. //------------------------------------------------------------------------------
  171. destructor TCommand.Destroy;
  172. begin
  173.   inherited Destroy;
  174.   FParameterItems.Free;
  175. end;   
  176. //------------------------------------------------------------------------------
  177. procedure TCommand.CheckCommandText;
  178. begin
  179.   if CommandText = '' then
  180.     DoAdoError(emNoCommandText);
  181.   CommandText:= CommandText;
  182. end;
  183. //------------------------------------------------------------------------------
  184. procedure TCommand.CheckActtiveConnection;
  185. begin
  186.   if not Assigned(Connection) then
  187.     DoAdoError(emNoConnection);
  188.   if not Connection.Active then
  189.     DoAdoError(emNoConnectionActive);
  190.   Command.Set_ActiveConnection(Connection.Connection);
  191. end;
  192. //------------------------------------------------------------------------------
  193. function  TCommand.Get_State: Integer;
  194. begin
  195.   Result := Command.State;
  196. end;
  197. //------------------------------------------------------------------------------
  198. procedure TCommand.Cancel;
  199. begin
  200.   Command.Cancel;
  201. end;    
  202. //------------------------------------------------------------------------------
  203. {function TCommand.Execute: IRecordset;
  204. var Params: OleVariant;
  205. begin
  206.   CheckExecute;    
  207.   Params:= EmptyParam; //ParameterItems.GetParametersArray;
  208.   result:= Command.Execute(EmptyParam, EmptyParam, adCmdText) as IRecordset;
  209.   UpdateParameterItems;
  210. end;  }
  211. //------------------------------------------------------------------------------
  212. function TCommand.Execute(var RecordsAffected: integer;
  213.   var Parameters: OleVariant; Options: Integer): IRecordset;
  214. var RecsAffected: OleVariant;
  215. begin            
  216.   CheckActtiveConnection;
  217.   CheckCommandText;    
  218.   //UpdateParameterItems;
  219.   RecsAffected:= RecordsAffected;
  220.   result:= Command.Execute(RecsAffected, Parameters, Options) as IRecordset;
  221.   RecordsAffected:= RecsAffected;
  222. end;
  223. //------------------------------------------------------------------------------
  224. procedure TCommand.UpdateParameterItems;
  225. begin
  226.   FParameterItems.Clear;
  227.   FParameterItems.AddParamteters(Parameters);
  228. end;
  229. //------------------------------------------------------------------------------
  230. function TCommand.CreateParameter(const Name: WideString;
  231.   Type_: DataTypeEnum; Direction: ParameterDirectionEnum; Size: Integer;
  232.   Value: OleVariant): IParameter;
  233. begin
  234.   CheckActtiveConnection;
  235.   result:= Command.CreateParameter(Name, Type_, Direction, Size, Value);
  236. end;
  237. //------------------------------------------------------------------------------
  238. function TCommand.GetParameterItems: TParameterItems;
  239. begin
  240.   UpdateParameterItems;
  241.   result:= FParameterItems;
  242. end;
  243. //------------------------------------------------------------------------------
  244. procedure TCommand.SetParameterItems(const Value: TParameterItems);
  245. begin
  246.   FParameterItems:= Value;
  247. end;
  248. //------------------------------------------------------------------------------
  249. function TCommand.Get_Parameters: IParameters;
  250. begin
  251.   result:= Command.Parameters as IParameters;
  252. end;
  253. //------------------------------------------------------------------------------
  254. function TCommand.Get_CommandText: WideString;
  255. begin
  256.   result:= Command.CommandText;
  257. end;
  258. //------------------------------------------------------------------------------
  259. procedure TCommand.Set_CommandText(const Value: WideString);
  260. begin
  261.   Command.Set_CommandText(Value);
  262. end;
  263. //------------------------------------------------------------------------------
  264. function TCommand.Get_CommandTimeout: Integer;
  265. begin
  266.   result:= Command.CommandTimeout;
  267. end;
  268. //------------------------------------------------------------------------------
  269. procedure TCommand.Set_CommandTimeout(const Value: Integer);
  270. begin
  271.   Command.Set_CommandTimeout(Value);
  272. end;
  273. //------------------------------------------------------------------------------
  274. function TCommand.Get_CommandType: CommandTypeEnum;
  275. begin
  276.   result:= Command.Get_CommandType;
  277. end;
  278. //------------------------------------------------------------------------------
  279. procedure TCommand.Set_CommandType(const Value: CommandTypeEnum);
  280. begin
  281.   if (Value = adCmdFile) or (Value = adCmdTableDirect) then
  282.     DoAdoError(emCommandType);
  283.   Command.CommandType:= Value;
  284. end;
  285. //------------------------------------------------------------------------------
  286. function TCommand.Get_Prepared: WordBool;
  287. begin
  288.   result:= Command.Prepared;
  289. end;
  290. //------------------------------------------------------------------------------
  291. procedure TCommand.Set_Prepared(const Value: WordBool);
  292. begin
  293.   Command.Set_Prepared(Value);
  294. end;
  295. //------------------------------------------------------------------------------
  296. function TCommand.GetActiveConnection: IConnection;
  297. begin
  298.   result:= Command.Get_ActiveConnection as IConnection;
  299. end;
  300. //------------------------------------------------------------------------------
  301. procedure TCommand.SetActiveConnection(const Value: IConnection);
  302. begin
  303.   Command._Set_ActiveConnection(Value as Connection15);
  304. end;
  305. //------------------------------------------------------------------------------
  306. procedure TCommand.SetName(const NewName: TComponentName);
  307. begin
  308.   inherited;
  309.   Command.Set_Name(NewName);
  310. end;
  311. //------------------------------------------------------------------------------
  312. procedure TCommand.SetConnection(const Value: TConnection);
  313. begin
  314.   if Value<>Connection then begin
  315.     if Assigned(Value) then
  316.       Value.FreeNotification(self);
  317.     FConnection := Value;
  318.   end;
  319. end;
  320. //------------------------------------------------------------------------------
  321. procedure TCommand.Notification(AComponent: TComponent;
  322.   Operation: TOperation);
  323. begin
  324.   inherited;
  325.   if (Operation = opRemove)  then begin
  326.     if (AComponent = FConnection) then begin
  327.       FConnection:= nil;
  328.     end;
  329.   end;
  330. end;
  331. //------------------------------------------------------------------------------
  332. { TParameterItem }
  333. //------------------------------------------------------------------------------
  334. constructor TParameterItem.Create(Collection: TCollection);
  335. begin
  336.   inherited;
  337.   Name:= 'ParameterName';
  338.   Value:= Null;
  339.   Type_:= adEmpty;
  340.   Direction:= adParamInput;
  341.   Precision:= 0;
  342.   NumericScale:= 0;
  343.   Attributes:= adParamSigned;
  344.   Size:= 0;
  345. end;
  346. //------------------------------------------------------------------------------
  347. destructor TParameterItem.Destroy;
  348. begin
  349.   inherited;
  350. end;
  351. //------------------------------------------------------------------------------
  352. procedure TParameterItem.Assign(Source: TPersistent);
  353. var src: TParameterItem;
  354. begin
  355.   inherited Assign(Source);
  356.   if Source is TParameterItem then begin
  357.     src:= TParameterItem(Source);
  358.     Name:= src.Name;
  359.     Value:= src.Value;
  360.     Type_:= src.Type_;
  361.     Direction:= src.Direction;
  362.     Precision:= src.Precision;
  363.     NumericScale:= src.NumericScale;
  364.     Attributes:= src.Attributes;
  365.     Size:= src.Size;
  366.   end;
  367. end;
  368. //------------------------------------------------------------------------------
  369. function TParameterItem.GetDisplayName: string;
  370. begin
  371.     result:= Name;
  372.   if result='' then
  373.     result:= inherited GetDisplayName;
  374. end;
  375. //------------------------------------------------------------------------------
  376. procedure TParameterItems.AddParameter(Parameter: IParameter);
  377. begin
  378.   with Add do begin
  379.     Name:= Parameter.Name;
  380.     Value:= Parameter.Value;
  381.     Type_:= Parameter.Type_;
  382.     Direction:= Parameter.Get_Direction;
  383.     Precision:= Parameter.Get_Precision;
  384.     NumericScale:= Parameter.Get_NumericScale;
  385.     Attributes:= Parameter.Get_Attributes;
  386.     Size:= Parameter.Get_Size;
  387.   end;
  388. end;
  389. //------------------------------------------------------------------------------
  390. procedure TParameterItems.AddParamteters(Parameters: IParameters);
  391. var i: integer;
  392. begin
  393.   for i:= 0 to Parameters.Count-1 do
  394.     AddParameter(Parameters[i] as IParameter);
  395. end;
  396. //------------------------------------------------------------------------------
  397. procedure TParameterItem.SetType_(const Value: DataTypeEnum);
  398. begin
  399.   FType_ := Value;
  400. end;
  401. //------------------------------------------------------------------------------
  402. procedure TParameterItem.SetValue(const Value: OleVariant);
  403. begin
  404.   FValue := Value;
  405.   FType_:= VarType(FValue);
  406. end;
  407. //------------------------------------------------------------------------------
  408. { TParameterItems }
  409. //------------------------------------------------------------------------------
  410. constructor TParameterItems.Create(Command: TCommand);
  411. begin
  412.   inherited Create(TParameterItem);
  413.   FCommand:= Command;
  414. end;
  415. //------------------------------------------------------------------------------
  416. function TParameterItems.Add: TParameterItem;
  417. begin
  418.   result:= inherited Add as TParameterItem;
  419. end;
  420. //------------------------------------------------------------------------------
  421. function TParameterItems.GetItem(Index: Integer): TParameterItem;
  422. begin
  423.   result:= inherited GetItem(Index) as TParameterItem;
  424. end;
  425. //------------------------------------------------------------------------------
  426. procedure TParameterItems.SetItem(Index: Integer; Value: TParameterItem);
  427. begin
  428.   inherited SetItem(Index, Value);
  429. end;
  430. //------------------------------------------------------------------------------
  431. function TParameterItems.GetParametersArray: OleVariant;
  432. var i: integer;
  433. begin
  434.   if Count = 0 then
  435.     result:= EmptyParam
  436.   else begin
  437.   result:= VarArrayCreate([0, Count-1], varDispatch);
  438.     for i:= 0 to Count-1 do
  439.       result:= Parameters[i];
  440.   end;
  441. end;
  442.  
  443. function TParameterItems.GetParameters(index: integer): IParameter;
  444. begin
  445.   with Items[index] do begin
  446.     result:= FCommand.CreateParameter(Name, Type_, Direction, Size, Value);
  447.     if not Assigned(result) then
  448.       DoAdoError(emCreateParameter);
  449.     result.Precision:= Precision;
  450.     result.NumericScale:= NumericScale;
  451.     result.Set_Attributes(Attributes);
  452.   end;
  453. end;
  454.  
  455. end.
  456.