-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuFoxProDBF.pas
345 lines (298 loc) · 8.32 KB
/
uFoxProDBF.pas
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
unit uFoxProDBF;
interface
uses
Classes, SysUtils, ADODB, DB
;
const
cNonError: Integer = 0;
cBadArgument: Integer = 1;
cNonConnection: Integer = 2;
cUnknown: Integer = 10;
type TVFP = class
private
fFullFilePath: String;
fConnected: Boolean;
fFieldsList: TStrings;
ADODBFConnection: TADOConnection;
qADODBF: TADOQuery;
//Ïîëó÷èòü ñïèñîê ïîëåé â òàáëèöå
procedure GetFieldList;
//Ïîèñê ïî èìåíè ïîëÿ
function SearchField(aFieldName: String): Boolean;
//Ïîäêëþ÷àåìñÿ ê DBF ôàéëó
function DBFConnection(const aFullPath: String): Boolean;
//Ïîëó÷èòü èìÿ ôàéëà
function GetFileName: String;
function GetFileNameNoExt: string;
function GetFileNameNo: string;
//Ïîëó÷àåì íàïèñàíèå òèïà äëÿ FoXPro
function GetDBFType(aType: TFieldType; aSize: Integer): String;
function CorrectName(aName: string): string;
public
tADODBF: TADOTable;
queryADODBF: TADOQuery;
property FullFilePath: String read fFullFilePath write fFullFilePath;
property FileName: String read GetFileName;
property FileNameNoExt: String read GetFileNameNoExt;
property FileNameNo: String read GetFileNameNo;
property Connected: Boolean read fConnected write fConnected;
property FieldsList: TStrings read fFieldsList;
procedure Connect(aFullFilePath: String);
procedure Disconnect;
//Ïîêà ðàáîòà ïðîèñõîäèò òîëüêî ñ òèïàìè: ñòðîêîé, öåëûì òèïîì è äàòîé
function AddFileld(aFieldName: String; aType: TFieldType; aSize: Integer): Integer;
function RemoveField(aFieldName: String): Integer;
function RemoveUniqueKey(aKeyName: String): Integer;
function CreateUniqKey(aFieldName: string): Integer;
function FillUniqValue(aFieldName: string): Integer;
function UpdateField(aFieldKey: string; aKeyValue: integer; aUpdateField, aUpdateValue: string): integer;
procedure Commit;
constructor Create();
destructor Destroy;
end;
implementation
procedure TVFP.Commit;
begin
ADODBFConnection.Close;
ADODBFConnection.Open;
//tADODBF.Active := True;
GetFieldList;
end;
function TVFP.GetFileNameNoExt: string;
begin
Result := '[' + StringReplace(ExtractFileName(fFullFilePath), ExtractFileExt(fFullFilePath), '', []) + ']';
end;
function TVFP.GetFileNameNo: string;
begin
Result := StringReplace(ExtractFileName(fFullFilePath), ExtractFileExt(fFullFilePath), '', []);
end;
function TVFP.CorrectName(aName: string): string;
const
scInvalidChars = '-!@#$%^&()=+[]{}';
var
i: Integer;
begin
Result := aName;
for I := 1 to length(scInvalidChars) do
Result := StringReplace(Result, scInvalidChars[i], '_', [rfReplaceAll, rfIgnoreCase]);
end;
function TVFP.GetDBFType(aType: TFieldType; aSize: Integer): String;
var
s: String;
begin
case (Integer(aType)) of
Integer(ftString): s := 'C(' + IntToStr(aSize) + ')';
Integer(ftSmallint), Integer(ftInteger), Integer(ftWord), Integer(ftFloat): s := 'N(' + IntToStr(aSize) + ')';
Integer(ftDate), Integer(ftDateTime): s:= 'D'
else
s := 'C(10)'; // Çíà÷åíèå ïî óìîë÷àíèþ, äëÿ íå îïèñàííûõ òèïîâ
end;
Result := s;
end;
function TVFP.GetFileName: String;
begin
Result := ExtractFileName(fFullFilePath);
end;
procedure TVFP.GetFieldList;
var
i: Integer;
begin
Assert(Assigned(fFieldsList), '{64E3ED23-7DC7-4CD4-9F2A-1C0DABC83B6C}');
//Assert(tADODBF.Active, '{3E5F6377-40C6-432E-83C0-23BBCD13635C}');
fFieldsList.Clear;
qADODBF.SQL.Text := 'select * from ' + FileNameNoExt + ' where 1 = 2';
qADODBF.Open;
for i := 0 to qADODBF.FieldCount - 1 do
fFieldsList.Add(qADODBF.Fields[i].DisplayName);
end;
function TVFP.SearchField(aFieldName: String): Boolean;
begin
Assert(Assigned(fFieldsList), '{F7B80790-D1BB-4F9A-9A47-95C533D40A9B}');
Result := False;
if (fFieldsList.Count = 0) then Exit;
if (fFieldsList.IndexOf(aFieldName) = -1) then Exit;
Result := True;
end;
function TVFP.DBFConnection(const aFullPath: String): Boolean;
Var
cs: String;
begin
//Íóæíî äîáàâèòü ïðîâåðêó íàëè÷èÿ äðàéâåðà
//DELETED=False - óêàçàâûâàåò, ÷òî ñ çàïèñÿ ïîìå÷åííûìè íà óäàëåíèå, ðàáîòàåì êàê ñ îáû÷íûìè
cs := Format(
'Provider=VFPOLEDB.1;Data Source=%s;DELETED=False'
, [aFullPath]
) ;
Result := False;
// if (ADODBFConnection.Connected) then
ADODBFConnection.Close;
ADODBFConnection.ConnectionString := cs;
tADODBF.TableName := FileNameNoExt;
try
ADODBFConnection.Open;
//tADODBF.Open;
except
fFullFilePath := '';
Exit;
end;
Result := True;//tADODBF.Active;
end;
constructor TVFP.Create;
begin
Inherited;
fFullFilePath := '';
fConnected := False;
fFieldsList := TStringList.Create;
ADODBFConnection := TADOConnection.Create(nil);
ADODBFConnection.Mode := cmShareExclusive;
qADODBF := TADOQuery.Create(nil);
queryADODBF := TADOQuery.Create(nil);
tADODBF := TADOTable.Create(nil);
tADODBF.LockType := ltBatchOptimistic;
qADODBF.Connection := ADODBFConnection;
queryADODBF.Connection := ADODBFConnection;
tADODBF.Connection := ADODBFConnection;
end;
destructor TVFP.Destroy;
begin
fFieldsList.Free;
ADODBFConnection.Free;
qADODBF.Free;
tADODBF.Free;
queryADODBF.Free;
inherited;
end;
procedure TVFP.Connect(aFullFilePath: String);
begin
if (not FileExists(aFullFilePath)) then Exit;
fFullFilePath := aFullFilePath;
if (not DBFConnection(aFullFilePath)) then Exit;
GetFieldList;
fConnected := True;
end;
procedure TVFP.Disconnect;
begin
ADODBFConnection.Close;
fFullFilePath := '';
fConnected := False;
fFieldsList.Clear;
end;
function TVFP.AddFileld(aFieldName: String; aType: TFieldType; aSize: Integer): Integer;
begin
Assert(ADODBFConnection.Connected, '{F7F75ACF-AA1C-432F-914B-981597F7F6F5}');
//Ïëîõèå óñëîâèÿ
if (not fConnected) then
begin
Result := cNonConnection;
Exit;
end;
if (aFieldName = '') then
begin
Result := cBadArgument;
Exit;
end;
//Ïîëå óæå äîáàâëåíî
if (SearchField(aFieldName)) then
begin
Result := cNonError;
Exit;
end;
qADODBF.SQL.Text := 'alter table ' + FileNameNoExt
+ ' ADD ' + aFieldName + ' ' + GetDBFType(aType, aSize);
try
qADODBF.ExecSQL;
except
Result := cUnknown;
end;
Result := cNonError;
end;
function TVFP.RemoveField(aFieldName: String): Integer;
begin
Assert(ADODBFConnection.Connected, '{2B019729-D2A5-4A0A-9130-33C14ECF91DF}');
//Ïëîõèå óñëîâèÿ
if (not fConnected) then
begin
Result := cNonConnection;
Exit;
end;
if (not SearchField(aFieldName)) then
begin
Result := cBadArgument;
Exit;
end;
qADODBF.SQL.Text := 'alter table ' + FileNameNoExt
+ ' DROP COLUMN ' + aFieldName;
try
qADODBF.ExecSQL;
//Commit;
except
Result := cUnknown;
end;
Result := cNonError;
end;
function TVFP.RemoveUniqueKey(aKeyName: String): Integer;
begin
Assert(ADODBFConnection.Connected, '{0EB06581-C83C-4EDF-8AB3-3E362A78D8E4}');
//Ïëîõèå óñëîâèÿ
if (not fConnected) then
begin
Result := cNonConnection;
Exit;
end;
qADODBF.SQL.Text := 'alter table ' + FileNameNoExt
+ ' DROP UNIQUE TAG ' + aKeyName;
try
qADODBF.ExecSQL;
except
Result := cUnknown;
end;
Result := cNonError;
end;
function TVFP.FillUniqValue(aFieldName: string): Integer;
begin
//Ïîêà ðàáîòàåò òîëüêî ñ ÷èñëîâûì òèïîì
if (not SearchField(aFieldName)) then
begin
Result := cBadArgument;
Exit;
end;
qADODBF.SQL.Text := 'update ' + FileNameNoExt + ' set ' + aFieldName + ' = recno()';
try
qADODBF.ExecSQL;
except
Result := cUnknown;
Exit;
end;
Result := cNonError;
end;
function TVFP.CreateUniqKey(aFieldName: string): Integer;
begin
//Ïîêà ðàáîòàåò òîëüêî ñ ÷èñëîâûì òèïîì
if (not SearchField(aFieldName)) then
begin
Result := cBadArgument;
Exit;
end;
try
qADODBF.SQL.Text := 'ALTER TABLE ' + FileNameNoExt + ' ADD unique ' + aFieldName;
qADODBF.ExecSQL;
Except
Result := cUnknown;
Exit;
end;
Result := cNonError;
end;
function TVFP.UpdateField(aFieldKey: string; aKeyValue: integer; aUpdateField, aUpdateValue: string): integer;
begin
qADODBF.SQL.Text := 'update ' + FileNameNoExt +
' set ' + aUpdateField + ' = ' + aUpdateValue +
' where ' + aFieldKey + ' = ' + IntToStr(aKeyValue);
try
qADODBF.ExecSQL;
except
Result := cUnknown;
Exit;
end;
Result := cNonError;
end;
end.