unit srvProvinciasPoblaciones_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

{$I Remobjects.inc}

interface

uses
  {vcl:} Classes, SysUtils, 
  {RemObjects:} uROXMLIntf, uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Required:} uRORemoteDataModule,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:} FactuGES_Intf, uDADataStreamer, uDABin2DataStreamer, uDAClasses;

type
  { TsrvProvinciasPoblaciones }
  TsrvProvinciasPoblaciones = class(TRORemoteDataModule, IsrvProvinciasPoblaciones)
    Bin2DataStreamer: TDABin2DataStreamer;
    schProvinciasPoblaciones: TDASchema;
  private
  protected
    { IsrvProvinciasPoblaciones methods }
    function DarListaProvincias: Binary;
    function DarListaPoblaciones(const ID_Provincia: Integer): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, Variants, uDAInterfaces,
  uDataModuleServer, uROClasses;


procedure Create_srvProvinciasPoblaciones(out anInstance : IUnknown);
begin
  anInstance := TsrvProvinciasPoblaciones.Create(nil);
end;

{ srvProvinciasPoblaciones }
function TsrvProvinciasPoblaciones.DarListaProvincias: Binary;
var
  ASchema : TDASchema;
  AConn : IDAConnection;
  dsData: IDADataset;
  ALista : TStringList;
begin
  Result := Binary.Create;

  ASchema := schProvinciasPoblaciones;
  AConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);

  try
    dsData := ASchema.NewDataset(AConn, 'Provincias');
  except
    RaiseError('No existe la tabla PROVINCIAS');
  end;

  ALista := TStringList.Create;
  try
    dsData.Active := True;
    ALista.Sorted := True;
    while not dsData.EOF do
    begin
      ALista.Add(Format('%s=%d', [dsData.Fields[1].AsString, dsData.Fields[0].AsInteger]));
      dsData.Next;
    end;

    ALista.SaveToStream(Result);
  finally
    FreeANDNIL(ALista);
    dsData := NIL;
    AConn := NIL;
  end;
end;

function TsrvProvinciasPoblaciones.DarListaPoblaciones(const ID_Provincia: Integer): Binary;
var
  ASchema : TDASchema;
  AConn : IDAConnection;
  dsData: IDADataset;
//  AWhere : TDAWhereExpression;
  ALista : TStringList;
begin
  Result := Binary.Create;

  ASchema := schProvinciasPoblaciones;
  AConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);

  try
    dsData := ASchema.NewDataset(AConn, 'Poblaciones', ['ID', 'DESCRIPCION'], '', '', False, True);

    with dsData.DynamicWhere do
    begin
      Clear;
      Expression := NewBinaryExpression(NewField('', 'ID_PROVINCIA'),
                      NewConstant(ID_Provincia, datInteger),
                      dboEqual);
    end;

    ALista := TStringList.Create;
    try
      dsData.Active := True;
      ALista.Sorted := True;
      while not dsData.EOF do
      begin
        ALista.Add(Format('%s=%d', [dsData.Fields[1].AsString, dsData.Fields[0].AsInteger]));
        dsData.Next;
      end;
      ALista.SaveToStream(Result);
    finally
      FreeANDNIL(ALista);
      dsData := NIL;
      AConn := NIL;
    end;
  except
    RaiseError('No existe la tabla POBLACIONES');
  end;
end;

initialization
  TROClassFactory.Create('srvProvinciasPoblaciones', Create_srvProvinciasPoblaciones, TsrvProvinciasPoblaciones_Invoker);

finalization

end.
