udbcontext.pas

来自「用DELPH写的DAO访问模块.有连接池.应用了各种模块,把数据库操作」· PAS 代码 · 共 113 行

PAS
113
字号
{*******************************************************}
{                                                       }
{       数据库环境对象基类                              }
{                                                       }
{       版权所有 (C) 2009 大道网络                      }
{                                                       }
{*******************************************************}

unit uDBContext;

interface

uses
  Classes, SysUtils, Windows, ADODB, DB, uDatabasePublic;

type

//==============================================================================
// 抽象数据库环境基本接口
//==============================================================================

  IDBContext = interface(IUnknown)
    ['{D9E176DD-F38C-4AE8-8E00-7E8686C1CCB0}']
    function getConnection: TCustomConnection;                //通过该接口取得数据库的Connection
    function Connection :Boolean;                             //连接数据库
    function getDBKind: TDBKind;                              //数据库类型
    procedure setDBConfig(pDBConfig :TDBConfig);              //获取Connection参数
  end;

//==============================================================================
// 抽象数据库连接对象基类
//==============================================================================

  TDBContext = class(TPersistent,IDBContext)
  private
    fDBKind: TDBKind;            //数据库连接方式
    fDBConfig: TDBConfig;        //数据库配置参数类
  protected
    FRefCount: Integer;
    pDBConn: TCustomConnection;
    
    //接口必须实现部分
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;

    procedure setDBKind(pDBKind :TDBKind);
    function getDBKind: TDBKind;
    procedure setDBConfig(pDBConfig :TDBConfig);
    function getDBConfig: TDBConfig;
  public
    destructor Destroy; override;
    //接口
    function getConnection: TCustomConnection; virtual; abstract;
    function Connection: Boolean; virtual; abstract;
    
  published
    property DBConnection: TCustomConnection read getConnection;
    property DBKind: TDBKind read getDBKind write setDBKind;
    property DBConfig: TDBConfig read getDBConfig write setDBConfig;
  end;
  
implementation

function TDBContext.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TDBContext._AddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TDBContext._Release: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
  if Result = 0 then
    Destroy;
end;

procedure TDBContext.setDBKind(pDBKind :TDBKind);
begin
  fDBKind := pDBKind;
end;

function TDBContext.getDBKind: TDBKind;
begin
  Result := fDBKind;
end;

procedure TDBContext.setDBConfig(pDBConfig :TDBConfig);
begin
  fDBConfig := pDBConfig;
end;

function TDBContext.getDBConfig: TDBConfig;
begin
  Result := fDBConfig;
end;

destructor TDBContext.Destroy;
begin
  fDBConfig.Free;
  inherited Destroy;
end;


end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?