File: MySQLCommonFuncs.pas

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (252 lines) | stat: -rw-r--r-- 8,327 bytes parent folder | download | duplicates (2)
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
unit MySQLCommonFuncs;

interface

uses
  gnugettext, SysUtils, AuxFuncs, Windows, Forms, TntRegistry, TntClasses, TntSysUtils;

function GetMySQLCommandLineClientPath: WideString;
function GetMySQLAdministratorCmd: WideString;
function GetMySQLQueryBrowserCmd: WideString;

procedure RegisterMySQLApplication(Name: WideString;
  Version: WideString; InstallPath: WideString);
function ScanRegistryForProductVersion(RegMainPath: WideString; RegAppName: WideString;
  Key: WideString; KeyType: Integer): WideString;

//----------------------------------------------------------------------------------------------------------------------

implementation

uses
  Math;
  
//----------------------------------------------------------------------------------------------------------------------

function GetMySQLCommandLineClientPath: WideString;

var
  InstallDir: WideString;

begin
  if(FileExists(GetProgramFilesDir+'MySQL\bin\mysql.exe'))then
    Result:=GetProgramFilesDir+'MySQL\bin\mysql.exe'
  else if(FileExists('c:\mysql\bin\mysql.exe'))then
    Result:='c:\mysql\bin\mysql.exe'
  else
  begin
    InstallDir:=ReadFromReg(HKEY_LOCAL_MACHINE,
      '\SOFTWARE\MySQL AB\MySQL Server 4.1', 'Location', 0, '', False);

    if(FileExists(IncludeTrailingPathDelimiter(InstallDir)+
      'bin\mysql.exe'))then
      Result:=IncludeTrailingPathDelimiter(InstallDir)+'bin\mysql.exe'
    else
    begin
      InstallDir:=ReadFromReg(HKEY_LOCAL_MACHINE,
        '\SOFTWARE\MySQL AB\MySQL Server 5.0', 'Location', 0, '', False);
      if (InstallDir='') then
        InstallDir := ScanRegistryForProductVersion('SOFTWARE\MySQL AB', 'MySQL Server', 'Location', 0);


      if(FileExists(IncludeTrailingPathDelimiter(InstallDir)+
        'bin\mysql.exe'))then
        Result:=IncludeTrailingPathDelimiter(InstallDir)+'bin\mysql.exe'
      else
        Result:='';
    end;
  end
end;

//----------------------------------------------------------------------------------------------------------------------

function GetMySQLAdministratorCmd: WideString;

var
  InstallDir: WideString;

begin
  if(FileExists(ExtractFilePath(Application.ExeName)+'MySQLAdministrator.exe'))then
    Result:=ExtractFilePath(Application.ExeName)+'MySQLAdministrator.exe'
  else
  begin
    InstallDir:=ReadFromReg(HKEY_LOCAL_MACHINE,
      '\SOFTWARE\MySQL AB\MySQL Administrator', 'Location', 0, '', False);

    if(FileExists(IncludeTrailingPathDelimiter(InstallDir)+'MySQLAdministrator.exe'))then
      Result:=IncludeTrailingPathDelimiter(InstallDir)+'MySQLAdministrator.exe'
    else
      Result:='';

    if(Result='')then
    begin
      InstallDir:=ReadFromReg(HKEY_LOCAL_MACHINE,
        '\SOFTWARE\MySQL AB\MySQL Administrator 1.0', 'Location', 0, '', False);
      if (InstallDir='') then
        InstallDir := ScanRegistryForProductVersion('SOFTWARE\MySQL AB', 'MySQL Administrator', 'Location', 0);

      if(FileExists(IncludeTrailingPathDelimiter(InstallDir)+'MySQLAdministrator.exe'))then
        Result:=IncludeTrailingPathDelimiter(InstallDir)+'MySQLAdministrator.exe'
      else
        Result:='';
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function GetMySQLQueryBrowserCmd: WideString;

var
  InstallDir: WideString;

