disqlite3database.pas

来自「DELPHI 访问SQLITE3 数据库的VCL控件」· PAS 代码 · 共 2,333 行 · 第 1/5 页

PAS
2,333
字号
{-------------------------------------------------------------------------------
 
 Copyright (c) 1999-2007 Ralf Junker, The Delphi Inspiration
 Internet: http://www.yunqa.de/delphi/
 E-Mail:   delphi@yunqa.de

-------------------------------------------------------------------------------}

unit DISQLite3Database;

{$I DI.inc}
{$I DISQLite3.inc}

interface

uses
  Classes,
  DISQLite3Api;

type

  TJulianDate = DISQLite3Api.TDIJulianDate;

  TDISQLite3Cell8 = packed record
    CellType: Byte;
    case Byte of
      1: (CellInteger: Int64);
      2: (CellFloat: Double);
      3: (CellText: packed record l: Integer; p: PAnsiChar; end);
      4: (CellBlob: packed record l: Integer; p: Pointer; end);
  end;

  TDISQLite3Cell16 = packed record
    CellType: Byte;
    case Byte of
      1: (CellInteger: Int64);
      2: (CellFloat: Double);
      3: (CellText: packed record l: Integer; p: PWideChar; end);
      4: (CellBlob: packed record l: Integer; p: Pointer; end);
  end;

  TDISQLite3Database = class;

  TDISQLite3Statement = class
  private
    FAfterClose: TNotifyEvent;
    FAfterOpen: TNotifyEvent;
    FBeforeClose: TNotifyEvent;
    FBeforeOpen: TNotifyEvent;
    FHandle: TDISQLite3StatementHandle;
    FDatabase: TDISQLite3Database;

    FSQL8: AnsiString;
    function GetActive: Boolean;

    function GetSql16: WideString;

    procedure SetActive(const AValue: Boolean);

    procedure SetDatabase(const AValue: TDISQLite3Database);

    procedure SetSQL8(const AValue: AnsiString);

    procedure SetSql16(const AValue: WideString);

  protected

    function Check(const AErrorCode: Integer): Integer;

    procedure CheckActive;

    procedure CheckInactive;

    procedure DoAfterClose;

    procedure DoAfterOpen;

    procedure DoBeforeClose;

    procedure DoBeforeOpen;

  public

    destructor Destroy; override;

    procedure Assign(const ASource: TDISQLite3Statement);

    procedure bind_Blob(
      const AParamIdx: Integer;
      const ABlobData: Pointer;
      const ABlobSize: Integer;
      const ADestroy: TSQLite_Bind_Destructor);

    procedure bind_Blob_By_Name(
      const AParamName: AnsiString;
      const ABlobData: Pointer;
      const ABlobSize: Integer;
      const ADestroy: TSQLite_Bind_Destructor);

    procedure bind_Blob_By_Name16(
      const AParamName: WideString;
      const ABlobData: Pointer;
      const ABlobSize: Integer;
      const ADestroy: TSQLite_Bind_Destructor);

    procedure Bind_Double(
      const AParamIdx: Integer;
      const AValue: Double);

    procedure Bind_Int(
      const AParamIdx: Integer;
      const AValue: Integer);

    procedure Bind_Int_By_Name(
      const AParamName: AnsiString;
      const AValue: Integer);

    procedure Bind_Int_By_Name16(
      const AParamName: WideString;
      const AValue: Integer);

    procedure bind_Int64(
      const AParamIdx: Integer;
      const AValue: Int64);

    procedure bind_Null(
      const AParamIdx: Integer);

    function bind_Parameter_Count: Integer;

    function bind_Parameter_Index(
      const AParamName: AnsiString): Integer;

    function bind_Parameter_Index16(
      const AParamName: WideString): Integer;

    function bind_Parameter_Name(
      const AParamIdx: Integer): AnsiString;

    function bind_Parameter_Name16(
      const AParamIdx: Integer): WideString;

    procedure Bind_Str(
      const AParamIdx: Integer;
      const AValue: AnsiString);

    procedure bind_Str_By_Name(
      const AParamName: AnsiString;
      const AValue: AnsiString);

    procedure bind_Str16(
      const AParamIdx: Integer;
      const AValue: WideString);

    procedure Bind_Str16_By_Name(
      const AParamName: WideString;
      const AValue: WideString);

    procedure bind_Variant(
      const AParamIdx: Integer;
      const AValue: Variant);

    procedure Clear_Bindings;

    procedure Close;

    function Column_Blob(
      const AColumnIdx: Integer): Pointer;

    function Column_Bytes(
      const AColumnIdx: Integer): Integer;

    {$IFNDEF SQLITE_OMIT_UTF16}

    function Column_Bytes16(
      const AColumnIdx: Integer): Integer;
    {$ENDIF SQLITE_OMIT_UTF16}

    function Column_Cell8(
      const AColumnIdx: Integer): TDISQLite3Cell8;

    procedure Column_Cell16(
      const AColumnIdx: Integer;
      out ACell: TDISQLite3Cell16);

    function Column_Count: Integer;

    {$IFDEF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Database_Name(
      const AColumnIdx: Integer): AnsiString;
    {$ENDIF SQLITE_ENABLE_COLUMN_METADATA}

    {$IFDEF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Database_Name16(
      const AColumnIdx: Integer): WideString;
    {$ENDIF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_DeclType(
      const AColumnIdx: Integer): AnsiString;

    function Column_DeclType16(
      const AColumnIdx: Integer): WideString;

    function Column_Double(
      const AColumnIdx: Integer): Double;

    function Column_Int(
      const AColumnIdx: Integer): Integer;

    function Column_Int64(
      const AColumnIdx: Integer): Int64;

    function Column_Name(
      const AColumnIdx: Integer): AnsiString;

    function Column_Name16(
      const AColumnIdx: Integer): WideString;

    {$IFDEF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Origin_Name(
      const AColumnIdx: Integer): AnsiString;
    {$ENDIF SQLITE_ENABLE_COLUMN_METADATA}

    {$IFDEF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Origin_Name16(
      const AColumnIdx: Integer): WideString;
    {$ENDIF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Str(
      const AColumnIdx: Integer): WideString;

    function Column_Str16(
      const AColumnIdx: Integer): WideString;

    {$IFDEF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Table_Name(
      const AColumnIdx: Integer): AnsiString;
    {$ENDIF SQLITE_ENABLE_COLUMN_METADATA}

    {$IFDEF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Table_Name16(
      const AColumnIdx: Integer): WideString;
    {$ENDIF SQLITE_ENABLE_COLUMN_METADATA}

    function Column_Text(
      const AColumnIdx: Integer): PAnsiChar;

    {$IFNDEF SQLITE_OMIT_UTF16}

    function Column_Text16(
      const AColumnIdx: Integer): PWideChar;
    {$ENDIF !SQLITE_OMIT_UTF16}

    function Column_Type(
      const AColumnIdx: Integer): Integer;

    procedure Open;

    function Step: Integer;

    function TableName(const AQuoteChar: AnsiChar = SQLITE3_IDENTIFIER_QUOTE_CHAR): AnsiString;

    function TableName16(const AQuoteChar: WideChar = SQLITE3_IDENTIFIER_QUOTE_CHAR): WideString;

    function Reset: Integer;

    property Handle: TDISQLite3StatementHandle read FHandle;

  published

    property Active: Boolean read GetActive write SetActive default False;

    property Database: TDISQLite3Database read FDatabase write SetDatabase;

    property SQL8: AnsiString read FSQL8 write SetSQL8;

    property SQL16: WideString read GetSql16 write SetSql16 stored False;

    property AfterClose: TNotifyEvent read FAfterClose write FAfterClose;

    property AfterOpen: TNotifyEvent read FAfterOpen write FAfterOpen;

    property BeforeClose: TNotifyEvent read FBeforeClose write FBeforeClose;

    property BeforeOpen: TNotifyEvent read FBeforeOpen write FBeforeOpen;

  end;

  TDISQLite3StatementClass = class of TDISQLite3Statement;

  TDISQLite3Encoding = (
    encUnknown,
    encUtf8,
    encUtf16,
    encUtf16le,
    encUtf16be);

  TDISQLite3TransactionType = (
    ttDeferred,
    ttImmediate,
    ttExclusive);

  TDISQLite3Database = class(TComponent)
  private
    FAfterConnect: TNotifyEvent;
    FAfterCreateDatabase: TNotifyEvent;
    FAfterDisconnect: TNotifyEvent;
    FBeforeConnect: TNotifyEvent;
    FBeforeCreateDatabase: TNotifyEvent;
    FBeforeDisconnect: TNotifyEvent;
    FDatabaseName: WideString;
    FHandle: TDISQLite3DatabaseHandle;
    FInitDatabase: TNotifyEvent;
    FPassword: AnsiString;
    FStatements: TList;
    FStreamedConnected: Boolean;
    FTransactionCount: Integer;

    procedure CheckActive;
    procedure CheckDatabaseName(const ADatabaseName: WideString);

    procedure CheckInactive;

    function GetCacheSize: Integer;
    procedure SetCacheSize(const AValue: Integer);

    function GetConnected: Boolean;
    procedure SetConnected(const AValue: Boolean);

    function GetInTransaction: Boolean;

    function GetSQLiteVersion: WideString;
    procedure SetSQLiteVersion(const Value: WideString);

    function GetAutoVacuum: Boolean;
    procedure SetAutoVacuum(const AValue: Boolean);

    procedure SetCaseSensitiveLike(const AValue: Boolean);

    function GetCountChanges: Boolean;
    procedure SetCountChanges(const AValue: Boolean);

    function GetDefaultCacheSize: Integer;
    procedure SetDefaultCacheSize(const AValue: Integer);

    procedure SetDatabaseName(const AValue: WideString);

    function GetEmptyResultCallbacks: Boolean;
    procedure SetEmptyResultCallbacks(const Value: Boolean);

    function GetEncoding: WideString;
    procedure SetEncoding(const AValue: WideString);

    procedure SetPassword(const AValue: AnsiString);

    function IsMemoryDatabaseName(const ADatabaseName: WideString): Boolean;

  protected

    procedure CloseStatements;

    procedure DoAfterConnect; dynamic;

    procedure DoAfterCreateDatabase; dynamic;

    procedure DoAfterDisconnect; dynamic;

    procedure DoBeforeConnect; dynamic;

    procedure DoBeforeCreateDatabase;

    procedure DoBeforeDisconnect; dynamic;

    procedure DoConnect;

    procedure DoDisconnect;

    procedure DoInitDatabase; dynamic;

    function GetStatementClass: TDISQLite3StatementClass; virtual;

    procedure Loaded; override;

    procedure RegisterStatement(const AStatement: TDISQLite3Statement);

    procedure UnRegisterStatement(const AStatement: TDISQLite3Statement);

    property StreamedConnected: Boolean read FStreamedConnected write FStreamedConnected;

  public

    constructor Create(AOwner: TComponent); override;

    destructor Destroy; override;

    procedure AttachDatabase16(
      const ADatabaseName: WideString;
      AAlias: WideString = '');

    function Changes: Integer;

    function Check(const AErrorCode: Integer): Integer;

    procedure Close;

    procedure CreateDatabase; dynamic;

    procedure Commit;

    procedure Execute(const ASQL: AnsiString);

    procedure Execute16(const ASQL: WideString);

    function IntegrityCheck: Integer;

    procedure Interrupt;

    function LastInsertRowID: Int64;

    procedure Open;

    function Prepare(const ASQL8: AnsiString): TDISQLite3Statement;

    function Prepare16(const ASQL16: WideString): TDISQLite3Statement;

    procedure ReKey(const ANewPassword: AnsiString);

    procedure Rollback;

    procedure StartTransaction(const ATransactionType: TDISQLite3TransactionType = ttDeferred);

    function TotalChanges: Integer;

    procedure Vacuum;

    property AutoVacuum: Boolean read GetAutoVacuum write SetAutoVacuum;

    property CacheSize: Integer read GetCacheSize write SetCacheSize;

    property CaseSensitiveLike: Boolean write SetCaseSensitiveLike;

    property EmptyResultCallbacks: Boolean read GetEmptyResultCallbacks write SetEmptyResultCallbacks;

    property CountChanges: Boolean read GetCountChanges write SetCountChanges;

    property DefaultCacheSize: Integer read GetDefaultCacheSize write SetDefaultCacheSize;

    property Handle: TDISQLite3DatabaseHandle read FHandle;

    property InTransaction: Boolean read GetInTransaction;

    property Encoding: WideString read GetEncoding write SetEncoding;

  published

    property Connected: Boolean read GetConnected write SetConnected default False;

    property DatabaseName: WideString read FDatabaseName write SetDatabaseName;

    property Password: AnsiString read FPassword write SetPassword;

⌨️ 快捷键说明

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