Blog Options
Archive
<< October 2025 >>-
Friday 24
- TMS Halloween Challenge: Share Your Spooky App Creations! -
Thursday 23
- Automate StellarDS database operations with AI via MCP -
Tuesday 21
- TMS AI Studio v1.4 is bringing HTTP.sys to MCP -
Wednesday 15
- Using AI Services securely in TMS AI Studio -
Tuesday 14
- Our components are ready for Windows 11 -
Thursday 9
- Seamlessly combining the power of TWebDataGrid with StellarDS.io backend -
Wednesday 8
- Automatic Token Renewal with FetchAccessToken in TMS FNC Cloud Pack -
Tuesday 7
- Next Generation Data Grid for Delphi: Paging (BETA) -
Thursday 2
- Celebrating 10 Years of TMS ALL-ACCESS
- Introducing Attributes Support for MCP Servers in Delphi
Authors
- Bernard Roussely (3)
- Wagner Landgraf (93)
- Dennis Röhner (1)
- Roman Yankovsky (2)
- Bart Holvoet (41)
- Aaron Decramer (58)
- Pieter Scheldeman (125)
- Nancy Lescouhier (32)
- Adrian Gallero (34)
- Bruno Fierens (445)
- Marcos Douglas B. Santos (5)
- Bernard (4)
- Bradley Velghe (35)
- Andrew Simard (86)
- Holger Flick (15)
- Gjalt Vanhouwaert (42)
- Tunde Keller (32)
- Masiha Zemarai (117)
Blog
All Blog Posts | Next Post | Previous Post
Crash 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