Skip to content

Commit

Permalink
Improved activerecord_showcase sample
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Sep 13, 2019
1 parent 241a07e commit 6cf4c51
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 166 deletions.
8 changes: 3 additions & 5 deletions packages/d103/dmvcframeworkDT.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)</DCC_Namespace>
<BT_BuildType>Debug</BT_BuildType>
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Locale>1033</VerInfo_Locale>
<DCC_UsePackage>rtl;IndySystem;IndyProtocols;IndyCore;dbrtl;dmvcframeworkRT;$(DCC_UsePackage)</DCC_UsePackage>
<DCC_Description>DelphiMVCFramework IDE Expert</DCC_Description>
<DCC_UnitSearchPath>..\..\sources;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win64)'!=''">
<DCC_UsePackage>rtl;IndySystem;IndyProtocols;IndyCore;dbrtl;$(DCC_UsePackage)</DCC_UsePackage>
Expand Down Expand Up @@ -178,11 +177,10 @@
<Source Name="MainSource">dmvcframeworkDT.dpk</Source>
</Source>
<Excluded_Packages>
<Excluded_Packages Name="C:\Users\Public\Documents\Embarcadero\Studio\20.0\Bpl\LockBoxFMXDD250.bpl">TurboPack LockBox Delphi FMX designtime package</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\DataExplorerDBXPluginEnt260.bpl">DBExpress Enterprise Data Explorer Integration</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\bcboffice2k260.bpl">Embarcadero C++Builder Office 2000 Servers Package</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\bcbofficexp260.bpl">Embarcadero C++Builder Office XP Servers Package</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k260.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dclofficexp260.bpl">Microsoft Office XP Sample Automation Server Wrapper Components</Excluded_Packages>
<Excluded_Packages Name="C:\Program Files (x86)\FastReports\LibD26\dclfrxtee26.bpl">FastReport 6.0 Tee Components</Excluded_Packages>
</Excluded_Packages>
</Delphi.Personality>
<Deployment Version="3">
Expand Down
16 changes: 12 additions & 4 deletions packages/d103/dmvcframeworkRT.dpk
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ requires
FireDACIBDriver,
FireDACMySQLDriver,
loggerproRT,
FireDACPgDriver;
FireDACPgDriver,
FireDACCommonODBC,
FireDACMSSQLDriver;

contains
Web.ApacheConst in 'c:\program files (x86)\embarcadero\studio\20.0\source\Internet\Web.ApacheConst.pas',
Expand Down Expand Up @@ -90,12 +92,18 @@ contains
MVCFramework.Serializer.JsonDataObjects.CustomTypes in '..\..\sources\MVCFramework.Serializer.JsonDataObjects.CustomTypes.pas',
MVCFramework.Serializer.JsonDataObjects in '..\..\sources\MVCFramework.Serializer.JsonDataObjects.pas',
MVCFramework.Session in '..\..\sources\MVCFramework.Session.pas',
MVCFramework.SQLGenerators.Firebird in '..\..\sources\MVCFramework.SQLGenerators.Firebird.pas',
MVCFramework.SQLGenerators.MySQL in '..\..\sources\MVCFramework.SQLGenerators.MySQL.pas',
MVCFramework.SysControllers in '..\..\sources\MVCFramework.SysControllers.pas',
MVCFramework.SystemJSONUtils in '..\..\sources\MVCFramework.SystemJSONUtils.pas',
MVCFramework.View.Cache in '..\..\sources\MVCFramework.View.Cache.pas',
MVCFramework.Controllers.Register in '..\..\sources\MVCFramework.Controllers.Register.pas';
MVCFramework.Controllers.Register in '..\..\sources\MVCFramework.Controllers.Register.pas',
MVCFramework.SQLGenerators.Firebird in '..\..\sources\MVCFramework.SQLGenerators.Firebird.pas',
MVCFramework.SQLGenerators.Interbase in '..\..\sources\MVCFramework.SQLGenerators.Interbase.pas',
MVCFramework.SQLGenerators.MSSQL in '..\..\sources\MVCFramework.SQLGenerators.MSSQL.pas',
MVCFramework.SQLGenerators.MySQL in '..\..\sources\MVCFramework.SQLGenerators.MySQL.pas',
MVCFramework.SQLGenerators.PostgreSQL in '..\..\sources\MVCFramework.SQLGenerators.PostgreSQL.pas',
MVCFramework.SQLGenerators.Sqlite in '..\..\sources\MVCFramework.SQLGenerators.Sqlite.pas',
MVCFramework.RQL.AST2MSSQL in '..\..\sources\MVCFramework.RQL.AST2MSSQL.pas',
MVCFramework.RQL.AST2SQLite in '..\..\sources\MVCFramework.RQL.AST2SQLite.pas';

