-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKLib.MySQL.Process.pas
More file actions
249 lines (214 loc) · 6.96 KB
/
Copy pathKLib.MySQL.Process.pas
File metadata and controls
249 lines (214 loc) · 6.96 KB
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
{
KLib Version = 4.0
The Clear BSD License
Copyright (c) 2020 by Karol De Nery Ortiz LLave. All rights reserved.
zitrokarol@gmail.com
Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
}
unit KLib.MySQL.Process;
interface
uses
KLib.MySQL.Credentials, KLib.MySQL.Info,
KLib.VC_Redist, KLib.Types, KLib.Constants;
type
TMySQLProcess = class
private
function checkIfMySQLIsStarted: boolean;
function getMySQLCredentials: KLib.MySQL.Credentials.TCredentials;
function getCredentials: KLib.Types.TCredentials;
procedure setCredentials(value: KLib.Types.TCredentials);
function getPort: integer;
procedure setPort(value: integer);
procedure setFirstPortAvaliable(enable: boolean);
procedure waitUntilProcessStart;
public
info: TMySQLInfo;
property isStarted: boolean read checkIfMySQLIsStarted;
property mySQLCredentials: KLib.MySQL.Credentials.TCredentials read getMySQLCredentials;
property credentials: KLib.Types.TCredentials read getCredentials write setCredentials;
property port: integer read getPort write setPort;
constructor create(mySQLInfo: TMySQLInfo);
procedure start(autoGetFirstPortAvaliable: boolean = true);
procedure stop(force: boolean = NOT_FORCE);
destructor Destroy; override;
end;
implementation
uses
System.SysUtils, System.UITypes,
Vcl.Dialogs, Vcl.Controls,
KLib.Utils, KLib.Validate, KLib.Windows, KLib.StringUtils,
KLib.MySQL.Utils, KLib.MySQL.CLIUtilities, KLib.MySQL.Validate;
constructor TMySQLProcess.create(mySQLInfo: TMySQLInfo);
const
ERR_MSG = 'MySQL version were not being specified.';
var
_tempCredentials: KLib.MySQL.Credentials.TCredentials;
begin
Self.info := mySQLInfo;
validateThatFileExists(Self.info.path_ini);
validateThatFileExists(Self.info.path_mysqld);
validateThatDirExists(Self.info.path_datadirIniFile);
case Self.info.version of
TMySQLVersion.v5_7:
;
TMySQLVersion.v_8:
;
else
raise Exception.Create(ERR_MSG);
end;
_tempCredentials := Self.info.credentials;
with _tempCredentials do
begin
server := LOCALHOST_IP_ADDRESS;
database := EMPTY_STRING;
end;
Self.info.credentials := _tempCredentials;
end;
procedure TMySQLProcess.start(autoGetFirstPortAvaliable: boolean = true);
const
ERR_MSG_VC_REDIST_NOT_INSTALLED = 'Microsoft Visual C++ Redistributable not installed.';
SHOW_WINDOW_HIDE = _SW_HIDE;
RAISE_EXCEPTION_IF_FUNCTION_FAILS = true;
NOT_ACCORDING_WINDOWS_ARCHITECTURE = false;
var
_mysqldParams: string;
_doubleQuotedIniPath: string;
_isVC_RedistInstalled: boolean;
begin
if not isStarted then
begin
_isVC_RedistInstalled := false;
case Self.info.version of
TMySQLVersion.v5_7:
_isVC_RedistInstalled := checkIfVC_Redist2013IsInstalled(NOT_ACCORDING_WINDOWS_ARCHITECTURE);
TMySQLVersion.v_8:
_isVC_RedistInstalled := checkIfVC_Redist2019X64IsInstalled;
end;
if not _isVC_RedistInstalled then
begin
raise Exception.Create(ERR_MSG_VC_REDIST_NOT_INSTALLED);
end;
setFirstPortAvaliable(autoGetFirstPortAvaliable);
_doubleQuotedIniPath := getDoubleQuotedString(info.path_ini);
_mysqldParams := ' --defaults-file=' + _doubleQuotedIniPath;
shellExecuteExe(info.path_mysqld, _mysqldParams, SHOW_WINDOW_HIDE, RAISE_EXCEPTION_IF_FUNCTION_FAILS);
waitUntilProcessStart;
end;
end;
procedure TMySQLProcess.setFirstPortAvaliable(enable: boolean);
begin
if enable then
begin
port := getFirstPortAvaliable(port);
end;
end;
procedure TMySQLProcess.waitUntilProcessStart;
const
MSG_WAIT = 'Apparentely MySQL takes long time to start, would you wait?.';
ERR_MSG = 'MySQL not started.';
var
i: integer;
_exit: boolean;
begin
i := 0;
_exit := false;
while not _exit do
begin
if (i > 10) then
begin
if messagedlg(MSG_WAIT, mtCustom, [mbYes, mbCancel], 0) = mrYes then
begin
i := 0;
end
else
begin
_exit := true;
end;
end;
try
validateMySQLCredentials(info.credentials);
_exit := true;
except
Inc(i, 1);
sleep(3000);
end;
end;
if not isStarted then
begin
raise Exception.Create(ERR_MSG);
end;
end;
procedure TMySQLProcess.stop(force: boolean = NOT_FORCE);
begin
if isStarted then
begin
mysqladminShutdown(info.path_mysqladmin, info.credentials, force);
end;
end;
function TMySQLProcess.checkIfMySQLIsStarted: boolean;
var
_result: boolean;
begin
_result := checkMySQLCredentials(mySQLCredentials);
Result := _result;
end;
function TMySQLProcess.getMySQLCredentials: KLib.MySQL.Credentials.TCredentials;
var
_mySQLCredentials: KLib.MySQL.Credentials.TCredentials;
begin
with _mySQLCredentials do
begin
credentials := Self.credentials;
port := Self.port;
server := LOCALHOST_IP_ADDRESS;
database := EMPTY_STRING;
end;
Result := _mySQLCredentials;
end;
function TMySQLProcess.getCredentials: KLib.Types.TCredentials;
begin
Result := info.credentials.credentials;
end;
procedure TMySQLProcess.setCredentials(value: KLib.Types.TCredentials);
var
_tempCredentials: KLib.MySQL.Credentials.TCredentials;
begin
_tempCredentials := info.credentials;
_tempCredentials.credentials := value;
info.credentials := _tempCredentials;
end;
procedure TMySQLProcess.setPort(value: integer);
begin
info.portIniFile := value;
end;
function TMySQLProcess.getPort: integer;
begin
result := info.credentials.port;
end;
destructor TMySQLProcess.Destroy;
begin
inherited;
end;
end.