⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqlclient.pas

📁 通过Tmysql来访问MSQL Server数据库的应用案例.
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit mySQLClient;

{
  Unit:    TmySQLClient - Common Data Structures
  Project: TmySQL - http://www.productivity.org/projects/tmysql
  Author:  Justin P. Yunke <yunke@productivity.org>
  Date:    November 1998 - January 2002
  ** Join the TmySQL mailing list at http://www.elists.org **

  Copyrights/Credits:
    Parts Copyright (c) 2001 Justin P. Yunke <yunke@productivity.org>
    Parts Copyright (c) 2001 Ken Kyler
    MySQL AB (mySQL software and component bitmap art)
    Bob Silva <bsilva@umesd.k12.or.us> (_libmysql.pas)

  License:
    Open Source.  You must retain copyrights and distribution information
    listed in these files.

  Description:
    This Delphi VCL/Custom Component set allows TCP/IP-based connections
  to mySQL servers, and support code to allow full use of the libmysql.DLL
  library provided by TcX for mysql.
    Programmers can either use mySQLClient VCL directly, or indirectly
  by using the TmySQLDB VCLs (see mySQLDB.pas).

  Requirements:
    Delphi 3.0 (I believe)
    libmysql.dll dated 1/5/99 or later

  Date Log:

  8/9/2001: Tons of undocumented changes over many months.

  1/6/99: Refreshed log.  Switched some things around to fit a model
  suggested by Ken.  Once we get good documentation up, it'll start to
  make sense.  Today marked an important day due to Monty's fix for the
  libmysql DLL, making it thread safe.  I plan to add many nice Threaded
  goodies/options over the next few weeks.  jy

  1/14/99a: A week later, I have some working Threaded code.  A restructuring
  occurred on how one uses TmySQLClientQuery and TmySQLClientModify.

  1/27/99a: Cleaned up, reviewed code.

  2/14/99a: Major revisions completed over the past two weeks, including the
  implementation of a queuing system for SQL queries/modifications.

  2/16/99a: Generation of TmySQLClientUtility class.  Began work on making
  all utility functions (connect, close, ping, etc.) threaded.  Extensive
  reworking of TmySQLClient VCL events.

  2/16/99b: All utility functions have been moved into the TmySQLClientUtility
  class.  Next step is to build the test app so that it tests everything.

  2/17/99a: Test app built and is running fine.  Complete some minor changes
  and improvements to various functions in mySQLCommon.

  2/22/99a: Minor changes to Refresh and Options capabilities.

  3/8/99: Quick one-line bug fix for queries.

  3/16/99: Spent time bug hunting.

  1/5/00: Major bugfixes and addition of connection timeout.

  7/23/01: Massive memory leak found and removed.
}

interface

uses
  mySQLCommon, Classes, ExtCtrls, mysql;

const
{Last Modification Information}
  TMYSQLCLIENT_VERSION       = '2.1a';
  TMYSQLCLIENT_LAST_MODIFIED = '01.27.2002';
  TMYSQLCLIENT_LAST_AUTHOR   = 'jpy';

  DEFAULT_CONNECTED_TIMEOUT_INTERVAL = 300000; // 5 minutes

