asgsqlite3.pas

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

PAS
1,276
字号
    FieldNumber: integer;
    FieldName: string;
    FieldType: string;
    FieldNN: integer;                   // 1 if notnull
    FieldDefault: string;
    FieldPK: integer;                   // 1 if primary key
  end;

  // object to 'play' with SQLite's default settings

  TASQLite3Pragma = class(TComponent)
  private
    FTempCacheSize: integer;
    FDefaultCacheSize: integer;
    FDefaultSynchronous: string;
    FDefaultTempStore: string;
    FTempStore: string;
    FSynchronous: string;
  protected
    function GetTempCacheSize: string;
    function GetDefaultCacheSize: string;
    function GetDefaultSynchronous: string;
    function GetDefaultTempStore: string;
    function GetTempStore: string;
    function GetSynchronous: string;
  published
    { Published declarations }
    property TempCacheSize: integer read FTempCacheSize write FTempCacheSize;
    property DefaultCacheSize: integer read FDefaultCacheSize write FDefaultCacheSize;
    property DefaultSynchronous: string read FDefaultSynchronous
      write FDefaultSynchronous;
    property DefaultTempStore: string read FDefaultTempStore write FDefaultTempStore;
    property TempStore: string read FTempStore write FTempStore;
    property Synchronous: string read FSynchronous write FSynchronous;
  end;

  // component to log messages
  // it's for debugging purpose and may be obsolete due
  // to the event implementation. not sure yet...

  TASQLite3Log = class(TComponent)
  private
    FLogFile: string;
    FLogDebugOut: boolean;
    FAppend: boolean;
    FLogSQL: boolean;
    FLogInt: boolean;
  protected
  public
    procedure Display(Msg: string);
  published
    { Published declarations }
    property LogFile: string read FLogFile write FLogFile;
    property LogDebugOut: boolean read FLogDebugOut write FLogDebugOut; // 20040225
    property Append: boolean read FAppend write FAppend;
    property LogSQL: boolean read FLogSQL write FLogSQL;
    property LogInternals: boolean read FLogInt write FLogInt;
  end;