begin
  if(FileExists(ExtractFilePath(Application.ExeName)+'MySQLQueryBrowser.exe'))then
    Result:=ExtractFilePath(Application.ExeName)+'MySQLQueryBrowser.exe'
  else
  begin
    InstallDir := ReadFromReg(HKEY_LOCAL_MACHINE,
      '\SOFTWARE\MySQL AB\MySQL Query Browser', 'Location', 0, '', False);

    if(FileExists(IncludeTrailingPathDelimiter(InstallDir)+'MySQLQueryBrowser.exe'))then
      Result:=IncludeTrailingPathDelimiter(InstallDir)+'MySQLQueryBrowser.exe'
    else
      Result:='';

    if(Result='')then
    begin
      InstallDir := ReadFromReg(HKEY_LOCAL_MACHINE,
        '\SOFTWARE\MySQL AB\MySQL Query Browser 1.0', 'Location', 0, '', False);
      if (InstallDir='') then
        InstallDir := ReadFromReg(HKEY_LOCAL_MACHINE,
          '\SOFTWARE\MySQL AB\MySQL Query Browser 1.1', 'Location', 0, '', False);
      if (InstallDir='') then
        InstallDir := ScanRegistryForProductVersion('SOFTWARE\MySQL AB', 'MySQL Query Browser', 'Location', 0);


      if(FileExists(IncludeTrailingPathDelimiter(InstallDir)+'MySQLQueryBrowser.exe'))then
        Result:=IncludeTrailingPathDelimiter(InstallDir)+'MySQLQueryBrowser.exe'
      else
        Result:='';
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function GetSubkeyNameByPrefix(RegMainPath: WideString; RegAppName: WideString) : WideString;

var
  ParentKey: HKEY;
  I: Integer;
  NumSubKeys: Integer;
  MaxKeySize: Integer;
  KeyName: WideString;
  KeyLen: Cardinal;
  L: Integer;
  RegResult: Integer;
  
begin
  Result := '';
  if RegOpenKeyExW(HKEY_LOCAL_MACHINE, PWideChar(RegMainPath), 0, KEY_READ, ParentKey) = ERROR_SUCCESS then
  begin
    if RegQueryInfoKey(ParentKey, nil, nil, nil, @NumSubKeys, @MaxKeySize, nil, nil, nil, nil, nil, nil) = ERROR_SUCCESS then
    begin
      // Add room for terminating 0.
      Inc(MaxKeySize);
      L := Length(RegAppName);
      SetLength(KeyName, MaxKeySize);
      for I := 0 to NumSubKeys - 1 do
      begin
        KeyLen := MaxKeySize;
        RegResult := RegEnumKeyExW(ParentKey, I, PWideChar(KeyName), KeyLen, nil, nil, nil, nil);
        if RegResult = ERROR_SUCCESS then
        begin
          if WideSameText(RegAppName, Copy(KeyName, 1, Min(L, KeyLen))) then
          begin
            Result := Copy(KeyName, 1, KeyLen);
            // Dont break as we are looking for the last key (in the hope the last key is also that with the
            // highest version number).
            // TODO: either return all installed version of one app or truly compare version numbers and pick the highest one.
          end;
        end;
      end;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function ScanRegistryForProductVersion(RegMainPath: WideString; RegAppName: WideString; Key: WideString; KeyType: Integer): WideString;

var
  FullAppKey: WideString;
//  i, j: Integer;

begin
  FullAppKey := RegMainPath + '\' + GetSubkeyNameByPrefix(RegMainPath, RegAppName);
  Result := ReadFromReg(HKEY_LOCAL_MACHINE, FullAppKey, Key, KeyType, '', False);

  // if you're going to uncomment code below notice a bug:
  // there's only one break - which breaks to outer loop,
  // thus always finding the version with lowest major
  // and highest minor numbers
{
  Result := '';

  for i:=10 downto 1 do
  begin
    for j:=30 downto 0 do
    begin
      Result := ReadFromReg(HKEY_LOCAL_MACHINE,
        Format(RegMainPath+'\'+RegAppName+' %d.%d', [i, j]),
        Key, KeyType, '', False);
      if (Result<>'') then
        break;
    end;
  end;
}
end;

//----------------------------------------------------------------------------------------------------------------------

procedure RegisterMySQLApplication(Name: WideString; Version: WideString; InstallPath: WideString);

var
  RegVersion: WideString;
  KeyName: WideString;
  MainVersion: WideString;
  p, p2: integer;
  
begin
  MainVersion:='';

  p:=Pos('.', Version);
  if(p>0)then
  begin
    p2:=Pos('.', Copy(Version, p+1, Length(Version)));

    if(p2>0)then
      MainVersion:=Copy(Version, 1, p2+1);
  end;

  if(MainVersion<>'')then
    KeyName:=Name+' '+MainVersion
  else
    KeyName:=Name;

  RegVersion:=ReadFromReg(HKEY_LOCAL_MACHINE,
    '\SOFTWARE\MySQL AB\'+KeyName, 'Version', 0, '');

  if(RegVersion<>Version)then
  begin
    WriteToReg(HKEY_LOCAL_MACHINE, '\SOFTWARE\MySQL AB\'+KeyName,
      'Version', 0, Version);
    WriteToReg(HKEY_LOCAL_MACHINE, '\SOFTWARE\MySQL AB\'+KeyName,
      'Location', 0, InstallPath);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

end.