zeoslib  UNKNOWN
 All Files
ZStreamBlob.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { Blob streams classes }
5 { }
6 { Originally written by Sergey Seroukhov }
7 { }
8 {*********************************************************}
9 
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
12 { }
13 { License Agreement: }
14 { }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
20 { }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
39 { }
40 { }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
45 { }
46 { http://www.sourceforge.net/projects/zeoslib. }
47 { }
48 { }
49 { Zeos Development Group. }
50 {********************************************************@}
51 
52 unit ZStreamBlob;
53 
54 interface
55 
56 {$I ZComponent.inc}
57 
58 uses Classes, SysUtils, {$IFDEF MSEgui}mclasses, mdb{$ELSE}DB{$ENDIF},
59  {$IFDEF WITH_WIDESTRUTILS}WideStrUtils, {$ENDIF}
60  ZDbcIntfs, ZCompatibility;
61 
62 type
63  {** Implements a class for blobs stream. }
64  TZBlobStream = class(TMemoryStream)
65  private
66  FField: TBlobField;
67  FBlob: IZBlob;
68  FMode: TBlobStreamMode;
69  FConSettings: PZConSettings;
70  protected
71  property Blob: IZBlob read FBlob write FBlob;
72  property Mode: TBlobStreamMode read FMode write FMode;
73  public
74  constructor Create(Field: TBlobField; Blob: IZBlob; Mode: TBlobStreamMode;
75  ConSettings: PZConSettings);
76  destructor Destroy; override;
77  end;
78 
79 implementation
80 
81 uses ZEncoding;
82 
83 { TZBlobStream }
84 
85 {**
86  Constructs this object and assignes the main properties.
87  @param Blob
88 }
89 constructor TZBlobStream.Create(Field: TBlobField; Blob: IZBlob;
90  Mode: TBlobStreamMode; ConSettings: PZConSettings);
91 var
92  TempStream: TStream;
93 begin
94  inherited Create;
95 
96  FBlob := Blob;
97  FMode := Mode;
98  FField := Field;
99  FConSettings := ConSettings;
100  if (Mode in [bmRead, bmReadWrite]) and not Blob.IsEmpty then
101  begin
102  TempStream := Blob.GetStream;
103  try
104  TempStream.Position := 0;
105  CopyFrom(TempStream, TempStream.Size);
106  Position := 0;
107  finally
108  TempStream.Free;
109  end;
110  end
111 end;
112 
113 type THackedDataset = class(TDataset);
114 
115 {**
116  Destroys this object and cleanups the memory.
117 }
118 destructor TZBlobStream.Destroy;
119 {$IFDEF WITH_WIDEMEMO}
120 var
121  TempStream: TStream;
122 {$ENDIF}
123 begin
124  if Mode in [bmWrite, bmReadWrite] then
125  begin
126  if Assigned(Self.Memory) then
127  begin
128  {$IFDEF WITH_WIDEMEMO}
129  if FField.DataType = ftWideMemo then
130  begin
131  TempStream := GetValidatedUnicodeStream(Memory, Cardinal(Size),
132  FConSettings, False);
133  Blob.SetStream(TempStream, True);
134  TempStream.Free;
135  end
136  else
137  {$ENDIF}
138  Blob.SetStream(Self)
139  end
140  else
141  Blob.SetStream(nil);
142  try
143  if Assigned(FField.Dataset) then
144  THackedDataset(FField.DataSet).DataEvent(deFieldChange, ULong(FField));
145  except
146  ApplicationHandleException(Self);
147  end;
148  end;
149  inherited Destroy;
150 end;
151 
152 end.
153