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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
unit Unit1;
// J.P. August 2013
// Firebird embedded support added 2014
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, db, sqldb, sqlite3conn, ibconnection, fbadmin,
FileUtil, Forms, Graphics, Dialogs, DbCtrls, Buttons, ExtCtrls, StdCtrls,
DBGrids, Grids, LR_Class, LR_DBSet;
type
{ TForm1 }
TForm1 = class(TForm)
BitBtn1: TBitBtn;
Button1: TButton;
Bt_Print: TButton;
Datasource1: TDatasource;
DBEdit1: TDBEdit;
DBGrid1: TDBGrid;
DBImage1: TDBImage;
DBMemo1: TDBMemo;
DBNavigator1: TDBNavigator;
DBText1: TDBText;
frDBDataSet1: TfrDBDataSet;
frReport1: TfrReport;
IBConnection1: TIBConnection;
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
OpenDialog1: TOpenDialog;
Panel1: TPanel;
Panel2: TPanel;
SQLite3Connection1: TSQLite3Connection;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
procedure Button1Click(Sender: TObject);
procedure Bt_PrintClick(Sender: TObject);
procedure Datasource1DataChange(Sender: TObject; Field: TField);
procedure DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;
Column: TColumn; AState: TGridDrawState);
procedure DBImage1DblClick(Sender: TObject);
procedure DBNavigator1BeforeAction(Sender: TObject; Button: TDBNavButtonType);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure frReport1EnterRect(Memo: TStringList; View: TfrView);
private
FUsingFirebird: boolean; //indicates whether we're using Firebird (true) or SQLite3 (false)
// Updates linked image field with image named in DBEdit1.Text
// Shows image to user
procedure UpdateImageLink;
// Lets user change image stored in database blob field
procedure ChangeImage;
// Load image from file into database blob
procedure LoadDbImage(aFileName: string);
// Recreates mushroom database
// for either SQLite3 (empty) or Firebird (filled)
function RecreateDB: boolean;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
const
FirebirdDB='ImageTest.fdb'; //database file for Firebird
SQLiteDB='ImageTest.db3'; //database file for SQLite3
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
ChangeImage;
end;
procedure TForm1.Bt_PrintClick(Sender: TObject);
begin
frReport1.LoadFromFile(ExtractFilePath(application.ExeName) +
'Mushroom_Report.lrf');
frReport1.ShowReport();
end;
procedure TForm1.Datasource1DataChange(Sender: TObject; Field: TField);
begin
if Field=nil then
UpdateImageLink;
end;
procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;
Column: TColumn; AState: TGridDrawState);
var MyTextStyle: TTextStyle;
begin
if (DataCol =5) then
begin
MyTextStyle := DbGrid1.Canvas.TextStyle;
MyTextStyle.SingleLine := false;
DbGrid1.Canvas.TextStyle := MyTextStyle;
end;
end;
procedure TForm1.DBImage1DblClick(Sender: TObject);
begin
ChangeImage;
end;
procedure TForm1.DBNavigator1BeforeAction(Sender: TObject; Button: TDBNavButtonType);
begin
if Button = nbRefresh then
begin
SQLQuery1.ApplyUpdates;
SQLTransaction1.CommitRetaining;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
SQLQuery1.ApplyUpdates;
SQLTransaction1.Commit;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SQLQuery1.Close;
// First try SQLite3 database in application directory
// If no sqlite3 driver is found, try Firebird
FUsingFirebird := false; //try sqlite first
if not(FUsingFirebird) then
begin
SQLite3Connection1.DatabaseName:=ExtractFilePath(ParamStr(0)) + SQLiteDB;
try
SQLQuery1.Active := true;
except
on E:Exception do
begin
//ShowMessage('Exception: '+E.ClassName+': '+E.Message);
if E.ClassNameIs('EInOutError') then
// Assume 'Can not load SQLite client library "sqlite3.dll". Check your installation.' => but this text may change
begin
FUsingFirebird:=true;
end
else
begin
if RecreateDb then
begin
SQLQuery1.Active := true;
ShowMessage('The SQLite3 database has been recreated');
end
else
begin
ShowMessage('Failure to create SQLite3 databse. Aborting');
Application.Terminate;
end;
end;
end;
end;
Caption := Caption + ' (SQLite3)';
end;
if FUsingFirebird then
begin
// Try Firebird. We'll need to reconnect some components as they were
// set up for sqlite
// Hostname was already empty in Object Inspector so we use embedded
//IBConnection1.HostName:='';
IBConnection1.UserName:='SYSDBA'; //for embedded, this should be an existing name
IBConnection1.CharSet:='UTF8';
IBConnection1.Params.Add('PAGE_SIZE=16384'); //enough space for indexes etc
IBConnection1.DatabaseName:=ExtractFilePath(ParamStr(0)) + FirebirdDB;
IBConnection1.Transaction:=SQLTransaction1;
SQLTransaction1.Database:=IBConnection1;
SQLQuery1.DataBase:=IBConnection1;
try
SQLQuery1.Active:=true;
except
on I: EIBDatabaseError do
begin
// Check for database file not found error message
if (I.GDSErrorcode=335544344) and (pos('I/O error',i.message)>0) then
begin
if RecreateDb then
begin
SQLQuery1.Active := true;
ShowMessage('The Firebird database has been recreated');
end
else
begin
ShowMessage('Failure to create Firebird databse. Aborting');
Application.Terminate;
end;
end
else
begin
ShowMessage('Have tried SQLite3 and Firebird dbs without success. '+LineEnding+
'Exception: '+I.ClassName+': '+I.Message+LineEnding+
'(Firebird GDS Eror code: '+inttostr(I.GDSErrorCode)+')'+LineEnding+
'Aborting.');
Application.Terminate;
end;
end;
on E: Exception do
begin
ShowMessage('Have tried SQLite3 and Firebird dbs without success. '+LineEnding+
'Exception: '+E.ClassName+': '+E.Message+LineEnding+
'Aborting.');
Application.Terminate;
end;
end;
Caption := Caption + ' (Firebird embedded)';
end;
end;
procedure TForm1.frReport1EnterRect(Memo: TStringList; View: TfrView);
begin
if (View.Name = 'Picture2') AND
(frDBDataSet1.DataSet.FieldByName('Image_Link').AsString <> '') then
TFrPictureView(View).Picture.LoadFromFile(ExtractFilePath(ParamStr(0)) +
'images' + DirectorySeparator + frDBDataSet1.DataSet.FieldByName('Image_Link').AsString);
end;
procedure TForm1.UpdateImageLink;
var
s: String;
begin
if DBEdit1.Text <> '' then
begin
s := ExtractFilePath(ParamStr(0)) + 'images' + DirectorySeparator + DBEdit1.Text;
Image1.Picture.LoadFromFile(s);
end
else
Image1.Picture.Clear;
end;
procedure TForm1.ChangeImage;
var
WasEditing: Boolean;
begin
if SQLQuery1.Active then
begin
OpenDialog1.Filter :=
'All image files (*.bmp,*.jpg,*.png,*.gif)|*.bmp;*.jpg;*.png;*.gif|' +
'BMP files (*.bmp)|*.bmp|' +
'JPEG files (*.jpg)|*.jpg|' +
'PNG files (*.png)|*.png|' +
'GIF files (*.gif)|*.gif' +
'TIFF files (*.tiff)|*.tiff;*.tif';
OpenDialog1.InitialDir := ExtractFilePath(ParamStr(0)) + 'images';
if OpenDialog1.Execute then
begin
WasEditing := (SQLQuery1.State = dsEdit);
if not WasEditing then
SQLQuery1.Edit;
DBEdit1.Text := ExtractFileName(OpenDialog1.FileName);
LoadDbImage(OpenDialog1.FileName);
if not WasEditing then
SQLQuery1.Post;
end;
end;
end;
procedure TForm1.LoadDbImage(aFileName: string);
begin
// Disabling writeheader means the image is compatible with Delphi TDBImage applications
// and other applications that only expect raw jpg data in the database blob.
// However, it makes it a bit more difficult to mix various kinds of image types
// e.g. jpg and tiff in the same database fields.
// Fortunately, Lazarus can deal with that for us.
DbImage1.WriteHeader:=false;
DbImage1.Picture.LoadFromFile(aFileName);
end;
function TForm1.RecreateDB: boolean;
var
BackupFile: string;
DBFile: string;
FBAdmin: TFBAdmin;
SQLInstructions: string; //file with sql instructions to create tables etc
Scripter: TSQLScript;
begin
result:=false;
if not(FUsingFirebird) then //not using Firebird, so using SQLite3
begin
try
SQLite3Connection1.ExecuteDirect(
'CREATE TABLE DeadlyMushrooms '+LineEnding+
'-- This table created by Jurassic Pork '+LineEnding+
'-- for Free Pascal Lazarus '+LineEnding+
'-- Create date:2013-08-05 23:55:09 '+LineEnding+
'( '+LineEnding+
' ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+LineEnding+
' Scientific_Name VARCHAR NOT NULL, '+LineEnding+
' Common_Name VARCHAR, '+LineEnding+
' `Order` VARCHAR, `Genus` VARCHAR, `Notes` TEXT, `Picture` BLOB, `Image_Link` VARCHAR)'
);
result := true;
except
result := false;
end;
end
else
begin //Using Firebird
try
DBFile:=ExtractFilePath(ParamStr(0)) + FirebirdDB;
BackupFile:=ChangeFileExt(DBFile,'.fbk');
// First try restoring backup file with full content
if fileexists(BackupFile) then
begin
FBAdmin:=TFBAdmin.Create(nil);
try
try
FBAdmin.UseExceptions:=true;
FBAdmin.Host:=''; //embedded
FBAdmin.Protocol:=IBSPLOCAL;
FBAdmin.User:='SYSDBA';
FBAdmin.Password:=''; //embedded: doesn't matter which password is used
FBAdmin.Connect;
// Now run the restore. It will not overwrite an existing file but will
// create a new db
if FBAdmin.Restore(DBFile,BackupFile,[IBResCreate]) then
begin
sleep(200); //at least needed on Windows: give the filesystem time to update so we don't get failures connecting
exit(true) //we're done; exit the function
end
else
result := false;
finally
FBAdmin.Free;
end;
except
result := false; //we can continue with the next fallback step
end;
end;
// If no backup was found or restore failed,
// create an empty .fdb file
if (FileExists(DBFile))=false then
begin
IBConnection1.CreateDB;
end;
if (FileExists(DBFile)=false) then
raise Exception.CreateFmt('Firebird error: failure to create database %s',[DBFile]);
// Now create needed tables etc:
if not(IBConnection1.Connected) then IBConnection1.Connected := true;
Scripter:=TSQLScript.Create(nil);
try
SQLInstructions := ExtractFilePath(ParamStr(0)) + 'mushrooms_firebird.sql';
if not fileexists(SQLInstructions) then
raise Exception.CreateFmt('Error creating db: could not load SQL definition file %s',[SQLInstructions]);
Scripter.Script.LoadFromFile(SQLInstructions);
Scripter.DataBase := IBConnection1;
Scripter.Transaction := SQLTransaction1;
Scripter.CommentsInSQL := false; //needed to circumvent bugs in TSQLScript
Scripter.UseSetTerm := true; //needed because we have SET TERM statements in the SQL file
// Now everything is loaded in, run all commands at once:
Scripter.Execute;
//... and then commit to make them stick and show them to the SQL that comes
// after the commit
SQLTransaction1.Commit;
result := true;
finally
Scripter.Free;
end;
except
result := false;
end;
end;
end;
end.
|