-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsvtoolscmd.dpr
221 lines (191 loc) · 6.63 KB
/
csvtoolscmd.dpr
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
program csvtoolscmd;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.StrUtils,
Classes,
dmCSVTools1 in 'dmCSVTools1.pas' {dmCSVTools: TDataModule},
classCSVOptions in 'classCSVOptions.pas',
ClassAppSettings in 'ClassAppSettings.pas',
ClassCSVDatasetExport in '..\SharedCode\ClassCSVDatasetExport.pas',
FunctionsCommandLine in '..\SharedCode\FunctionsCommandLine.pas';
type
TEventHandler = class(TObject)
public
procedure HandleAfterDBConnect(Sender : TObject);
procedure ExportProgressHandler(Sender : TObject; rowcount : integer);
end;
var
dmCSV : TdmCSVTools;
Evnt : TEventHandler;
fAppSettings : TAppSettings;
fLog : TStringList;
{$R *.res}
procedure LoadSettings;
begin
fAppSettings.FileName := ExtractFilePath(paramstr(0)) + 'csvtools.ini';
fAppSettings.ReadSettings;
end;
function DBConnect : boolean;
begin
dmCSV.AfterDBConnect := Evnt.HandleAfterDBConnect;
result := dmCSV.DBConnect(fAppSettings);
end;
procedure Display(const S : string; aAddToLog : boolean = TRUE);
begin
if AAddToLog then
fLog.Add(S);
WriteLn(s);
end;
procedure Main();
var
lCSVOptions: TCSVOptions;
strSetting : string;
intSetting: integer;
aTableName: string;
aFileName: string;
I: Integer;
aParam: string;
aCmdParam: string;
aValue: AnsiString;
intSeparator: integer;
intDelimiter: integer;
begin
fAppSettings := nil;
lCSVOptions := nil;
Evnt := nil;
fLog := nil;
dmCSV := nil;
try
lCSVoptions := TCSVOptions.Create;
Evnt := TEventHandler.Create;
fAppSettings := TAppSettings.Create;
fLog := TStringList.Create;
dmCSV := TdmCSVTools.Create(nil);
{ TODO 1 -oSC -cSettings : Make this cmd line tool capable of multiple export jobs via a single INI file }
{ TODO 1 -oSC -cSettings : Make this cmd line tool capable of multiple export jobs via a multiple INI files }
{ TODO 1 -oSC -cExcel : Add XLSX ability with FlexCel - including adding multiple exports to the same workbook as individual worksheets }
{ TODO 3 -oSC -cThreading : process multiple jobs in parallel - although file access would suggest this would become a problem }
try
LoadSettings;
dmCSV.OnExportProgress := Evnt.ExportProgressHandler;
strSetting := fAppSettings.Setting['Headers'];
intSetting := StrToIntDef(strSetting, 1);
lCSVoptions.Headers := intSetting = 1;
if fAppSettings.Setting['SeparatorChar'] <> EmptyStr then
begin
intSeparator := StrToInt(fAppSettings.Setting['SeparatorChar']);
lCSVOptions.Separator := Char(intSeparator);
end else if fAppSettings.Setting['Separator'] <> EmptyStr then // Legacy setting
begin
lCSVOptions.Separator := fAppSettings.Setting['Separator'];
end else
lCSVOptions.Separator := '';
if fAppSettings.Setting['DelimiterChar'] <> EmptyStr then
begin
intDelimiter := StrToInt(fAppSettings.Setting['DelimiterChar']);
lCSVOptions.Delimiter := Char(intDelimiter);
end else if fAppSettings.Setting['Delimiter'] <> EmptyStr then // Legacy setting
begin
lCSVOptions.Delimiter := fAppSettings.Setting['Delimiter'];
end else
lCSVOptions.Delimiter := '';
strSetting := fAppSettings.Setting['ExportEncoding'];
intSetting := StrToIntDef(strSetting, Ord(enASCII));
dmCSV.FileEncoding := TMyEncoding(intSetting);
aTableName := fAppSettings.Setting['ExportTablename'];
aFileName := fAppSettings.Setting['ExportFilename'];
// Now get command line overrides:-
if GetParamCount > 0 then
begin
for I := 1 to GetParamCount do
begin
aCmdParam := FunctionsCommandLine.GetParamStr(I);
aParam := LeftStr(aCmdParam, Pos('=', aCmdParam)-1);
aValue := RightStr(aCmdParam, Length(aCmdParam)-Length(aParam)-1);
if SameText(aParam, '-tablename') then
aTableName := aValue;
if SameText(aParam, '-filename') then
aFilename := aValue;
if SameText(aParam, '-DriverID') then
fAppSettings.DriverID := aValue; //mssql
if SameText(aParam, '-Server') then
fAppSettings.Server := aValue;
if SameText(aParam, '-Database') then
fAppSettings.DatabaseName := aValue;
if SameText(aParam, '-User_Name') then
fAppSettings.UserName := aValue;
if SameText(aParam, '-Password') then
fAppSettings.Password := aValue;
end;
end;
if fAppSettings.DriverID = EmptyStr then
fAppSettings.DriverID := 'MSSQL';
if not DBConnect then
Raise Exception.Create('Could not connect to database - please check settings');
if aTableName = '' then
Raise Exception.Create('No table name provided');
if aFileName = EmptyStr then
Raise Exception.Create('No export filename provided');
if dmCSV.ExportToCSV(aTableName, aFilename, lCSVOptions) then
begin
Display(Format('Exported row %d from %s', [dmCSV.ExportedRowCount, dmCSV.TableName]));
Display('Export successful, please check the log for more info.')
end
else
Display('Export failed - check the log');
except
on E:Exception do
begin
Display(e.Message + ' (' + e.ClassName + ')');
end;
end;
fLog.AddStrings(dmCSV.ActivityLog);
fLog.SaveToFile(ExtractFilepath(paramstr(0)) + 'csvtoolslog.txt');
finally
dmCSV.free;
lCSVOptions.Free;
Evnt.free;
fLog.Free;
fAppSettings.Free;
end;
end;
procedure DisplayHelp;
begin
WriteLn('CSV Tools - Dataset Exporter');
WriteLn('');
WriteLn('Run the csvtools.exe to configure and test run the export.');
WriteLn('Aternatively, edit csvtools.ini in a text editor.');
WriteLn('Afterwards you can run the csvtoolscmd.exe to automatically');
WriteLn('re-produce the export - it will overwrite any existing file');
WriteLn('');
WriteLn('Once configured, build csvtoolscmd.exe into your scheduled script');
WriteLn('');
WriteLn('Run "csvtoolscmd -help" to display this information.');
WriteLn('');
WriteLn('Press [Enter] to exit');
Readln;
end;
{ TEventHandler }
procedure TEventHandler.ExportProgressHandler(Sender: TObject; rowcount: integer);
begin
// note Sender is TCSVDatasetExport as passed in from dmCSV
if rowcount mod 50 = 0 then
Display(Format('Exported row %d from %s', [rowcount, dmCSV.TableName]), FALSE);
end;
procedure TEventHandler.HandleAfterDBConnect(Sender: TObject);
begin
// ??? Nothing needed
end;
begin
try
if Paramstr(1) = '-help' then
DisplayHelp
else
Main();
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.