File: frmsqlconnect.pp

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (231 lines) | stat: -rw-r--r-- 6,993 bytes parent folder | download | duplicates (3)
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
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************
}
unit frmSQLConnect;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, ExtCtrls, ComCtrls, ButtonPanel, lazdatadeskstr, dmImages;

type
  TTestConnectionEvent = Procedure (Sender : TObject;Const ADriver : String; Params : TStrings) of object;

  { TSQLConnectionForm }

  TSQLConnectionForm = class(TForm)
    BTest: TBitBtn;
    BPButtons: TButtonPanel;
    ECharset: TEdit;
    EHostName: TEdit;
    EDatabaseName: TEdit;
    EUserName: TEdit;
    EPassword: TEdit;
    LCharset: TLabel;
    LEUserName: TLabel;
    LEPassword: TLabel;
    LEHostName: TLabel;
    LEDatabaseName: TLabel;
    Panel1: TPanel;
    procedure BTestClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FDriver : String;
    FOnTest: TTestConnectionEvent;
    function GetShowHost: Boolean;
    function GetString(Index: integer): String;
    procedure SetOnTest(AValue: TTestConnectionEvent);
    procedure SetShowHost(const AValue: Boolean);
    procedure SetString(Index: integer; const AValue: String);
    procedure TestConnection;
    { private declarations }
  public
    { public declarations }
    Property ShowHost : Boolean Read GetShowHost Write SetShowHost;
    Property HostName : String Index 0 Read GetString Write SetString;
    Property DatabaseName : String Index 1 Read GetString Write SetString;
    Property UserName : String Index 2 Read GetString Write SetString;
    Property Password : String Index 3 Read GetString Write SetString;
    Property Charset : String Index 4 Read GetString Write SetString;
    Property Driver : String Index 5 Read GetString Write SetString;
    Property OnTestConnection : TTestConnectionEvent Read FOnTest Write SetOnTest;
  end; 

var
  SQLConnectionForm: TSQLConnectionForm;

function GetSQLDBConnectString(HostSupported : Boolean; Initial,ADriver : String; OnTest : TTestConnectionEvent = Nil): String;

implementation

{$R *.lfm}

uses fpdatadict, fpddsqldb ,strutils;

function GetSQLDBConnectString(HostSupported : Boolean; Initial,ADriver : String; OnTest : TTestConnectionEvent = Nil): String;

Var
  L: TStringList;

begin
  Result:='';
  With TSQLConnectionForm.Create(Application) do
    try
      ShowHost:=HostSupported;
      L:=TStringList.Create;
      try
        if (Initial<>'') then
          begin
          L.CommaText:=Initial;
          if HostSupported then
            HostName:=L.Values[KeyHostName];
          DatabaseName:=L.Values[KeyDatabaseName];
          UserName:=L.Values[KeyUserName];
          Password:=XorDecode(KeyEncode,L.Values[KeyPassword]);
          Charset:=L.Values[KeyCharset];
          end;
        Driver:=ADriver;
        OnTestConnection:=OnTest;
        if (ShowModal=mrOK) then
          begin
          L.Clear;
          if HostSupported then
            L.Values[KeyHostName]:=HostName;
          L.Values[KeyDatabaseName]:=DatabaseName;
          L.Values[KeyUserName]:=UserName;
          L.Values[KeyPassword]:=XorEncode(KeyEncode,Password);
          L.Values[KeyCharset]:=Charset;
          Result:=L.CommaText;
          end;
      finally
        L.Free;
      end;
    finally
      Free;
    end;
end;

procedure TSQLConnectionForm.FormCreate(Sender: TObject);
begin
  //
  Caption:= Format(sld_Connecttoadatabase,[sld_UnknownType]);
  LEHostName.Caption:= sld_Host;
  LEDatabaseName.Caption:= sld_Database;
  LEUserName.Caption:= sld_Username;
  LEPassword.Caption:= sld_Password;
  LCharset.Caption:= sld_Charset;
  BTest.Caption:=sld_TestConnection;
  //
end;

procedure TSQLConnectionForm.TestConnection;

Var
  P : TStrings;

begin
  if Not Assigned(FOnTest) then
    exit;
  P:=TStringList.Create;
  try
    if ShowHost then
      P.Values[KeyHostName]:=HostName;
    P.Values[KeyDatabaseName]:=DatabaseName;
    P.Values[KeyUserName]:=UserName;
    P.Values[KeyPassword]:=XorEncode(KeyEncode,Password);
    P.Values[KeyCharset]:=CharSet;
    FOnTest(Self,Driver,P);
    // No errors.
    ShowMessage(sld_SuccesConnecting);
  finally
    P.Free;
  end;
end;

procedure TSQLConnectionForm.BTestClick(Sender: TObject);

begin
  TestConnection;
end;

procedure TSQLConnectionForm.FormActivate(Sender: TObject);
begin
  ClientHeight := Panel1.Height + BPButtons.Height + 2*BPButtons.BorderSpacing.Around;
  Constraints.MinHeight := Height;
  Constraints.MaxHeight := Height;
end;

function TSQLConnectionForm.GetShowHost: Boolean;
begin
  Result:=EHostName.Enabled;
end;

function TSQLConnectionForm.GetString(Index: integer): String;
begin
  Case Index of
    0 : Result:=EHostName.Text;
    1 : Result:=EDatabaseName.Text;
    2 : Result:=EUserName.Text;
    3 : Result:=EPassword.Text;
    4 : Result:=ECharSet.Text;
    5 : Result:=FDriver;
  end;
end;

procedure TSQLConnectionForm.SetOnTest(AValue: TTestConnectionEvent);
begin
  if FOnTest=AValue then Exit;
  FOnTest:=AValue;
  BTest.Enabled:=AValue<>Nil;
end;

procedure TSQLConnectionForm.SetShowHost(const AValue: Boolean);
begin
  EHostName.Enabled:=AValue;
end;

procedure TSQLConnectionForm.SetString(Index: integer; const AValue: String);


var
  ADesc, AType: String;
  ACap: TFPDDEngineCapabilities;
begin
  Case Index of
    0 : EHostName.Text:=AValue;
    1 : EDatabaseName.Text:=AValue;
    2 : EUserName.Text:=AValue;
    3 : EPassword.Text:=AValue;
    4 : ECharset.Text:=Avalue;
    5 :
      begin
      FDriver:=AValue;
      if GetDictionaryEngineInfo(AValue,ADesc,AType,ACap) then
        Caption:= Format(sld_Connecttoadatabase,[ADesc]);
      end;
  end;
end;

end.