#1 2011-01-14 13:25:59

corchi72
Member
Registered: 2010-12-10
Posts: 232

how to save an xml file in a field of type blob.

When I go to read the field value is no longer compatible with xml. I have declared a field of type TSQLRawBlob.
See the following example:

TSQLTemp = class(TSQLRecord)
  private
   fXMLObject:TSQLRawBlob;
...
  published
  property XMLObject:TSQLRawBlob read fXMLObject write fXMLObject;
...
end;



...create record
var
  temp:= TSQLTemp;
  FXmlDoc: IXMLDocument;
begin
      try
      temp:= TSQLTemp.Create;
      FXmlDoc:= NewXMLDocument;
      FXMLDoc.LoadFromFile(sfilename);
      temp.XMLObject := FXMLDoc.XML.Text;

      globalClient.Add(temp, true);

      finally
          FreeAndNil(temp);
     
      end;

end;



...read record

function TForm1.LoadTemplate(const ATempID: integer): TSQLTemp;
var
  temp:= TSQLTemp;
  FXmlDoc: IXMLDocument;
begin
      try
      temp:= TSQLTemp.Create(globalClient, ATempID);
     
      FXmlDoc:= NewXMLDocument;
      FXMLDoc.LoadFromFile(Temp.XMLObject); //here I can not read a xml

   
      finally
          FreeAndNil(temp);
     
      end;

end;

Offline

#2 2011-01-14 13:42:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: how to save an xml file in a field of type blob.

Look at the documentation about BLOBs.
At least the few lines above the TSQLRawBlob type declaration, about "Lazy loading" enabled by default.

If you define your field as RawByteString, it will be handled as a BLOB.
That is, by default, it is not retrieved or updated with other fields content (to save bandwidth).

You have either:
1) to use dedicated RetrieveBlobFields/UpdateBlobFields methods
2) to set SQLRestClientURI.ForceBlobTransfert property to TRUE

Don't forget to check the documentation first!

Offline

#3 2011-01-14 17:25:23

corchi72
Member
Registered: 2010-12-10
Posts: 232

Re: how to save an xml file in a field of type blob.

Thanks I put the flag true to globalClient.ForceBlobTransfert but my problem was another, I had to add WideString otherwise gave me error invalid character

write
  Atemp.XMLObject := XMLQVObjects.XML;

read
FXmlDoc:= NewXMLDocument;
FXMLDoc.LoadFromXML(widestring(ATemp.XMLObject));






.....

procedure InitClient(filename:String);
begin
  Model:= CreateNuvolaModel;
  globalClient:= TSQLRestClientDB.Create(Model, CreateNuvolaModel, filename, TSQLRestServerDB);
  TSQLRestClientDB(globalClient).Server.CreateMissingTables(0);
  globalClient.ForceBlobTransfert := true;
end;

Offline

#4 2011-01-14 17:31:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: how to save an xml file in a field of type blob.

Which version of Delphi do you use?

For 2009/2010/XE, perhaps you could use those functions:

function RawBlobToString(const s: TSQLRawBlob): UnicodeString;
begin
  SetString(result,PWideChar(pointer(s)),length(s) shr 1);
end;  

function StringToRawBlob(const s: UnicodeString): TSQLRawBlob;
begin
  SetString(result,PAnsiChar(pointer(s)),length(s)*2);
end;

So that you'll have a safe retrieval from the BLOB into a Delphi 2009/2010/XE Unicode string.
WideString typecast is not a good idea.

But you should better use a RawUTF8 field, then use UTF8ToString() and StringToUTF8() to use the XML from this.
Blob won't give you any benefit, even more if you use ForceBlobTransfert=true

Offline

Board footer

Powered by FluxBB