{ The TCustom_mySQLClient component is for deriving your own custom components. }
{ Most people will probably use TmySQLClient through the VCL palette.         jy}
type
  TCustom_mySQLClient = class(TComponent)
  private
    FThreaded                 : boolean;

    FSession                  : tmysql;

    FCap                      : integer;

    FTaskHandler              : TmySQLClientTaskHandler;

    FQuery                    : TmySQLClientQuery;
    FModify                   : TmySQLClientModify;
    FUtility                  : TmySQLClientUtility;

    FConnected                : boolean;

    FConnectedTimeoutInterval : integer;
    FConnectedTimer           : TTimer;
    FTimedOut                 : boolean;

    FBlockOnClose             : boolean;

    FDLLPath,
    FHost,
    FUser,
    FPasswd,
    FDb                       : string;
    FPort                     : integer;
    FCapabilities             : TSetClientCapabilities;

    FOnConnect                : TmySQLClientTask_OnComplete;
    FOnConnectError           : TmySQLClientTask_OnError;

    FOnClose                  : TmySQLClientTask_OnComplete;

    FOnQuery                  : TmySQLClientTask_OnComplete;
    FOnQueryError             : TmySQLClientTask_OnError;

    FOnModify                 : TmySQLClientTask_OnComplete;
    FOnModifyError            : TmySQLClientTask_OnError;

    FOnSelectDatabase         : TmySQLClientTask_OnComplete;
    FOnSelectDatabaseError    : TmySQLClientTask_OnError;

    FOnCreateDatabase         : TmySQLClientTask_OnComplete;
    FOnCreateDatabaseError    : TmySQLClientTask_OnError;

    FOnDropDatabase           : TmySQLClientTask_OnComplete;
    FOnDropDatabaseError      : TmySQLClientTask_OnError;

    FOnListDatabases          : TmySQLClientTask_OnComplete;
    FOnListDatabasesError     : TmySQLClientTask_OnError;

    FOnListTables             : TmySQLClientTask_OnComplete;
    FOnListTablesError        : TmySQLClientTask_OnError;

    FOnListFields             : TmySQLClientTask_OnComplete;
    FOnListFieldsError        : TmySQLClientTask_OnError;

    FOnListProcesses          : TmySQLClientTask_OnComplete;
    FOnListProcessesError     : TmySQLClientTask_OnError;

    FOnPing                   : TmySQLClientTask_OnComplete;
    FOnPingError              : TmySQLClientTask_OnError;

    FOnShutdown               : TmySQLClientTask_OnComplete;
    FOnShutdownError          : TmySQLClientTask_OnError;

    FOnKill                   : TmySQLClientTask_OnComplete;
    FOnKillError              : TmySQLClientTask_OnError;

    FOnOptions                : TmySQLClientTask_OnComplete;
    FOnOptionsError           : TmySQLClientTask_OnError;

    FOnRefresh                : TmySQLClientTask_OnComplete;
    FOnRefreshError           : TmySQLClientTask_OnError;

    FOnStatus                 : TmySQLClientTask_OnStatus;

    procedure   SetDLLPath(const S : string);
    function    GetDLLPath : string;

    procedure   SetHostname(const S : string);
    function    GetHostname : string;
    procedure   SetPort(const i : integer);
    function    GetPort : integer;

    procedure   SetUsername(const S : string);
    function    GetUsername : string;
    procedure   SetPassword(const S : string);
    function    GetPassword : string;

    procedure   SetDatabase(const S : string);
    function    GetDatabase : string;

    procedure   SetCapabilities (const C : TSetClientCapabilities);
    function    GetCapabilities : TSetClientCapabilities;

    procedure   SetOnStatus(OS : TmySQLClientTask_OnStatus);
    function    GetOnStatus : TmySQLClientTask_OnStatus;

    function    GetThreaded : boolean;
    procedure   SetThreaded(const b : boolean);

    procedure   _OnConnect(Sender : TObject);
    procedure   _OnClose(Sender : TObject);
    procedure   _OnTimeOut(Sender : TObject);
    procedure   _OnTaskFinished(Sender : TObject);

    procedure   StartTimer;
    procedure   StopTimer;

    function    CreateQuery : TmySQLClientQuery;
    function    CreateModify : TmySQLClientModify;
  protected
    property    Connected             : boolean read FConnected;

    property    BlockOnClose          : boolean read FBlockOnClose write FBlockOnClose;

    property    Threaded              : boolean read GetThreaded  write SetThreaded;

    property    DLLPath               : string read GetDLLPath write SetDLLPath;

    property    Hostname              : string read GetHostname write SetHostname;
    property    Port                  : integer read GetPort write SetPort;

    property    Username              : string read GetUsername write SetUsername;
    property    Password              : string read GetPassword write SetPassword;

    property    Database              : string read GetDatabase write SetDatabase;

    property    ConnectedTimer        : TTimer read FConnectedTimer write FConnectedTimer;
    property    ConnectedTimeOutInterval : integer read FConnectedTimeOutInterval write FConnectedTimeOutInterval;

    property    Capabilities          : TSetClientCapabilities read GetCapabilities write SetCapabilities;

    property    OnConnect             : TmySQLClientTask_OnComplete read FOnConnect      write FOnConnect;
    property    OnConnectError        : TmySQLClientTask_OnError    read FOnConnectError write FOnConnectError;

    property    OnClose               : TmySQLClientTask_OnComplete read FOnClose write FOnClose;

    property    OnQuery               : TmySQLClientTask_OnComplete read FOnQuery         write FOnQuery;
    property    OnQueryError          : TmySQLClientTask_OnError    read FOnQueryError    write FOnQueryError;

    property    OnModify              : TmySQLClientTask_OnComplete read FOnModify write FOnModify;
    property    OnModifyError         : TmySQLClientTask_OnError    read FOnModifyError    write FOnModifyError;

    property    OnSelectDatabase      : TmySQLClientTask_OnComplete read FOnSelectDatabase write FOnSelectDatabase;
    property    OnSelectDatabaseError : TmySQLClientTask_OnError    read FOnSelectDatabaseError write FOnSelectDatabaseError;

    property    OnCreateDatabase      : TmySQLClientTask_OnComplete read FOnCreateDatabase write FOnCreateDatabase;
    property    OnCreateDatabaseError : TmySQLClientTask_OnError    read FOnCreateDatabaseError write FOnCreateDatabaseError;

    property    OnDropDatabase        : TmySQLClientTask_OnComplete read FOnDropDatabase write FOnDropDatabase;
    property    OnDropDatabaseError   : TmySQLClientTask_OnError    read FOnDropDatabaseError write FOnDropDatabaseError;

    property    OnListDatabases       : TmySQLClientTask_OnComplete read FOnListDatabases write FOnListDatabases;
    property    OnListDatabasesError  : TmySQLClientTask_OnError    read FOnListDatabasesError write FOnListDatabasesError;

    property    OnListTables          : TmySQLClientTask_OnComplete read FOnListTables write FOnListTables;
    property    OnListTablesError     : TmySQLClientTask_OnError    read FOnListTablesError write FOnListTablesError;

    property    OnListFields          : TmySQLClientTask_OnComplete read FOnListFields write FOnListFields;
    property    OnListFieldsError     : TmySQLClientTask_OnError    read FOnListFieldsError write FOnListFieldsError;

    property    OnListProcesses       : TmySQLClientTask_OnComplete read FOnListProcesses write FOnListProcesses;
    property    OnListProcessesError  : TmySQLClientTask_OnError    read FOnListProcessesError write FOnListProcessesError;

    property    OnPing                : TmySQLClientTask_OnComplete read FOnPing write FOnPing;
    property    OnPingError           : TmySQLClientTask_OnError    read FOnPingError write FOnPingError;

    property    OnOptions             : TmySQLClientTask_OnComplete read FOnOptions write FOnOptions;
    property    OnOptionsError        : TmySQLClientTask_OnError    read FOnOptionsError write FOnOptionsError;

    property    OnRefresh             : TmySQLClientTask_OnComplete read FOnRefresh write FOnRefresh;
    property    OnRefreshError        : TmySQLClientTask_OnError    read FOnRefreshError write FOnRefreshError;

    property    OnShutdown            : TmySQLClientTask_OnComplete read FOnShutdown write FOnShutdown;
    property    OnShutdownError       : TmySQLClientTask_OnError    read FOnShutdownError write FOnShutdownError;

    property    OnStatus              : TmySQLClientTask_OnStatus  read GetOnStatus write SetOnStatus;
  public
    property    Query   : TmySQLClientQuery   read FQuery   write FQuery;
    property    Modify  : TmySQLClientModify  read FModify  write FModify;
    property    Utility : TmySQLClientUtility read FUtility write FUtility;

    property    TaskHandler : TmySQLClientTaskHandler read FTaskHandler write FTaskHandler;

    constructor Create(AOwner : TComponent); override;
    destructor  Destroy; override;

    procedure   Connect;
    procedure   Close;
    procedure   ForceClose;

    procedure   Execute;
  end;

