2013-10-29 16:51:16 +01:00
|
|
|
unit MainDataModuleUnit;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
uses
|
|
|
|
System.SysUtils,
|
2013-10-29 16:51:16 +01:00
|
|
|
System.Classes,
|
|
|
|
Data.DB,
|
2015-04-01 17:01:23 +02:00
|
|
|
Data.SqlExpr,
|
2017-04-29 23:56:56 +02:00
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
{$IF CompilerVersion <= 27}
|
2015-04-01 17:01:23 +02:00
|
|
|
Data.DBXJSON,
|
2017-04-29 23:56:56 +02:00
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
{$ELSE}
|
2015-04-01 17:01:23 +02:00
|
|
|
System.JSON,
|
2017-04-29 23:56:56 +02:00
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
{$ENDIF}
|
|
|
|
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,
|
2015-04-01 17:01:23 +02:00
|
|
|
FireDAC.Stan.Param,
|
2018-08-05 20:31:33 +02:00
|
|
|
FireDAC.DatS,
|
|
|
|
FireDAC.DApt.Intf,
|
|
|
|
FireDAC.DApt,
|
|
|
|
FireDAC.Comp.DataSet,
|
|
|
|
FireDAC.Phys.IBBase,
|
2015-04-01 17:01:23 +02:00
|
|
|
FireDAC.Phys.FB,
|
2018-08-05 20:31:33 +02:00
|
|
|
WinesBO,
|
|
|
|
FireDAC.Phys.FBDef,
|
|
|
|
FireDAC.VCLUI.Wait;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
TWineCellarDataModule = class(TDataModule)
|
2014-04-01 02:12:34 +02:00
|
|
|
Connection: TFDConnection;
|
|
|
|
qryWines: TFDQuery;
|
|
|
|
updWines: TFDUpdateSQL;
|
|
|
|
FDPhysFBDriverLink1: TFDPhysFBDriverLink;
|
|
|
|
procedure ConnectionBeforeConnect(Sender: TObject);
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
public
|
2017-04-29 23:56:56 +02:00
|
|
|
function GetWineById(id: Integer): TDataSet;
|
|
|
|
function FindWines(Search: string): TDataSet;
|
2018-08-05 20:31:33 +02:00
|
|
|
function GetAllWines: TDataSet;
|
2016-11-18 00:17:18 +01:00
|
|
|
procedure AddWine(AWine: TWine);
|
2014-05-22 23:37:13 +02:00
|
|
|
procedure UpdateWine(AWine: TWine);
|
2016-11-18 00:17:18 +01:00
|
|
|
procedure DeleteWine(id: Integer);
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
2017-04-29 23:56:56 +02:00
|
|
|
uses
|
|
|
|
System.StrUtils,
|
2013-10-29 16:51:16 +01:00
|
|
|
Data.DBXCommon,
|
2017-04-29 23:56:56 +02:00
|
|
|
MVCFramework.FireDAC.Utils;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
{ TCellarSM }
|
|
|
|
|
2016-11-18 00:17:18 +01:00
|
|
|
procedure TWineCellarDataModule.AddWine(AWine: TWine);
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2017-04-29 23:56:56 +02:00
|
|
|
TFireDACUtils.ObjectToParameters(updWines.Commands[arInsert].Params, AWine, 'NEW_');
|
2014-05-22 23:37:13 +02:00
|
|
|
updWines.Commands[arInsert].Execute;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2016-11-18 00:17:18 +01:00
|
|
|
procedure TWineCellarDataModule.DeleteWine(id: Integer);
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-04-01 02:12:34 +02:00
|
|
|
updWines.Commands[arDelete].ParamByName('OLD_ID').AsInteger := id;
|
|
|
|
updWines.Commands[arDelete].Execute;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2014-04-01 02:12:34 +02:00
|
|
|
procedure TWineCellarDataModule.ConnectionBeforeConnect(Sender: TObject);
|
2021-02-17 00:04:58 +01:00
|
|
|
var
|
|
|
|
lDBPath: string;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2017-04-29 23:56:56 +02:00
|
|
|
// if you are using firebird 2.5, uses the file WINES_FB25.FDB
|
2018-08-05 20:31:33 +02:00
|
|
|
if not IsLibrary then
|
|
|
|
begin
|
|
|
|
// Is compiled as EXE
|
|
|
|
Connection.Params.Values['Database'] := ExtractFilePath(ParamStr(0)) + '..\..\WINES_FB30.FDB';
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2021-02-17 00:04:58 +01:00
|
|
|
// compiled as apache module or isapi
|
|
|
|
lDBPath := ExtractFilePath(GetModuleName(HInstance)) + '..\..\winecellarserver\WINES_FB30.FDB';
|
|
|
|
if lDBPath.StartsWith('\\?\') then
|
|
|
|
lDBPath := lDBPath.Remove(0, 4);
|
|
|
|
Connection.Params.Values['Database'] := lDBPath;
|
2018-08-05 20:31:33 +02:00
|
|
|
end;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2017-04-29 23:56:56 +02:00
|
|
|
function TWineCellarDataModule.FindWines(Search: string): TDataSet;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-04-01 02:12:34 +02:00
|
|
|
if Search.IsEmpty then
|
|
|
|
qryWines.Open('SELECT * FROM wine')
|
|
|
|
else
|
|
|
|
qryWines.Open('SELECT * FROM wine where NAME CONTAINING ?', [Search]);
|
2017-04-29 23:56:56 +02:00
|
|
|
Result := qryWines;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
function TWineCellarDataModule.GetAllWines: TDataSet;
|
|
|
|
begin
|
|
|
|
Result := FindWines('');
|
|
|
|
end;
|
|
|
|
|
2017-04-29 23:56:56 +02:00
|
|
|
function TWineCellarDataModule.GetWineById(id: Integer): TDataSet;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2014-04-01 02:12:34 +02:00
|
|
|
qryWines.Open('SELECT * FROM wine where id = ?', [id]);
|
2017-04-29 23:56:56 +02:00
|
|
|
Result := qryWines;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2014-05-22 23:37:13 +02:00
|
|
|
procedure TWineCellarDataModule.UpdateWine(AWine: TWine);
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2017-04-29 23:56:56 +02:00
|
|
|
TFireDACUtils.ObjectToParameters(updWines.Commands[arUpdate].Params, AWine, 'NEW_');
|
2014-05-22 23:37:13 +02:00
|
|
|
updWines.Commands[arUpdate].Params.ParamByName('OLD_ID').AsInteger := AWine.id;
|
|
|
|
updWines.Commands[arUpdate].Execute;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|