forked from vladk1973/npp.connections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstUnit.pas
383 lines (334 loc) · 11.4 KB
/
ConstUnit.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
unit ConstUnit;
interface
uses Winapi.Windows, Winapi.Messages, System.StrUtils,
Vcl.Controls, System.Classes, System.SysUtils,
System.Math;
type
TCommand = string;
TPathName = string;
TFileName = string;
TModalResultArray = array[False..True] of TModalResult;
TGarbageArray = array[0..9] of string;
TBeginProcArray = array[0..4] of string;
TBeginAlignArray = array[0..9] of string;
TOperatorsArray = array[0..3] of string;
TShowMode = (shSql,shPr,shCI);
TCursorMode = (crNormal,crWait);
TBdType = (bdMSSQL, bdSybase, bdPostgreSQL, bdODBC);
TBdTypeStringArray = array [TBdType] of string;
//TItemType - the order of the types must match TBdType
TItemType = (itServerMS,itServerSYB,itServerPostgreSQL,itODBC,itBase,itBaseRTI,itLogin);
THelpType = (spHelp, spHelpindex, spHelpText);
THelpArray = array[TBdType,THelpType] of string;
TStringProcedure = procedure(const SqlText: string) of object;
const
WM_USER_MESSAGE_FROM_THREAD = WM_USER + 1;
cnstModalResultArray: TModalResultArray = (mrCancel,mrOk);
constDBList = 'SELECT name FROM master..sysdatabases ORDER BY name';
constPostgreDBList = 'SELECT datname FROM pg_database;';
{$IFNDEF NPPCONNECTIONS}
cnstDllKey = 'Software\dia2notepad';
constDSBDCaptionArray: TBdTypeStringArray = ('Ñåðâåð','Ñåðâåð','Ñåðâåð','Èñòî÷íèê');
cnstErroCaption = 'Îøèáêà';
cnstNoBaseSelected = '×òîáû âûïîëíèòü SQL çàïðîñ, íåîáõîäèìî âûáðàòü ñåðâåð è áàçó!';
cnstConfirm = 'Ïîäòâåðæäåíèå';
cnstSqlNoPossible = 'Âûïîëíåíèå êîìàíäû íåâîçìîæíî';
cnstSqlNoResults = 'Êîìàíäà âûïîëíåíà';
constLoginBDCaption = 'Ïîäêëþ÷åíèå ê ';
cnstSqlExec = 'Ðåçóëüòàòû: %s\%s (%s)';
cnstOpenPlanMessage = 'Îòêðûòü ïëàí çàïðîñà?';
{$ELSE}
cnstDllKey = 'Software\nppconnections';
constDSBDCaptionArray: TBdTypeStringArray = ('Server','Server','Server','Source');
cnstErroCaption = 'Error';
cnstNoBaseSelected = 'Please select Server and Base before before execution!';
cnstConfirm = 'Confirm';
cnstSqlNoPossible = 'Command execution is not possible';
cnstSqlNoResults = 'The command is executed';
constLoginBDCaption = 'Connect to ';
cnstSqlExec = 'Results: %s\%s (%s)';
cnstOpenPlanMessage = 'Open a query plan?';
{$ENDIF}
cnstNumbers = '0123456789';
cnstAlphabet = '0123456789abcdefghijklmnopqrstuvwxyz_()';
cnstAlphabetStartWord = 'abcdefghijklmnopqrstuvwxyz';
cnstGarbageArray: TGarbageArray =(
'--',
'/*',
'(',
')',
'\',
' ',
#09,
#13#10,
#13,
#10
);
cnstBeginProcArray: TBeginProcArray =(
'create proc ',
'create procedure ',
'dcl_proc_begin(',
'api_create_proc(',
'create or replace '
{'create trigger',
'create view ',
'create table ',
'#include ',
'#define '}
);
cnstAlignStartWords: TBeginAlignArray = (
'declare',
'execute',
'exec' ,
'@',
'select',
'if',
'begin',
'insert',
'delete',
'while'
);
cnstShowPlanXML_ON = 'SET SHOWPLAN_XML ON';
cnstShowPlanXML_OFF = 'SET SHOWPLAN_XML OFF';
cnstShowPlan_ON = 'SET SHOWPLAN ON';
cnstShowPlan_OFF = 'SET SHOWPLAN OFF';
cnstShowSpHelp = 'SP_HELP %s';
cnstShowSpHelpIndex = 'SP_HELPINDEX %s';
cnstShowSpHelpText = 'SP_HELPTEXT %s';
cnstShowPlan_PostgreSQL = 'EXPLAIN ';
cnstShowSpHelp_PostgreSQL = 'SELECT ''%s'' AS name;'+sLineBreak+
'SELECT * FROM information_schema.columns WHERE table_name = ''%0:s'';'+sLineBreak+
'SELECT * FROM pg_indexes WHERE tablename = ''%0:s'';';
cnstShowSpHelpIndex_PostgreSQL = 'SELECT * FROM pg_indexes WHERE tablename = ''%s'';';
cnstShowSpHelpText_PostgreSQL = 'SELECT'+sLineBreak+
'pg_get_functiondef(('+sLineBreak+
'SELECT'+sLineBreak+
'oid FROM pg_proc'+sLineBreak+
'WHERE'+sLineBreak+
'proname = ''%s''));';
cnstHelpArray: THelpArray = ((cnstShowSpHelp,cnstShowSpHelpIndex,cnstShowSpHelpText),
(cnstShowSpHelp,cnstShowSpHelpIndex,cnstShowSpHelpText),
(cnstShowSpHelp_PostgreSQL,cnstShowSpHelpIndex_PostgreSQL,cnstShowSpHelpText_PostgreSQL),
('%s','%s','%s')
);
{$IFNDEF NPPCONNECTIONS}
cnstGetTables_MsSql = 'SELECT name FROM sys.tables WHERE name LIKE ''[t,p]%'' ORDER BY name';
cnstGetTables_Sybase = 'SELECT name FROM sysobjects WHERE type = ''U'' AND name LIKE ''[t,p]%'' ORDER BY name';
cnstGetTables_PostgreSQL = 'SELECT tablename FROM pg_catalog.pg_tables WHERE tablename SIMILAR TO ''[p|t]%'' ORDER BY tablename;';
{$ELSE}
cnstGetTables_MsSql = 'SELECT name FROM sys.tables ORDER BY name';
cnstGetTables_Sybase = 'SELECT name FROM sysobjects WHERE type = ''U'' ORDER BY name';
cnstGetTables_PostgreSQL = 'SELECT tablename FROM pg_catalog.pg_tables ORDER BY tablename;';
{$ENDIF}
cnstGetTablesArray: TBdTypeStringArray = (cnstGetTables_MsSql,cnstGetTables_Sybase,cnstGetTables_PostgreSQL,'');
cnstGetFieldsSQL = 'select * from %s where 1=0';
cnstAseOleDB = '[ASEOLEDB]';
cnstShowPlan = 'PLAN:';
cnstShowIndx = 'INDX:';
cnstSqlplanExt = '.sqlplan';
cnstGo = 'go';
cnstT1 = 'join ';
cnstT2 = 'from ';
cnstOperator1 = 'select ';
cnstOperator2 = 'insert ';
cnstOperator3 = 'update ';
cnstOperator4 = 'delete ';
cnstOperators: TOperatorsArray = (cnstOperator1,cnstOperator2,cnstOperator3,cnstOperator4);
cnstTracingChar = 40;
cnstTracingFieldChar = 46;
constConnectionMSSQL = 'Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;%sData Source=%s';
constConnectionSybase = 'Provider=ASEOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Data Source=%s:%s;%sExtended Properties="LANGUAGE=us_english";Connect Timeout=3';
constConnectionPostgre = 'Provider=MSDASQL.1;Password=%s;Persist Security Info=True;User ID=%s;Extended Properties="DRIVER={PostgreSQL Unicode};SERVER=%s;PORT=%s;%s"';
constConnectionODBC = 'Provider=MSDASQL.1;Persist Security Info=False;Data Source=%s';
constConnectionStringArray: TBdTypeStringArray = (constConnectionMSSQL,
constConnectionSybase,
constConnectionPostgre,
constConnectionODBC);
constLoginBDCaptionArray: TBdTypeStringArray = ('MSSQL','Sybase','PostgreSQL','ODBC');
constLoginBDArray: TBdTypeStringArray = ('sa','sa','postgres','admin');
function RemoveGarbage(const S: string): string;
function RemoveCarriageReturn(const S: string): string;
function ItIsAWord(Txt: AnsiString): boolean;
function WholeWords(Txt: AnsiString; const NoDuplacates: boolean = False): TStringList;
function WholeWord(const Txt: AnsiString; const WordIndex: Integer): AnsiString;
function WordBefore(const Searched,Txt: AnsiString): AnsiString;
function RemoveComments(S: string): string;
function FindOperator(const S: string): boolean;
function TempPath: string;
implementation
function FindOperator(const S: string): boolean;
var
S0: string;
iPos: integer;
begin
iPos := -1;
for S0 in cnstOperators do
begin
iPos := Max(iPos,S.IndexOf(' ' + S0));
iPos := Max(iPos,S.IndexOf(sLineBreak + S0));
iPos := Max(iPos,S.IndexOf('(' + S0));
if (iPos<0) and ((S.StartsWith(S0)) or (S.Trim = S0.Trim)) then
begin
iPos := 0;
Break;
end;
end;
Result := iPos>=0;
end;
function RemoveGarbage(const S: string): string;
var
j: Longint;
S0: string;
begin
Result := Trim(S);
for S0 in cnstGarbageArray do
begin
j := Result.IndexOf(S0);
if j >= 0 then Result := Copy(Result,1,j);
end;
end;
function RemoveCarriageReturn(const S: string): string;
begin
Result := StringReplace(S,#13#10,'',[rfReplaceAll]);
end;
function ItIsAWord(Txt: AnsiString): boolean;
var i: Longint;
S0,S: string;
begin
S := Trim(string(Txt));
S0 := '';
for i := 1 to Length(S) do
begin
if i = 1 then
begin
if pos(LowerCase(S[i]),cnstAlphabetStartWord) > 0 then
S0 := S0 + S[i];
end
else
if pos(LowerCase(S[i]),cnstAlphabet) > 0 then
S0 := S0 + S[i];
end;
Result := S = S0;
end;
function WholeWords(Txt: AnsiString; const NoDuplacates: boolean = False): TStringList;
var i: Longint;
S0,S: string;
begin
Result := nil;
S := Trim(string(Txt));
S0 := '';
for i := 1 to Length(S) do
begin
if pos(LowerCase(S[i]),'@'+cnstAlphabet) > 0 then
S0 := S0 + S[i]
else
begin
if Length(S0)>0 then
begin
if not Assigned(Result) then
begin
Result := TStringList.Create;
if NoDuplacates then Result.Duplicates := dupIgnore;
end;
Result.Add(S0);
end;
S0 := '';
end;
end;
if Length(S0)>0 then
begin
if not Assigned(Result) then Result := TStringList.Create;
Result.Add(S0);
end;
end;
function WholeWord(const Txt: AnsiString; const WordIndex: Integer): AnsiString;
var
Strings: TStringList;
begin
Result := '';
Strings := WholeWords(Txt);
if Assigned(Strings) then
begin
try
if WordIndex <= Strings.Count then
Result := Strings[WordIndex-1]
else
if (Strings.Count>0) then
Result := Strings[Strings.Count-1]
finally
FreeAndNil(Strings);
end;
end;
end;
function WordBefore(const Searched,Txt: AnsiString): AnsiString;
var
Strings: TStringList;
i: Integer;
begin
Result := '';
Strings := WholeWords(Txt);
if Assigned(Strings) then
begin
try
for i := Strings.Count-1 downto 0 do
begin
if Strings[i] = string(Searched) then
begin
if i>0 then Result := Strings[i-1];
break;
end;
end;
finally
FreeAndNil(Strings);
end;
end;
end;
function RemoveComments(S: string): string;
var
i,ipos, ipos1: Integer;
Strings: TStringList;
begin
S := StringReplace(S,#9,' ',[rfReplaceAll]);
while true do
begin
ipos := pos('/*',S);
ipos1:= pos('*/',S);
if (ipos = 0) and (ipos1 = 0) then break;
if (ipos = 0) and (ipos1 > 0) then S := Copy(S,ipos1+2,MaxInt);
if (ipos > 0) and (ipos1 = 0) then S := Copy(S,1,ipos-1);
if (ipos > 0) and (ipos1 > 0) then
begin
if ipos > ipos1 then S := Copy(S,ipos1+2,MaxInt);
if ipos < ipos1 then S := Copy(S,1,ipos-1) + #13#10 + Copy(S,ipos1+2,MaxInt);
end;
end;
Strings := TStringList.Create;
try
Strings.Text := S;
for i := Strings.Count - 1 downto 0 do
begin
S := Strings[i];
ipos := pos('--',S);
if ipos > 0 then S := Copy(S,1,ipos-1);
S := Trim(S);
if S = '' then
Strings.Delete(i)
else
Strings[i] := S;
end;
S := Strings.Text;
finally
Strings.Free;
end;
Result := S;
end;
function TempPath: string;
var
i: integer;
begin
SetLength(Result, MAX_PATH);
i := GetTempPath(Length(Result), PChar(Result));
SetLength(Result, i);
IncludeTrailingPathDelimiter(Result);
end;
end.