Skip to content

Commit

Permalink
New feature: ObjectVersioning for TMVCActiveRecord - see foVersion
Browse files Browse the repository at this point in the history
- TMVCActiveRecord.TableName is now readonly
  • Loading branch information
danieleteti committed Nov 2, 2023
1 parent 7c7443d commit a2f190d
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 198 deletions.
42 changes: 42 additions & 0 deletions samples/activerecord_showcase/EntitiesU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,35 @@ TIntegersAsBooleans = class(TMVCActiveRecord)
end;


[MVCNameCase(ncLowerCase)]
[MVCTable('customers_with_version')]
TCustomerWithVersion = class(TCustomEntity)
private
[MVCTableField('id', [foPrimaryKey, foAutoGenerated])]
fID: NullableInt64;
[MVCTableField('code')]
fCode: NullableString;
[MVCTableField('description')]
fCompanyName: NullableString;
[MVCTableField('city')]
fCity: string;
[MVCTableField('rating')]
fRating: NullableInt32;
[MVCTableField('note')]
fNote: string;
[MVCTableField('objversion', [foVersion])]
fObjVersion: Integer;
public
function ToString: String; override;
property ID: NullableInt64 read fID write fID;
property Code: NullableString read fCode write fCode;
property CompanyName: NullableString read fCompanyName write fCompanyName;
property City: string read fCity write fCity;
property Rating: NullableInt32 read fRating write fRating;
property Note: string read fNote write fNote;
end;



implementation

Expand Down Expand Up @@ -896,4 +925,17 @@ destructor TDefaultValuesTest.Destroy;
inherited;
end;

{ TCustomerWithVersion }

function TCustomerWithVersion.ToString: String;
begin
Result := '';
if PKIsNull then
Result := '<null>'
else
Result := fID.ValueOrDefault.ToString;
Result := Format('[ID: %6s][CODE: %6s][CompanyName: %18s][City: %16s][Rating: %3d][Note: %s][Version: %d]',[
Result, fCode.ValueOrDefault, fCompanyName.ValueOrDefault, fCity, fRating.ValueOrDefault, fNote, fObjVersion]);
end;

end.
25 changes: 17 additions & 8 deletions samples/activerecord_showcase/MainFormU.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ object MainForm: TMainForm
Left = 0
Top = 0
Caption = 'TMVCActiveRecord - ShowCase'
ClientHeight = 593
ClientWidth = 1104
ClientHeight = 626
ClientWidth = 1094
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Expand All @@ -13,8 +13,8 @@ object MainForm: TMainForm
OnDestroy = FormDestroy
OnShow = FormShow
DesignSize = (
1104
593)
1094
626)
TextHeight = 13
object btnCRUD: TButton
Left = 8
Expand All @@ -37,8 +37,8 @@ object MainForm: TMainForm
object Memo1: TMemo
Left = 280
Top = 8
Width = 816
Height = 577
Width = 806
Height = 610
Anchors = [akLeft, akTop, akRight, akBottom]
Ctl3D = True
DoubleBuffered = True
Expand All @@ -55,8 +55,8 @@ object MainForm: TMainForm
TabOrder = 2
WantReturns = False
WordWrap = False
ExplicitWidth = 812
ExplicitHeight = 576
ExplicitWidth = 802
ExplicitHeight = 609
end
object btnRelations: TButton
Left = 8
Expand Down Expand Up @@ -294,6 +294,15 @@ object MainForm: TMainForm
TabOrder = 28
OnClick = btnIntegersAsBoolClick
end
object btnObjectVersion: TButton
Left = 8
Top = 562
Width = 121
Height = 34
Caption = 'Object Version'
TabOrder = 29
OnClick = btnObjectVersionClick
end
object FDConnection1: TFDConnection
Left = 312
Top = 40
Expand Down
29 changes: 29 additions & 0 deletions samples/activerecord_showcase/MainFormU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ TMainForm = class(TForm)
btnNamedQuery: TButton;
btnVirtualEntities: TButton;
btnIntegersAsBool: TButton;
btnObjectVersion: TButton;
procedure btnCRUDClick(Sender: TObject);
procedure btnInheritanceClick(Sender: TObject);
procedure btnMultiThreadingClick(Sender: TObject);
Expand Down Expand Up @@ -92,6 +93,7 @@ TMainForm = class(TForm)
procedure btnNamedQueryClick(Sender: TObject);
procedure btnVirtualEntitiesClick(Sender: TObject);
procedure btnIntegersAsBoolClick(Sender: TObject);
procedure btnObjectVersionClick(Sender: TObject);
private
procedure Log(const Value: string);
procedure LoadCustomers(const HowManyCustomers: Integer = 50);
Expand Down Expand Up @@ -1951,6 +1953,33 @@ procedure TMainForm.btnWithSpacesClick(Sender: TObject);
end;
end;

