-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathcsv_detector.pas
More file actions
404 lines (345 loc) · 13.2 KB
/
Copy pathcsv_detector.pas
File metadata and controls
404 lines (345 loc) · 13.2 KB
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
unit csv_detector;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, SynEdit, SynMemo, extra_controls, apphelpers,
loaddata, dbconnection, Vcl.ExtCtrls, gnugettext, dbstructures, System.Math, SynRegExpr, System.IOUtils,
System.StrUtils;
type
TfrmCsvDetector = class(TExtForm)
btnScan: TButton;
SynMemoCreateTable: TSynMemo;
btnCancel: TButton;
btnSave: TButton;
TimerStartScan: TTimer;
procedure FormShow(Sender: TObject);
procedure btnScanClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
private
FLoadDataFrm: Tloaddataform;
FConnection: TDBConnection;
function DetectColumnAttributes(Rows: TGridRows; IgnoreLines: Integer): TTableColumnList;
function ComposeCreateStatement(Columns: TTableColumnList): String;
public
end;
var
frmCsvDetector: TfrmCsvDetector;
implementation
{$R *.dfm}
uses main;
procedure TfrmCsvDetector.FormCreate(Sender: TObject);
begin
HasSizeGrip := True;
FLoadDataFrm := Tloaddataform(Owner);
FConnection := MainForm.ActiveConnection;
end;
procedure TfrmCsvDetector.FormShow(Sender: TObject);
begin
SynMemoCreateTable.Highlighter := MainForm.SynSQLSynUsed;
MainForm.SetupSynEditors(Self);
TimerStartScan.Enabled := True;
end;
procedure TfrmCsvDetector.btnScanClick(Sender: TObject);
var
Stream: TFileStream;
Encoding: TEncoding;
GridRows: TGridRows;
GridRow: TGridRow;
GridValue: TGridValue;
Term, Encl, Escp, LineTerm: String;
RowNum, IgnoreLines: Integer;
P, ContentLen, ProgressCharsPerStep, ProgressChars: Integer;
EnclLen, TermLen, LineTermLen: Integer;
Contents: String;
EnclTest, TermTest, LineTermTest: String;
Value: String;
IsEncl, IsTerm, IsLineTerm, IsEof: Boolean;
InEncl: Boolean;
Columns: TTableColumnList;
const
TestChunkSize = 20*SIZE_MB;
procedure NextChar;
begin
Inc(P);
Inc(ProgressChars);
if ProgressChars >= ProgressCharsPerStep then begin
Mainform.ProgressStep;
Mainform.ShowStatusMsg(f_('Parsing textfile, row %s, %d%%', [FormatNumber(GridRows.Count-IgnoreLines), Mainform.ProgressBarStatus.Position]));
//Mainform.LogSQL(f_('Analyzing textfile, row %s, %d%%', [FormatNumber(Rows.Count-IgnoreLines), Mainform.ProgressBarStatus.Position]));
ProgressChars := 0;
end;
end;
function TestLeftChars(var Portion: String; CompareTo: String; Len: Integer): Boolean;
var i: Integer;
begin
if Len > 0 then begin
for i:=1 to Len-1 do
Portion[i] := Portion[i+1];
Portion[Len] := Contents[P];
Result := Portion = CompareTo;
end else
Result := False;
end;
procedure AddValue;
begin
if Copy(Value, 1, EnclLen) = Encl then begin
Delete(Value, 1, EnclLen);
Delete(Value, Length(Value)-EnclLen+1, EnclLen);
end;
GridValue := TGridValue.Create;
GridValue.OldText := Value;
GridValue.OldIsNull := Value = 'NULL';
if GridRow = nil then
GridRow := TGridRow.Create(True);
GridRow.Add(GridValue);
Value := '';
end;
procedure AddRow;
begin
Inc(RowNum);
GridRows.Add(GridRow);
GridRow := TGridRow.Create(True);
end;
begin
// Scan user selected file for column types
TimerStartScan.Enabled := False;
Screen.Cursor := crHourGlass;
btnScan.ImageIndex := 150;
btnScan.Enabled := False;
btnSave.Enabled := False;
// Parse contents to a TGridRows instance
GridRows := TGridRows.Create(True);
GridRow := nil;
Term := FConnection.UnescapeString(FLoadDataFrm.editFieldTerminator.Text);
Encl := FConnection.UnescapeString(FLoadDataFrm.editFieldEncloser.Text);
LineTerm := FConnection.UnescapeString(FLoadDataFrm.editLineTerminator.Text);
Escp := FConnection.UnescapeString(FLoadDataFrm.editFieldEscaper.Text);
RowNum := 0;
TermLen := Length(Term);
EnclLen := Length(Encl);
LineTermLen := Length(LineTerm);
SetLength(TermTest, TermLen);
SetLength(EnclTest, EnclLen);
SetLength(LineTermTest, LineTermLen);
InEncl := False;
try
MainForm.ShowStatusMsg(f_('Reading textfile (%s) ...', [FormatByteNumber(TestChunkSize)]));
Encoding := FLoadDataFrm.FileEncoding;
OpenTextfile(FLoadDataFrm.editFilename.Text, Stream, Encoding);
Contents := ReadTextfileChunk(Stream, Encoding, TestChunkSize);
Stream.Free;
ContentLen := Length(Contents);
MainForm.ShowStatusMsg;
P := 0;
ProgressCharsPerStep := ContentLen div FLoadDataFrm.ProgressBarSteps;
ProgressChars := 0;
MainForm.EnableProgress(FLoadDataFrm.ProgressBarSteps);
IgnoreLines := FLoadDataFrm.updownIgnoreLines.Position;
NextChar;
while P <= ContentLen do begin
// Check characters left-side from current position
IsEncl := TestLeftChars(EnclTest, Encl, EnclLen);
IsTerm := TestLeftChars(TermTest, Term, TermLen);
IsLineTerm := TestLeftChars(LineTermTest, LineTerm, LineTermLen);
IsEof := P = ContentLen;
Value := Value + Contents[P];
if IsEncl then
InEncl := not InEncl;
if IsEof or (not InEncl) then begin
if IsLineTerm then begin
SetLength(Value, Length(Value)-LineTermLen);
AddValue;
end else if IsEof then begin
AddValue;
end else if IsTerm then begin
SetLength(Value, Length(Value)-TermLen);
AddValue;
end;
end;
if IsLineTerm and (not InEncl) then
AddRow;
NextChar;
end;
Contents := '';
// Find matching column types for values
Columns := DetectColumnAttributes(GridRows, IgnoreLines);
SynMemoCreateTable.Text := ComposeCreateStatement(Columns);
btnSave.Enabled := True;
except
on E:EFOpenError do
ErrorDialog(E.Message);
end;
GridRows.Free;
MainForm.ShowStatusMsg;
MainForm.DisableProgress;
btnScan.ImageIndex := -1;
btnScan.Enabled := True;
Screen.Cursor := crDefault;
end;
function TfrmCsvDetector.DetectColumnAttributes(Rows: TGridRows; IgnoreLines: Integer): TTableColumnList;
var
Row: TGridRow;
Value: TGridValue;
Col, UnknownColumn: TTableColumn;
i, j, k: Integer;
UnknownTypeYet, IsInteger, IsFloat, IsDate, IsDatetime, IsText: Boolean;
ValueSize, TypeSize: Int64;
FloatValue: Extended;
LoopType: TDBDatatype;
const
FloatChars = ['0'..'9', '.'];
begin
MainForm.ShowStatusMsg(f_('Analyzing %s rows...', [FormatNumber(Rows.Count)]));
MainForm.EnableProgress(Rows.Count);
Result := TTableColumnList.Create;
for Row in Rows do begin
for Value in Row do begin
Col := TTableColumn.Create(FConnection);
if IgnoreLines > 0 then
Col.Name := FConnection.CleanIdent(Value.OldText)
else
Col.Name := 'col_'+Row.IndexOf(Value).ToString;
Col.DataType := FConnection.Datatypes[0]; // UNKNOWN by default
Col.AllowNull := False; // Make True as soon as we encounter NULL or empty strings in the values
Col.Unsigned := False; // No detection for unsigned types
Col.LengthSet := '';
Result.Add(Col);
end;
Break;
end;
UnknownColumn := TTableColumn.Create(FConnection);
for i:=IgnoreLines to Rows.Count-1 do begin
MainForm.ProgressStep;
for j:=0 to Rows[i].Count-1 do begin
Value := Rows[i][j];
if j >= Result.Count then
Col := UnknownColumn
else
Col := Result[j];
// Detect data type of current value
IsInteger := IntToStr(StrToInt64Def(Value.OldText, -1)) = Value.OldText;
FloatValue := StrToFloatDef(Value.OldText, -1, MainForm.FormatSettings);
IsFloat := Value.OldText.Contains('.');
if IsFloat then begin
for k:=1 to Length(Value.OldText) do begin
IsFloat := IsFloat and CharInSet(Value.OldText[k], FloatChars);
if not IsFloat then
Break;
end;
end;
{ Using StrToDateTimeDef allows values like '2020-12-08 foo'
IsDate := (not IsInteger) and (not IsFloat)
and (StrToDateDef(Value.OldText, MaxDateTime) <> MaxDateTime);
IsDatetime := (not IsInteger) and (not IsFloat)
and (StrToDateTimeDef(Value.OldText, MaxDateTime) <> MaxDateTime);}
IsDate := (not IsInteger) and (not IsFloat)
and ExecRegExpr('^\d{4}-\d{2}-\d{2}$', Value.OldText);
IsDatetime := (not IsInteger) and (not IsFloat)
and ExecRegExpr('^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$', Value.OldText);
IsText := (not IsInteger) and (not IsFloat) and (not IsDate) and (not IsDatetime);
ValueSize := IfThen(IsInteger, StrToInt64Def(Value.OldText, -1), Length(Value.OldText));
//MainForm.LogSQL(Format('Value:"%s" IsInteger:%d IsFloat:%d IsDate:%d IsDateTime:%d IsText:%d',
// [Value.OldText, IsInteger.ToInteger, IsFloat.ToInteger, IsDate.ToInteger, IsDatetime.ToInteger, IsText.ToInteger]), lcDebug);
// Now, find a fitting data type for this column
for k:=Low(FConnection.Datatypes) to High(FConnection.Datatypes) do begin
LoopType := FConnection.Datatypes[k];
UnknownTypeYet := Col.DataType.Index = dbdtUnknown;
if (ValueSize = 0) or (CompareText(Value.OldText, 'null')=0) or (CompareText(Value.OldText, '\N')=0) then
Col.AllowNull := True;
// Integer types
if (LoopType.Category = dtcInteger) and IsInteger and (UnknownTypeYet or (Col.DataType.Category = dtcInteger)) then begin
if (ValueSize > Col.DataType.MaxSize) and (ValueSize <= LoopType.MaxSize)
then begin
Col.DataType := LoopType;
end;
end;
// Float types
if (LoopType.Category = dtcReal) and IsFloat and (UnknownTypeYet or (Col.DataType.Category = dtcReal)) then begin
if (ValueSize > Col.DataType.MaxSize) and (ValueSize <= LoopType.MaxSize)
then begin
Col.DataType := LoopType;
if LoopType.RequiresLength then
Col.LengthSet := LoopType.DefLengthSet;
end;
end;
// Datetime type
if IsDatetime and (UnknownTypeYet or (Col.DataType.Index in [dbdtDate, dbdtDatetime])) and (LoopType.Index = dbdtDatetime) then begin
Col.DataType := LoopType;
end;
// Date type
if IsDate and (UnknownTypeYet or (Col.DataType.Index = dbdtDate)) and (LoopType.Index = dbdtDate) then begin
Col.DataType := LoopType;
end;
// Text types - fall back here if nothing else matches
if (LoopType.Category = dtcText) and IsText then begin
if ((not Col.LengthSet.IsEmpty) and (ValueSize > StrToInt64Def(Col.LengthSet, 0)))
or ((ValueSize > Col.DataType.MaxSize) and (ValueSize <= LoopType.MaxSize))
or (Col.DataType.Category <> LoopType.Category)
then begin
if Col.DataType.Index <> LoopType.Index then begin
MainForm.LogSQL('Preferring '+LoopType.Name+' type over '+Col.DataType.Name+' for '+col.Name+' due to value "'+Value.OldText+'"', lcDebug);
end;
Col.DataType := LoopType;
if Col.DataType.RequiresLength then begin
TypeSize := Max(ValueSize, 1);
TypeSize := System.Math.Ceil(TypeSize / 10) * 10;
Col.LengthSet := Min(TypeSize, LoopType.MaxSize).ToString;
end else
Col.LengthSet := '';
end;
end;
end;
end;
//break;
end;
end;
function TfrmCsvDetector.ComposeCreateStatement(Columns: TTableColumnList): String;
var
Col: TTableColumn;
TableName: String;
begin
// Compose CREATE TABLE
TableName := TPath.GetFileNameWithoutExtension(FLoadDataFrm.editFilename.Text);
TableName := FConnection.CleanIdent(TableName);
Result := 'CREATE TABLE '+FConnection.QuoteIdent(FLoadDataFrm.comboDatabase.Text)+'.'+FConnection.QuoteIdent(TableName)+' (' + sLineBreak;
for Col in Columns do begin
Result := Result + CodeIndent + Col.SQLCode;
if Col <> Columns.Last then
Result := Result + ',';
Result := Result + sLineBreak;
end;
Result := Result + ')' + sLineBreak;
end;
procedure TfrmCsvDetector.btnSaveClick(Sender: TObject);
var
rx: TRegExpr;
TableName, Quote: String;
begin
// Run code
try
Screen.Cursor := crHourGlass;
FConnection.Query(SynMemoCreateTable.Text);
FConnection.ShowWarnings;
ModalResult := mrOk;
rx := TRegExpr.Create;
rx.ModifierI := True;
Quote := QuoteRegExprMetaChars(FConnection.QuoteChar);
rx.Expression := '^\s*CREATE\s+TABLE\s+' + Quote + '[^'+Quote+']+' + Quote + '.' + Quote + '([^'+Quote+']+)' + Quote;
Mainform.LogSQL(rx.Expression, lcDebug);
if rx.Exec(SynMemoCreateTable.Text) then
TableName := rx.Match[1]
else
TableName := '';
Mainform.LogSQL(TableName, lcDebug);
FLoadDataFrm.comboTablePopulate(TableName, True);
Screen.Cursor := crDefault;
except
on E:EDbError do begin
Screen.Cursor := crDefault;
ErrorDialog(E.Message);
ModalResult := mrNone;
end;
end;
end;
end.