unit uCustomerServerModule;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, DataSnapServer_TLB, StdVcl, FIBDatabase, pFIBDatabase, Forms,
  Provider, pFIBClientDataSet, DB, FIBDataSet, pFIBDataSet, TConnect,
  FIBQuery, pFIBQuery, Dialogs;

type
  TCustomerServerModule = class(TRemoteDataModule, ICustomerServerModule)
    db: TpFIBDatabase;
    trread: TpFIBTransaction;
    trwrite: TpFIBTransaction;
    dtCusts: TpFIBDataSet;
    dtSales: TpFIBDataSet;
    pvCusts: TpFIBDataSetProvider;
    pvSales: TpFIBDataSetProvider;
    localConnection: TLocalConnection;
    usql: TpFIBQuery;
    procedure RemoteDataModuleCreate(Sender: TObject);
    procedure RemoteDataModuleDestroy(Sender: TObject);
    procedure pvCustsGetTableName(Sender: TObject; DataSet: TDataSet;
      var TableName: String);
    procedure pvCustsBeforeUpdateRecord(Sender: TObject;
      SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
      UpdateKind: TUpdateKind; var Applied: Boolean);
  private
    { Private declarations }
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
  public

  end;

implementation

uses Unit1;

{$R *.DFM}

class procedure TCustomerServerModule.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

procedure TCustomerServerModule.RemoteDataModuleCreate(Sender: TObject);
begin
//  if not db.Connected then
  db.DBName := 'localhost:' + ExtractFileDir(Application.ExeName) + '\db\employee.fdb';
  db.Connected := True;

  PostMessage(Form1.Handle, UM_CONNECT, 1, 0);
end;

procedure TCustomerServerModule.RemoteDataModuleDestroy(Sender: TObject);
begin
  PostMessage(Form1.Handle, UM_CONNECT, -1, 0);

//  db.CloseDataSets;
  db.Close;
end;

procedure TCustomerServerModule.pvCustsGetTableName(Sender: TObject;
  DataSet: TDataSet; var TableName: String);
begin
  TableName := 'CUSTOMER';
end;

procedure TCustomerServerModule.pvCustsBeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
  UpdateKind: TUpdateKind; var Applied: Boolean);

  procedure ExecUpdateQuery(SQLText: String);
  var i: Integer;
  begin
    usql.SQL.Text := SQLText;
    usql.Prepare;
    for i := 0 to usql.Params.Count - 1 do
      if usql.Params[i].Name = 'OLD_CUST_NO'
      then usql.Params[i].Value := DeltaDS.FieldByName('CUST_NO').NewValue
      else usql.Params[i].Value := DeltaDS.FindField(usql.Params[i].Name).Value;
    if UpdateKind = ukInsert then
      if usql.ParamByName('CUST_NO').IsNull then begin
        usql.ParamByName('CUST_NO').Value := db.Gen_Id(dtCusts.AutoUpdateOptions.GeneratorName, 1);
        DeltaDS.FieldByName('CUST_NO').NewValue := usql.ParamByName('CUST_NO').Value; 
      end;

    usql.ExecQuery;
  end;
begin
  case UpdateKind of
   ukInsert: ExecUpdateQuery(dtCusts.InsertSQL.Text);
   ukModify: ExecUpdateQuery(dtCusts.UpdateSQL.Text);
   ukDelete: ExecUpdateQuery(dtCusts.DeleteSQL.Text);
  end;
  Applied := True;
end;

initialization
  TComponentFactory.Create(ComServer, TCustomerServerModule,
    Class_CustomerServerModule, ciMultiInstance, tmApartment);
end.
