@@ -37,14 +37,14 @@ public override void CloseConnection()
3737
3838 public IDbConnection ? Connection => _dbConnection ;
3939
40- public override void OpenConnection ( )
40+ protected override bool OpenConnection ( bool showErrorAlert = false )
4141 {
4242 if ( string . IsNullOrWhiteSpace ( _connectionString ) )
43- return ;
44- if ( IsConnected ( ) ) return ;
43+ return false ;
44+ if ( IsConnected ( ) ) return true ;
4545 lock ( this )
4646 {
47- if ( IsConnected ( ) ) return ;
47+ if ( IsConnected ( ) ) return true ;
4848 if ( _dbConnection == null )
4949 {
5050 _dbConnection ? . Close ( ) ;
@@ -70,12 +70,17 @@ public override void OpenConnection()
7070 try
7171 {
7272 _dbConnection . Open ( ) ;
73+ return IsConnected ( ) ;
7374 }
7475 catch ( Exception e )
7576 {
7677 MsAppCenterHelper . Error ( e , new Dictionary < string , string > ( ) { { "_databaseType" , _databaseType . ToString ( ) } } ) ;
77- MessageBoxHelper . ErrorAlert ( "Access Denied: " + e . Message ) ;
78+ if ( showErrorAlert )
79+ {
80+ MessageBoxHelper . ErrorAlert ( "Access Denied: " + e . Message ) ;
81+ }
7882 }
83+ return false ;
7984 }
8085 }
8186
@@ -111,8 +116,7 @@ public override bool IsConnected()
111116
112117 public override void InitTables ( )
113118 {
114- OpenConnection ( ) ;
115- if ( ! IsConnected ( ) ) return ;
119+ if ( ! OpenConnection ( true ) ) return ;
116120 _dbConnection ? . Execute ( @$ "
117121CREATE TABLE IF NOT EXISTS `{ Config . TABLE_NAME } ` (
118122 `{ nameof ( Config . Key ) } ` VARCHAR (64) PRIMARY KEY
@@ -140,8 +144,7 @@ NOT NULL
140144 {
141145 lock ( this )
142146 {
143- OpenConnection ( ) ;
144- if ( ! IsConnected ( ) ) return null ;
147+ if ( ! OpenConnection ( ) ) return null ;
145148 Debug . Assert ( id > 0 ) ;
146149 var dbServer =
147150 _dbConnection ? . QueryFirstOrDefault < Server > (
@@ -155,8 +158,7 @@ NOT NULL
155158 {
156159 lock ( this )
157160 {
158- OpenConnection ( ) ;
159- if ( ! IsConnected ( ) ) return null ;
161+ if ( ! OpenConnection ( ) ) return null ;
160162#pragma warning disable CS8619
161163 return _dbConnection ? . Query < Server > ( $ "SELECT * FROM `{ Server . TABLE_NAME } `") . Select ( x => x ? . ToProtocolServerBase ( ) ) . Where ( x => x != null ) . ToList ( ) ;
162164#pragma warning restore CS8619
@@ -167,8 +169,7 @@ public override int GetServerCount()
167169 {
168170 lock ( this )
169171 {
170- OpenConnection ( ) ;
171- if ( ! IsConnected ( ) ) return 0 ;
172+ if ( ! OpenConnection ( ) ) return 0 ;
172173 return _dbConnection ? . ExecuteScalar < int > ( $ "SELECT COUNT(*) FROM `{ Server . TABLE_NAME } `") ?? 0 ;
173174 }
174175 }
@@ -188,8 +189,7 @@ public override string AddServer(ProtocolBase protocolBase)
188189 {
189190 lock ( this )
190191 {
191- OpenConnection ( ) ;
192- if ( ! IsConnected ( ) ) return string . Empty ;
192+ if ( ! OpenConnection ( true ) ) return string . Empty ;
193193 if ( protocolBase . IsTmpSession ( ) )
194194 protocolBase . Id = Ulid . NewUlid ( ) . ToString ( ) ;
195195 var server = protocolBase . ToDbServer ( ) ;
@@ -204,8 +204,7 @@ public override int AddServer(IEnumerable<ProtocolBase> protocolBases)
204204 {
205205 lock ( this )
206206 {
207- OpenConnection ( ) ;
208- if ( ! IsConnected ( ) ) return 0 ;
207+ if ( ! OpenConnection ( true ) ) return 0 ;
209208 var rng = new NUlid . Rng . MonotonicUlidRng ( ) ;
210209 foreach ( var protocolBase in protocolBases )
211210 {
@@ -229,8 +228,7 @@ public override bool UpdateServer(ProtocolBase server)
229228 {
230229 lock ( this )
231230 {
232- OpenConnection ( ) ;
233- if ( ! IsConnected ( ) ) return false ;
231+ if ( ! OpenConnection ( true ) ) return false ;
234232 var ret = _dbConnection ? . Execute ( SqlUpdate , server . ToDbServer ( ) ) > 0 ;
235233 if ( ret )
236234 SetDataUpdateTimestamp ( ) ;
@@ -242,8 +240,7 @@ public override bool UpdateServer(IEnumerable<ProtocolBase> servers)
242240 {
243241 lock ( this )
244242 {
245- OpenConnection ( ) ;
246- if ( ! IsConnected ( ) ) return false ;
243+ if ( ! OpenConnection ( true ) ) return false ;
247244 var dbss = servers . Select ( x => x . ToDbServer ( ) ) ;
248245 var ret = _dbConnection ? . Execute ( SqlUpdate , dbss ) > 0 ;
249246 if ( ret )
@@ -256,8 +253,7 @@ public override bool DeleteServer(string id)
256253 {
257254 lock ( this )
258255 {
259- OpenConnection ( ) ;
260- if ( ! IsConnected ( ) ) return false ;
256+ if ( ! OpenConnection ( true ) ) return false ;
261257 var ret = _dbConnection ? . Execute ( $@ "DELETE FROM `{ Server . TABLE_NAME } ` WHERE `{ nameof ( Server . Id ) } ` = @{ nameof ( Server . Id ) } ;", new { Id = id } ) > 0 ;
262258 if ( ret )
263259 SetDataUpdateTimestamp ( ) ;
@@ -269,8 +265,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
269265 {
270266 lock ( this )
271267 {
272- OpenConnection ( ) ;
273- if ( ! IsConnected ( ) ) return false ;
268+ if ( ! OpenConnection ( true ) ) return false ;
274269 var ret = _dbConnection ? . Execute ( $@ "DELETE FROM `{ Server . TABLE_NAME } ` WHERE `{ nameof ( Server . Id ) } ` IN @{ nameof ( Server . Id ) } ;", new { Id = ids } ) > 0 ;
275270 if ( ret )
276271 SetDataUpdateTimestamp ( ) ;
@@ -287,8 +282,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
287282 {
288283 lock ( this )
289284 {
290- OpenConnection ( ) ;
291- if ( ! IsConnected ( ) ) return null ;
285+ if ( ! OpenConnection ( ) ) return null ;
292286 var config = _dbConnection ? . QueryFirstOrDefault < Config > ( $ "SELECT * FROM `{ Config . TABLE_NAME } ` WHERE `{ nameof ( Config . Key ) } ` = @{ nameof ( Config . Key ) } ",
293287 new { Key = key , } ) ;
294288 return config ? . Value ;
@@ -307,8 +301,7 @@ protected bool SetConfigPrivate(string key, string value)
307301 {
308302 lock ( this )
309303 {
310- OpenConnection ( ) ;
311- if ( ! IsConnected ( ) ) return false ;
304+ if ( ! OpenConnection ( true ) ) return false ;
312305 var existed = GetConfigPrivate ( key ) != null ;
313306 return _dbConnection ? . Execute ( existed ? SqlUpdateConfig : SqlInsertConfig , new { Key = key , Value = value , } ) > 0 ;
314307 }
0 commit comments