Delphi class with optional integer field

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.  

Interface Implementation

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

The test can be done with the code below (just save in a .html file and open in the 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><scriptsrc="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>
    
asked by anonymous 03.11.2015 / 12:23

1 answer

3

Although you did not tell us how your city entity was defined, I assume you have a foreign key linking person x city. If this is the case you will have a key violation if you attempt to enter zero value or do not exist in the city entity.

We know that Delphi is strongly typed, Integer, Boolean, DateTime ... can not be null, they are born with a default value.

There are some implementations that allow you to set nil to an Integer type eg Nullable .

The Spring4D project 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;
    
20.06.2016 / 22:57