forked from vladk1973/npp.connections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionFormHelpersUnit.pas
181 lines (158 loc) · 4.61 KB
/
connectionFormHelpersUnit.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
unit connectionFormHelpersUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.ComCtrls, Vcl.WinXCtrls, TreeViewExUnit, plugin;
type
TSmallWinControlHelper = class helper for TWinControl
procedure ShowMe;
procedure HideMe;
end;
TSplitViewHelper = class helper for TSplitView
private
const constInCatalog = 'Initial Catalog=%s;';
const constConnectionMSSQL = 'Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;%sData Source=%s';
const 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';
const constConnectionODBC = 'Provider=MSDASQL.1;Persist Security Info=False;Data Source=%s';
public
class function GetConnectionString(DataSource: string; const DataBase: string = ''): string;
function CurrentTreeView: TTreeViewEx;
function CurrentNode: TTreeNode;
function CurrentBase: string;
function CurrentUser: string;
function CurrentServer: string;
function ConnectionString: string;
function BdType: TBdType;
end;
implementation
{ TSplitViewHelper }
function TSplitViewHelper.BdType: TBdType;
var
i: Integer;
begin
for i := 0 to Self.ControlCount-1 do
begin
if (Self.Controls[i] is TTreeViewEx) and TTreeViewEx(Self.Controls[i]).Visible then
begin
Result := TTreeViewEx(Self.Controls[i]).BdType;
break;
end;
end;
end;
function TSplitViewHelper.ConnectionString: string;
var
Node: TTreeNode;
begin
Result := '';
Node := Self.CurrentNode;
if Assigned(Node) then
begin
if Node.ItemType = itLogin then Exit;
if Node.ItemType in [itServerMS,itServerSYB,itODBC] then
Result := GetConnectionString(TTreeNodeEx(Node).DataSource);
if Node.ItemType in [itBase,itBaseRTI] then
Result := GetConnectionString(TTreeNodeEx(Node.Parent).DataSource,Node.Text);
end;
end;
function TSplitViewHelper.CurrentBase: string;
var
Node: TTreeNode;
begin
Result := 'Unknown';
Node := Self.CurrentNode;
if Assigned(Node) then
begin
if Node.ItemType in [itBase,itBaseRTI] then
Result := Node.Text;
end;
end;
function TSplitViewHelper.CurrentNode: TTreeNode;
begin
Result := CurrentTreeView.Selected;
end;
function TSplitViewHelper.CurrentServer: string;
var
Node: TTreeNode;
begin
Result := '';
Node := Self.CurrentNode;
if Assigned(Node) then
begin
if Assigned(Node.Parent) then Node := Node.Parent;
Result := Node.Text;
end;
end;
function TSplitViewHelper.CurrentTreeView: TTreeViewEx;
var
i: Integer;
begin
Result := nil;
for i := 0 to Self.ControlCount-1 do
if (Self.Controls[i] is TTreeViewEx) and TTreeViewEx(Self.Controls[i]).Visible then
begin
Result := TTreeViewEx(Self.Controls[i]);
break;
end;
end;
function TSplitViewHelper.CurrentUser: string;
var
Node: TTreeNode;
i: Integer;
begin
Result := 'Unknown';
Node := Self.CurrentNode;
if Assigned(Node) then
begin
if Assigned(Node.Parent) then Node := Node.Parent;
for i := 0 to Node.Count-1 do
if Node.Item[i].ItemType = itLogin then
begin
Result := Node.Item[i].Text;
Exit;
end;
end;
end;
class function TSplitViewHelper.GetConnectionString(DataSource: string;
const DataBase: string = ''): string;
var
Strings: TStringList;
DB,FServer,FPassword,FLogin,FDataSource,FPort: string;
begin
Strings := TStringList.Create;
try
Strings.Delimiter := '|';
Strings.DelimitedText := DataSource;
DB := '';
FDataSource := Strings[0];
FServer := Strings.Values['Server'];
FPort := Strings.Values['Port'];
FLogin := Strings.Values['Login'];
FPassword := Strings.Values['Password'];
if DataBase <> '' then DB := Format(constInCatalog,[DataBase]);
if TBdType(Strings.Values['BdType'].ToInteger) = bdMSSQL then
Result := Format(constConnectionMSSQL,[FPassword,FLogin,DB,FDataSource])
else
if TBdType(Strings.Values['BdType'].ToInteger) = bdSybase then
Result := Format(constConnectionSybase,[FPassword,FLogin,FServer,FPort,DB])
else
begin
Result := Format(constConnectionODBC,[FDataSource]);
if FLogin <> '' then Result := Result + ';User ID=' + FLogin;
if FPassword <> '' then Result := Result + ';Password=' + FPassword;
end;
finally
Strings.Free;
end;
end;
{ TSmallWinControlHelper }
procedure TSmallWinControlHelper.HideMe;
begin
Self.Align := alNone;
Self.Visible := False;
end;
procedure TSmallWinControlHelper.ShowMe;
begin
Self.Align := alClient;
Self.Visible := True;
end;
end.