end.

Expand Down
12 changes: 10 additions & 2 deletions packages/d103/dmvcframeworkRT.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
<DCCReference Include="FireDACMySQLDriver.dcp"/>
<DCCReference Include="loggerproRT.dcp"/>
<DCCReference Include="FireDACPgDriver.dcp"/>
<DCCReference Include="FireDACCommonODBC.dcp"/>
<DCCReference Include="FireDACMSSQLDriver.dcp"/>
<DCCReference Include="c:\program files (x86)\embarcadero\studio\20.0\source\Internet\Web.ApacheConst.pas"/>
<DCCReference Include="c:\program files (x86)\embarcadero\studio\20.0\source\Internet\Web.Win.IsapiHTTP.pas"/>
<DCCReference Include="c:\program files (x86)\embarcadero\studio\20.0\source\Internet\Web.ApacheHTTP.pas"/>
Expand Down Expand Up @@ -167,12 +169,18 @@
<DCCReference Include="..\..\sources\MVCFramework.Serializer.JsonDataObjects.CustomTypes.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Serializer.JsonDataObjects.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Session.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.Firebird.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.MySQL.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SysControllers.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SystemJSONUtils.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.View.Cache.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Controllers.Register.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.Firebird.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.Interbase.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.MSSQL.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.MySQL.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.PostgreSQL.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.SQLGenerators.Sqlite.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.RQL.AST2MSSQL.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.RQL.AST2SQLite.pas"/>
<BuildConfiguration Include="Release">
<Key>Cfg_2</Key>
<CfgParent>Base</CfgParent>
Expand Down
4 changes: 3 additions & 1 deletion samples/activerecord_showcase/FDConnectionConfigU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ procedure CreatePostgresqlPrivateConnDef(AIsPooled: boolean);
procedure CreateSqlitePrivateConnDef(AIsPooled: boolean);
var
LParams: TStringList;
lFName: string;
begin
LParams := TStringList.Create;
try
LParams.Add('Database=C:\DEV\delphimvcframework\samples\data\activerecorddb.db');
lFName := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..\data\activerecorddb.db');
LParams.Add('Database=' + lFName);
if AIsPooled then
begin
LParams.Add('Pooled=True');
Expand Down
2 changes: 1 addition & 1 deletion samples/activerecord_showcase/MainFormU.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ object MainForm: TMainForm
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
OnShow = FormShow
DesignSize = (
635
401)
Expand Down
152 changes: 71 additions & 81 deletions samples/activerecord_showcase/MainFormU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ interface
FireDAC.VCLUI.Wait,
Data.DB, FireDAC.Comp.Client;


type
TMainForm = class(TForm)
btnCRUD: TButton;
Expand All @@ -45,9 +44,9 @@ TMainForm = class(TForm)
procedure btnRQLClick(Sender: TObject);
procedure btnSelectClick(Sender: TObject);
procedure btnValidationClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnTransientFieldsClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure Log(const Value: string);
public
Expand All @@ -61,7 +60,6 @@ implementation

{$R *.dfm}


