delphimvcframework/samples/winecellar/MainDataModuleUnit.pas

88 lines
2.5 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit MainDataModuleUnit;
interface
uses System.SysUtils,
System.Classes,
Data.DBXFirebird,
Data.DB,
2015-04-01 17:01:23 +02:00
Data.SqlExpr,
{$IF CompilerVersion <= 27}
Data.DBXJSON,
2014-05-22 01:06:35 +02:00
{$ELSE}
2015-04-01 17:01:23 +02:00
System.JSON,
2014-05-22 01:06:35 +02:00
{$ENDIF}
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,
FireDAC.Stan.Param,
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Phys.IBBase,
FireDAC.Phys.FB,
WinesBO, FireDAC.Phys.FBDef;
2013-10-29 16:51:16 +01:00
type
TWineCellarDataModule = class(TDataModule)
Connection: TFDConnection;
qryWines: TFDQuery;
updWines: TFDUpdateSQL;
FDPhysFBDriverLink1: TFDPhysFBDriverLink;
procedure ConnectionBeforeConnect(Sender: TObject);
2013-10-29 16:51:16 +01:00
public
function GetWineById(id: Integer): TJSONObject;
function FindWines(Search: string): TJSONArray;
function AddWine(AWine: TWine): TJSONObject;
procedure UpdateWine(AWine: TWine);
2013-10-29 16:51:16 +01:00
function DeleteWine(id: Integer): TJSONObject;
end;
implementation
{$R *.dfm}
uses System.StrUtils,
Data.DBXCommon,
ObjectsMappers;
2013-10-29 16:51:16 +01:00
{ TCellarSM }
function TWineCellarDataModule.AddWine(AWine: TWine): TJSONObject;
2013-10-29 16:51:16 +01:00
begin
Mapper.ObjectToFDParameters(updWines.Commands[arInsert].Params, AWine, 'NEW_');
updWines.Commands[arInsert].Execute;
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.DeleteWine(id: Integer): TJSONObject;
2013-10-29 16:51:16 +01:00
begin
updWines.Commands[arDelete].ParamByName('OLD_ID').AsInteger := id;
updWines.Commands[arDelete].Execute;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarDataModule.ConnectionBeforeConnect(Sender: TObject);
2013-10-29 16:51:16 +01:00
begin
2015-04-01 17:01:23 +02:00
Connection.Params.Values['Database'] := ExtractFilePath(ParamStr(0)) + '..\..\WINES.FDB';
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.FindWines(Search: string): TJSONArray;
begin
if Search.IsEmpty then
qryWines.Open('SELECT * FROM wine')
else
qryWines.Open('SELECT * FROM wine where NAME CONTAINING ?', [Search]);
Result := qryWines.AsJSONArray;
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.GetWineById(id: Integer): TJSONObject;
begin
qryWines.Open('SELECT * FROM wine where id = ?', [id]);
Result := qryWines.AsJSONObject;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarDataModule.UpdateWine(AWine: TWine);
2013-10-29 16:51:16 +01:00
begin
Mapper.ObjectToFDParameters(updWines.Commands[arUpdate].Params, AWine, 'NEW_');
updWines.Commands[arUpdate].Params.ParamByName('OLD_ID').AsInteger := AWine.id;
updWines.Commands[arUpdate].Execute;
2013-10-29 16:51:16 +01:00
end;
end.