Blog
All Blog Posts | Next Post | Previous PostCrash Course TMS Aurelius – Blobs
Thursday, February 28, 2013
Using blobs in Aurelius is very straightforward and yet very powerful. In summary, all you have to do is declare your field/property as TBlob (declared in unit Aurelius.Types.Blob.pas). This is enough to map it to an existing blob field in your table, and you will be able to save/load the blob content is several many ways. Consider the following mapping:[Entity, Automapping] TCustomer = class private FId: integer; FName: string; FDocument: TBlob; [Column('Photo', [TColumnProp.Lazy])] FPhoto: TBlob; [Column('Descr_Field', [], 65536)] FDescription: string; public property Id: integer read FId write FId; property Name: string read FName write FName; property Document: TBlob read FDocument write FDocument; property Photo: TBlob read FPhoto write FPhoto; property Description: string read FDescription write FDescription; end;
There is another interesting feature about blobs: Photo is declared as lazy (TColumnProp.Lazy). This indicates that Aurelius will not bring the blob from database when Customer data is retrieved. The blob is only retrieved when your code explicitly reads the content of Photo property.
The following code shows different ways of dealing with blobs (saving and loading):
function SaveCustomerWithBlobs(Manager: TObjectManager): integer; var Customer: TCustomer; begin Customer := TCustomer.Create; Customer.Name := 'John'; Customer.Photo := TFile.ReadAllBytes('picture.bmp'); Customer.Document.AsBytes := TFile.ReadAllBytes('document.pdf'); Customer.Description := TFile.ReadAllText('description.txt'); Manager.Save(Customer); Result := Customer.Id; end; procedure LoadCustomerAndExportBlobs(Manager: TObjectManager; CustomerId: integer); var Customer: TCustomer; begin Customer := Manager.Find<TCustomer>(CustomerId); TFile.WriteAllText('description2.txt', Customer.Description); TFile.WriteAllBytes('document2.pdf', Customer.Document); TFile.WriteAllBytes('picture2.bmp', Customer.Photo.AsBytes); end;
Customer.Photo := TBlob.Create(TFile.ReadAllBytes('picture.bmp')); Customer.Document.AsString := 'Some document'; Stream := TFile.Open('picture.bmp', TFileMode.fmOpen); Customer.Photo.LoadFromStream(Stream); Stream.Free;
As a final note: we have added TColumnProp.Lazy to the Photo blob. We can verify if the blob is loaded using the Loaded property. We can change LoadCustomerAndExportBlobs function to check it:
Customer := Manager.Find<TCustomer>(CustomerId); Assert(Customer.Document.Loaded); TFile.WriteAllBytes('document2.pdf', Customer.Document); Assert(not Customer.Photo.Loaded); TFile.WriteAllBytes('picture2.bmp', Customer.Photo.AsBytes); Assert(Customer.Photo.Loaded);
CREATE TABLE CUSTOMER ( ID INTEGER NOT NULL, NAME VARCHAR(255) NOT NULL, DOCUMENT BLOB, Photo BLOB, Descr_Field BLOB SUB_TYPE TEXT, CONSTRAINT PK_CUSTOMER PRIMARY KEY (ID)); CREATE GENERATOR SEQ_CUSTOMER; SELECT GEN_ID(SEQ_CUSTOMER, 1) FROM RDB$DATABASE; INSERT INTO CUSTOMER ( ID, NAME, DOCUMENT, Photo, Descr_Field) VALUES ( :A_ID, :A_NAME, :A_DOCUMENT, :A_Photo, :A_Descr_Field); SELECT A.ID AS A_ID, A.NAME AS A_NAME, A.DOCUMENT AS A_DOCUMENT, A.Descr_Field AS A_Descr_Field FROM CUSTOMER A WHERE A.ID = :p_0; SELECT A.Photo As f0_ FROM CUSTOMER A WHERE A.ID = :p_0;
Wagner Landgraf
This blog post has received 2 comments.
2. Tuesday, November 25, 2014 at 10:44:53 AM
Please contact us directly for support (see our Support menu option at our site). Basically you can use AsBytes property to read/write the content as byte array.
Wagner Landgraf
All Blog Posts | Next Post | Previous Post
Could you pleas provide source example for saving blob image from a TImage component.
Also loading Blob content to a TImage component
Thanks
Kenzo
kenzo