Pull request: made a fix to Persistence for SQLite, that I want to share

Issue #266 closed
Markus Schadegg created an issue

SQLite 3.8.8.1 and a table with the following DDL:

CREATE TABLE arbeitstag(IDX integer not null primary key autoincrement, datum da
te, kommt1 time, geht1 time, kommt2 time, geht2 time, kommt3 time, geht3 time, k
ommt4 time, geht4 time);

Following value object:

  {$M+}
  [Entity]
  [Table('ARBEITSTAG')]
  TArbeitstag = class
  private

    [AutoGenerated]
    [Column('IDX', [cpRequired, cpPrimaryKey, cpNotNull, cpDontInsert], 0, 0, 0, 'Primary Key')]
    FIDX: Integer;
    FDatum: TDate;
    FKommt1: TTime;
    FGeht1: TTime;
    FKommt2: TTime;
    FGeht2: TTime;
    FKommt3: TTime;
    FGeht3: TTime;
    FKommt4: TTime;
    FGeht4: TTime;
    procedure SetDatum(AValue: TDate);
    procedure SetKommt1(AValue: TTime);
    procedure SetKommt2(AValue: TTime);
    procedure SetKommt3(AValue: TTime);
    procedure SetKommt4(AValue: TTime);
    procedure SetGeht1(AValue: TTime);
    procedure SetGeht2(AValue: TTime);
    procedure SetGeht3(AValue: TTime);
    procedure SetGeht4(AValue: TTime);

    function GetDatum: TDate;
    function GetKommt1: TTime;
    function GetKommt2: TTime;
    function GetKommt3: TTime;
    function GetKommt4: TTime;
    function GetGeht1: TTime;
    function GetGeht2: TTime;
    function GetGeht3: TTime;
    function GetGeht4: TTime;

  public
    constructor Create; virtual;
    destructor Destroy; override;

    property IDX: Integer read FIDX;

    [Column('DATUM')]
    property Datum: TDate read GetDatum write SetDatum;

    [Column('KOMMT1')]
    property Kommt1: TTime read GetKommt1 write SetKommt1;

    [Column('GEHT1')]
    property Geht1: TTime read GetGeht1 write SetGeht1;

    [Column('KOMMT2')]
    property Kommt2: TTime read GetKommt2 write SetKommt2;

    [Column('GEHT2')]
    property Geht2: TTime read GetGeht2 write SetGeht2;

    [Column('KOMMT3')]
    property Kommt3: TTime read GetKommt3 write SetKommt3;

    [Column('GEHT3')]
    property Geht3: TTime read GetGeht3 write SetGeht3;

    [Column('KOMMT4')]
    property Kommt4: TTime read GetKommt4 write SetKommt4;

    [Column('GEHT4')]
    property Geht4: TTime read GetGeht4 write SetGeht4;
  end;
  {$M-}

Code to save:

procedure Save(AArbeitstag: TArbeitstag):
var
FSession: TSession;
begin
  FTransaction := FSession.BeginTransaction;
  FSession.Save(AArbeitstag);
  FTransaction.Commit;
end;

Code to select:

function getByDate(ADate: TDate): TArbeitstag;
var
  aDatumCol: IProperty;
  aCriteria: ICriteria<TArbeitstag>;
  aArbeitsTagList: IList<TArbeitstag>;
begin
  aDatumCol := TProperty<TArbeitstag>.Create('DATUM');
  aCriteria := FSession.CreateCriteria<TArbeitstag>;

  // doesn't work
  //aArbeitsTagList := aCriteria
  //  .Add(aDatumCol.Eq(ADate))
  //  .ToList;

  // works:
  aArbeitsTagList := aCriteria
    .Add(aDatumCol.Eq(TValue.From<TDate>(ADate)))
    .ToList;

  if aArbeitsTagList.Count = 1 then begin
    Result := aArbeitstagList.Items[0];
  end
  else begin
    Result := nil;
  end;
end;

Current spring4d code (Repository downloaded today, 2017-09-20: https://bitbucket.org/sglienke/spring4d/get/4cf6393bf1ae.zip, when clicking on Downloads-> Download Repository)

Does not restore the persisted object (above save method) by selecting it by date (getByDate methode above).

It works after changing the attached two files SQLiteTable3.pas Spring.Persistence.Adapters.SQLite.pas

Comments (10)

  1. Markus Schadegg reporter

    sqlite3 .dump shows, that the data is stored the following way: INSERT INTO "arbeitstag" VALUES(14,'2017-09-20','11:02:00','12:10:00','12:40:00' ,'16:48:57','00:00:00','00:00:00','00:00:00','00:00:00');

    I use Delphi 10.2 Tokyo, Update 1

  2. Markus Schadegg reporter

    Is it really necessary to write

      aArbeitsTagList := aCriteria
        .Add(aDatumCol.Eq(TValue.From<TDate>(ADate)))
        .ToList;
    

    instead of

      aArbeitsTagList := aCriteria
        .Add(aDatumCol.Eq(ADate))
        .ToList;
    

    as ADate is already a TDate?

    The later does not find the dataset...

  3. Stefan Glienke repo owner

    Not if you put Spring into the uses which adds an implicit operator for TDate, TDateTime and TTime to TValue. Otherwise the implicit overload for Extended will be used.

    As the issue has been solved please continue any discussion on the forums.

  4. Log in to comment