{ This CLASS is the visual component you will most likely }
{ use.                                                  jy}
  TmySQLClient = class(TCustom_mySQLClient)
  public
    property    Connected;
  published
    property    DLLPath;

    property    Hostname;
    property    Port;

    property    Username;
    property    Password;

    property    Database;

    property    Capabilities;

    property    Threaded;

    property    ConnectedTimeOutInterval;
    property    ConnectedTimer;

    property    BlockOnClose;

    property    OnConnect;
    property    OnConnectError;

    property    OnClose;

    property    OnQuery;
    property    OnQueryError;

    property    OnModify;
    property    OnModifyError;

    property    OnSelectDatabase;
    property    OnSelectDatabaseError;

    property    OnCreateDatabase;
    property    OnCreateDatabaseError;

    property    OnDropDatabase;
    property    OnDropDatabaseError;

    property    OnListDatabases;
    property    OnListDatabasesError;

    property    OnListTables;
    property    OnListTablesError;

    property    OnListFields;
    property    OnListFieldsError;

    property    OnListProcesses;
    property    OnListProcessesError;

    property    OnPing;
    property    OnPingError;

    property    OnRefresh;
    property    OnRefreshError;

    property    OnOptions;
    property    OnOptionsError;

    property    OnShutdown;
    property    OnShutdownError;

    property    OnStatus;
  end;

procedure Register;

implementation

uses
  SysUtils;

constructor TCustom_mySQLClient.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);

  FConnected               :=FALSE;

  FConnectedTimer          :=nil;
  FConnectedTimeOutInterval:=DEFAULT_CONNECTED_TIMEOUT_INTERVAL;
  FTimedOut                :=FALSE;

  FBlockOnClose            :=TRUE;

  FThreaded                :=FALSE;

  FTaskHandler             :=TmySQLClientTaskHandler.Create(FThreaded);
  FTaskHandler.Session     :=@Fsession;
  FTaskHandler.Thread.OnFinished:=_OnTaskFinished;

  FUtility                 :=TmySQLClientUtility.Create;
  FUtility.TaskHandler     :=FTaskHandler;
  FQuery                   :=nil;
  FModify                  :=nil;

  FHost                    :='localhost';
  FUser                    :='';
  FPasswd                  :='';
  FDb                      :='';
  FPort                    :=MYSQL_PORT;
  FCapabilities            :=[_CLIENT_LONG_PASSWORD,_CLIENT_CONNECT_WITH_DB];

  FOnConnect               :=nil;
  FOnConnectError          :=nil;

  FOnQuery                 :=nil;
  FOnQueryError            :=nil;

  FOnModify                :=nil;
  FOnModifyError           :=nil;

  FOnSelectDatabase        :=nil;
  FOnSelectDatabaseError   :=nil;
end;

destructor  TCustom_mySQLClient.Destroy;
begin
  ForceClose;

  FTaskHandler.Free;

⌨️ 快捷键说明

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