數(shù)據(jù)庫(kù)的BLOB字段,可以用來(lái)保存字節(jié)流數(shù)據(jù),比如圖片、聲音文件、html文檔和大文本等等。Delphi傳統(tǒng)的DataSnap技術(shù),客戶端數(shù)據(jù)集的數(shù)據(jù)包缺省不包含Blob字段數(shù)據(jù),但可以在真正需要的時(shí)刻,實(shí)時(shí)自動(dòng)從數(shù)據(jù)庫(kù)取得,這種自動(dòng)實(shí)現(xiàn)是基于IAppServer接口的。但在DataSnap 2009中,中間層通過方法調(diào)用返回TDataSet的時(shí)候,客戶端無(wú)法再通過IAppServer接口取得Blob字段數(shù)據(jù)了,因?yàn)檫@是方法調(diào)用方式,沒有IAppServer接口的實(shí)現(xiàn)。要解決客戶端讀取Blob字段數(shù)據(jù)的問題,可以在中間層單獨(dú)申明和定義一個(gè)返回Blob字段數(shù)據(jù)流的方法,客戶端在適當(dāng)?shù)臅r(shí)機(jī)調(diào)用此方法就可以返回Blob字段數(shù)據(jù)了。下面以一個(gè)簡(jiǎn)單例子說(shuō)明整個(gè)讀寫過程的實(shí)現(xiàn)。
假設(shè)我們有一個(gè)Oracle的Poster表,保存電影的海報(bào)信息。包含兩個(gè)字段:
Film_id Integer
Picture BLOB
中間層返回海報(bào)表中的所有記錄:
function TMoviesMethods.GetPictures: TDataSet;
begin
with dsQuery do
begin
Close;
CommandText := 'select * from poster';
Open;
end;
Result := dsQuery;
end;
中間層再定義一個(gè)根據(jù)指定ID返回圖片數(shù)據(jù)流的方法:
function TMoviesMethods.GetPictureById(film_id: Integer): TStream;
begin
Result := nil;
with dsQuery do
begin
Close;
CommandText := 'select * from poster where film_id=:film_id';
ParamByName('film_id').AsInteger := film_id;
Open;
if not eof then
Result := CreateBlobStream(FieldByName('picture'), bmRead);
end;
end;
客戶端用一個(gè)TClientDataSet控件來(lái)接收數(shù)據(jù),用一個(gè)TEdit顯示當(dāng)前記錄的ID,一個(gè)TImage控件顯示當(dāng)前記錄的圖片。當(dāng)TClientDataSet.AfterScroll事件發(fā)生時(shí),填寫TEdit和TImage控件的內(nèi)容,其中,TMoviesMethodsClient是自動(dòng)生成的中間層方法的客戶端代理類:
procedure TForm1.ClientDataSet1AfterScroll(DataSet: TDataSet);
var
ResultStream: TStream;
ServiceProxy: TMoviesMethodsClient;
begin
Edit1.Text := IntToStr(ClientDataSet1.FieldByName('film_id').AsInteger);
ServiceProxy := TMoviesMethodsClient.Create(SQLConnection1.DBXConnection);
try
ResultStream := ServiceProxy.GetPictureById(ClientDataSet1.FieldByName('film_id').AsInteger);
if ResultStream <> nil then
begin
Image1.Picture.Bitmap.LoadFromStream(ResultStream);
end;
finally
ServiceProxy.Free;
end;
end;
接下來(lái),演示客戶端新增一條記錄,為Blob字段添加圖片數(shù)據(jù),并寫回?cái)?shù)據(jù)庫(kù)的過程。
客戶端新增一條記錄:
procedure TForm1.Button1Click(Sender: TObject);
var
AStream: TMemoryStream;
begin
with ClientDataSet1 do
begin
AStream := TMemoryStream.Create;
try
Image1.Picture.Graphic.SaveToStream(AStream);
Append;
FieldByName('film_id').AsInteger := StrToInt(Edit1.Text);
TBlobField(FieldByName('picture')).LoadFromStream(AStream);
Post;
finally
AStream.Free;
end;
end;
end;
向中間層提交更新。其中,SavePictures方法是中間層實(shí)現(xiàn)的一個(gè)方法:
procedure TForm1.Button2Click(Sender: TObject);
begin
with SqlServerMethod1 do
begin
ServerMethodName := 'TMoviesMethods.SavePictures';
ParamByName('PosterDelta').Value := ClientDataSet1.Delta;
ExecuteMethod;
end;
end;
中間層實(shí)現(xiàn)保存Delta數(shù)據(jù)包的方法。過程中,用到一個(gè)TDataSetProvider控件dspUpdate,利用它來(lái)解析Delta包,生成SQL語(yǔ)句,提交數(shù)據(jù)庫(kù)執(zhí)行;UpdatePosterDelta過程真正實(shí)現(xiàn)手動(dòng)更新Delta包:
function TMoviesMethods.SavePictures(PosterDelta: OleVariant): Boolean;
var
ErrorCount: Integer;
begin
Result := False;
if not VarIsError(PosterDelta) then
begin
try
FOnDeltaRecordUpdate := UpdatePosterDelta;
dspUpdate.ApplyUpdates(PosterDelta, 0, ErrorCount);
Result := (ErrorCount = 0);
finally
FOnDeltaRecordUpdate := nil;
end;
end;
end;
當(dāng)dspUpdate的BeforeUpdateRecord事件被觸發(fā)時(shí),調(diào)用UpdatePosterDelta方法,以實(shí)現(xiàn)手動(dòng)更新Delta包的目的:
procedure TMoviesMethods.dspUpdateBeforeUpdateRecord(Sender: TObject;
SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
var Applied: Boolean);
begin
if Assigned(FOnDeltaRecordUpdate) then
FOnDeltaRecordUpdate(Sender, SourceDS, DeltaDS, UpdateKind, Applied);
end;
UpdatePosterDelta方法。這里使用了一個(gè)TSQLDataSet控件dsQuery,利用它來(lái)提交SQL語(yǔ)句:
procedure TMoviesMethods.UpdatePosterDelta(Sender: TObject; SourceDS: TDataSet;
DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);
begin
Applied := False;
case UpdateKind of
ukModify:
begin
with dsQuery do
begin
Close;
CommandText := 'update poster set picture=:picture ' +
'where film_id=:film_id';
ParamByName('film_id').AsInteger := DeltaDS.FieldByName('film_id').OldValue;
ParamByName('picture').AsBlob := DeltaDS.FieldByName('picture').AsBytes;
if ExecSQL() = 0 then Abort;
end;
end;
ukInsert:
begin
with dsQuery do
begin
Close;
CommandText := 'insert into poster(film_id, picture) ' +
'values(:film_id, :picture)';
ParamByName('film_id').AsInteger := DeltaDS.FieldByName('film_id').AsInteger;
ParamByName('picture').AsBlob := DeltaDS.FieldByName('picture').AsBytes;
if ExecSQL() = 0 then Abort;
end;
end;
ukDelete:
begin
with dsQuery do
begin
Close;
CommandText := 'delete poster ' +
'where film_id=:film_id';
ParamByName('film_id').AsInteger := DeltaDS.FieldByName('film_id').OldValue;
if ExecSQL() = 0 then Abort;
end;
end;
end;
Applied := True;
end;
|