Delphi class with optional full-type field

Asked

Viewed 332 times

3

The question is: how should I build my TPESSOA class so that the _id_city field can be null.

Database

Create Table PESSOA (
ID  INTEGER NOT NULL, 
NOME VARCHAR(100) NOT NULL,
ID_CIDADE INTEGER)

No Dephi

  TPESSOA = Class(TRemotable)
  private
        _id : Integer;
        _Nome: String;
        _id_cidade : integer;
    procedure Set_id_cidade(const Value: Integer);
    function Get_id_cidade: integer;
  published
        property id : Integer read _id write _id;
        property Nome : String read _nome write _nome;
        property id_cidade : Integer read Get_id_cidade write Set_id_cidade;
  End;

//Service interface

{ Invokable interface IEmpresa }
unit EmpresaIntf;
interface
uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, System.Generics.Collections, EMPRESA;
type
    { Invokable interfaces must derive from IInvokable }
  IEmpresa = interface(IInvokable)
  ['{D5980639-6211-4748-B065-B7FD7287C7C6}']

    function savePessoa(Pessoa : TPessoa):TPessoa;stdcall;
  end;

implementation

initialization
  { Invokable interfaces must be registered }
  InvRegistry.RegisterInterface(TypeInfo(IEmpresa));

end.  

Implementation of the Interface

function TEmpresa.savePessoa(Pessoa: TPessoa): TPessoa;
begin
    Result := Pessoa
end;

The test can be done with the code below (just save to . html file and open in browser).

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
  <script src="js/funcoes.js"></script>
  <script type="text/javascript"> 
    $(document).ready(function () { 
                $("#btcarregar").click(function() {
                    var envelope = '<?xml version="1.0"?> \
                                    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">\
                                    <SOAP-ENV:Body xmlns:NS1="urn:EmpresaIntf-IEmpresa" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS2="urn:EMPRESA">\
                                    <NS1:savePessoa><Pessoa href="#1"/></NS1:savePessoa>\
                                    <NS2:TPESSOA id="1" xsi:type="NS2:TPESSOA">\
                                    <id xsi:type="xsd:int">1</id>\
                                    <Nome xsi:type="xsd:string">Nome de teste</Nome>\
                                    <id_cidade xsi:type="xsd:int">5</id_cidade>\
                                    </NS2:TPESSOA></SOAP-ENV:Body></SOAP-ENV:Envelope>\
                                ';
                    getSOAP(envelope);} 
                    ); 

                $("#btincorreto").click(function() {
                    var envelope = '<?xml version="1.0"?> \
                                    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">\
                                    <SOAP-ENV:Body xmlns:NS1="urn:EmpresaIntf-IEmpresa" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS2="urn:EMPRESA">\
                                    <NS1:savePessoa><Pessoa href="#1"/></NS1:savePessoa>\
                                    <NS2:TPESSOA id="1" xsi:type="NS2:TPESSOA">\
                                    <id xsi:type="xsd:int">1</id>\
                                    <Nome xsi:type="xsd:string">Nome de teste</Nome>\
                                    <id_cidade xsi:type="xsd:int"></id_cidade>\
                                    </NS2:TPESSOA></SOAP-ENV:Body></SOAP-ENV:Envelope>\
                                ';
                    getSOAP(envelope);} 
                    );                  
            });


function getSOAP(envelope) {
                    var wsUrl = "http://alexcamilo.no-ip.org:9000/soap/IEMPRESA";
                    $.ajax({
                        url: wsUrl,
                        type: "POST",
                        dataType: "xml",
                        data: envelope,
                        contentType: "text/plain",
                        cache: false,
//                        complete: endSaveProduct,
                        success: processSuccess,
                        error: processError
                    });                  
                }

function processSuccess(data, status, req){
    $("#resultado").text(data.activeElement.innerHTML);
    }

</script> 
<title>Teste</title>
</head>
</body>
<div>
<input type="button" id="btcarregar">Chamada passando valor</input>
<input type="button" id="btincorreto">Chamada passando vazio</input>
<div>
<div id="resultado" />

</body>
</html>
  • Post the part of the code that is giving the error!

  • Shows how you are passing the data friend!

  • Is there any problem passing as 0 ? Pq convert this won’t go from no ehim (I think)

  • It doesn’t work like this: Id_city : Integer=9999; similar to class parameters!?

  • The problem of passing as 0(zero) is when the value actually is zero.

  • @Celsomarigojr, Delphi accepts to declare a variable with defined value only if it is Global! If it solves it can do this!

  • @Junior, I meant the way we do a function, for example: Function Tfrmprincipal.Linhacommand(Codven, Dirfiles : String; Consultaped : String = '' ): String;, in this example the Consultaped field has by default the value ''. You meant that this is only possible in Delphi if the variable is global?

  • @Celsomarigojr No, I am saying in his case that the variable is of the type Integer, and it was not initialized!

  • @Camilassos then passes a negative value by Default, passes -1 and we will see the result, since there is the possibility of such ID starting with 0!

  • @Junior, My Intention is to make this interface available (via wsdl) to anyone who wants to use it, stay free, to develop a client in any language, but, it would not be elegant to force the user to enter a value, when the field is null.

  • @Camilassos,then friend, switch to String every structure or put -1 as Default as I said!

  • @Júniormoreira, Thanks for the help, but it was stupid my own, the field is foreign key, so I must expose the foreign key class and not the field, then it may be null. Obliged.

  • Flw good luck!!!!

Show 8 more comments

1 answer

3

Although you have not informed how your entity was defined city, I assume you have a Foreign key relating person x city. If this is the case you will get a key violation if you try to enter value zero or that does not exist in the entity city.

We know that Delphi is strongly typed, types Integer, Boolean, Datetime.... cannot be null, they are born with a default value.

There are some implementations that enable setar nil to a guy Integer for example, it is called Nullable.

The project Spring4d brings this implementation, you can have something like:

TObject = class
private
    FQuantity: Nullable<Integer>;
    FDescription: Nullable<string>;
public
    property Quantity: Nullable<Integer> read FQuantity write FQuantity;
    property Description: Nullable<string> read FDescription write FDescription;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.