uses
MVCFramework.ActiveRecord,
EntitiesU,
Expand All @@ -70,15 +68,14 @@ implementation
MVCFramework.DataSet.Utils,
MVCFramework.RQL.Parser,
System.Math,
FDConnectionConfigU;
FDConnectionConfigU, EngineChoiceFormU;

procedure TMainForm.btnCRUDClick(Sender: TObject);
var
lCustomer: TCustomer;
lID: Integer;
begin
ShowMessage('There are ' + TMVCActiveRecord.Count<TCustomer>().ToString +
' row/s for entity ' + TCustomer.ClassName);
ShowMessage('There are ' + TMVCActiveRecord.Count<TCustomer>().ToString + ' row/s for entity ' + TCustomer.ClassName);

Log('Simple CRUD test');
lCustomer := TCustomer.Create;
Expand Down Expand Up @@ -138,8 +135,7 @@ procedure TMainForm.btnMultiThreadingClick(Sender: TObject);
lProc: TProc;
lConnParams: string;
const
Cities: array [0 .. 4] of string = ('Rome', 'New York', 'London', 'Melbourne',
'Berlin');
Cities: array [0 .. 4] of string = ('Rome', 'New York', 'London', 'Melbourne', 'Berlin');
CompanySuffix: array [0 .. 5] of string = ('Corp.', 'Inc.', 'Ltd.', 'Srl', 'SPA', 'doo');
Stuff: array [0 .. 4] of string = ('Burger', 'GAS', 'Motors', 'House', 'Boats');
begin
Expand All @@ -164,11 +160,8 @@ procedure TMainForm.btnMultiThreadingClick(Sender: TObject);
try
lCustomer.Code := Format('%5.5d', [TThread.CurrentThread.ThreadID, I]);
lCustomer.City := Cities[Random(high(Cities) + 1)];
lCustomer.CompanyName := Format('%s %s %s', [
lCustomer.City,
Stuff[Random(High(Stuff) + 1)],
CompanySuffix[Random(High(CompanySuffix) + 1)]
]);
lCustomer.CompanyName := Format('%s %s %s', [lCustomer.City, Stuff[Random(High(Stuff) + 1)],
CompanySuffix[Random(High(CompanySuffix) + 1)]]);
lCustomer.Insert;
finally
lCustomer.Free;
Expand All @@ -179,12 +172,10 @@ procedure TMainForm.btnMultiThreadingClick(Sender: TObject);
end;
end;

lTasks := [
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc)];
lTasks := [TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc)];
TTask.WaitForAll(lTasks);

