Menu

[r8320]: / trunk / examples / blobs / ZBlobsMain.pas  Maximize  Restore  History

Download this file

288 lines (259 with data), 8.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{*********************************************************}
{ }
{ Zeos Database Objects }
{ Blobs Example Application }
{ }
{ Originally written by Sergey Seroukhov }
{ }
{*********************************************************}
{@********************************************************}
{ Copyright (c) 1999-2006 Zeos Development Group }
{ }
{ License Agreement: }
{ }
{ This library is distributed in the hope that it will be }
{ useful, but WITHOUT ANY WARRANTY; without even the }
{ implied warranty of MERCHANTABILITY or FITNESS FOR }
{ A PARTICULAR PURPOSE. See the GNU Lesser General }
{ Public License for more details. }
{ }
{ The source code of the ZEOS Libraries and packages are }
{ distributed under the Library GNU General Public }
{ License (see the file COPYING / COPYING.ZEOS) }
{ with the following modification: }
{ As a special exception, the copyright holders of this }
{ library give you permission to link this library with }
{ independent modules to produce an executable, }
{ regardless of the license terms of these independent }
{ modules, and to copy and distribute the resulting }
{ executable under terms of your choice, provided that }
{ you also meet, for each linked independent module, }
{ the terms and conditions of the license of that module. }
{ An independent module is a module which is not derived }
{ from or based on this library. If you modify this }
{ library, you may extend this exception to your version }
{ of the library, but you are not obligated to do so. }
{ If you do not wish to do so, delete this exception }
{ statement from your version. }
{ }
{ }
{ The project web site is located on: }
{ http://zeos.firmos.at (FORUM) }
{ http://zeosbugs.firmos.at (BUGTRACKER) }
{ svn://zeos.firmos.at/zeos/trunk (SVN Repository) }
{ }
{ http://www.sourceforge.net/projects/zeoslib. }
{ http://www.zeoslib.sourceforge.net }
{ }
{ }
{ }
{ Zeos Development Group. }
{********************************************************@}
unit ZBlobsMain;
interface
uses
SysUtils, {$IFDEF VER140}Types, {$ENDIF}Classes, DB,
{$IFDEF LINUX}
QGraphics, QControls, QForms, QDialogs,
QStdCtrls, QExtCtrls, QDBCtrls, QGrids, QDBGrids,
{$ELSE}
Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids,
{$ENDIF}
ZConnection, ZDataset,
ZDbcMySQL, ZDbcPostgreSQL, ZDbcInterbase6, ZDbcCache, ZSqlUpdate, ZSqlMetadata,
ZDbcIntfs, ZDbcProperties;
type
{** Defines the main form of the application. }
TfrmMain = class(TForm)
pnControl: TPanel;
pnMain: TPanel;
navMain: TDBNavigator;
gdMain: TDBGrid;
pnDetail: TPanel;
splMain: TSplitter;
splDetail: TSplitter;
memText: TDBMemo;
lblProtocol: TLabel;
cbxProtocol: TComboBox;
lblHostName: TLabel;
edtHostName: TEdit;
lblDatabase: TLabel;
edtDatabase: TEdit;
lblUserName: TLabel;
edtUserName: TEdit;
edtPassword: TEdit;
lblPassword: TLabel;
btnConnect: TButton;
btnDisconnect: TButton;
btnOpen: TButton;
btnClose: TButton;
btnApplyUpdates: TButton;
btnCancelUpdates: TButton;
dsMain: TDataSource;
imgBlob: TDBImage;
lblTableName: TLabel;
edtTableName: TEdit;
lblMemoColumn: TLabel;
edtMemoColumn: TEdit;
lblBlobColumn: TLabel;
edtBlobColumn: TEdit;
btnLoadImage: TButton;
dlgOpenFile: TOpenDialog;
cbxOidAsBlob: TCheckBox;
procedure cbxOidAsBlobClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnConnectClick(Sender: TObject);
procedure btnDisconnectClick(Sender: TObject);
procedure btnOpenClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnApplyUpdatesClick(Sender: TObject);
procedure btnCancelUpdatesClick(Sender: TObject);
procedure PropertiesChange(Sender: TObject);
procedure btnLoadImageClick(Sender: TObject);
private
FConnection: TZConnection;
FDataset: TZQuery;
FMetadata: TZSqlMetadata;
public
property Connection: TZConnection read FConnection write FConnection;
property Metadata:TZSqlMetadata read FMetadata write FMetadata;
property Dataset: TZQuery read FDataset write FDataset;
end;
var
frmMain: TfrmMain;
implementation
{$IFDEF VER140}
{$R *.xfm}
{$ELSE}
{$R *.dfm}
{$ENDIF}
{**
Initializes this form properties.
@param Sender an event sender object reference.
}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
Connection := TZConnection.Create(Self);
Dataset := TZQuery.Create(Self);
Dataset.Connection := Connection;
Dataset.ReadOnly := False;
// Dataset.CachedUpdates := True;
// make sure, we get the supported protocol names
cbxProtocol.Clear;
Connection.GetProtocolNames(cbxProtocol.Items);
cbxProtocol.Sorted := true;
dsMain.Dataset := Dataset;
PropertiesChange(Self);
end;
{**
Updates database properties.
@param Sender an event sender object reference.
}
procedure TfrmMain.PropertiesChange(Sender: TObject);
Var iIndex: Integer;
begin
Connection.Protocol := cbxProtocol.Text;
Connection.HostName := edtHostName.Text;
Connection.Database := edtDatabase.Text;
Connection.User := edtUserName.Text;
Connection.Password := edtPassword.Text;
Dataset.SQL.Text := 'SELECT * FROM ' + edtTableName.Text;
memText.DataField := edtMemoColumn.Text;
imgBlob.DataField := edtBlobColumn.Text;
cbxOidAsBlob.Enabled := (pos('postgre', cbxProtocol.Text) <> 0);
if cbxOidAsBlob.Enabled then
cbxOidAsBlobClick(Sender)
else
Connection.Properties.Clear;
end;
{**
Establishes a connection to SQL server.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
Connection.Connect;
end;
{**
Closes a connection to SQL server.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnDisconnectClick(Sender: TObject);
begin
Connection.Disconnect;
end;
{**
Opens a SQL query object.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnOpenClick(Sender: TObject);
var sTyp: String;
begin
Dataset.Open;
end;
{**
Closes a SQL query object.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
Dataset.Close;
end;
{**
Posts cached update to database from SQL query.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnApplyUpdatesClick(Sender: TObject);
begin
Dataset.Post;
// Dataset.CommitUpdates;
end;
{**
Cancel all previously made update in SQL query.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnCancelUpdatesClick(Sender: TObject);
begin
// Dataset.CancelUpdates;
end;
{**
Load an image from file and stores it into the blob column.
@param Sender an event sender object reference.
}
procedure TfrmMain.btnLoadImageClick(Sender: TObject);
var
BlobStream: TStream;
FileStream: TStream;
begin
if Dataset.Active then begin
dlgOpenFile.Filter := 'Bitmap files (*.bmp)|*.BMP';
if dlgOpenFile.Execute then begin
begin
if Dataset.State <> dsEdit then
Dataset.Edit;
BlobStream := Dataset.CreateBlobStream(
Dataset.FieldByName(edtBlobColumn.Text), bmWrite);
try
FileStream := TFileStream.Create(dlgOpenFile.FileName, fmOpenRead);
try
BlobStream.CopyFrom(FileStream, FileStream.Size);
finally
FileStream.Free;
end;
finally
BlobStream.Free;
end;
Dataset.Post;
end;
end;
end;
end;
procedure TfrmMain.cbxOidAsBlobClick(Sender: TObject);
begin
if cbxOidAsBlob.Checked then
Connection.Properties.Values[DSProps_OidAsBlob] := 'true'
else
Connection.Properties.Clear;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.