From: Carlo B. <car...@us...> - 2005-02-03 22:50:15
|
Update of /cvsroot/instantobjects/Demos/Test/BuildPrimerDatabase In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20674/Demos/Test/BuildPrimerDatabase Added Files: BuildPrimerDatabase.dpr Connections.xml ReadMe.txt UBuilder.pas Log Message: Demo console application to build and populate a database --- NEW FILE: UBuilder.pas --- unit UBuilder; interface uses InstantPersistence; function Connect(const ConnectionFileName, ConnectionName : string) : TInstantConnector; procedure BuildDatabase(Connector : TInstantConnector); procedure CreateRandomContacts(Count: Integer; LoadPictures : boolean = False; const PicturePath : string = ''); implementation uses SysUtils, { Note: This demo attempts to include brokers for the data access layers supported natively by Delphi. To include additional brokers, please add the broker unit(s) to the following list. If you have not installed all brokers, please remove the missing broker unit(s) from the list. } //Begin Broker inclusion section {$IFDEF MSWINDOWS} {$IFNDEF VER130} InstantDBX, {$ENDIF} InstantADO, InstantBDE, InstantIBX, {$ENDIF} {$IFDEF LINUX} InstantDBX, {$ENDIF} InstantXML, //End Broker inclusion section {$IFDEF MSWINDOWS} Jpeg, Graphics, {$ENDIF} {$IFDEF LINUX} QGraphics, {$ENDIF} InstantConnectionManager, InstantClasses, Classes, Contnrs, DemoData, RandomData, Model; var ConnectionManager : TInstantConnectionManager; function Connect(const ConnectionFileName, ConnectionName : string) : TInstantConnector; var ConnectionDef : TInstantConnectionDef; begin Result := nil; // To use XML format for ConnectionManager file: ConnectionManager.FileFormat := sfXML; ConnectionManager.FileName := ConnectionFileName; ConnectionManager.LoadConnectionDefs; ConnectionDef := ConnectionManager.ConnectionDefs.Find(ConnectionName) as TInstantConnectionDef; if Assigned(ConnectionDef) then begin ConnectionManager.CurrentConnectionDef := ConnectionDef; Result := ConnectionDef.CreateConnector(nil); end; end; procedure BuildDatabase(Connector : TInstantConnector); begin //This line works only on few Brokers (eg. IBX or UIB) //For other brokers the database must exists if not Connector.DatabaseExists then Connector.CreateDatabase; //Recreate data structures (and lost data) Connector.BuildDatabase(InstantModel); end; //procedure similar to then same contained in main.pas of Primer Demo procedure AssignRandomPicture(Male : boolean; InstantBlob : TInstantBlob; const PicturePath : string); const {$IFDEF MSWINDOWS} ARandomExt : Array[0..2] of string = ('.bmp','.jpg','.emf'); {$ENDIF} {$IFDEF LINUX} ARandomExt : Array[0..3] of string = ('.bmp','.jpg','.emf','.png'); {$ENDIF} var Picture: TPicture; PictureName : string; begin PictureName := '0'+IntToStr(Random(5)+1)+ARandomExt[Random(High(ARandomExt)+1)]; if Male then PictureName := 'man'+PictureName else PictureName := 'woman'+PictureName; PictureName := PicturePath+PictureName; if FileExists(PictureName) then begin Picture := TPicture.Create; try Picture.LoadFromFile(PictureName); InstantBlob.AssignPicture(Picture); finally Picture.Free; end; end; end; //procedure similar to the same contained in main.pas of Primer Demo procedure CreateRandomContacts(Count: Integer; LoadPictures : boolean = False; const PicturePath : string = ''); var Companies: TObjectList; Gender: TGender; function CreateRandomContact: TContact; var Company: TCompany; begin if Random(2) = 0 then begin if (Random(2) = 0) and (Companies.Count > 10) then Company := Companies[Random(Companies.Count)] as TCompany else Company := nil; Result := CreateRandomPerson(Company, Gender); if LoadPictures then AssignRandomPicture(Gender=gnMale, TPerson(Result)._Picture, PicturePath); end else begin Result := CreateRandomCompany; if Random(2) = 0 then begin if Companies.Count > 50 then Companies.Delete(0); Result.AddRef; Companies.Add(Result); end; end; end; var I, CommitCount: Integer; begin CommitCount := 200; Randomize; Companies := TObjectList.Create; try InstantDefaultConnector.StartTransaction; try for I := 0 to Pred(Count) do begin with CreateRandomContact do try Store; finally Free; end; if (Succ(I) mod CommitCount) = 0 then with InstantDefaultConnector do begin CommitTransaction; StartTransaction; end; end; InstantDefaultConnector.CommitTransaction; except InstantDefaultConnector.RollbackTransaction; raise; end; finally Companies.Free; end; end; initialization ConnectionManager := TInstantConnectionManager.Create(nil); {$IFDEF MSWINDOWS} InstantRegisterGraphicClass(gffJpeg, TJPEGImage); {$ENDIF} finalization ConnectionManager.Free; end. --- NEW FILE: Connections.xml --- <TInstantConnectionDefs> <TInstantIBXConnectionDef> <Name>IBX FB</Name> <IsBuilt>TRUE</IsBuilt> <LoginPrompt>FALSE</LoginPrompt> <Path>C:\IBDATA\PRIMER.FDB</Path> <NetType>ntLocal</NetType> <Options></Options> <Params>User_Name=SYSDBA password=a</Params> </TInstantIBXConnectionDef> <TInstantBDEConnectionDef> <Name>BDE PARADOX</Name> <IsBuilt>TRUE</IsBuilt> <BlobStreamFormat>sfXML</BlobStreamFormat> <LoginPrompt>FALSE</LoginPrompt> <DriverName>STANDARD</DriverName> <Parameters>PATH=D:\BDEBROKER DEFAULT DRIVER=PARADOX ENABLE BCD=TRUE </Parameters> </TInstantBDEConnectionDef> </TInstantConnectionDefs> --- NEW FILE: ReadMe.txt --- BuildPrimerDatabase (command line utility) ------------------------------------------ This console application demonstrate building and population of an InstantObject Database. It uses: - same model unit of Primer demo located in '..\..\PrimerCross\Model\Model.pas' If you want to test external Model use '..\..\PrimerCross\ModelExternal\Model.pas' - same DemoData unit of Primer demo located in '..\..\PrimerCross\DemoData.pas' - same RandomData unit of Primer demo located in '..\..\PrimerCross\RandomData.pas' steps: 1) Prepare Connections.xml file: - Compile the Demos\PrimerCross\Primer.dpr with those lines: // To use XML format for ConnectionManager file: ConnectionManager.FileFormat := sfXML; ConnectionManager.FileName := ChangeFileExt(Application.ExeName, '.xml'); - Run Primer.exe and build your custom connection definitions with LoginPrompt set to False! - Test Connection, Build and database population into Primer, then exit. - Copy Primer.xml file generated into this folder and rename it to Connections.xml. 2) Include brokers: Change lines of BuildPrimerDatabase to include/exclude your preferred Brokers. 3) Compile and run with those parameters: - Connection Name (stored in Connections.xml) - Number of random contacts to create - /picture if you want to add pictures to database. Enjoy, Carlo Barazzetta Last revision: 3 Feb 2005 --- NEW FILE: BuildPrimerDatabase.dpr --- program BuildPrimerDatabase; {$APPTYPE CONSOLE} uses SysUtils, InstantPersistence, DemoData in '..\..\PrimerCross\DemoData.pas', RandomData in '..\..\PrimerCross\RandomData.pas', Model in '..\..\PrimerCross\Model\Model.pas', UBuilder in 'UBuilder.pas'; {$R *.mdr} {Model} var Connector : TInstantConnector; ApplicationPath : string; Count : integer; ConnectionFile : string; begin //Connect to database Try ApplicationPath := ExtractFilePath(ParamStr(0)); ConnectionFile := ApplicationPath+'Connections.xml'; if ParamStr(1) = '' then raise Exception.Create('Connection Name Missing'); Connector := UBuilder.Connect(ConnectionFile, ParamStr(1)); if Connector = nil then raise Exception.CreateFmt('Connection definition for "%s" not found in %s',[ParamStr(1),ConnectionFile]); WriteLn('Building Database structure... please wait.'); //Build Database (empty all data) UBuilder.BuildDatabase(Connector); //Set default connector Connector.IsDefault := True; WriteLn('Connecting to Database.'); //Open connection Connector.Connect; if ParamStr(2) <> '' then Count := StrToInt(ParamStr(2)) else Count := 100; if SameText(ParamStr(3),'/pictures') then begin //Populate Database with Random-Data and Pictures WriteLn(Format('Building %d Contacs with Pictures... please wait.',[Count])); UBuilder.CreateRandomContacts(Count, True, ApplicationPath+'..\..\PrimerCross\Pictures\'); end else begin //Populate Database with Random-Data, without Pictures WriteLn(Format('Building %d Contacs without Pictures... please wait.',[Count])); UBuilder.CreateRandomContacts(Count); end; //Close connection Connector.Disconnect; WriteLn('Done!'); Except on E: Exception do WriteLn(E.Message); End; end. |