summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-db/src/datadict/fpdddbf.pp
blob: b67aa204ad6a44e38ab7b32d5f3a773a143bd036 (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
176
177
178
179
180
181
182
183
184
{
    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

    DBF Data Dictionary Engine Implementation.

    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.

 **********************************************************************}
unit fpddDbf;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db,dbf, fpdatadict;
  
Type

  { TDBFDDimporter }

  { TDBFDDEngine }

  TDBFDDEngine = Class(TFPDDEngine)
  Private
    FDBF : TDBF;
  Public
    Procedure Disconnect ; override;
    Function Connect(const AConnectString : String) : Boolean; override;
    Function GetTableList(List : TStrings) : Integer; override;
    Function ImportFields(Table : TDDTableDef) : Integer; override;
    Function ViewTable(Const TableName: String; DatasetOwner : TComponent) : TDataset; override;
    Function GetTableIndexDefs(ATableName : String; Defs : TDDIndexDefs) : integer ; override;
    Class function Description : string; override;
    Class function DBType : String; override;
    Class function EngineCapabilities : TFPDDEngineCapabilities; override;
  end;
  
Procedure InitDBFImporter;
Procedure DoneDBFImporter;

implementation

uses dbf_idxfile;

procedure TDBFDDEngine.Disconnect;
begin
  FConnectString:='';
  FConnected:=False;
  FreeAndNil(FDBF);
end;

function TDBFDDEngine.Connect(const AConnectString : String): Boolean;
begin
  FDBF:=TDBF.Create(Self);
  FDBF.FilePath:=AConnectString;
  FConnected:=True;
  FConnectString:=AConnectString;
  Result:=True;
end;

Function TDBFDDEngine.GetTableList(List: TStrings) : Integer;

Var
  Info : TSearchrec;
  FN : String;
 
begin
  Result:=0;
  If Assigned(FDBF) then
    begin
    FN:=IncludeTrailingPathDelimiter(FDBF.FilePath);
    If FindFirst(FN+'*.dbf',0,Info)=0 then
      try
        Repeat
          inc(Result);
          If Assigned(List) then
            List.Add(info.name);
        Until (FindNext(Info)<>0);
      finally
        FindClose(Info);
      end;
    end;
end;

Function TDBFDDEngine.ImportFields(Table: TDDTableDef) : Integer;
begin
  Result:=0;
  if Assigned(FDBF) then
    begin
    FDBF.TableName:=Table.TableName;
    FDBF.Open;
    Try
      Table.ImportFromDataset(FDBF);
    Finally
      FDBF.Close;
    end;
    end;
end;

function TDBFDDEngine.ViewTable(const TableName: String;
  DatasetOwner: TComponent): TDataset;
  
Var
  D : TDBF;
  
begin
  If DatasetOwner=Nil then
   DatasetOwner:=Self;
  D:=TDBF.Create(DatasetOwner);
  D.FilePath:=FDBF.FilePath;
  D.TableName:=TableName;
  Result:=D;
end;

function TDBFDDEngine.GetTableIndexDefs(ATableName: String; Defs: TDDIndexDefs
  ): integer;
  
Var
  D : TDBF;
  DBD : TDBFIndexDef;
  DD : TDDIndexDef;
  I : Integer;
  
begin
  D:=TDBF.Create(Self);
  Try
    D.FilePath:=FDBF.FilePath;
    D.TableName:=ATableName;
    D.IndexDefs.Update;
    Defs.Clear;
    For I:=0 to D.IndexDefs.Count-1 do
      begin
      DBD:=D.IndexDefs[i];
      DD:=Defs.AddDDIndexDef(DBD.Name);
      DD.Fields:=DBD.SortField;
      DD.Options:=DBD.Options;
      end;
    Result:=Defs.Count;
  Finally
    D.Free;
  end;
end;

Class function TDBFDDEngine.Description: string;
begin
  Result:='DBase database dictionary importer';
end;

class function TDBFDDEngine.DBType: String;
begin
  Result:='DBase files';
end;

class function TDBFDDEngine.EngineCapabilities: TFPDDEngineCapabilities;
begin
  Result:=[ecImport,ecViewTable];
end;

Procedure InitDBFImporter;

begin
  RegisterDictionaryEngine(TDBFDDEngine);
end;

Procedure DoneDBFImporter;

begin
  UnRegisterDictionaryEngine(TDBFDDEngine);
end;

Initialization
  InitDBFImporter;
Finalization
  DoneDBFImporter;
end.