ShowMessage('Just inserted ' + TMVCActiveRecord.Count(TCustomer,
Expand Down Expand Up @@ -225,8 +216,7 @@ procedure TMainForm.btnRelationsClick(Sender: TObject);
lOrderDetail.Price := Random(j * 10);
lOrderDetail.Discount := j;
lOrderDetail.Quantity := j * 2;
lOrderDetail.Description := 'MY PRODUCT ' + I.ToString + '/' +
j.ToString;
lOrderDetail.Description := 'MY PRODUCT ' + I.ToString + '/' + j.ToString;
lOrderDetail.Total := j * j * j;
lOrderDetail.Insert;
finally
Expand All @@ -243,15 +233,12 @@ procedure TMainForm.btnRelationsClick(Sender: TObject);
Log(lCustomer.CompanyName);
for lOrder in lCustomer.Orders do
begin
Log(Format(' %5.5d - %s - %m', [lOrder.ID, datetostr(lOrder.OrderDate),
lOrder.Total]));
lOrderRows := TMVCActiveRecord.Where<TOrderDetail>('id_order = ?',
[lOrder.ID]);
Log(Format(' %5.5d - %s - %m', [lOrder.ID, datetostr(lOrder.OrderDate), lOrder.Total]));
lOrderRows := TMVCActiveRecord.Where<TOrderDetail>('id_order = ?', [lOrder.ID]);
try
for lOrderRow in lOrderRows do
begin
Log(Format(' %-20s - %4d - %m', [lOrderRow.Description,
lOrderRow.Quantity, lOrder.Total]));
Log(Format(' %-20s - %4d - %m', [lOrderRow.Description, lOrderRow.Quantity, lOrder.Total]));
end;
Log('');
finally
Expand Down Expand Up @@ -280,8 +267,7 @@ procedure TMainForm.btnRQLClick(Sender: TObject);
for lItem in lList do
begin
lCustomer := TCustomer(lItem);
Log(Format('%5s - %s (%s)', [lCustomer.Code, lCustomer.CompanyName,
lCustomer.City]));
Log(Format('%5s - %s (%s)', [lCustomer.Code, lCustomer.CompanyName, lCustomer.City]));
end;
finally
lList.Free;
Expand All @@ -293,8 +279,7 @@ procedure TMainForm.btnRQLClick(Sender: TObject);
Log(lCustList.Count.ToString + ' record/s found');
for lCustomer in lCustList do
begin
Log(Format('%5s - %s (%s)', [lCustomer.Code, lCustomer.CompanyName,
lCustomer.City]));
Log(Format('%5s - %s (%s)', [lCustomer.Code, lCustomer.CompanyName, lCustomer.City]));
end;
finally
lCustList.Free;
Expand All @@ -307,8 +292,7 @@ procedure TMainForm.btnRQLClick(Sender: TObject);
for lItem in lList do
begin
lCustomer := TCustomer(lItem);
Log(Format('%5s - %s (%s)', [lCustomer.Code, lCustomer.CompanyName,
lCustomer.City]));
Log(Format('%5s - %s (%s)', [lCustomer.Code, lCustomer.CompanyName, lCustomer.City]));
end;
finally
lList.Free;
Expand All @@ -326,23 +310,16 @@ procedure TMainForm.btnSelectClick(Sender: TObject);
// Bypassing the RQL parser you can use DBMS-specific features or just joining your tables.
// This is just a sample, you can do the "select" also using the RQL engine
if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'firebird' then
lCustomers := TMVCActiveRecord.Select<TCustomer>
('SELECT * FROM customers WHERE description CONTAINING ?', ['google'])
else
if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'mysql' then
lCustomers := TMVCActiveRecord.Select<TCustomer>
('SELECT * FROM customers WHERE description LIKE ''%google%''', [])
else
if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'postgresql' then
lCustomers := TMVCActiveRecord.Select<TCustomer>
('SELECT * FROM customers WHERE description ILIKE ''%google%''', [])
else
if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'sqlite' then
lCustomers := TMVCActiveRecord.Select<TCustomer>
('SELECT * FROM customers WHERE description LIKE ''%google%''', [])
lCustomers := TMVCActiveRecord.Select<TCustomer>('SELECT * FROM customers WHERE description CONTAINING ?',
['google'])
else if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'mysql' then
lCustomers := TMVCActiveRecord.Select<TCustomer>('SELECT * FROM customers WHERE description LIKE ''%google%''', [])
else if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'postgresql' then
lCustomers := TMVCActiveRecord.Select<TCustomer>('SELECT * FROM customers WHERE description ILIKE ''%google%''', [])
else if ActiveRecordConnectionsRegistry.GetCurrentBackend = 'sqlite' then
lCustomers := TMVCActiveRecord.Select<TCustomer>('SELECT * FROM customers WHERE description LIKE ''%google%''', [])
else
raise Exception.Create('Unsupported backend: ' +
ActiveRecordConnectionsRegistry.GetCurrentBackend);
raise Exception.Create('Unsupported backend: ' + ActiveRecordConnectionsRegistry.GetCurrentBackend);

try
for lCustomer in lCustomers do
Expand All @@ -358,8 +335,7 @@ procedure TMainForm.btnSelectClick(Sender: TObject);
try
while not lDS.Eof do
begin
Log(Format('%8.5s - %s', [lDS.FieldByName('code').AsString,
lDS.FieldByName('description').AsString]));
Log(Format('%8.5s - %s', [lDS.FieldByName('code').AsString, lDS.FieldByName('description').AsString]));
lDS.Next;
end;
finally
Expand Down Expand Up @@ -426,47 +402,61 @@ procedure TMainForm.btnValidationClick(Sender: TObject);

lCustomer := TMVCActiveRecord.GetByPK<TCustomerWithLogic>(lID);
try
Log(lCustomer.CompanyName + ' => IsLocatedInRome: ' +
BoolToStr(lCustomer.IsLocatedInRome, True));
Log(lCustomer.CompanyName + ' => IsLocatedInRome: ' + BoolToStr(lCustomer.IsLocatedInRome, True));
lCustomer.Code := '';
lCustomer.Update; // raise exception
finally
lCustomer.Free;
end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
procedure TMainForm.FormDestroy(Sender: TObject);
begin
// To use Postgresql enable POSTGRESQL build configuration
{$IFDEF POSTGRESQL}
FDConnectionConfigU.CreatePostgresqlPrivateConnDef(True);
{$ENDIF}

// To use FirebirdSQL enable FIREBIRD build configuration
{$IFDEF FIREBIRD}
FDConnectionConfigU.CreateFirebirdPrivateConnDef(True);
{$ENDIF}

// To use MySQL enable MYSQL build configuration
{$IFDEF MYSQL}
FDConnectionConfigU.CreateMySQLPrivateConnDef(True);
{$ENDIF}

// To use SQLite enable SQLite build configuration
{$IFDEF SQLITE}
FDConnectionConfigU.CreateSqlitePrivateConnDef(True);
{$ENDIF}
{ ************* }
FDConnection1.Params.Clear;
FDConnection1.ConnectionDefName := FDConnectionConfigU.CON_DEF_NAME;
ActiveRecordConnectionsRegistry.AddConnection('default', FDConnection1);
Caption := Caption + ' (Curr Backend: ' + ActiveRecordConnectionsRegistry.
GetCurrentBackend + ')';
ActiveRecordConnectionsRegistry.RemoveConnection('default');
end;

procedure TMainForm.FormDestroy(Sender: TObject);
procedure TMainForm.FormShow(Sender: TObject);
begin
ActiveRecordConnectionsRegistry.RemoveConnection('default');
case TEngineChoiceForm.Execute of
TRDBMSEngine.PostgreSQL:
begin
FDConnectionConfigU.CreatePostgresqlPrivateConnDef(True);
end;
TRDBMSEngine.Firebird:
begin
FDConnectionConfigU.CreateFirebirdPrivateConnDef(True);
end;
TRDBMSEngine.Interbase:
begin
raise Exception.Create('This DEMO doesn''t support Interbase (while the framework does)');
end;
TRDBMSEngine.MySQL:
begin
FDConnectionConfigU.CreateMySQLPrivateConnDef(True);
end;
TRDBMSEngine.MariaDB:
begin
FDConnectionConfigU.CreateMySQLPrivateConnDef(True);
end;
TRDBMSEngine.SQLite:
begin
FDConnectionConfigU.CreateSqlitePrivateConnDef(True);
end;
TRDBMSEngine.MSSQLServer:
begin
// FDConnectionConfigU.CreatePostgresqlPrivateConnDef(True);
raise Exception.Create('This DEMO doesn''t support MSSQLServer (while the framework does)');
end;
else
raise Exception.Create('Unknown RDBMS');
end;

FDConnection1.Params.Clear;
FDConnection1.ConnectionDefName := FDConnectionConfigU.CON_DEF_NAME;
FDConnection1.Connected := True;

ActiveRecordConnectionsRegistry.AddConnection('default', FDConnection1);
Caption := Caption + ' (Curr Backend: ' + ActiveRecordConnectionsRegistry.GetCurrentBackend + ')';
end;

procedure TMainForm.Log(const Value: string);
Expand Down
Loading

0 comments on commit 6cf4c51

Please sign in to comment.