procedure TMainForm.btnObjectVersionClick(Sender: TObject);
begin
var lID: NullableInt64;
var lCust := TCustomerWithVersion.Create();
try
Log('Entity ' + TCustomerWithVersion.ClassName + ' is mapped to table ' + lCust.TableName);
lCust.CompanyName := 'Google Inc.';
lCust.City := 'Montain View, CA';
lCust.Note := 'Μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος οὐλομένην 😁';
lCust.Insert;
lID := lCust.ID;
Log('Just inserted CustomerWithVersion ' + lID.ValueOrDefault.ToString);
lCust.Store;
finally
lCust.Free;
end;


lCust := TMVCActiveRecord.GetByPK<TCustomerWithVersion>(lID);
try
lCust.CompanyName := 'Alphabet Inc.';
lCust.Store;
finally
lCust.Free;
end;
end;

procedure TMainForm.btnOOPClick(Sender: TObject);
begin
Log('** OOP with ActiveRecord (person, employee, manager)');
Expand Down
Binary file modified samples/data/ACTIVERECORDDB.FDB
Binary file not shown.
Binary file modified samples/data/activerecorddb.db
Binary file not shown.
11 changes: 11 additions & 0 deletions samples/data/activerecorddb_firebird_script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,16 @@ create table integers_as_booleans (
done_int smallint not null
);

CREATE TABLE customers_with_version (
id bigint generated by default as identity NOT NULL,
code varchar(20),
description varchar(200),
city varchar(200),
note varchar(1000),
rating integer,
objversion integer
);


ALTER TABLE orders ADD CONSTRAINT orders_customers_fk FOREIGN KEY (id_customer) REFERENCES customers(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE order_details ADD CONSTRAINT order_details_orders_fk FOREIGN KEY (id_order) REFERENCES orders(id) ON DELETE CASCADE ON UPDATE CASCADE;
16 changes: 16 additions & 0 deletions samples/data/activerecorddb_interbase_script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ CREATE TABLE "customers with spaces" (
CONSTRAINT cust_with_space_pk PRIMARY KEY ("id with spaces")
);

create table integers_as_booleans (
id bigint generated by default as identity primary key,
done_bool boolean not null,
done_int smallint not null
);

CREATE TABLE customers_with_version (
id bigint generated by default as identity NOT NULL,
code varchar(20),
description varchar(200),
city varchar(200),
note varchar(1000),
rating integer,
objversion integer
);


ALTER TABLE orders ADD CONSTRAINT orders_customers_fk FOREIGN KEY (id_customer) REFERENCES customers(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE order_details ADD CONSTRAINT order_details_orders_fk FOREIGN KEY (id_order) REFERENCES orders(id) ON DELETE CASCADE ON UPDATE CASCADE;
Expand Down
10 changes: 10 additions & 0 deletions samples/data/activerecorddb_mysql_script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,15 @@ create table integers_as_booleans (
done_int smallint not null
);

CREATE TABLE customers_with_version (
id bigint not null auto_increment primary key,
code varchar(20),
description varchar(200),
city varchar(200),
note varchar(1000),
rating integer,
objversion integer
);

ALTER TABLE orders ADD CONSTRAINT orders_customers_fk FOREIGN KEY (id_customer) REFERENCES customers(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE order_details ADD CONSTRAINT order_details_orders_fk FOREIGN KEY (id_order) REFERENCES orders(id) ON DELETE CASCADE ON UPDATE CASCADE;
13 changes: 13 additions & 0 deletions samples/data/activerecorddb_postgresql_script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ CREATE TABLE public.customers (

ALTER TABLE public.customers OWNER TO postgres;


CREATE TABLE public.customers_with_version (
id bigint generated by default as identity NOT NULL,
code character varying(20),
description character varying(200),
city character varying(200),
note text,
rating integer,
objversion integer
);

ALTER TABLE public.customers_with_version OWNER TO postgres;

--
-- TOC entry 205 (class 1259 OID 58864)
-- Name: customers with spaces; Type: TABLE; Schema: public; Owner: postgres
Expand Down
Loading

0 comments on commit a2f190d

Please sign in to comment.