delphimvcframework/samples/apachemodule/MainDataModuleUnit.pas

88 lines
2.5 KiB
ObjectPascal
Raw Normal View History

2014-05-22 12:48:17 +02:00
unit MainDataModuleUnit;
interface
2017-05-09 21:49:34 +02:00
uses
System.SysUtils,
2014-05-22 12:48:17 +02:00
System.Classes,
Data.DB,
2017-05-09 21:49:34 +02:00
MVCFramework.TypesAliases,
2015-04-01 17:01:23 +02:00
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf,
FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Comp.Client,
2017-05-09 21:49:34 +02:00
FireDAC.Stan.Param, WinesBO,
2015-04-01 17:01:23 +02:00
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Phys.IBBase,
FireDAC.Phys.FB, FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait;
2014-05-22 12:48:17 +02:00
type
TWineCellarDataModule = class(TDataModule)
Connection: TFDConnection;
qryWines: TFDQuery;
updWines: TFDUpdateSQL;
FDPhysFBDriverLink1: TFDPhysFBDriverLink;
procedure ConnectionBeforeConnect(Sender: TObject);
public
2017-05-09 21:49:34 +02:00
function GetWineById(id: Integer): TDataSet;
function FindWines(Search: string): TDataSet;
procedure AddWine(AWine: TWine);
procedure UpdateWine(Wine: TWine);
procedure DeleteWine(id: Integer);
2014-05-22 12:48:17 +02:00
end;
implementation
2017-05-09 21:49:34 +02:00
uses
MVCFramework.Commons,
MVCFramework.DataSet.Utils,
MVCFramework.SystemJSONUtils,
System.StrUtils,
MVCFramework.FireDAC.Utils;
2014-05-22 12:48:17 +02:00
{$R *.dfm}
{ TCellarSM }
2017-05-09 21:49:34 +02:00
procedure TWineCellarDataModule.AddWine(AWine: TWine);
2014-05-22 12:48:17 +02:00
begin
2017-05-09 21:49:34 +02:00
TFireDACUtils.ObjectToParameters(updWines.Commands[arInsert].Params, AWine, 'NEW_');
updWines.Commands[arInsert].Execute;
2014-05-22 12:48:17 +02:00
end;
procedure TWineCellarDataModule.DeleteWine(id: Integer);
2014-05-22 12:48:17 +02:00
begin
updWines.Commands[arDelete].ParamByName('OLD_ID').AsInteger := id;
updWines.Commands[arDelete].Execute;
end;
procedure TWineCellarDataModule.ConnectionBeforeConnect(Sender: TObject);
begin
Connection.Params.Values['Database'] :=
{ change this path to be compliant with your system }
'D:\DEV\dmvcframework\samples\winecellarserver\WINES.FDB';
2014-05-22 12:48:17 +02:00
end;
2017-05-09 21:49:34 +02:00
function TWineCellarDataModule.FindWines(Search: string): TDataSet;
2014-05-22 12:48:17 +02:00
begin
if Search.IsEmpty then
qryWines.Open('SELECT * FROM wine')
else
qryWines.Open('SELECT * FROM wine where NAME CONTAINING ?', [Search]);
2017-05-09 21:49:34 +02:00
Result := qryWines;
2014-05-22 12:48:17 +02:00
end;
2017-05-09 21:49:34 +02:00
function TWineCellarDataModule.GetWineById(id: Integer): TDataSet;
2014-05-22 12:48:17 +02:00
begin
qryWines.Open('SELECT * FROM wine where id = ?', [id]);
2017-05-09 21:49:34 +02:00
Result := qryWines;
2014-05-22 12:48:17 +02:00
end;
2017-05-09 21:49:34 +02:00
procedure TWineCellarDataModule.UpdateWine(Wine: TWine);
2014-05-22 12:48:17 +02:00
begin
2017-05-09 21:49:34 +02:00
TFireDACUtils.ObjectToParameters(updWines.Commands[arUpdate].Params, Wine, 'NEW_');
updWines.Commands[arUpdate].Params.ParamByName('OLD_ID').AsInteger := Wine.id;
updWines.Commands[arUpdate].Execute;
2014-05-22 12:48:17 +02:00
end;
end.