DBBitType (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use low-level features in the DBX platform, in order to help you manage bit values.

Code

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WideStrings, DBXOracle, FMTBcd, StdCtrls, DBClient, Provider, DB,
  SqlExpr, DBXMySql;

type
  TForm1 = class(TForm)
    SQLConnection1: TSQLConnection;
    SQLQuery1: TSQLQuery;
    DataSetProvider1: TDataSetProvider;
    ClientDataSet1: TClientDataSet;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses math, dbxplatform;

procedure TForm1.Button1Click(Sender: TObject);
var
  OrigValue: TBytes;
  Int64Representation: Int64;
begin
  ClientDataSet1.Active := True;
  SetLength(OrigValue, 8);
  OrigValue := ClientDataSet1.FieldByName('bitcol').AsBytes;
  Int64Representation := OrigValue[0]
       + (OrigValue[1]*Round(IntPower(256, 1)))
       + (OrigValue[2]*Round(IntPower(256, 2)))
       + (OrigValue[3]*Round(IntPower(256, 3)))
       + (OrigValue[4]*Round(IntPower(256, 4)))
       + (OrigValue[5]*Round(IntPower(256, 5)))
       + (OrigValue[6]*Round(IntPower(256, 6)))
       + (OrigValue[7]*Round(IntPower(256, 7)));
  Edit1.Text := IntToStr(Int64Representation);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  NewValue: TBytes;
begin
  if Edit1.Text <> '' then
  begin
    ClientDataSet1.Edit;
    SetLength(NewValue, 8);
    TDBXPlatform.CopyInt64(StrToInt64(Edit1.Text), NewValue, 0);
    ClientDataSet1.FieldByName('bitcol').AsBytes := NewValue;
    ClientDataSet1.ApplyUpdates(0);
  end;
end;

Uses