delphimvcframework/samples/winecellarserver/MainDataModuleUnit.pas

95 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.DB,
2015-04-01 17:01:23 +02:00
Data.SqlExpr,
{$IF CompilerVersion <= 27}
2015-04-01 17:01:23 +02:00
Data.DBXJSON,
{$ELSE}
2015-04-01 17:01:23 +02:00
System.JSON,
{$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, FireDAC.VCLUI.Wait;
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): TDataSet;
function FindWines(Search: string): TDataSet;
procedure AddWine(AWine: TWine);
procedure UpdateWine(AWine: TWine);
procedure DeleteWine(id: Integer);
2013-10-29 16:51:16 +01:00
end;
implementation
{$R *.dfm}
uses
System.StrUtils,
2013-10-29 16:51:16 +01:00
Data.DBXCommon,
MVCFramework.FireDAC.Utils;
2013-10-29 16:51:16 +01:00
{ TCellarSM }
procedure TWineCellarDataModule.AddWine(AWine: TWine);
2013-10-29 16:51:16 +01:00
begin
TFireDACUtils.ObjectToParameters(updWines.Commands[arInsert].Params, AWine, 'NEW_');
updWines.Commands[arInsert].Execute;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarDataModule.DeleteWine(id: Integer);
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
// if you are using firebird 2.5, uses the file WINES_FB25.FDB
Connection.Params.Values['Database'] := ExtractFilePath(ParamStr(0)) + '..\..\WINES_FB30.FDB';
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.FindWines(Search: string): TDataSet;
2013-10-29 16:51:16 +01:00
begin
if Search.IsEmpty then
qryWines.Open('SELECT * FROM wine')
else
qryWines.Open('SELECT * FROM wine where NAME CONTAINING ?', [Search]);
Result := qryWines;
2013-10-29 16:51:16 +01:00
end;
function TWineCellarDataModule.GetWineById(id: Integer): TDataSet;
2013-10-29 16:51:16 +01:00
begin
qryWines.Open('SELECT * FROM wine where id = ?', [id]);
Result := qryWines;
2013-10-29 16:51:16 +01:00
end;
procedure TWineCellarDataModule.UpdateWine(AWine: TWine);
2013-10-29 16:51:16 +01:00
begin
TFireDACUtils.ObjectToParameters(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.