// This component can be used to store sql outside the pascal source.
// It is useful for automatically creating tables on open of a temporary database
// (i.e. in-memory database)

  TASQLite3InlineSQL = class(TComponent)
  private
    FSQL: TStrings;
    procedure SetSQL(const Value: TStrings);
    function GetSQL: TStrings;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property SQL: TStrings read GetSQL write SetSQL;
  end;

  { Basic Database component }
  TASQLite3DB = class;
  TCompareFunc = function(db : TASQLite3DB; s1Len : integer; s1 : PAnsiChar; s2Len : integer; s2 : PAnsiChar) : integer; cdecl;

  TASQLite3DB = class(TComponent)
  private
    { Private declarations }
    FAfterConnect: TASQLite3NotifyEvent;
    FBeforeConnect: TASQLite3NotifyEvent;
    FAfterDisconnect: TASQLite3NotifyEvent;
    FBeforeDisconnect: TASQLite3NotifyEvent;
    function FGetDefaultExt: string;
    function FGetDriverDLL: string;
  protected
    { Protected declarations }
    FInlineSQL: TASQLite3InlineSQL;
    FExecuteInlineSQL: boolean;
    FDatabase: string;
    FTransactionType: string;
    FInTransaction : boolean;
    FSQLiteVersion: string;
    FDefaultExt: string;
    FDefaultDir: string;
    FDriverDll: string;
    FConnected: boolean;
    FMustExist: boolean;
    FVersion: string;
    FCharEnc: string;
    FUtf8: boolean;
    DBHandle: Pointer;
    FASQLitePragma: TASQLite3Pragma;
    FASQLiteLog: TASQLite3Log;
    FLastError: string;
    SQLite3_Open: function(dbname: PAnsiChar; var db: pointer): integer; cdecl;
    SQLite3_Close: function(db: pointer): integer; cdecl;
    SQLite3_Exec: function(DB: Pointer; SQLStatement: PAnsiChar; Callback: TSQLite3_Callback;
                           UserDate: Pointer; var ErrMsg: PAnsiChar): Integer; cdecl;
    SQLite3_LibVersion: function(): PAnsiChar; cdecl;
    SQLite3_ErrorString: function(db: pointer): PAnsiChar; cdecl;
    SQLite3_GetTable: function(db: Pointer; SQLStatement: PAnsiChar; var ResultPtr: Pointer;
      var RowCount: cardinal; var ColCount: cardinal; var ErrMsg: PAnsiChar): integer; cdecl;
    SQLite3_FreeTable: procedure(Table: PAnsiChar); cdecl;
    SQLite3_FreeMem: procedure(P: PAnsiChar); cdecl;
    SQLite3_Complete: function(P: PAnsiChar): boolean; cdecl;
    SQLite3_LastInsertRow: function(db: Pointer): integer; cdecl;
    SQLite3_Cancel: procedure(db: Pointer); cdecl;
    SQLite3_BusyHandler: procedure(db: Pointer; CallbackPtr: Pointer; Sender: TObject); cdecl;
    SQLite3_BusyTimeout: procedure(db: Pointer; TimeOut: integer); cdecl;
    SQLite3_Changes: function(db: Pointer): integer; cdecl;
    SQLite3_Prepare: function(db: Pointer; SQLStatement: PAnsiChar; nBytes: integer;
      var hstatement: pointer; var Tail: PAnsiChar): integer; cdecl;
    SQLite3_Finalize: function(hstatement: pointer): integer; cdecl;
    SQLite3_Reset: function(hstatement: pointer): integer; cdecl;
    SQLite3_Step: function(hstatement: pointer): integer; cdecl;
    SQLite3_Column_blob: function(hstatement: pointer; iCol: integer): pointer; cdecl;
    SQLite3_Column_bytes: function(hstatement: pointer; iCol: integer): integer; cdecl;
    SQLite3_Column_count: function(hstatement: pointer): integer; cdecl;
    SQLite3_Column_decltype: function(hstatement: pointer; iCol: integer): PAnsiChar; cdecl;
    SQLite3_Column_double: function(hstatement: pointer; iCol: integer): double; cdecl;
    SQLite3_Column_int: function(hstatement: pointer; iCol: integer): integer; cdecl;
    SQLite3_Column_int64: function(hstatement: pointer; iCol: integer): int64; cdecl;
    SQLite3_Column_name: function(hstatement: pointer; iCol: integer): PAnsiChar; cdecl;
    SQLite3_Column_text: function(hstatement: pointer; iCol: integer): PAnsiChar; cdecl;
    SQLite3_Column_text16: function(hstatement: pointer; iCol: integer): PWideChar; cdecl;
    SQLite3_Column_type: function(hstatement: pointer; iCol: integer): integer; cdecl;
    SQLite3_Bind_Blob: function(hstatement: pointer; iCol: integer; buf: PAnsiChar; n: integer; DestroyPtr: Pointer): integer; cdecl;
    SQLite3_Bind_Text16: function(hstatement: pointer; iCol: integer; buf: pointer; n: integer; DestroyPtr: Pointer): integer; cdecl;//\\\
    SQLite3_Bind_Parameter_Count: function(hstatement: pointer): integer; cdecl;//\\\
    SQLite3_create_collation: function(db: Pointer; zName : PChar; pref16 : integer; data : pointer; cmp : TCompareFunc): integer; cdecl;//\\\
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure DBConnect(Connected: boolean);
    function SQLite3_PrepareResult(DB: Pointer; TheStatement: string; FParams: TParams; Sender: TObject) : pointer;
    function SQLite3_GetNextResult(DB: Pointer; TheStatement: pointer; FParams: TParams; Sender: TObject) : pointer;
    procedure SQLite3_CloseResult(TheStatement : pointer);
  public
    DLLHandle: THandle;
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function LoadLibs: boolean;
    procedure FSetDatabase(Database: string);
    function RowsAffected: integer;
    function  TableExists(const ATableName: AnsiString): Boolean;
    procedure ExecStartTransaction(TransType: string);
    procedure StartTransaction;
    procedure StartDeferredTransaction;
    procedure StartImmediateTransaction;
    procedure StartExclusiveTransaction;
    procedure Open;
    procedure Close;
    procedure Commit;
    procedure RollBack;
    procedure ShowDatabases(List: TStrings);
    procedure GetTableNames(List: TStrings; SystemTables: boolean = false; TempTables: boolean = false);
    procedure GetTableInfo(TableName: string; List: TList);
    procedure GetIndexNames(List: TStrings; SystemTables: boolean = false);
    procedure GetIndexFieldNames(IndexName: string; List: TStrings);
    procedure GetFieldNames(TableName: string; List: TStrings);
    procedure GetPrimaryKeys(TableName: string; List: TStrings);
    procedure GetTableIndexNames(TableName: string; List: TStrings);
    procedure ExecPragma;
