-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL2DBF.PAS
449 lines (427 loc) · 10.2 KB
/
SQL2DBF.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
{ @author: Sylvain Maltais ([email protected])
@created: 2023
@website(https://www.gladir.com/dbase-tools)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program SQL2DBF;
Uses DOS;
Const
CommandList:Array[0..5]of String[8]=(
'CREATE','DELETE','INSERT','SELECT','SHOW','UPDATE'
);
Type
{Structure de fichier DBase III}
DBaseIIIFileHeaderRec=Record
HeadType,Year,Month,Day:Byte;
RecordCount:LongInt;
HeaderLength,RecordSize:Integer;
Fill:Array[1..20]of Byte;
End;
DBaseIIIFieldRec=Record
FieldName:Array[1..11]of Char;
FieldType:Char;
Spare1,Spare2:Integer;
Width,Dec:Byte;
WorkSpace:Array[1..14]of Byte;
End;
Var
SourceSQL:Text;
TargetDBF:File;
CommandFound:Boolean;
FieldCount:Integer;
TargetName,CurrLine,CurrCommand,ParamList,TableName:String;
J,ByteWrited:Integer;
HDBase:DBaseIIIFileHeaderRec; { Structure de l'entete d'un fichier DBase }
FieldDBase:DBaseIIIFieldRec; { Structure d'un champ de DBase }
FieldDBaseList:Array[0..127]of DBaseIIIFieldRec;
Function TrimL(S:String):String;
Var
I:Byte;
Begin
For I:=1to Length(S)do Begin
If S[I]<>' 'Then Begin
TrimL:=Copy(S,I,255);
Exit;
End;
End;
TrimL:=S;
End;
Function TrimR(s:String):String;
Var
i:Integer;
Begin
i:=Length(s);
While (i>0)and(s[i]in[#9,' '])do Dec(i);
s[0]:=Chr(i);
TrimR:=S;
End;
Function Trim(s:String):String;Begin
Trim:=TrimL(TrimR(s));
End;
Function PadRight(S:String;Space:Byte):String;
Var
I:Byte;
Begin
If Length(S)<Space Then For I:=Length(S)+1 to Space do S:=S+' ';
PadRight:=S;
End;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
Function Path2Name(S:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(S,D,N,E);
Path2Name:=N;
End;
Function Path2Ext(S:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(S,D,N,E);
Path2Ext:=E;
End;
Function Path2NoExt(S:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(S,D,N,E);
Path2NoExt:=D+N;
End;
Procedure ExtractCommand;
Var
I,J:Byte;
Begin
For I:=1 to Length(CurrLine)do Begin
If Not(CurrLine[I]in['A'..'Z','a'..'z','_','-','0'..'9'])Then Begin
CurrCommand:=StrToUpper(Copy(CurrLine,1,I-1));
ParamList:=TrimL(Copy(CurrLine,I,255));
Exit;
End;
End;
CurrCommand:=StrToUpper(CurrLine);
ParamList:='';
End;
Procedure CreateCommand;
Var
Step:(_None,_Open,_Sep,_Close);
Finished:Boolean;
CurrWord,CurrWord2:String;
I:Byte;
ByteWrited:Integer;
Begin
Finished:=False;
If ParamList=''Then ReadLn(SourceSQL,ParamList);
CurrWord:='';
I:=1;
While(I<=Length(ParamList))and(ParamList[I]in['A'..'Z','a'..'z'])do Begin
CurrWord:=CurrWord+ParamList[I];
Inc(I);
End;
If StrToUpper(CurrWord)<>'TABLE'Then Begin
WriteLn('Mot clef TABLE attendu !');
Halt;
End;
While(I<=Length(ParamList))and(ParamList[I]in[' ',#9])do Inc(I);
CurrWord2:='';
While(I<=Length(ParamList))and(ParamList[I]in['A'..'Z','a'..'z'])do Begin
CurrWord2:=CurrWord2+ParamList[I];
Inc(I);
End;
If CurrWord2<>''Then Begin
TableName:=CurrWord2;
End
Else
Begin
WriteLn('Nom de la table attendu !');
Halt;
End;
Step:=_None;
Repeat
Repeat
While(I<=Length(ParamList))and(ParamList[I]in[' ',#9])do Inc(I);
If(Step=_None)Then Begin
If(I<=Length(ParamList))and(ParamList[I]='(')Then Begin
Step:=_Open;
Inc(I);
End;
End
Else
If(Step=_Sep)Then Begin
If(I<=Length(ParamList))and(ParamList[I]=')')Then Finished:=True Else
If(I<=Length(ParamList))and(ParamList[I]=',')Then Begin
Inc(I);
Step:=_Open;
End
Else
Begin
WriteLn('S‚parateur attendue !');
End;
End
Else
Begin
FillChar(CurrWord,SizeOf(CurrWord),#0);
CurrWord:='';
While(I<=Length(ParamList))and(ParamList[I]in['A'..'Z','a'..'z'])do Begin
CurrWord:=CurrWord+ParamList[I];
Inc(I);
End;
If CurrWord<>''Then Begin
While(I<=Length(ParamList))and(ParamList[I]in[' ',#9])do Inc(I);
CurrWord2:='';
While(I<=Length(ParamList))and(ParamList[I]in['A'..'Z','a'..'z'])do Begin
CurrWord2:=CurrWord2+ParamList[I];
Inc(I);
End;
If CurrWord2<>''Then Begin
Move(CurrWord[1],FieldDBaseList[FieldCount].FieldName,11);
If StrToUpper(CurrWord2)='BYTE'Then Begin
FieldDBaseList[FieldCount].FieldType:='L';
Inc(HDBase.RecordSize);
End
Else
If StrToUpper(CurrWord2)='TEXT'Then Begin
FieldDBaseList[FieldCount].FieldType:='C';
FieldDBaseList[FieldCount].Width:=255;
Inc(HDBase.RecordSize,255);
End
Else
If StrToUpper(CurrWord2)='INTEGER'Then Begin
FieldDBaseList[FieldCount].FieldType:='I';
Inc(HDBase.RecordSize,2);
End
Else
If StrToUpper(CurrWord2)='SMALLINT'Then Begin
FieldDBaseList[FieldCount].FieldType:='I';
Inc(HDBase.RecordSize,2);
End
Else
If StrToUpper(CurrWord2)='VARCHAR'Then Begin
FieldDBaseList[FieldCount].FieldType:='C';
FieldDBaseList[FieldCount].Width:=255;
Inc(HDBase.RecordSize,255);
End
Else
Begin
WriteLn('Type de donn‚es non reconnu : ',CurrWord2);
Halt;
End;
Inc(FieldCount);
Step:=_Sep;
End
Else
Begin
WriteLn('Type de donn‚es attendue');
Halt;
End;
End;
End;
Until(I>=Length(ParamList))or(Finished);
If Not(Finished)Then Begin
If EOF(SourceSQL)Then Finished:=True
Else
Begin
ReadLn(SourceSQL,ParamList);
I:=1;
End;
End;
Until Finished;
FieldDBaseList[FieldCount].FieldName[1]:=#13;
HDBase.HeadType:=$3;
HDBase.HeaderLength:=SizeOf(HDBase)+SizeOf(DBaseIIIFieldRec)*Succ(FieldCount)-1;
Seek(TargetDBF,0);
BlockWrite(TargetDBF,HDBase,SizeOf(HDBase),ByteWrited);
BlockWrite(TargetDBF,FieldDBaseList,
SizeOf(DBaseIIIFieldRec)*Succ(FieldCount),ByteWrited);
End;
Procedure DeleteCommand;Begin
WriteLn('Cette commande n''est pas mise en oeuvre');
End;
Procedure InsertCommand;
Var
Finished:Boolean;
CurrWord,CurrWord2:String;
I:Byte;
Step:(_None,_Open,_Sep,_Close);
CurrField,ByteWrited:Integer;
Begin
Finished:=False;
CurrField:=0;
If ParamList=''Then ReadLn(SourceSQL,ParamList);
CurrWord:='';
I:=1;
While(I<=Length(ParamList))and(ParamList[I]in['A'..'Z','a'..'z'])do Begin
CurrWord:=CurrWord+ParamList[I];
Inc(I);
End;
If StrToUpper(CurrWord)<>'INTO'Then Begin
WriteLn('Mot clef INTO attendu !');
Halt;
End;
Step:=_None;
While(I<=Length(ParamList))and(ParamList[I]in[' ',#9])do Inc(I);
CurrWord2:='';
While(I<=Length(ParamList))and(ParamList[I]in['A'..'Z','a'..'z'])do Begin
CurrWord2:=CurrWord2+ParamList[I];
Inc(I);
End;
If CurrWord2<>''Then Begin
TableName:=CurrWord2;
End;
Repeat
Repeat
While(I<=Length(ParamList))and(ParamList[I]in[' ',#9])do Inc(I);
If(Step=_None)Then Begin
If(I<=Length(ParamList))and(ParamList[I]='(')Then Begin
Step:=_Open;
Inc(I);
End;
End
Else
If(Step=_Sep)Then Begin
If(I<=Length(ParamList))and(ParamList[I]=')')Then Begin
Inc(I);
Step:=_Close;
Finished:=True;
End
Else
If(I<=Length(ParamList))and(ParamList[I]=',')Then Begin
Inc(I);
Step:=_Open;
End
Else
Begin
WriteLn('Position ',I,', caractŠre rencontr‚ : ',ParamList[I]);
WriteLn('S‚parateur attendue !');
Halt;
End;
End
Else
Begin
FillChar(CurrWord,SizeOf(CurrWord),#0);
CurrWord:='';
If(I<=Length(ParamList))and(ParamList[I]='''')Then Begin
Inc(I);
While(I<=Length(ParamList))and(ParamList[I]<>'''')do Begin
CurrWord:=CurrWord+ParamList[I];
Inc(I);
End;
If(I<=Length(ParamList))and(ParamList[I]='''')Then Begin
Inc(I);
End
Else
Begin
WriteLn('Chaine de caractŠres sans terminaison.');
Halt;
End;
Step:=_Sep;
End
Else
If(I<=Length(ParamList))and(ParamList[I]in['0'..'9'])Then Begin
While(I<=Length(ParamList))and(ParamList[I]in['0'..'9'])do Begin
CurrWord:=CurrWord+ParamList[I];
Inc(I);
End;
Step:=_Sep;
End
Else
Begin
WriteLn('Donn‚es attendue !');
Halt;
End;
CurrWord:=PadRight(CurrWord,FieldDBaseList[CurrField].Width);
BlockWrite(TargetDBF,CurrWord[1],FieldDBaseList[CurrField].Width,ByteWrited);
Inc(CurrField);
End;
Until(I>=Length(ParamList))or(Finished);
If Not(Finished)Then Begin
If EOF(SourceSQL)Then Finished:=True
Else
Begin
ReadLn(SourceSQL,ParamList);
I:=1;
End;
End;
Until Finished;
Inc(HDBase.RecordCount);
End;
Procedure SelectCommand;Begin
WriteLn('Cette commande n''est pas mise en oeuvre');
End;
Procedure ShowComamnd;Begin
WriteLn('Cette commande n''est pas mise en oeuvre');
End;
Procedure ShowCommand;Begin
WriteLn('Cette commande n''est pas mise en oeuvre');
End;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('SQL2DBF - Cette commande permet de convertir un fichier SQL en DBF.');
WriteLn;
WriteLn('Syntaxe : SQL2DBF source.SQL');
End
Else
Begin
TableName:='';
FieldCount:=0;
FillChar(HDBase,SizeOf(HDBase),0);
FillChar(FieldDBaseList,SizeOf(FieldDBaseList),0);
{$I-}Assign(SourceSQL,ParamStr(1));
Reset(SourceSQL);{$I+}
If IoResult<>0Then Begin
WriteLn('Impossible d''ouvrir le fichier ',ParamStr(1));
Halt;
End;
If ParamStr(2)<>''Then TargetName:=ParamStr(2)
Else TargetName:=Path2NoExt(ParamStr(1))+'.DBF';
{$I-}Assign(TargetDBF,TargetName);
Rewrite(TargetDBF,1);{$I+}
If IoResult<>0Then Begin
WriteLn('Impossible d''‚crire le fichier ',TargetName);
Halt;
End;
While Not EOF(SourceSQL)do Begin
ReadLn(SourceSQL,CurrLine);
ExtractCommand;
CommandFound:=False;
For J:=Low(CommandList) to High(CommandList) do Begin
If CurrCommand=CommandList[J]Then Begin
CommandFound:=True;
Case(J)of
0:CreateCommand;
1:DeleteCommand;
2:InsertCommand;
3:SelectCommand;
4:ShowComamnd;
5:ShowCommand;
Else Begin
WriteLn('Commande non reconnu');
Halt;
End;
End;
End;
End;
If Not(CommandFound)Then Begin
WriteLn('Command non support‚');
Halt;
End;
End;
Seek(TargetDBF,0);
BlockWrite(TargetDBF,HDBase,SizeOf(HDBase),ByteWrited);
Close(TargetDBF);
Close(SourceSQL);
End;
END.