summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-db/src/datadict/fpddregstd.pp
blob: 98505ddb5390928fa626ecd444c48bb57060a66c (plain)
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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2007 by Michael Van Canneyt, member of the
    Free Pascal development team

    Standard Data Dictionary Engines registration.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program 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.

 **********************************************************************}
{
  This unit has a routine and a component to register standard distributed
  Data dictionary engines in an application. The Component version is meant for
  use in Lazarus: Drop it on a form, set the engines you want to see
  registered, and set active to true. When the form is created a run-time,
  the selected engines will be registered.

  The simple call takes an optional single argument, a set which tells
  the call which engines to register. If none is specified, all formats
  are registered.

}
unit fpddregstd;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpdatadict;

Type
  TDataDictEngine = (teDBF,teFirebird,teOracle,teMySQL40,teMySQL41,teMySQL50,
                       tePostgreSQL,teSQLite3,teODBC);
  TDataDictEngines = set of TDataDictEngine;

Const
  AllStdDDEngines = [teDBF,teFirebird,teOracle,teMySQL40,teMySQL41,teMySQL50,
                     tePostgreSQL,teSQLite3,teODBC];
                     
Type

  { TStandardDDEngines }

  TStandardDDEngines = Class(TComponent)
  Private
    FActive: Boolean;
    FRegistered,
    FEngines: TDataDictEngines;
    procedure SetActive(const AValue: Boolean);
    Procedure DoRegister;
    Procedure DoUnregister;
  Public
    Constructor Create(AOwner : TComponent); override;
    Procedure Loaded; override;
  Published
    Property Active : Boolean Read FActive Write SetActive;
    Property Engines : TDataDictEngines Read FEngines Write FEngines Default AllStdDDEngines;
  end;

Function RegisterStdDDEngines(Engines : TDataDictEngines) : TDataDictEngines; overload;
Function RegisterStdDDEngines : TDataDictEngines; overload;
Function UnRegisterStdDDEngines(Engines : TDataDictEngines) : TDataDictEngines;

implementation

uses
  fpdddbf,
  fpddfb,
  fpddpq,
  fpddOracle,
  fpddsqlite3,
  fpddmysql40,
  fpddmysql41,
  fpddmysql50,
  fpddodbc;
  
Const

  StdEngineClasses : Array [TDataDictEngine] of TFPDDEngineClass
                   = (TDBFDDEngine, TSQLDBFBDDEngine, TSQLDBOracleDDEngine,
                      TSQLDBMySql40DDEngine, TSQLDBMySql41DDEngine ,
                      TSQLDBMySql5DDEngine, TSQLDBPostGreSQLDDEngine,
                      TSQLDBSQLite3DDEngine,TSQLDBODBCDDEngine);

  StdEngineRegs : Array [TDataDictEngine] of procedure
                = (@InitDBFImporter, @RegisterFBDDEngine, @RegisterOracleDDEngine,
                  @RegisterMySQL40DDEngine, @RegisterMySQL41DDEngine,
                  @RegisterMySQL50DDEngine, @RegisterPostgreSQLDDengine,
                  @RegisterSQLite3DDEngine, @RegisterODBCDDengine);

  StdEngineUnRegs : Array [TDataDictEngine] of procedure
                = (@DoneDBFImporter, @UnRegisterFBDDEngine, @UnRegisterOracleDDEngine,
                  @UnRegisterMySQL40DDEngine, @UnRegisterMySQL41DDEngine,
                  @UnRegisterMySQL50DDEngine, @UnRegisterPostgreSQLDDengine,
                  @UnRegisterSQLite3DDEngine, @UnRegisterODBCDDengine);
                  
function RegisterStdDDEngines(Engines: TDataDictEngines): TDataDictEngines;

Var
  E : TDataDictEngine;

begin
  Result:=[];
  For E:=Low(TDataDictEngine) to High(TDataDictEngine) do
    If (E in Engines) and (Not IsDictionaryEngineRegistered(StdEngineClasses[E])) then
      begin
      StdEngineRegs[E];
      Include(Result,E);
      end;
end;

function RegisterStdDDEngines: TDataDictEngines;
begin
  Result:=RegisterStdDDEngines(AllStdDDEngines);
end;

function UnRegisterStdDDEngines(Engines: TDataDictEngines): TDataDictEngines;

Var
  E : TDataDictEngine;

begin
  Result:=[];
  For E:=Low(TDataDictEngine) to High(TDataDictEngine) do
    If (E in Engines) and IsDictionaryEngineRegistered(StdEngineClasses[E]) then
      begin
      StdEngineUnRegs[E];
      Include(Result,E);
      end;

end;

{ TStandardDDEngines }

procedure TStandardDDEngines.SetActive(const AValue: Boolean);
begin
  if FActive=AValue then
    exit;
  FActive:=AValue;
  If Not (csLoading in ComponentState) then
    If Active then
      DoRegister
    else
      DoUnregister;
end;

procedure TStandardDDEngines.Loaded;
begin
  If FActive then
    DoRegister;
end;

procedure TStandardDDEngines.DoRegister;
begin
  FRegistered:=RegisterSTDDDengines(FEngines);
end;

procedure TStandardDDEngines.DoUnregister;
begin
  UnRegisterSTDDDengines(FRegistered);
end;

constructor TStandardDDEngines.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

end.