//    function SQLite_XExec(db: Pointer; SQLStatement: PAnsiChar;
//      CallbackPtr: Pointer; Sender: TObject; var ErrMsg: PAnsiChar): integer; cdecl;
    function SQLite3_Execute(db: Pointer; TheStatement: string; FParams: TParams; Sender: TObject): integer;
    function SQLite3_ExecSQL(TheStatement: string; Blobs: TList=nil): integer;
    procedure ShowError;
    function GetUserVersion(database : string=''): integer;
    procedure SetUserVersion(Version : integer; Database : string='');
    function GetSchemaVersion(database : string=''): integer;
    procedure SetSchemaVersion(Version : integer; Database : string='');
    function InTransaction : boolean;
  published
    { Published declarations }
    property CharacterEncoding: string read FCharEnc write FCharEnc;
    property TransactionType: string read FTransactionType write FTransactionType;
    property Database: string read FDatabase write FSetDatabase;
    property ASQLitePragma: TASQLite3Pragma read FASQLitePragma write FASQLitePragma;
    property ASQLiteLog: TASQLite3Log read FASQLiteLog write FASQLiteLog;
    property DefaultExt: string read FGetDefaultExt write FDefaultExt;
    property DefaultDir: string read FDefaultDir write FDefaultDir;
    property Version: string read FVersion write FVersion;
//  property CharacterEncoding: string Read FCharEncoding Write FCharEncoding;
    property DriverDLL: string read FGetDriverDLL write FDriverDLL;
    property Connected: boolean read FConnected write DBConnect;
    property MustExist: boolean read FMustExist write FMustExist;
    property ASQLiteInlineSQL: TASQLite3InlineSQL read FInlineSQL write FInlineSQL;
    property ExecuteInlineSQL: boolean read FExecuteInlineSQL write FExecuteInlineSQL;
    property AfterConnect: TASQLite3NotifyEvent read FAfterConnect write FAfterConnect;
    property BeforeConnect: TASQLite3NotifyEvent read FBeforeConnect write FBeforeConnect;
    property AfterDisconnect: TASQLite3NotifyEvent read FAfterDisconnect write FAfterDisconnect;
    property BeforeDisconnect: TASQLite3NotifyEvent read FBeforeDisconnect write FBeforeDisconnect;
  end;

  AsgError = class(Exception);

{ TRecInfo }

{   This structure is used to access additional information stored in
  each record buffer which follows the actual record data.

    Buffer: PAnsiChar;
   ||
   \/
    --------------------------------------------
    |  Record Data  | Bookmark | Bookmark Flag |
    --------------------------------------------
                    ^-- PRecInfo = Buffer + FRecInfoOfs

  Keep in mind that this is just an example of how the record buffer
  can be used to store additional information besides the actual record
  data.  There is no requirement that TDataSet implementations do it this
  way.

  For the purposes of this demo, the bookmark format used is just an integer
  value.  For an actual implementation the bookmark would most likely be
  a native bookmark type (as with BDE), or a fabricated bookmark for
  data providers which do not natively support bookmarks (this might be
  a variant array of key values for instance).

  The BookmarkFlag is used to determine if the record buffer contains a
  valid bookmark and has special values for when the dataset is positioned
  on the "cracks" at BOF and EOF. }

  PRecInfo = ^TRecInfo;

  TRecInfo = packed record
    Bookmark : integer;
    BookmarkFlag : TBookmarkFlag;
//    Nulls :
  end;

 //============================================================================== TFResult
 // The TFResult class is used to maintain the resultlist in memory. This
 // will only be the case for 'normal' data. Blobs and Clobs will be treated
 // differently, but they are not supported yet.
 //==============================================================================
  TASQLite3BaseQuery = class;

  TFResult = class
  protected
    Data: TList;

⌨️ 快捷键说明

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