djjd47130

Data Pump New

Jan 9th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 46.47 KB | None | 0 0
  1. unit DataPumpNEW;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages,
  7.   System.SysUtils, System.Variants, System.Classes
  8.   , Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs
  9.   , Data.DB, Data.Win.ADODB
  10.   , IniFiles
  11. {$IFDEF DEBUG}
  12.   , Clipbrd
  13. {$ENDIF}
  14.   , DataPumpLookup
  15.   ;
  16.  
  17. type
  18.   TLookupItem = class;
  19.   TLookupTable = class;
  20.   TLookupTables = class;
  21.   TRecordProp = class;
  22.   TRecord = class;
  23.   TRecords = class;
  24.   TConnection = class;
  25.   TConnections = class;
  26.   TEntityProp = class;
  27.   TEntity = class;
  28.   TEntities = class;
  29.   TSchemaLink = class;
  30.   TSchemaLinks = class;
  31.   TSchema = class;
  32.   TSchemas = class;
  33.   TDataPump = class;
  34.  
  35.   TPerspective = (psNone, psSource, psDestination);
  36.  
  37.   TFieldLinkType = (flText, flInt, flFloat, flBool);
  38.   TFieldLinkTypes = set of TFieldLinkType;
  39.  
  40.   TConnectedEvent = procedure(Sender: TObject; const Perspective: TPerspective;
  41.     const Connected: Boolean) of object;
  42.  
  43.   //Individual property value referenced with a unique ID
  44.   TLookupItem = class(TPersistent)
  45.   private
  46.     FOwner: TLookupTable;
  47.     FID: Integer;
  48.     FValue: String;
  49.     procedure SetID(const Value: Integer);
  50.     procedure SetValue(const Value: String);
  51.   public
  52.     constructor Create(AOwner: TLookupTable);
  53.     destructor Destroy; override;
  54.   published
  55.     property ID: Integer read FID;
  56.     property Value: String read FValue write SetValue;
  57.   end;
  58.  
  59.   //List of property values of a certain lookup type
  60.   TLookupTable = class(TPersistent)
  61.   private
  62.     FOwner: TLookupTables;
  63.     FItems: TList;
  64.     FLastID: Integer;
  65.     FCaseSensitive: Boolean;
  66.     FName: String;
  67.     procedure SetCaseSensitive(const Value: Boolean);
  68.     function NewID: Integer;
  69.     procedure SetName(const Value: String);
  70.     function GetItem(const Index: Integer): TLookupItem;
  71.   public
  72.     constructor Create(AOwner: TLookupTables);
  73.     destructor Destroy; override;
  74.     function Add: TLookupItem; overload;
  75.     function Add(const Value: String): TLookupItem; overload;
  76.     procedure Delete(const Index: Integer);
  77.     procedure Clear;
  78.     function Count: Integer;
  79.     function Lookup(const Value: String;
  80.       const CanCreate: Boolean = False): Integer;
  81.     property Items[const Index: Integer]: TLookupItem read GetItem; default;
  82.   published
  83.     property Name: String read FName write SetName;
  84.     property CaseSensitive: Boolean
  85.       read FCaseSensitive write SetCaseSensitive;
  86.   end;
  87.  
  88.   //Group of lookup tables
  89.   TLookupTables = class(TPersistent)
  90.   private
  91.     FOwner: TRecords;
  92.     FItems: TList;
  93.     function GetItem(Index: Integer): TLookupTable;
  94.   public
  95.     constructor Create(AOwner: TRecords);
  96.     destructor Destroy; override;
  97.     function Add: TLookupTable;
  98.     procedure Delete(const Index: Integer);
  99.     procedure Clear;
  100.     function Count: Integer;
  101.     function TableByName(const Name: String): TLookupTable;
  102.     procedure Load;
  103.     property Items[Index: Integer]: TLookupTable read GetItem; default;
  104.   end;
  105.  
  106.   //Individual property of a specific record
  107.   TRecordProp = class(TPersistent)
  108.   private
  109.     FOwner: TRecord;
  110.     FName: String;
  111.     FValue: String;
  112.     FPropType: TFieldLinkType;
  113.     FLookup: String;
  114.     function Encode(const Value: String): String;
  115.     function Decode(const Value: String): String;
  116.     function GetAsBoolean: Boolean;
  117.     function GetAsFloat: Double;
  118.     function GetAsInteger: Integer;
  119.     function GetAsString: String;
  120.     procedure SetAsBoolean(const Value: Boolean);
  121.     procedure SetAsFloat(const Value: Double);
  122.     procedure SetAsInteger(const Value: Integer);
  123.     procedure SetAsString(const Value: String);
  124.     procedure SetName(const Value: String);
  125.     procedure SetPropType(const Value: TFieldLinkType);
  126.     procedure SetLookup(const Value: String);
  127.   public
  128.     constructor Create(AOwner: TRecord);
  129.     destructor Destroy; override;
  130.     property AsString: String read GetAsString write SetAsString;
  131.     property AsInteger: Integer read GetAsInteger write SetAsInteger;
  132.     property AsFloat: Double read GetAsFloat write SetAsFloat;
  133.     property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
  134.   published
  135.     property Name: String read FName write SetName;
  136.     property Lookup: String read FLookup write SetLookup;
  137.     property PropType: TFieldLinkType read FPropType write SetPropType;
  138.   end;
  139.  
  140.   //Individual unique record from a data source
  141.   TRecord = class(TPersistent)
  142.   private
  143.     FOwner: TRecords;
  144.     FProps: TList;
  145.     FID: Integer;
  146.     FCaption: String;
  147.     procedure SetCaption(const Value: String);
  148.     procedure SetID(const Value: Integer);
  149.     function GetName(const Index: Integer): String;
  150.     function GetPropByName(const Name: String): TRecordProp;
  151.   public
  152.     constructor Create(AOwner: TRecords);
  153.     destructor Destroy; override;
  154.     procedure Clear;
  155.     function Count: Integer;
  156.     procedure Delete(const Index: Integer);
  157.     procedure Assign(Source: TPersistent); override;
  158.     function IndexOfProp(const Name: String): Integer;
  159.     function ReadString(const Name: String): String;
  160.     function ReadInteger(const Name: String): Integer;
  161.     function ReadFloat(const Name: String): Double;
  162.     function ReadBoolean(const Name: String): Boolean;
  163.     procedure WriteString(const Name, Value: String);
  164.     procedure WriteInteger(const Name: String; const Value: Integer);
  165.     procedure WriteFloat(const Name: String; const Value: Double);
  166.     procedure WriteBoolean(const Name: String; const Value: Boolean);
  167.     property Names[const Index: Integer]: String read GetName;
  168.     property PropByName[const Name: String]: TRecordProp
  169.       read GetPropByName;
  170.     property AsString[const Name: String]: String
  171.       read ReadString write WriteString;
  172.     property AsInteger[const Name: String]: Integer
  173.       read ReadInteger write WriteInteger;
  174.     property AsFloat[const Name: String]: Double
  175.       read ReadFloat write WriteFloat;
  176.     property AsBoolean[const Name: String]: Boolean
  177.       read ReadBoolean write WriteBoolean;
  178.   published
  179.     property ID: Integer read FID write SetID;
  180.     property Caption: String read FCaption write SetCaption;
  181.   end;
  182.  
  183.   //Group of records from a data source
  184.   TRecords = class(TPersistent)
  185.   private
  186.     FOwner: TDataPump;
  187.     FItems: TList;
  188.     FLookup: TLookupTables;
  189.     function GetRecord(Index: Integer): TRecord;
  190.   public
  191.     constructor Create(AOwner: TDataPump);
  192.     destructor Destroy; override;
  193.     function Add: TRecord;
  194.     procedure Delete(const Index: Integer);
  195.     procedure Clear;
  196.     function Count: Integer;
  197.     function IndexOf(const ID: Integer): Integer; overload;
  198.     function IndexOf(const Caption: String): Integer; overload;
  199.     function IndexOf(ARecord: TRecord): Integer; overload;
  200.     property Records[Index: Integer]: TRecord read GetRecord; default;
  201.   end;
  202.  
  203.   //Individual possible database connection
  204.   TConnection = class(TPersistent)
  205.   private
  206.     FOwner: TConnections;
  207.     FConnection: TADOConnection;
  208.     FActive: Boolean;
  209.     FCaption: String;
  210.     FConnectionString: String;
  211.     FOnConnect: TConnectedEvent;
  212.     procedure DoOnConnect(const Connected: Boolean);
  213.     procedure DBConnect(Sender: TObject);
  214.     procedure DBDisconnect(Sender: TObject);
  215.     procedure SetActive(const Value: Boolean);
  216.     procedure SetConnectionString(const Value: String);
  217.     procedure SetCaption(const Value: String);
  218.   public
  219.     constructor Create(AOwner: TConnections);
  220.     destructor Destroy; override;
  221.     procedure Connect;
  222.     procedure Disconnect;
  223.     function Perspective: TPerspective;
  224.     function NewQuery: TADOQuery;
  225.     function ExecSQL(const SQL: String): Integer;
  226.     function Connected: Boolean;
  227.   published
  228.     property Active: Boolean read FActive write SetActive;
  229.     property Caption: String read FCaption write SetCaption;
  230.     property ConnectionString: String read FConnectionString write SetConnectionString;
  231.     property OnConnect: TConnectedEvent read FOnConnect write FOnConnect;
  232.   end;
  233.  
  234.   //Group of database connections
  235.   TConnections = class(TPersistent)
  236.   private
  237.     FOwner: TDataPump;
  238.     FItems: TList;
  239.     procedure DBConnection(Sender: TObject; const Perspective: TPerspective;
  240.       const Connected: Boolean);
  241.     function GetConnection(Index: Integer): TConnection;
  242.   public
  243.     constructor Create(AOwner: TDataPump);
  244.     destructor Destroy; override;
  245.     function Owner: TDataPump;
  246.     function Count: Integer;
  247.     procedure Clear;
  248.     procedure Delete(const Index: Integer);
  249.     function Add: TConnection;
  250.     function IndexOf(Source: TConnection): Integer;
  251.     property Items[Index: Integer]: TConnection read GetConnection; default;
  252.   end;
  253.  
  254.   //Individual possible property of a particular entity
  255.   TEntityProp = class(TPersistent)
  256.   private
  257.     FOwner: TEntity;
  258.     FName: String;
  259.     FIsPrimaryKey: Boolean;
  260.     FIsIdentity: Boolean;
  261.     FIsReadOnly: Boolean;
  262.     FSize: Integer;
  263.     FLinkType: TFieldLinkType;
  264.     FLookupName: String;
  265.     procedure SetIsIdentity(const Value: Boolean);
  266.     procedure SetIsPrimaryKey(const Value: Boolean);
  267.     procedure SetIsReadOnly(const Value: Boolean);
  268.     procedure SetName(const Value: String);
  269.     procedure SetSize(const Value: Integer);
  270.     procedure SetLinkType(const Value: TFieldLinkType);
  271.     procedure SetLookupName(const Value: String);
  272.   public
  273.     constructor Create(AOwner: TEntity);
  274.     destructor Destroy; override;
  275.   published
  276.     property Name: String read FName write SetName;
  277.     property Size: Integer read FSize write SetSize;
  278.     property LinkType: TFieldLinkType read FLinkType write SetLinkType;
  279.     property LookupName: String read FLookupName write SetLookupName;
  280.     property IsPrimaryKey: Boolean
  281.       read FIsPrimaryKey write SetIsPrimaryKey;
  282.     property IsIdentity: Boolean
  283.       read FIsIdentity write SetIsIdentity;
  284.     property IsReadOnly: Boolean
  285.       read FIsReadOnly write SetIsReadOnly;
  286.   end;
  287.  
  288.   //Mapping for ommonly shared type of data source
  289.   TEntity = class(TPersistent)
  290.   private
  291.     FOwner: TEntities;
  292.     FProps: TList;
  293.     FFilename: String;
  294.     FName: String;
  295.     FCaption: String;
  296.     FDescription: String;
  297.     procedure SetCaption(const Value: String);
  298.     procedure SetDescription(const Value: String);
  299.     procedure SetFilename(const Value: String);
  300.     procedure SetName(const Value: String);
  301.     function GetProp(Index: Integer): TEntityProp;
  302.   public
  303.     constructor Create(AOwner: TEntities);
  304.     destructor Destroy; override;
  305.     function PathAndFilename: String;
  306.     procedure LoadFromFile;
  307.     procedure SaveToFile;
  308.     procedure Clear;
  309.     function Count: Integer;
  310.     procedure Delete(const Index: Integer);
  311.     property Props[Index: Integer]: TEntityProp read GetProp; default;
  312.   published
  313.     property Name: String read FName write SetName;
  314.     property Caption: String read FCaption write SetCaption;
  315.     property Description: String read FDescription write SetDescription;
  316.     property Filename: String read FFilename write SetFilename;
  317.   end;
  318.  
  319.   //Group of entity mappings
  320.   TEntities = class(TPersistent)
  321.   private
  322.     FOwner: TDataPump;
  323.     FItems: TList;
  324.     function GetItem(Index: Integer): TEntity;
  325.   public
  326.     constructor Create(AOwner: TDataPump);
  327.     destructor Destroy; override;
  328.     function Owner: TDataPump;
  329.     function Count: Integer;
  330.     procedure Clear;
  331.     procedure Delete(const Index: Integer);
  332.     function Add: TEntity;
  333.     function IndexOf(Source: TEntity): Integer;
  334.     property Items[Index: Integer]: TEntity read GetItem; default;
  335.   end;
  336.  
  337.   //Individual field of a database's schema for a particular entity
  338.   TSchemaLink = class(TPersistent)
  339.   private
  340.     FOwner: TSchemaLinks;
  341.     FName: String;
  342.     FLookup: Boolean;
  343.     FLookupTable: String;
  344.     FFieldName: String;
  345.     FLookupID: String;
  346.     FLookupCaption: String;
  347.     FIsPrimaryKey: Boolean;
  348.     FIsIdentity: Boolean;
  349.     FIsReadOnly: Boolean;
  350.     FAlias: String;
  351.     procedure SetFieldName(const Value: String);
  352.     procedure SetLookup(const Value: Boolean);
  353.     procedure SetLookupCaption(const Value: String);
  354.     procedure SetLookupID(const Value: String);
  355.     procedure SetLookupTable(const Value: String);
  356.     procedure SetName(const Value: String);
  357.     procedure SetIsIdentity(const Value: Boolean);
  358.     procedure SetIsPrimaryKey(const Value: Boolean);
  359.     procedure SetIsReadOnly(const Value: Boolean);
  360.     procedure SetAlias(const Value: String);
  361.   public
  362.     constructor Create(AOwner: TSchemaLinks);
  363.     destructor Destroy; override;
  364.   published
  365.     property Name: String read FName write SetName;
  366.     property Alias: String read FAlias write SetAlias;
  367.     property FieldName: String read FFieldName write SetFieldName;
  368.     property Lookup: Boolean read FLookup write SetLookup;
  369.     property LookupTable: String read FLookupTable write SetLookupTable;
  370.     property LookupID: String read FLookupID write SetLookupID;
  371.     property LookupCaption: String read FLookupCaption write SetLookupCaption;
  372.     property IsPrimaryKey: Boolean
  373.       read FIsPrimaryKey write SetIsPrimaryKey;
  374.     property IsIdentity: Boolean
  375.       read FIsIdentity write SetIsIdentity;
  376.     property IsReadOnly: Boolean
  377.       read FIsReadOnly write SetIsReadOnly;
  378.   end;
  379.  
  380.   //Group of field links for a Schema
  381.   TSchemaLinks = class(TPersistent)
  382.   private
  383.     FOwner: TSchema;
  384.     FItems: TList;
  385.     function GetItem(Index: Integer): TSchemaLink;
  386.   public
  387.     constructor Create(AOwner: TSchema);
  388.     destructor Destroy; override;
  389.     function Owner: TSchema;
  390.     function Count: Integer;
  391.     procedure Clear;
  392.     procedure Delete(const Index: Integer);
  393.     function Add: TSchemaLink;
  394.     function IndexOf(Source: TSchemaLink): Integer;
  395.     property Items[Index: Integer]: TSchemaLink read GetItem; default;
  396.   end;
  397.  
  398.   //Mapping of a particular database to a particular entity
  399.   TSchema = class(TPersistent)
  400.   private
  401.     FOwner: TSchemas;
  402.     FLinks: TSchemaLinks;
  403.     FEntity: String;
  404.     FJoins: TStringList;
  405.     FMatch: TStringList;
  406.     FTableName: String;
  407.     FAlias: String;
  408.     FIDField: String;
  409.     FCaptionField: String;
  410.     FFilename: String;
  411.     FUpdateSQL: String;
  412.     FCheckSQL: String;
  413.     FInsertSQL: String;
  414.     procedure SetLinks(const Value: TSchemaLinks);
  415.     procedure SetEntity(const Value: String);
  416.     function GetJoins: TStrings;
  417.     procedure SetJoins(const Value: TStrings);
  418.     procedure Clear;
  419.     function GetMatch: TStrings;
  420.     procedure SetMatch(const Value: TStrings);
  421.     procedure SetAlias(const Value: String);
  422.     procedure SetCaptionField(const Value: String);
  423.     procedure SetIDField(const Value: String);
  424.     procedure SetTableName(const Value: String);
  425.     procedure SetFilename(const Value: String);
  426.     procedure SetCheckSQL(const Value: String);
  427.     procedure SetInsertSQL(const Value: String);
  428.     procedure SetUpdateSQL(const Value: String);
  429.   public
  430.     constructor Create(AOwner: TSchemas);
  431.     destructor Destroy; override;
  432.     function PathAndFilename: String;
  433.     procedure LoadFromFile;
  434.     procedure SaveToFile;
  435.   published
  436.     property Filename: String read FFilename write SetFilename;
  437.     property Links: TSchemaLinks read FLinks write SetLinks;
  438.     property Entity: String read FEntity write SetEntity;
  439.     property Joins: TStrings read GetJoins write SetJoins;
  440.     property Match: TStrings read GetMatch write SetMatch;
  441.     property TableName: String read FTableName write SetTableName;
  442.     property Alias: String read FAlias write SetAlias;
  443.     property IDField: String read FIDField write SetIDField;
  444.     property CaptionField: String read FCaptionField write SetCaptionField;
  445.     property InsertSQL: String read FInsertSQL write SetInsertSQL;
  446.     property UpdateSQL: String read FUpdateSQL write SetUpdateSQL;
  447.     property CheckSQL: String read FCheckSQL write SetCheckSQL;
  448.   end;
  449.  
  450.   //Group of schemas
  451.   TSchemas = class(TPersistent)
  452.   private
  453.     FOwner: TDataPump;
  454.     FItems: TList;
  455.   public
  456.     constructor Create(AOwner: TDataPump);
  457.     destructor Destroy; override;
  458.     function Owner: TDataPump;
  459.     function Count: Integer;
  460.     procedure Clear;
  461.     procedure Delete(const Index: Integer);
  462.     function Add: TSchema;
  463.     function IndexOf(Source: TSchema): Integer;
  464.   end;
  465.  
  466.   //Primary component - pumps data from source database to destination
  467.   TDataPump = class(TComponent)
  468.   private
  469.     FConnections: TConnections;
  470.     FEntities: TEntities;
  471.     FSchemas: TSchemas;
  472.     FRecords: TRecords;
  473.     FRootDir: String;
  474.     FActive: Boolean;
  475.     FDBSourceIndex: Integer;
  476.     FDBDestIndex: Integer;
  477.     FWhereSQL: TStringList;
  478.     FOnConnect: TConnectedEvent;
  479.     FEntityIndex: Integer;
  480.     procedure SetConnections(const Value: TConnections);
  481.     procedure SetEntities(const Value: TEntities);
  482.     procedure SetRootDir(const Value: String);
  483.     procedure SetActive(const Value: Boolean);
  484.     procedure SetDBDestIndex(const Value: Integer);
  485.     procedure SetDBSourceIndex(const Value: Integer);
  486.     function GetWhereSQL: TStrings;
  487.     procedure SetWhereSQL(const Value: TStrings);
  488.     function CanConnect: Boolean;
  489.     function Connected: Boolean;
  490.     function GetRootDir: String;
  491.     procedure SetEntityIndex(const Value: Integer);
  492.   public
  493.     constructor Create(AOwner: TComponent); override;
  494.     destructor Destroy; override;
  495.     procedure Connect;
  496.     procedure Disconnect;
  497.     function Source: TConnection;
  498.     function Dest: TConnection;
  499.     function Entity: TEntity;
  500.   published
  501.     property Active: Boolean read FActive write SetActive;
  502.     property Connections: TConnections read FConnections write SetConnections;
  503.     property Entities: TEntities read FEntities write SetEntities;
  504.     property RootDir: String read GetRootDir write SetRootDir;
  505.     property DBSourceIndex: Integer read FDBSourceIndex write SetDBSourceIndex;
  506.     property DBDestIndex: Integer read FDBDestIndex write SetDBDestIndex;
  507.     property EntityIndex: Integer read FEntityIndex write SetEntityIndex;
  508.     property WhereSQL: TStrings read GetWhereSQL write SetWhereSQL;
  509.     property OnConnect: TConnectedEvent read FOnConnect write FOnConnect;
  510.   end;
  511.  
  512. procedure ClipCopy(const S: String);
  513.  
  514. implementation
  515.  
  516. procedure ClipCopy(const S: String);
  517. begin
  518. {$IFDEF DEBUG}
  519.   Clipboard.AsText:= S;
  520. {$ENDIF}
  521. end;
  522.  
  523. procedure EraseINI(AIniFile: TIniFile);
  524. var
  525.   L: TStringList;
  526.   X: Integer;
  527. begin
  528.   if Assigned(AIniFile) then begin
  529.     L:= TStringList.Create;
  530.     try
  531.       AIniFile.ReadSections(L);
  532.       for X := 0 to L.Count - 1 do
  533.         AIniFile.EraseSection(L[X]);
  534.     finally
  535.       L.Free;
  536.     end;
  537.   end;
  538. end;
  539.  
  540. { TLookupItem }
  541.  
  542. constructor TLookupItem.Create(AOwner: TLookupTable);
  543. begin
  544.   FID:= 0;
  545.   FValue:= '';
  546. end;
  547.  
  548. destructor TLookupItem.Destroy;
  549. begin
  550.  
  551.   inherited;
  552. end;
  553.  
  554. procedure TLookupItem.SetID(const Value: Integer);
  555. begin
  556.   FID := Value;
  557. end;
  558.  
  559. procedure TLookupItem.SetValue(const Value: String);
  560. begin
  561.   FValue := Value;
  562. end;
  563.  
  564. { TLookupTable }
  565.  
  566. constructor TLookupTable.Create(AOwner: TLookupTables);
  567. begin
  568.   FOwner:= AOwner;
  569.   FItems:= TList.Create;
  570.   FLastID:= 0;
  571.   CaseSensitive:= False;
  572. end;
  573.  
  574. destructor TLookupTable.Destroy;
  575. begin
  576.   Clear;
  577.   FItems.Free;
  578.   inherited;
  579. end;
  580.  
  581. function TLookupTable.GetItem(const Index: Integer): TLookupItem;
  582. begin
  583.   Result:= TLookupItem(FItems[Index]);
  584. end;
  585.  
  586. procedure TLookupTable.Delete(const Index: Integer);
  587. begin
  588.   TLookupItem(FItems[Index]).Free;
  589.   FItems.Delete(Index);
  590. end;
  591.  
  592. procedure TLookupTable.Clear;
  593. begin
  594.   while Count > 0 do
  595.     Delete(0);
  596. end;
  597.  
  598. function TLookupTable.Count: Integer;
  599. begin
  600.   Result:= FItems.Count;
  601. end;
  602.  
  603. function TLookupTable.Lookup(const Value: String;
  604.   const CanCreate: Boolean): Integer;
  605. var
  606.   X: Integer;
  607.   I: TLookupItem;
  608.   V1, V2: String;
  609. begin
  610.   Result:= 0;
  611.   for X := 0 to FItems.Count - 1 do begin
  612.     I:= TLookupItem(FItems[X]);
  613.     if CaseSensitive then begin
  614.       V1:= LowerCase(Value);
  615.       V2:= LowerCase(I.FValue);
  616.     end else begin
  617.       V1:= Value;
  618.       V2:= I.FValue;
  619.     end;
  620.     if V1 = V2 then begin
  621.       Result:= X;
  622.       Break;
  623.     end;
  624.   end;
  625.   if Result = 0 then begin
  626.     if CanCreate then begin
  627.       Result:= Add(Value).ID;
  628.     end;
  629.   end;
  630. end;
  631.  
  632. procedure TLookupTable.SetCaseSensitive(const Value: Boolean);
  633. begin
  634.   FCaseSensitive := Value;
  635. end;
  636.  
  637. procedure TLookupTable.SetName(const Value: String);
  638. begin
  639.   FName := Value;
  640. end;
  641.  
  642. function TLookupTable.NewID: Integer;
  643. begin
  644.   Inc(FLastID);
  645.   Result:= FLastID;
  646. end;
  647.  
  648. function TLookupTable.Add: TLookupItem;
  649. begin
  650.   Result:= TLookupItem.Create(Self);
  651.   FItems.Add(Result);
  652.   Result.FID:= NewID;
  653. end;
  654.  
  655. function TLookupTable.Add(const Value: String): TLookupItem;
  656. begin
  657.  
  658. end;
  659.  
  660. { TLookupTables }
  661.  
  662. constructor TLookupTables.Create(AOwner: TRecords);
  663. begin
  664.   FOwner:= AOwner;
  665.   FItems:= TList.Create;
  666.  
  667. end;
  668.  
  669. destructor TLookupTables.Destroy;
  670. begin
  671.   Clear;
  672.   FItems.Free;
  673.   inherited;
  674. end;
  675.  
  676. function TLookupTables.GetItem(Index: Integer): TLookupTable;
  677. begin
  678.   Result:= TLookupTable(FItems[Index]);
  679. end;
  680.  
  681. procedure TLookupTables.Load;
  682. var
  683.   X, Y: Integer;
  684.   T: TLookupTable;
  685.   I: TLookupItem;
  686.   E: TEntity;
  687.   P: TEntityProp;
  688. begin
  689.   Clear;
  690.   E:= FOwner.FOwner.Entity;
  691.   for X := 0 to E.Count - 1 do begin
  692.     P:= E[X];
  693.     if P.LookupName <> '' then begin
  694.       T:= TableByName(P.LookupName);
  695.       if not Assigned(T) then begin
  696.         T:= Self.Add;
  697.         T.Name:= P.LookupName;
  698.       end;
  699.     end;
  700.   end;
  701.   for X := 0 to Count - 1 do begin
  702.     T:= Items[X];
  703.  
  704.   end;
  705. end;
  706.  
  707. function TLookupTables.Add: TLookupTable;
  708. begin
  709.   Result:= TLookupTable.Create(Self);
  710.   FItems.Add(Result);
  711. end;
  712.  
  713. procedure TLookupTables.Clear;
  714. begin
  715.   while Count > 0 do
  716.     Delete(0);
  717. end;
  718.  
  719. function TLookupTables.Count: Integer;
  720. begin
  721.   Result:= FItems.Count;
  722. end;
  723.  
  724. procedure TLookupTables.Delete(const Index: Integer);
  725. begin
  726.   TLookupTable(FItems[Index]).Free;
  727.   FItems.Delete(Index);
  728. end;
  729.  
  730. function TLookupTables.TableByName(const Name: String): TLookupTable;
  731. var
  732.   X: Integer;
  733.   T: TLookupTable;
  734. begin
  735.   Result:= nil;
  736.   for X := 0 to Count - 1 do begin
  737.     T:= TLookupTable(FItems[X]);
  738.     if UpperCase(T.Name) = UpperCase(Name) then begin
  739.       Result:= T;
  740.       Break;
  741.     end;
  742.   end;
  743. end;
  744.  
  745. { TRecordProp }
  746.  
  747. constructor TRecordProp.Create(AOwner: TRecord);
  748. begin
  749.  
  750. end;
  751.  
  752. destructor TRecordProp.Destroy;
  753. begin
  754.  
  755.   inherited;
  756. end;
  757.  
  758. function TRecordProp.Encode(const Value: String): String;
  759. begin
  760.   Result:= StringReplace(Value, '<', '&lt;', [rfReplaceAll,rfIgnoreCase]);
  761.   Result:= StringReplace(Result, '>', '&gt;', [rfReplaceAll,rfIgnoreCase]);
  762.   Result:= StringReplace(Result, '&', '&amp;', [rfReplaceAll,rfIgnoreCase]);
  763.   Result:= StringReplace(Result, '''', '&apos;', [rfReplaceAll,rfIgnoreCase]);
  764.   Result:= StringReplace(Result, '"', '&quot;', [rfReplaceAll,rfIgnoreCase]);
  765.   Result:= StringReplace(Result, #10, '&lb10;', [rfReplaceAll,rfIgnoreCase]);
  766.   Result:= StringReplace(Result, #13, '&lb13;', [rfReplaceAll,rfIgnoreCase]);
  767.   Result:= StringReplace(Result, '=', '&equ;', [rfReplaceAll,rfIgnoreCase]);
  768. end;
  769.  
  770. function TRecordProp.Decode(const Value: String): String;
  771. begin
  772.   Result:= StringReplace(Value, '&lt;', '<', [rfReplaceAll,rfIgnoreCase]);
  773.   Result:= StringReplace(Result, '&gt;', '>', [rfReplaceAll,rfIgnoreCase]);
  774.   Result:= StringReplace(Result, '&amp;', '&', [rfReplaceAll,rfIgnoreCase]);
  775.   Result:= StringReplace(Result, '&apos;', '''', [rfReplaceAll,rfIgnoreCase]);
  776.   Result:= StringReplace(Result, '&quot;', '"', [rfReplaceAll,rfIgnoreCase]);
  777.   Result:= StringReplace(Result, '&lb10;', #10, [rfReplaceAll,rfIgnoreCase]);
  778.   Result:= StringReplace(Result, '&lb13;', #13, [rfReplaceAll,rfIgnoreCase]);
  779.   Result:= StringReplace(Result, '&equ;', '=', [rfReplaceAll,rfIgnoreCase]);
  780. end;
  781.  
  782. procedure TRecordProp.SetLookup(const Value: String);
  783. begin
  784.   FLookup := Value;
  785. end;
  786.  
  787. function TRecordProp.GetAsString: String;
  788. var
  789.   T: TLookupTable;
  790.   I: Integer;
  791. begin
  792.   if FLookup <> '' then begin
  793.     T:= FOwner.FOwner.FLookup.TableByName(FLookup);
  794.  
  795.   end else begin
  796.     Result:= Decode(FValue);
  797.   end;
  798. end;
  799.  
  800. procedure TRecordProp.SetAsString(const Value: String);
  801. begin
  802.   FValue:= Encode(Value);
  803. end;
  804.  
  805. function TRecordProp.GetAsInteger: Integer;
  806. begin
  807.   Result:= StrToIntDef(AsString, 0);
  808. end;
  809.  
  810. procedure TRecordProp.SetAsInteger(const Value: Integer);
  811. begin
  812.   AsString:= IntToStr(Value);
  813. end;
  814.  
  815. function TRecordProp.GetAsFloat: Double;
  816. begin
  817.   Result:= StrToFloatDef(AsString, 0);
  818. end;
  819.  
  820. procedure TRecordProp.SetAsFloat(const Value: Double);
  821. begin
  822.   AsString:= FloatToStr(Value);
  823. end;
  824.  
  825. function TRecordProp.GetAsBoolean: Boolean;
  826. var
  827.   V: String;
  828. begin
  829.   V:= UpperCase(AsString);
  830.   Result:= (V = 'Y') or (V = 'YES') or (V = 'T') or (V = 'TRUE') or (V = '1');
  831. end;
  832.  
  833. procedure TRecordProp.SetAsBoolean(const Value: Boolean);
  834. begin
  835.   if Value then
  836.     AsString:= '1'
  837.   else
  838.     AsString:= '0';
  839. end;
  840.  
  841. procedure TRecordProp.SetName(const Value: String);
  842. begin
  843.   FName := Value;
  844. end;
  845.  
  846. procedure TRecordProp.SetPropType(const Value: TFieldLinkType);
  847. begin
  848.   FPropType := Value;
  849. end;
  850.  
  851. { TRecord }
  852.  
  853. constructor TRecord.Create(AOwner: TRecords);
  854. begin
  855.   FOwner:= AOwner;
  856.   FProps:= TList.Create;
  857.  
  858. end;
  859.  
  860. destructor TRecord.Destroy;
  861. begin
  862.   Clear;
  863.   FProps.Free;
  864.   inherited;
  865. end;
  866.  
  867. procedure TRecord.Assign(Source: TPersistent);
  868. begin
  869.   if Source is TRecord then begin
  870.     FProps.Assign((Source as TRecord).FProps);
  871.   end else begin
  872.     raise Exception.Create('Source must be of type "TRecord"');
  873.   end;
  874. end;
  875.  
  876. procedure TRecord.Clear;
  877. begin
  878.   while Count > 0 do
  879.     Delete(0);
  880. end;
  881.  
  882. function TRecord.Count: Integer;
  883. begin
  884.   Result:= FProps.Count;
  885. end;
  886.  
  887. procedure TRecord.Delete(const Index: Integer);
  888. begin
  889.   TRecordProp(FProps[Index]).Free;
  890.   FProps.Delete(Index);
  891. end;
  892.  
  893. function TRecord.GetName(const Index: Integer): String;
  894. begin
  895.   Result:= TRecordProp(FProps[Index]).Name;
  896. end;
  897.  
  898. function TRecord.IndexOfProp(const Name: String): Integer;
  899. var
  900.   X: Integer;
  901. begin
  902.   Result:= -1;
  903.   for X := 0 to Count - 1 do begin
  904.     if LowerCase(Name) = LowerCase(TRecordProp(FProps[X]).Name) then begin
  905.       Result:= X;
  906.       Break;
  907.     end;
  908.   end;
  909. end;
  910.  
  911. function TRecord.GetPropByName(const Name: String): TRecordProp;
  912. var
  913.   I: Integer;
  914. begin
  915.   I:= IndexOfProp(Name);
  916.   if I = -1 then begin
  917.     Result:= TRecordProp.Create(Self);
  918.     FProps.Add(Result);
  919.     Result.FName:= Name;
  920.   end else begin
  921.     Result:= TRecordProp(FProps[IndexOfProp(Name)]);
  922.   end;
  923. end;
  924.  
  925. procedure TRecord.SetCaption(const Value: String);
  926. begin
  927.   FCaption := Value;
  928. end;
  929.  
  930. procedure TRecord.SetID(const Value: Integer);
  931. begin
  932.   FID := Value;
  933. end;
  934.  
  935. function TRecord.ReadString(const Name: String): String;
  936. begin
  937.   Result:= PropByName[Name].AsString;
  938. end;
  939.  
  940. procedure TRecord.WriteString(const Name, Value: String);
  941. begin
  942.   PropByName[Name].AsString:= Value;
  943. end;
  944.  
  945. function TRecord.ReadInteger(const Name: String): Integer;
  946. begin
  947.   Result:= PropByName[Name].AsInteger;
  948. end;
  949.  
  950. procedure TRecord.WriteInteger(const Name: String; const Value: Integer);
  951. begin
  952.   PropByName[Name].AsInteger:= Value;
  953. end;
  954.  
  955. function TRecord.ReadFloat(const Name: String): Double;
  956. begin
  957.   Result:= PropByName[Name].AsFloat;
  958. end;
  959.  
  960. procedure TRecord.WriteFloat(const Name: String; const Value: Double);
  961. begin
  962.   PropByName[Name].AsFloat:= Value;
  963. end;
  964.  
  965. function TRecord.ReadBoolean(const Name: String): Boolean;
  966. begin
  967.   Result:= PropByName[Name].AsBoolean;
  968. end;
  969.  
  970. procedure TRecord.WriteBoolean(const Name: String; const Value: Boolean);
  971. begin
  972.   PropByName[Name].AsBoolean:= Value;
  973. end;
  974.  
  975. { TRecords }
  976.  
  977. constructor TRecords.Create(AOwner: TDataPump);
  978. begin
  979.   FOwner:= AOwner;
  980.   FLookup:= TLookupTables.Create(Self);
  981.   FItems:= TList.Create;
  982. end;
  983.  
  984. destructor TRecords.Destroy;
  985. begin
  986.   Clear;
  987.   FItems.Free;
  988.   FLookup.Free;
  989.   inherited;
  990. end;
  991.  
  992. function TRecords.GetRecord(Index: Integer): TRecord;
  993. begin
  994.   Result:= TRecord(FItems[Index]);
  995. end;
  996.  
  997. function TRecords.Add: TRecord;
  998. begin
  999.   Result:= TRecord.Create(Self);
  1000.   FItems.Add(Result);
  1001. end;
  1002.  
  1003. procedure TRecords.Clear;
  1004. begin
  1005.   while Count > 0 do
  1006.     Delete(0);
  1007. end;
  1008.  
  1009. function TRecords.Count: Integer;
  1010. begin
  1011.   Result:= FItems.Count;
  1012. end;
  1013.  
  1014. procedure TRecords.Delete(const Index: Integer);
  1015. begin
  1016.   TRecord(FItems[Index]).Free;
  1017.   FItems.Delete(Index);
  1018. end;
  1019.  
  1020. function TRecords.IndexOf(const Caption: String): Integer;
  1021. var
  1022.   X: Integer;
  1023. begin
  1024.   Result:= -1;
  1025.   for X := 0 to FItems.Count - 1 do begin
  1026.     if LowerCase(TRecord(FItems[X]).Caption) = LowerCase(Caption) then begin
  1027.       Result:= X;
  1028.       Break;
  1029.     end;
  1030.   end;
  1031. end;
  1032.  
  1033. function TRecords.IndexOf(const ID: Integer): Integer;
  1034. var
  1035.   X: Integer;
  1036. begin
  1037.   Result:= -1;
  1038.   for X := 0 to FItems.Count - 1 do begin
  1039.     if TRecord(FItems[X]).ID = ID then begin
  1040.       Result:= X;
  1041.       Break;
  1042.     end;
  1043.   end;
  1044. end;
  1045.  
  1046. function TRecords.IndexOf(ARecord: TRecord): Integer;
  1047. begin
  1048.   Result:= FItems.IndexOf(ARecord);
  1049. end;
  1050.  
  1051. { TConnection }
  1052.  
  1053. function TConnection.Connected: Boolean;
  1054. begin
  1055.   Result:= FConnection.Connected;
  1056. end;
  1057.  
  1058. constructor TConnection.Create(AOwner: TConnections);
  1059. begin
  1060.   FOwner:= AOwner;
  1061.   FConnection:= TADOConnection.Create(nil);
  1062.   FConnection.LoginPrompt:= False;
  1063.   FConnection.AfterConnect:= DBConnect;
  1064.   FConnection.AfterDisconnect:= DBDisconnect;
  1065. end;
  1066.  
  1067. destructor TConnection.Destroy;
  1068. begin
  1069.   Disconnect;
  1070.   FConnection.Free;
  1071.   inherited;
  1072. end;
  1073.  
  1074. procedure TConnection.DBConnect(Sender: TObject);
  1075. begin
  1076.   DoOnConnect(True);
  1077. end;
  1078.  
  1079. procedure TConnection.DBDisconnect(Sender: TObject);
  1080. begin
  1081.   DoOnConnect(False);
  1082. end;
  1083.  
  1084. procedure TConnection.Connect;
  1085. begin
  1086.   if not FActive then begin
  1087.     FActive:= True;
  1088.     FConnection.ConnectionString:= FConnectionString;
  1089.     FConnection.Connected:= True;
  1090.   end;
  1091. end;
  1092.  
  1093. procedure TConnection.Disconnect;
  1094. begin
  1095.   if FActive then begin
  1096.     FActive:= False;
  1097.     FConnection.Connected:= False;
  1098.   end;
  1099. end;
  1100.  
  1101. procedure TConnection.DoOnConnect(const Connected: Boolean);
  1102. begin
  1103.   if assigned(FOnConnect) then
  1104.     FOnConnect(Self, Perspective, Connected);
  1105. end;
  1106.  
  1107. function TConnection.ExecSQL(const SQL: String): Integer;
  1108. var
  1109.   Q: TADOQuery;
  1110. begin
  1111.   Q:= NewQuery;
  1112.   try
  1113.     Q.SQL.Text:= SQL;
  1114.     Result:= Q.ExecSQL;
  1115.   finally
  1116.     Q.Free;
  1117.   end;
  1118. end;
  1119.  
  1120. function TConnection.NewQuery: TADOQuery;
  1121. begin
  1122.   Result:= TADOQuery.Create(nil);
  1123.   Result.Connection:= FConnection;
  1124. end;
  1125.  
  1126. function TConnection.Perspective: TPerspective;
  1127. begin
  1128.   if (Self = FOwner.FOwner.Dest) then
  1129.     Result:= psSource
  1130.   else if (Self = FOwner.FOwner.Source) then
  1131.     Result:= psDestination
  1132.   else
  1133.     Result:= psNone;
  1134. end;
  1135.  
  1136. procedure TConnection.SetActive(const Value: Boolean);
  1137. begin
  1138.   if Value then begin
  1139.     Connect;
  1140.   end else begin
  1141.     Disconnect;
  1142.   end;
  1143. end;
  1144.  
  1145. procedure TConnection.SetCaption(const Value: String);
  1146. begin
  1147.   FCaption := Value;
  1148. end;
  1149.  
  1150. procedure TConnection.SetConnectionString(const Value: String);
  1151. begin
  1152.   FConnectionString:= Value;
  1153. end;
  1154.  
  1155. { TConnections }
  1156.  
  1157. constructor TConnections.Create(AOwner: TDataPump);
  1158. begin
  1159.   FOwner:= AOwner;
  1160.   FItems:= TList.Create;
  1161. end;
  1162.  
  1163. destructor TConnections.Destroy;
  1164. begin
  1165.   Clear;
  1166.   FItems.Free;
  1167.   inherited;
  1168. end;
  1169.  
  1170. function TConnections.GetConnection(Index: Integer): TConnection;
  1171. begin
  1172.   Result:= TConnection(FItems[Index]);
  1173. end;
  1174.  
  1175. procedure TConnections.DBConnection(Sender: TObject;
  1176.   const Perspective: TPerspective; const Connected: Boolean);
  1177. begin
  1178.   if assigned(FOwner.OnConnect) then
  1179.     FOwner.OnConnect(FOwner, Perspective, Connected);
  1180. end;
  1181.  
  1182. function TConnections.Add: TConnection;
  1183. begin
  1184.   Result:= TConnection.Create(Self);
  1185.   Result.OnConnect:= DBConnection;
  1186.   FItems.Add(Result);
  1187. end;
  1188.  
  1189. procedure TConnections.Clear;
  1190. begin
  1191.   while Count > 0 do
  1192.     Delete(0);
  1193. end;
  1194.  
  1195. function TConnections.Count: Integer;
  1196. begin
  1197.   Result:= FItems.Count;
  1198. end;
  1199.  
  1200. procedure TConnections.Delete(const Index: Integer);
  1201. begin
  1202.   TConnection(FItems[Index]).Free;
  1203.   FItems.Delete(Index);
  1204. end;
  1205.  
  1206. function TConnections.IndexOf(Source: TConnection): Integer;
  1207. begin
  1208.   Result:= FItems.IndexOf(Source);
  1209. end;
  1210.  
  1211. function TConnections.Owner: TDataPump;
  1212. begin
  1213.   Result:= FOwner;
  1214. end;
  1215.  
  1216. { TEntityProp }
  1217.  
  1218. constructor TEntityProp.Create(AOwner: TEntity);
  1219. begin
  1220.   FOwner:= AOwner;
  1221.   FName:= '';
  1222.   FSize:= 0;
  1223. end;
  1224.  
  1225. destructor TEntityProp.Destroy;
  1226. begin
  1227.  
  1228.   inherited;
  1229. end;
  1230.  
  1231. procedure TEntityProp.SetIsIdentity(const Value: Boolean);
  1232. begin
  1233.   FIsIdentity := Value;
  1234. end;
  1235.  
  1236. procedure TEntityProp.SetIsPrimaryKey(const Value: Boolean);
  1237. begin
  1238.   FIsPrimaryKey := Value;
  1239. end;
  1240.  
  1241. procedure TEntityProp.SetIsReadOnly(const Value: Boolean);
  1242. begin
  1243.   FIsReadOnly := Value;
  1244. end;
  1245.  
  1246. procedure TEntityProp.SetLinkType(const Value: TFieldLinkType);
  1247. begin
  1248.   FLinkType := Value;
  1249. end;
  1250.  
  1251. procedure TEntityProp.SetLookupName(const Value: String);
  1252. begin
  1253.   FLookupName := Value;
  1254. end;
  1255.  
  1256. procedure TEntityProp.SetName(const Value: String);
  1257. begin
  1258.   FName := Value;
  1259. end;
  1260.  
  1261. procedure TEntityProp.SetSize(const Value: Integer);
  1262. begin
  1263.   FSize := Value;
  1264. end;
  1265.  
  1266. { TEntity }
  1267.  
  1268. constructor TEntity.Create(AOwner: TEntities);
  1269. begin
  1270.   FOwner:= AOwner;
  1271.   FProps:= TList.Create;
  1272. end;
  1273.  
  1274. procedure TEntity.Delete(const Index: Integer);
  1275. begin
  1276.   TEntityProp(FProps[Index]).Free;
  1277.   FProps.Delete(Index);
  1278. end;
  1279.  
  1280. destructor TEntity.Destroy;
  1281. begin
  1282.   Clear;
  1283.   FProps.Free;
  1284.   inherited;
  1285. end;
  1286.  
  1287. procedure TEntity.Clear;
  1288. begin
  1289.   while Count > 0 do
  1290.     Delete(0);
  1291. end;
  1292.  
  1293. function TEntity.Count: Integer;
  1294. begin
  1295.   Result:= FProps.Count;
  1296. end;
  1297.  
  1298. function TEntity.GetProp(Index: Integer): TEntityProp;
  1299. begin
  1300.   Result:= TEntityProp(FProps[Index]);
  1301. end;
  1302.  
  1303. function TEntity.PathAndFilename: String;
  1304. begin
  1305.   Result:= FOwner.FOwner.RootDir + 'Entities\'+FFilename;
  1306. end;
  1307.  
  1308. procedure TEntity.LoadFromFile;
  1309. var
  1310.   I: TIniFile;
  1311.   L: TStringList;
  1312.   X: Integer;
  1313.   SN: String;
  1314.   P: TEntityProp;
  1315.   T: String; //Temp
  1316.   function IsPropSection(const SectionName: String): Boolean;
  1317.   var
  1318.     S: String;
  1319.   begin
  1320.     S:= LowerCase(SectionName);
  1321.     Result:= (S <> 'table:settings');
  1322.   end;
  1323. begin
  1324.   I:= TIniFile.Create(PathAndFilename);
  1325.   try
  1326.     FName:=         I.ReadString('table', 'name', '');
  1327.     FCaption:=      I.ReadString('table', 'caption', '');
  1328.     FDescription:=  I.ReadString('table', 'description', '');
  1329.     L:= TStringList.Create;
  1330.     try
  1331.       I.ReadSections(L);
  1332.       for X := 0 to L.Count - 1 do begin
  1333.         SN:= LowerCase(L[X]);
  1334.         if IsPropSection(SN) then begin
  1335.           P:= TEntityProp.Create(Self);
  1336.           P.FName:= SN;
  1337.           P.FSize:= I.ReadInteger(SN, 'size', 0);
  1338.           P.FIsPrimaryKey:= I.ReadBool(SN, 'pk', False);
  1339.           P.FIsIdentity:= I.ReadBool(SN, 'id', False);
  1340.           P.FIsReadOnly:= I.ReadBool(SN, 'ro', False);
  1341.           T:= LowerCase(I.ReadString(SN, 'type', 'string'));
  1342.           if T = 'string' then begin
  1343.             P.FLinkType:= flText;
  1344.           end else
  1345.           if T = 'integer' then begin
  1346.             P.FLinkType:= flInt;
  1347.           end else
  1348.           if T = 'float' then begin
  1349.             P.FLinkType:= flFloat;
  1350.           end else
  1351.           if T = 'boolean' then begin
  1352.             P.FLinkType:= flBool;
  1353.           end else begin
  1354.             P.FLinkType:= flText;
  1355.           end;
  1356.  
  1357.         end;
  1358.       end;
  1359.     finally
  1360.       L.Free;
  1361.     end;
  1362.   finally
  1363.     I.Free;
  1364.   end;
  1365. end;
  1366.  
  1367. procedure TEntity.SaveToFile;
  1368. var
  1369.   I: TIniFile;
  1370. begin
  1371.   I:= TIniFile.Create(PathAndFilename);
  1372.   try
  1373.     I.WriteString('table', 'name', FName);
  1374.     I.WriteString('table', 'caption', FCaption);
  1375.     I.WriteString('table', 'description', FDescription);
  1376.  
  1377.   finally
  1378.     I.Free;
  1379.   end;
  1380. end;
  1381.  
  1382. procedure TEntity.SetCaption(const Value: String);
  1383. begin
  1384.   FCaption := Value;
  1385. end;
  1386.  
  1387. procedure TEntity.SetDescription(const Value: String);
  1388. begin
  1389.   FDescription := Value;
  1390. end;
  1391.  
  1392. procedure TEntity.SetFilename(const Value: String);
  1393. begin
  1394.   FFilename := Value;
  1395. end;
  1396.  
  1397. procedure TEntity.SetName(const Value: String);
  1398. begin
  1399.   FName := Value;
  1400. end;
  1401.  
  1402. { TEntities }
  1403.  
  1404. constructor TEntities.Create(AOwner: TDataPump);
  1405. begin
  1406.   FOwner:= AOwner;
  1407.   FItems:= TList.Create;
  1408. end;
  1409.  
  1410. destructor TEntities.Destroy;
  1411. begin
  1412.   Clear;
  1413.   FItems.Free;
  1414.   inherited;
  1415. end;
  1416.  
  1417. function TEntities.GetItem(Index: Integer): TEntity;
  1418. begin
  1419.   Result:= TEntity(FItems[Index]);
  1420. end;
  1421.  
  1422. function TEntities.Add: TEntity;
  1423. begin
  1424.   Result:= TEntity.Create(Self);
  1425.   FItems.Add(Result);
  1426. end;
  1427.  
  1428. procedure TEntities.Clear;
  1429. begin
  1430.   while Count > 0 do
  1431.     Delete(0);
  1432. end;
  1433.  
  1434. function TEntities.Count: Integer;
  1435. begin
  1436.   Result:= FItems.Count;
  1437. end;
  1438.  
  1439. procedure TEntities.Delete(const Index: Integer);
  1440. begin
  1441.   TEntity(FItems[Index]).Free;
  1442.   FItems.Delete(Index);
  1443. end;
  1444.  
  1445. function TEntities.IndexOf(Source: TEntity): Integer;
  1446. begin
  1447.   Result:= FItems.IndexOf(Source);
  1448. end;
  1449.  
  1450. function TEntities.Owner: TDataPump;
  1451. begin
  1452.   Result:= FOwner;
  1453. end;
  1454.  
  1455. { TSchemaLink }
  1456.  
  1457. constructor TSchemaLink.Create(AOwner: TSchemaLinks);
  1458. begin
  1459.   FOwner:= AOwner;
  1460. end;
  1461.  
  1462. destructor TSchemaLink.Destroy;
  1463. begin
  1464.  
  1465.   inherited;
  1466. end;
  1467.  
  1468. procedure TSchemaLink.SetAlias(const Value: String);
  1469. begin
  1470.   FAlias := Value;
  1471. end;
  1472.  
  1473. procedure TSchemaLink.SetFieldName(const Value: String);
  1474. begin
  1475.   FFieldName := Value;
  1476. end;
  1477.  
  1478. procedure TSchemaLink.SetIsIdentity(const Value: Boolean);
  1479. begin
  1480.   FIsIdentity := Value;
  1481. end;
  1482.  
  1483. procedure TSchemaLink.SetIsPrimaryKey(const Value: Boolean);
  1484. begin
  1485.   FIsPrimaryKey := Value;
  1486. end;
  1487.  
  1488. procedure TSchemaLink.SetIsReadOnly(const Value: Boolean);
  1489. begin
  1490.   FIsReadOnly := Value;
  1491. end;
  1492.  
  1493. procedure TSchemaLink.SetLookup(const Value: Boolean);
  1494. begin
  1495.   FLookup := Value;
  1496. end;
  1497.  
  1498. procedure TSchemaLink.SetLookupCaption(const Value: String);
  1499. begin
  1500.   FLookupCaption := Value;
  1501. end;
  1502.  
  1503. procedure TSchemaLink.SetLookupID(const Value: String);
  1504. begin
  1505.   FLookupID := Value;
  1506. end;
  1507.  
  1508. procedure TSchemaLink.SetLookupTable(const Value: String);
  1509. begin
  1510.   FLookupTable := Value;
  1511. end;
  1512.  
  1513. procedure TSchemaLink.SetName(const Value: String);
  1514. begin
  1515.   FName := Value;
  1516. end;
  1517.  
  1518. { TSchemaLinks }
  1519.  
  1520. constructor TSchemaLinks.Create(AOwner: TSchema);
  1521. begin
  1522.   FOwner:= AOwner;
  1523.   FItems:= TList.Create;
  1524. end;
  1525.  
  1526. destructor TSchemaLinks.Destroy;
  1527. begin
  1528.   Clear;
  1529.   FItems.Free;
  1530.   inherited;
  1531. end;
  1532.  
  1533. function TSchemaLinks.GetItem(Index: Integer): TSchemaLink;
  1534. begin
  1535.   Result:= TSchemaLink(FItems[Index]);
  1536. end;
  1537.  
  1538. function TSchemaLinks.Add: TSchemaLink;
  1539. begin
  1540.   Result:= TSchemaLink.Create(Self);
  1541.   FItems.Add(Result);
  1542. end;
  1543.  
  1544. procedure TSchemaLinks.Clear;
  1545. begin
  1546.   while Count > 0 do
  1547.     Delete(0);
  1548. end;
  1549.  
  1550. function TSchemaLinks.Count: Integer;
  1551. begin
  1552.   Result:= FItems.Count;
  1553. end;
  1554.  
  1555. procedure TSchemaLinks.Delete(const Index: Integer);
  1556. begin
  1557.   TSchemaLink(FItems[Index]).Free;
  1558.   FItems.Delete(Index);
  1559. end;
  1560.  
  1561. function TSchemaLinks.IndexOf(Source: TSchemaLink): Integer;
  1562. begin
  1563.   Result:= FItems.IndexOf(Source);
  1564. end;
  1565.  
  1566. function TSchemaLinks.Owner: TSchema;
  1567. begin
  1568.   Result:= FOwner;
  1569. end;
  1570.  
  1571. { TSchema }
  1572.  
  1573. constructor TSchema.Create(AOwner: TSchemas);
  1574. begin
  1575.   FOwner:= AOwner;
  1576.   FLinks:= TSchemaLinks.Create(Self);
  1577.   FJoins:= TStringList.Create;
  1578.   FMatch:= TStringList.Create;
  1579. end;
  1580.  
  1581. destructor TSchema.Destroy;
  1582. begin
  1583.   FMatch.Free;
  1584.   FJoins.Free;
  1585.   Clear;
  1586.   FLinks.Free;
  1587.   inherited;
  1588. end;
  1589.  
  1590. procedure TSchema.Clear;
  1591. begin
  1592.   FJoins.Clear;
  1593.   FLinks.Clear;
  1594. end;
  1595.  
  1596. function TSchema.GetJoins: TStrings;
  1597. begin
  1598.   Result:= TStrings(FJoins);
  1599. end;
  1600.  
  1601. function TSchema.GetMatch: TStrings;
  1602. begin
  1603.   Result:= TStrings(FMatch);
  1604. end;
  1605.  
  1606. function TSchema.PathAndFilename: String;
  1607. begin
  1608.   Result:= IncludeTrailingBackslash(FOwner.FOwner.FRootDir)+FFilename;
  1609. end;
  1610.  
  1611. procedure TSchema.LoadFromFile;
  1612. var
  1613.   I: TIniFile;
  1614.   L: TStringList;
  1615.   X: Integer;
  1616.   Link: TSchemaLink;
  1617.   SN: String;
  1618.   function IsFieldSection(const FieldName: String): Boolean;
  1619.   begin
  1620.     Result:= not ((Pos('<', FieldName) = 1) and (Pos('>', FieldName) > 1))
  1621.   end;
  1622. begin
  1623.   Clear;
  1624.   I:= TIniFile.Create(PathAndFilename);
  1625.   try
  1626.     L:= TStringList.Create;
  1627.     try
  1628.       FTableName:= I.ReadString('<table>', 'tbl', '');
  1629.       FAlias:= I.ReadString('<table>', 'alias', '');
  1630.       FIDField:= I.ReadString('<table>', 'id', '');
  1631.       FCaptionField:= I.ReadString('<table>', 'caption', '');
  1632.       I.ReadSection('<match>', L);
  1633.       for X := 0 to L.Count - 1 do
  1634.         FMatch.Append(L.Values[L.Names[X]]);
  1635.       I.ReadSection('<joins>', L);
  1636.       for X := 0 to L.Count - 1 do
  1637.         FJoins.Append(L.Values[L.Names[X]]);
  1638.       I.ReadSections(L);
  1639.       for X := 0 to L.Count - 1 do begin
  1640.         SN:= L[X];
  1641.         if IsFieldSection(SN) then begin
  1642.           Link:= FLinks.Add;
  1643.           Link.FFieldName:= I.ReadString(SN, 'fld', '');
  1644.           Link.FName:= I.ReadString(SN, 'nam', '');
  1645.           Link.FAlias:= I.ReadString(SN, 'alias', '');
  1646.           Link.FLookupTable:= I.ReadString(SN, 'tbl', '');
  1647.           Link.FLookup:= Link.FLookupTable <> '';
  1648.           Link.FLookupID:= I.ReadString(SN, 'lnk', '');
  1649.           Link.FLookupCaption:= I.ReadString(SN, 'val', '');
  1650.           Link.FIsPrimaryKey:= I.ReadBool(SN, 'pk', False);
  1651.           Link.FIsIdentity:= I.ReadBool(SN, 'id', False);
  1652.           Link.FIsReadOnly:= I.ReadBool(SN, 'ro', Link.FIsIdentity);
  1653.         end;
  1654.       end;
  1655.     finally
  1656.       L.Free;
  1657.     end;
  1658.   finally
  1659.     I.Free;
  1660.   end;
  1661. end;
  1662.  
  1663. procedure TSchema.SaveToFile;
  1664. var
  1665.   I: TIniFile;
  1666.   X: Integer;
  1667.   Link: TSchemaLink;
  1668. begin
  1669.   I:= TIniFile.Create(PathAndFilename);
  1670.   try
  1671.     EraseINI(I);
  1672.     I.WriteString('<table>', 'tbl', FTableName);
  1673.     I.WriteString('<table>', 'alias', FAlias);
  1674.     I.WriteString('<table>', 'id', FIDField);
  1675.     I.WriteString('<table>', 'caption', FCaptionField);
  1676.     for X := 0 to FMatch.Count - 1 do
  1677.       I.WriteString('<match>', IntToStr(X), FMatch[X]);
  1678.     for X := 0 to FJoins.Count - 1 do
  1679.       I.WriteString('<joins>', IntToStr(X), FJoins[X]);
  1680.     for X := 0 to FLinks.Count - 1 do begin
  1681.       Link:= Links[X];
  1682.       I.WriteString(IntToStr(X), 'fld', Link.FFieldName);
  1683.       I.WriteString(IntToStr(X), 'nam', Link.FName);
  1684.       I.WriteString(IntToStr(X), 'alias', Link.FAlias);
  1685.       //I.WriteString(IntToStr(X), 'typ', Link);
  1686.  
  1687.     end;
  1688.   finally
  1689.     I.Free;
  1690.   end;
  1691. end;
  1692.  
  1693. procedure TSchema.SetAlias(const Value: String);
  1694. begin
  1695.   FAlias := Value;
  1696. end;
  1697.  
  1698. procedure TSchema.SetCaptionField(const Value: String);
  1699. begin
  1700.   FCaptionField := Value;
  1701. end;
  1702.  
  1703. procedure TSchema.SetCheckSQL(const Value: String);
  1704. begin
  1705.   FCheckSQL := Value;
  1706. end;
  1707.  
  1708. procedure TSchema.SetEntity(const Value: String);
  1709. begin
  1710.   FEntity := Value;
  1711. end;
  1712.  
  1713. procedure TSchema.SetFilename(const Value: String);
  1714. begin
  1715.   FFilename := Value;
  1716. end;
  1717.  
  1718. procedure TSchema.SetIDField(const Value: String);
  1719. begin
  1720.   FIDField := Value;
  1721. end;
  1722.  
  1723. procedure TSchema.SetInsertSQL(const Value: String);
  1724. begin
  1725.   FInsertSQL := Value;
  1726. end;
  1727.  
  1728. procedure TSchema.SetJoins(const Value: TStrings);
  1729. begin
  1730.   FJoins.Assign(Value);
  1731. end;
  1732.  
  1733. procedure TSchema.SetLinks(const Value: TSchemaLinks);
  1734. begin
  1735.   FLinks.Assign(Value);
  1736. end;
  1737.  
  1738. procedure TSchema.SetMatch(const Value: TStrings);
  1739. begin
  1740.   FMatch.Assign(Value);
  1741. end;
  1742.  
  1743. procedure TSchema.SetTableName(const Value: String);
  1744. begin
  1745.   FTableName := Value;
  1746. end;
  1747.  
  1748. procedure TSchema.SetUpdateSQL(const Value: String);
  1749. begin
  1750.   FUpdateSQL := Value;
  1751. end;
  1752.  
  1753. { TSchemas }
  1754.  
  1755. constructor TSchemas.Create(AOwner: TDataPump);
  1756. begin
  1757.   FOwner:= AOwner;
  1758.   FItems:= TList.Create;
  1759. end;
  1760.  
  1761. destructor TSchemas.Destroy;
  1762. begin
  1763.   Clear;
  1764.   FItems.Free;
  1765.   inherited;
  1766. end;
  1767.  
  1768. function TSchemas.Add: TSchema;
  1769. begin
  1770.   Result:= TSchema.Create(Self);
  1771.   FItems.Add(Result);
  1772. end;
  1773.  
  1774. procedure TSchemas.Clear;
  1775. begin
  1776.   while Count > 0 do
  1777.     Delete(0);
  1778. end;
  1779.  
  1780. function TSchemas.Count: Integer;
  1781. begin
  1782.   Result:= FItems.Count;
  1783. end;
  1784.  
  1785. procedure TSchemas.Delete(const Index: Integer);
  1786. begin
  1787.   TSchema(FItems[Index]).Free;
  1788.   FItems.Delete(Index);
  1789. end;
  1790.  
  1791. function TSchemas.IndexOf(Source: TSchema): Integer;
  1792. begin
  1793.   Result:= FItems.IndexOf(Source);
  1794. end;
  1795.  
  1796. function TSchemas.Owner: TDataPump;
  1797. begin
  1798.   Result:= FOwner;
  1799. end;
  1800.  
  1801. { TDataPump }
  1802.  
  1803. constructor TDataPump.Create(AOwner: TComponent);
  1804. begin
  1805.   inherited;
  1806.   FConnections:= TConnections.Create(Self);
  1807.   FEntities:= TEntities.Create(Self);
  1808.   FSchemas:= TSchemas.Create(Self);
  1809.   FRecords:= TRecords.Create(Self);
  1810.   FWhereSQL:= TStringList.Create;
  1811. end;
  1812.  
  1813. destructor TDataPump.Destroy;
  1814. begin
  1815.   FWhereSQL.Free;
  1816.   FRecords.Free;
  1817.   FSchemas.Free;
  1818.   FEntities.Free;
  1819.   FConnections.Free;
  1820.   RootDir:= ExtractFilePath(Application.ExeName);
  1821.   inherited;
  1822. end;
  1823.  
  1824. function TDataPump.CanConnect: Boolean;
  1825. begin
  1826.   Result:=
  1827.     (FConnections.Count > 1) and
  1828.     (FDBSourceIndex >= 0) and
  1829.     (FDBDestIndex >= 0) and
  1830.     (FDBSourceIndex < FConnections.Count) and
  1831.     (FDBDestIndex < FConnections.Count) and
  1832.     (FDBSourceIndex <> FDBDestIndex);
  1833. end;
  1834.  
  1835. function TDataPump.Connected: Boolean;
  1836. begin
  1837.   Result:= (CanConnect);
  1838.   if Result then
  1839.     Result:= (Source.Connected) and (Dest.Connected);
  1840. end;
  1841.  
  1842. procedure TDataPump.Connect;
  1843. begin
  1844.   if not FActive then begin
  1845.     if CanConnect then begin
  1846.       FActive:= True;
  1847.       try
  1848.         Source.Connect;
  1849.         Dest.Connect;
  1850.       except
  1851.         on e: exception do begin
  1852.           Source.Disconnect;
  1853.           Dest.Disconnect;
  1854.         end;
  1855.       end;
  1856.     end else begin
  1857.       raise Exception.Create('Unable to connect with current configuration.');
  1858.     end;
  1859.   end;
  1860. end;
  1861.  
  1862. procedure TDataPump.Disconnect;
  1863. begin
  1864.   if FActive then begin
  1865.     if CanConnect then begin
  1866.       FActive:= False;
  1867.       Source.Disconnect;
  1868.       Dest.Disconnect;
  1869.     end;
  1870.   end;
  1871. end;
  1872.  
  1873. function TDataPump.Entity: TEntity;
  1874. begin
  1875.   Result:= FEntities[FEntityIndex];
  1876. end;
  1877.  
  1878. function TDataPump.GetRootDir: String;
  1879. begin
  1880.   Result:= IncludeTrailingBackslash(FRootDir);
  1881. end;
  1882.  
  1883. function TDataPump.GetWhereSQL: TStrings;
  1884. begin
  1885.   Result:= TStrings(FWhereSQL);
  1886. end;
  1887.  
  1888. procedure TDataPump.SetActive(const Value: Boolean);
  1889. begin
  1890.   if Value then begin
  1891.     Connect;
  1892.   end else begin
  1893.     Disconnect;
  1894.   end;
  1895. end;
  1896.  
  1897. procedure TDataPump.SetConnections(const Value: TConnections);
  1898. begin
  1899.   FConnections.Assign(Value);
  1900. end;
  1901.  
  1902. procedure TDataPump.SetDBDestIndex(const Value: Integer);
  1903. begin
  1904.   FDBDestIndex := Value;
  1905. end;
  1906.  
  1907. procedure TDataPump.SetDBSourceIndex(const Value: Integer);
  1908. begin
  1909.   FDBSourceIndex := Value;
  1910. end;
  1911.  
  1912. procedure TDataPump.SetEntities(const Value: TEntities);
  1913. begin
  1914.   FEntities.Assign(Value);
  1915. end;
  1916.  
  1917. procedure TDataPump.SetEntityIndex(const Value: Integer);
  1918. begin
  1919.   FEntityIndex := Value;
  1920. end;
  1921.  
  1922. procedure TDataPump.SetRootDir(const Value: String);
  1923. begin
  1924.   FRootDir := IncludeTrailingBackslash(Value);
  1925. end;
  1926.  
  1927. procedure TDataPump.SetWhereSQL(const Value: TStrings);
  1928. begin
  1929.   FWhereSQL.Assign(Value);
  1930. end;
  1931.  
  1932. function TDataPump.Source: TConnection;
  1933. begin
  1934.   Result:= FConnections[FDBSourceIndex];
  1935. end;
  1936.  
  1937. function TDataPump.Dest: TConnection;
  1938. begin
  1939.   Result:= FConnections[FDBDestIndex];
  1940. end;
  1941.  
  1942. end.
Advertisement
Add Comment
Please, Sign In to add comment