-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDBUtil.dpr
315 lines (283 loc) · 10.9 KB
/
EDBUtil.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
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
program EDBUtil;
{$APPTYPE CONSOLE}
uses
DB,
Math,
Classes,
AnsiStrings,
SysUtils,
StrUtils,
Variants,
edbcomps,
edbtype,
IniFiles,
DatasetJSON in 'DatasetJSON.pas',
ConsoleHelper in 'ConsoleHelper.pas',
SessionManager in 'SessionManager.pas',
SetDBPath in 'Commands\SetDBPath.pas',
CmdLineParameters in 'CmdLineParameters.pas',
QueryCmd in 'Commands\QueryCmd.pas',
CommandBase in 'Commands\CommandBase.pas',
VerifyRepairCmd in 'Commands\VerifyRepairCmd.pas',
RestoreCmd in 'Commands\RestoreCmd.pas',
ExportToMSSQLCmd in 'Commands\ExportToMSSQLCmd.pas',
ImportCSVCmd in 'Commands\ImportCSVCmd.pas',
ReleaseSessionsCmd in 'Commands\ReleaseSessionsCmd.pas',
CopyToStoreCmd in 'Commands\CopyToStoreCmd.pas',
MustacheDefCmd in 'Commands\MustacheDefCmd.pas',
JSONHelper in 'JSONHelper.pas',
SynCommons in 'Synopse\SynCommons.pas',
SynMustache in 'Synopse\SynMustache.pas',
SynLZ in 'Synopse\SynLZ.pas';
var
isVerbose : Boolean;
procedure ShowHelp;
begin
writeln('Elevate Database Utilities');
Writeln('Copyright(c) 2011 Bay Lakes Information Systems LLC');
writeln('A utility for a variety of functions for Elevate DB.');
writeln('');
writeln('Usage: EDBUtil <command> <parameters>');
writeln('Commands:');
writeln(' /setDBPath - sets the path for the database.');
writeln(' Params:');
writeln(' /configPath - path of the configuration for the database engine');
writeln(' /newPath - the new path for the database');
writeln;
writeln(' /query - executes a query against the database.');
writeln(' Params:');
writeln(' /sql - sql to execute (use @ to specify a file containing the sql)');
writeln(' /maxstring - maximum string length (default 50)');
writeln(' /dump - show the SQL statements (default false)');
Writeln(' /format - the format of the output. Possible options are Table, CSV, JSON, mustache. (default table)');
writeln(' /NoHeader - header not included in output.');
writeln(' /Mustache - Mustache template (use @ to specify a file containing a mustache template). NOTE: Use {{#ds}}{{/ds}} for section');
writeln;
writeln(' /mustacheDef - creates a JSON form of a table definition, and can map it to a mustache file.');
writeln(' Params:');
writeln(' /table - name of table for which to generate JSON definition');
writeln(' /mustach - Mustache template (use @ to specify a file containing a mustache template). NOTE: Use {{#tableDef}}{{#fields}}{{/fields}}{{/tableDef}} for section');
writeln;
writeln(' /verify - runs VERIFY on table.');
writeln(' Params:');
writeln(' /table - name of table to verify (default *, means verify all)');
Writeln(' /startat - when repairing all, you can start at a specific table. Tables are in alpha order. (optional)');
writeln;
writeln(' /repair - runs REPAIR on table.');
writeln(' Params:');
writeln(' /table - name of table to repair (default *, means repair all)');
Writeln(' /startat - when repairing all, you can start at a specific table. Tables are in alpha order. (optional)');
writeln;
writeln(' /restore - restores a backup to the database specified by /db parameter.');
writeln(' Params:');
writeln(' /store - name of the store which contains the backup (required)');
writeln(' /name - name of the backup file to restore. Use * to restore the file with the latest timestamp (required)');
writeln(' /quiet - do not verify with user the name of the latest backup (e.g. when /name * is used)');
writeln;
writeln(' /Import - imports a CSV file into a table');
writeln(' Params:');
writeln(' /table - table to which to import the data');
writeln(' /input - CSV file from which to get the data');
writeln(' /hasHeader - indicates that the CSV file''s first line is a header');
writeln(' /op - required. Specify APPEND to add the records to existing data. Specify OVERWRITE to delete existing data.');
writeln(' /map - list of field names and column position in CSV in the form "fieldnameA=x;fieldnameB=y"');
writeln(' Where fieldNameA and fieldNameB are names in the table');
writeln(' x and y are the column positions to import into the table.');
writeln(' if the /map parameter starts with @, then the mappings are read from a file');
writeln(' /disableTriggers - turn off triggers on table. This is useful if there are triggers that update');
writeln(' data (like LastUpdatedDate).');
Writeln(' /trueValue - the value in a column that represents TRUE. Everything else is false. Default true');
WriteLn(' /truncStrings - Truncate strings that are too long');
Writeln;
Writeln(' /MSSQL - exports create table and data');
writeln(' Params:');
writeln(' /table - table for which to create MSSQL stuff');
writeln;
Writeln(' /ReleaseSessions - releases all of the sessions on the selected database.');
Writeln;
Writeln(' /CopyToStore - copys a local file to an Elevate store. Useful when copying workstation files to the server.');
Writeln(' Params:');
Writeln(' /file - name of the file to copy');
Writeln(' /store - name of the Elevate store to which to copy');
writeln;
Writeln(' /CreateDB - creates a new database');
Writeln(' Params:');
Writeln(' /name - name of the database');
Writeln(' /path - location of database');
writeln;
writeln('All commands require the following parameters:');
writeln(' /db - database name');
writeln(' /userid - database user id');
writeln(' /pwd - database password');
writeln(' /hostName - name of the server');
writeln(' /hostPort - the port number to communicate with the server (default 12010)');
writeln;
writeln('Special Switches:');
writeln(' /verbose displays more information');
writeln(' /nostop won''t wait for enter key after exception');
end;
procedure GetStandardParams(var hostName, userId, pwd, db : String; var hostPort : Integer);
begin
hostName := ParamAsString('/hostName', true);
userId := ParamAsString('/userid', true);
pwd := ParamAsString('/pwd', true);
db := ParamAsString('/db', true);
hostPort := ParamAsInteger('/hostPort', false, '12010');
end;
var
hostName, userId, pwd, db : String;
hostPort : Integer;
storeName : String;
begin
CmdLineParameters.Initialize;
ExitCode := 0;
try
// the first parameter indicates the operation:
if params.Count = 0 then
begin
ShowHelp;
ExitCode := 1;
end
else if lowercase(params[0]) = '/help' then
ShowHelp
else // real command
begin
isVerbose := ParamExists('/verbose');
ConsoleHelper.SetVerbosity(isVerbose);
GetStandardParams(hostName, userid, pwd, db, hostPort);
if lowercase(params[0]) = '/setdbpath' then
begin
SetDatabasePath(ParamAsString('/configPath', true),
ParamAsString('/newPath', true),
db,
userid,
pwd,
hostName,
hostPort);
end
else if lowercase(params[0]) = '/query' then
begin
Query(ParamAsString('/sql', true),
ParamAsInteger('/maxString', false, '50'),
db,
userId,
pwd,
hostName,
hostPort,
ParamExists('/dump'),
LowerCase(ParamAsString('/format', False, 'Table')),
Not ParamExists('/noheader'),
ParamAsString('/mustache'));
end
else if lowercase(params[0]) = '/verify' then
begin
VerifyRepair('VERIFY',
ParamAsString('/table', false, '*'),
ParamAsString('/startat', False, ''),
db,
userid,
pwd,
hostName,
hostPort);
end
else if lowercase(params[0]) = '/repair' then
begin
VerifyRepair('REPAIR',
ParamAsString('/table', false, '*'),
ParamAsString('/startat', False, ''),
db,
userid,
pwd,
hostName,
hostPort)
end
else if lowercase(params[0]) = '/restore' then
begin
storeName := ParamAsString('/store', true);
Restore(
storeName,
ParamAsString('/name', true),
Not ParamExists('/quiet'),
db,
userId,
pwd,
hostName,
hostPort);
end
else if lowercase(params[0]) = '/mssql' then
begin
ExportToMSSQL(
ParamAsString('/table', false, '*'),
db,
userId,
pwd,
hostName,
hostPort);
end
else if lowercase(params[0]) = '/import' then
begin
ImportCSV(ParamAsString('/table', true),
ParamAsString('/input', true),
ParamExists('/hasHeader'),
ParamAsString('/map', true),
ParamAsString('/op', true),
ParamExists('/disableTriggers'),
ParamAsString('/date', False, ''),
ParamAsString('/truevalue', False, 'true'),
ParamExists('/truncStrings'),
db,
userId,
pwd,
hostName,
hostPort);
end
else if LowerCase(params[0]) = '/releasesessions' then
begin
ReleaseSessions(db,
userId,
pwd,
hostName,
hostPort);
end
else if LowerCase(params[0]) = '/copytostore' then
CopyToStore(ParamAsString('/file', true),
ParamAsString('/store', true),
userId,
pwd,
hostName,
hostPort)
else if LowerCase(params[0]) = '/delstorefile' then
Query(Format('Delete File "%s" FROM store "%s"', [ParamAsString('/file', true), ParamAsString('/store', true)]), 255, 'Configuration', userId, pwd, hostName, hostPort, false, '', false, '')
else if LowerCase(params[0]) = '/mustachedef' then
begin
MustacheDef(ParamAsString('/table', true),
ParamAsString('/mustache', false),
ParamAsString('/extraJSON', false),
db,
userId,
pwd,
hostName,
hostPort);
end
else if LowerCase(params[0]) = '/test' then
// Test(db,
// userId,
// pwd,
// hostName,
// hostPort)
else
begin
ShowHelp;
ExitCode := 1;
end;
end;
except
on E:Exception do
begin
ExitCode := 100;
WritelnError(E.Classname + ': ' + E.Message);
if not ParamExists('/nostop') then
readln;
end;
end;
end.