📄 unitdatainterface.pas
字号:
unit UnitDataInterface;
interface
uses
Windows, Classes, DB, adodb, SysUtils, Variants, provider, dbclient;
type
TBaseCommand = class;
TBaseController = class;
TBaseCommandClass = class of TBaseCommand;
TBaseControllerClass = class of TBaseController;
TBaseCommand = class(TComponent)
private
FClientData: Boolean;
FClientDataSet: TClientDataSet;
FProvider: TDataSetProvider;
protected
FParams: TParams;
FCommandText: string;
FCommandType: TCommandType;
FController: TBaseController;
FDataObject: TComponent;
protected
function GetClientData(AData: TDataSet): TDataSet;
procedure SetCommandText(const Value: string); virtual;
function GetParams: TParams; virtual; abstract;
procedure SetCommandType(const Value: TCommandType); virtual;
procedure CreateObject;virtual;abstract;
public
property Controller: TBaseController read FController write FController;
property Params: TParams read GetParams;
property ClientData: Boolean read FClientData write FClientData;
constructor Create(AOwner: TComponent; AController: TBaseController); reintroduce; overload;
constructor Create(AOwner: TComponent; AController: TBaseController;
AText: string; AType: TCommandType); reintroduce; overload;
destructor Destroy; override;
procedure AddParameter(AParamName: string; AParamValue: Variant);
procedure AddSQLParameter(AParamName: string; AParamValue: Variant);
function Execute(CloseConnection: Boolean = false): Boolean; virtual; abstract;
function Open: TDataSet; virtual; abstract;
procedure Close; virtual; abstract;
published
property CommandText: string read FCommandText write SetCommandText;
property CommandType: TCommandType read FCommandType write SetCommandType;
end;
TBaseController = class(TComponent)
private
FConnection: TCustomConnection;
function GetConnected: Boolean;
// function GetConnection: TCustomConnection;
protected
FConnect: string;
procedure CreateConnection; virtual;
procedure SetConnectString(const Value: string); virtual;
public
constructor Create(AOwner: TComponent; AConnect: string); reintroduce;
destructor Destroy; override;
procedure CloseConnection;
function CreateCommand(AText: string; AType: TCommandType): TBaseCommand; virtual; abstract;
function IsAccessDB: Boolean;
function OpenConnection: Boolean;
function InTransaction: Boolean; virtual;
function BeginTrans: integer; virtual;
procedure CommitTrans; virtual;
procedure RollbackTrans; virtual;
function Execute(ASQL: string): Boolean; virtual;
function NewController: TBaseController;
published
property Connected: Boolean read GetConnected;
property Connection: TCustomConnection read FConnection write FConnection;
property ConnectString: string read FConnect write SetConnectString;
end;
implementation
uses UnitAppLogger;
{ TBaseCommand }
constructor TBaseCommand.Create(AOwner: TComponent;
AController: TBaseController);
begin
inherited Create(AOwner);
FController := AController;
FProvider := TDataSetProvider.Create(Self);
end;
constructor TBaseCommand.Create(AOwner: TComponent; AController: TBaseController; AText: string;
AType: TCommandType);
begin
Create(AOwner, AController);
FCommandText := AText;
FCommandType := AType;
end;
procedure TBaseCommand.AddParameter(AParamName: string;
AParamValue: Variant);
var
fParam: TParam;
begin
fParam := Params.FindParam(AParamName);
if fParam = nil then
fParam := Params.CreateParam(VarTypeToDataType(VarType(AParamValue)) , AParamName, ptInput);
fParam.Value := AParamValue;
end;
procedure TBaseCommand.SetCommandText(const Value: string);
begin
FCommandText := Value;
end;
procedure TBaseCommand.SetCommandType(const Value: TCommandType);
begin
FCommandType := Value;
end;
procedure TBaseCommand.AddSQLParameter(AParamName: string;
AParamValue: Variant);
begin
AddParameter('@' + AParamName, AParamValue);
end;
destructor TBaseCommand.Destroy;
begin
// if FClientDataSet <> nil then
// FClientDataSet.Free;
if FParams <> nil then
FreeAndNil(FParams);
FProvider.Free;
inherited;
end;
function TBaseCommand.GetClientData(AData: TDataSet): TDataSet;
begin
FProvider.DataSet := AData;
if FClientDataSet = nil then
FClientDataSet := TClientDataSet.Create(Owner);
FClientDataSet.Data := FProvider.Data;
result := FClientDataSet;
Close;
end;
{ TBaseController }
function TBaseController.BeginTrans: integer;
begin
end;
procedure TBaseController.CloseConnection;
begin
if FConnection <> nil then
FConnection.Close;
end;
procedure TBaseController.CommitTrans;
begin
end;
constructor TBaseController.Create(AOwner: TComponent; AConnect: string);
begin
inherited Create(AOwner);
FConnect := AConnect;
CreateConnection;
end;
//function TBaseController.CreateCommand(TBaseCommandClass; AText: string;
// AType: TCommandType): TBaseCommand;
//begin
//// result := ACommandType.Create(Self, Self, AText, AType);
//// result.Controller := Self;
//end;
procedure TBaseController.CreateConnection;
begin
end;
destructor TBaseController.Destroy;
begin
CloseConnection;
FConnection.Free;
inherited;
end;
function TBaseController.Execute(ASQL: string): Boolean;
begin
result := False;
end;
function TBaseController.GetConnected: Boolean;
begin
result := False;
if FConnection <> nil then
result := FConnection.Connected;
end;
//function TBaseController.GetConnection: TCustomConnection;
//begin
//// if not Assigned( FConnection) then
// result := FConnection;
//end;
function TBaseController.InTransaction: Boolean;
begin
end;
function TBaseController.IsAccessDB: Boolean;
const
JetProvider = 'Microsoft.Jet.OLEDB';
begin
result := (AnsiPos(JetProvider, ConnectString) > 0);
end;
function TBaseController.NewController: TBaseController;
var
fType: TBaseControllerClass;
begin
// result := ClassInfo. Create(Owner, ConnectString);
fType := TBaseControllerClass(ClassType);
result := fType.Create(Owner, ConnectString);
end;
function TBaseController.OpenConnection: Boolean;
begin
result := False;
if Connection <> nil then
begin
if not Connection.Connected then
begin
try
SetConnectString(ConnectString);
Connection.Open;
result := True;
except
on e: Exception do
begin
AppLogger.AddLog(Self, '%s.OpenConnection: %s', [ClassName, E.Message]);
// raise;
end;
end; // try/except
end;
result := Connection.Connected;
end;
end;
procedure TBaseController.RollbackTrans;
begin
end;
procedure TBaseController.SetConnectString(const Value: string);
begin
if not SameText(FConnect, Value) then
begin
FConnect := Value;
if Connected then
CloseConnection;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -