@@ -66,7 +66,16 @@ public override void OpenConnection()
6666 throw new NotImplementedException ( _databaseType . ToString ( ) + " not supported!" ) ;
6767 }
6868 }
69- _dbConnection . Open ( ) ;
69+
70+ try
71+ {
72+ _dbConnection . Open ( ) ;
73+ }
74+ catch ( Exception e )
75+ {
76+ MsAppCenterHelper . Error ( e , new Dictionary < string , string > ( ) { { "_databaseType" , _databaseType . ToString ( ) } } ) ;
77+ MessageBoxHelper . ErrorAlert ( EnumDatabaseStatus . AccessDenied . GetErrorInfo ( ) + " ----> " , e . Message ) ;
78+ }
7079 }
7180 }
7281
@@ -102,6 +111,8 @@ public override bool IsConnected()
102111
103112 public override void InitTables ( )
104113 {
114+ OpenConnection ( ) ;
115+ if ( ! IsConnected ( ) ) return ;
105116 _dbConnection ? . Execute ( @$ "
106117CREATE TABLE IF NOT EXISTS `{ Config . TABLE_NAME } ` (
107118 `{ nameof ( Config . Key ) } ` VARCHAR (64) PRIMARY KEY
@@ -129,6 +140,8 @@ NOT NULL
129140 {
130141 lock ( this )
131142 {
143+ OpenConnection ( ) ;
144+ if ( ! IsConnected ( ) ) return null ;
132145 Debug . Assert ( id > 0 ) ;
133146 var dbServer =
134147 _dbConnection ? . QueryFirstOrDefault < Server > (
@@ -142,6 +155,8 @@ NOT NULL
142155 {
143156 lock ( this )
144157 {
158+ OpenConnection ( ) ;
159+ if ( ! IsConnected ( ) ) return null ;
145160#pragma warning disable CS8619
146161 return _dbConnection ? . Query < Server > ( $ "SELECT * FROM `{ Server . TABLE_NAME } `") . Select ( x => x ? . ToProtocolServerBase ( ) ) . Where ( x => x != null ) . ToList ( ) ;
147162#pragma warning restore CS8619
@@ -152,6 +167,8 @@ public override int GetServerCount()
152167 {
153168 lock ( this )
154169 {
170+ OpenConnection ( ) ;
171+ if ( ! IsConnected ( ) ) return 0 ;
155172 return _dbConnection ? . ExecuteScalar < int > ( $ "SELECT COUNT(*) FROM `{ Server . TABLE_NAME } `") ?? 0 ;
156173 }
157174 }
@@ -162,10 +179,17 @@ public override int GetServerCount()
162179VALUES
163180(@{ nameof ( Server . Id ) } , @{ nameof ( Server . Protocol ) } , @{ nameof ( Server . ClassVersion ) } , @{ nameof ( Server . Json ) } );" ;
164181
182+ /// <summary>
183+ /// return new id
184+ /// </summary>
185+ /// <param name="protocolBase"></param>
186+ /// <returns></returns>
165187 public override string AddServer ( ProtocolBase protocolBase )
166188 {
167189 lock ( this )
168190 {
191+ OpenConnection ( ) ;
192+ if ( ! IsConnected ( ) ) return string . Empty ;
169193 if ( protocolBase . IsTmpSession ( ) )
170194 protocolBase . Id = Ulid . NewUlid ( ) . ToString ( ) ;
171195 var server = protocolBase . ToDbServer ( ) ;
@@ -180,6 +204,8 @@ public override int AddServer(IEnumerable<ProtocolBase> protocolBases)
180204 {
181205 lock ( this )
182206 {
207+ OpenConnection ( ) ;
208+ if ( ! IsConnected ( ) ) return 0 ;
183209 var rng = new NUlid . Rng . MonotonicUlidRng ( ) ;
184210 foreach ( var protocolBase in protocolBases )
185211 {
@@ -204,6 +230,7 @@ public override bool UpdateServer(ProtocolBase server)
204230 lock ( this )
205231 {
206232 OpenConnection ( ) ;
233+ if ( ! IsConnected ( ) ) return false ;
207234 var ret = _dbConnection ? . Execute ( SqlUpdate , server . ToDbServer ( ) ) > 0 ;
208235 if ( ret )
209236 SetDataUpdateTimestamp ( ) ;
@@ -216,6 +243,7 @@ public override bool UpdateServer(IEnumerable<ProtocolBase> servers)
216243 lock ( this )
217244 {
218245 OpenConnection ( ) ;
246+ if ( ! IsConnected ( ) ) return false ;
219247 var dbss = servers . Select ( x => x . ToDbServer ( ) ) ;
220248 var ret = _dbConnection ? . Execute ( SqlUpdate , dbss ) > 0 ;
221249 if ( ret )
@@ -229,6 +257,7 @@ public override bool DeleteServer(string id)
229257 lock ( this )
230258 {
231259 OpenConnection ( ) ;
260+ if ( ! IsConnected ( ) ) return false ;
232261 var ret = _dbConnection ? . Execute ( $@ "DELETE FROM `{ Server . TABLE_NAME } ` WHERE `{ nameof ( Server . Id ) } ` = @{ nameof ( Server . Id ) } ;", new { Id = id } ) > 0 ;
233262 if ( ret )
234263 SetDataUpdateTimestamp ( ) ;
@@ -241,6 +270,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
241270 lock ( this )
242271 {
243272 OpenConnection ( ) ;
273+ if ( ! IsConnected ( ) ) return false ;
244274 var ret = _dbConnection ? . Execute ( $@ "DELETE FROM `{ Server . TABLE_NAME } ` WHERE `{ nameof ( Server . Id ) } ` IN @{ nameof ( Server . Id ) } ;", new { Id = ids } ) > 0 ;
245275 if ( ret )
246276 SetDataUpdateTimestamp ( ) ;
@@ -258,6 +288,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
258288 lock ( this )
259289 {
260290 OpenConnection ( ) ;
291+ if ( ! IsConnected ( ) ) return null ;
261292 var config = _dbConnection ? . QueryFirstOrDefault < Config > ( $ "SELECT * FROM `{ Config . TABLE_NAME } ` WHERE `{ nameof ( Config . Key ) } ` = @{ nameof ( Config . Key ) } ",
262293 new { Key = key , } ) ;
263294 return config ? . Value ;
@@ -277,44 +308,12 @@ protected bool SetConfigPrivate(string key, string value)
277308 lock ( this )
278309 {
279310 OpenConnection ( ) ;
311+ if ( ! IsConnected ( ) ) return false ;
280312 var existed = GetConfigPrivate ( key ) != null ;
281313 return _dbConnection ? . Execute ( existed ? SqlUpdateConfig : SqlInsertConfig , new { Key = key , Value = value , } ) > 0 ;
282314 }
283315 }
284316
285- public override bool SetConfigRsa ( string privateKeyPath , string publicKey , IEnumerable < ProtocolBase > servers )
286- {
287- lock ( this )
288- {
289- if ( _dbConnection == null )
290- return false ;
291- OpenConnection ( ) ;
292- var data = servers . Select ( x => x . ToDbServer ( ) ) ;
293- using var tran = _dbConnection . BeginTransaction ( ) ;
294- try
295- {
296- var existedPrivate = GetConfigPrivate ( "RSA_PrivateKeyPath" ) != null ;
297- var existedPublic = GetConfigPrivate ( "RSA_PublicKey" ) != null ;
298-
299- _dbConnection . Execute ( existedPrivate ? SqlUpdateConfig : SqlInsertConfig , new { Key = "RSA_PrivateKeyPath" , Value = privateKeyPath , } , tran ) ;
300- _dbConnection . Execute ( existedPublic ? SqlUpdateConfig : SqlInsertConfig , new { Key = "RSA_PublicKey" , Value = publicKey , } , tran ) ;
301- if ( data . Any ( ) )
302- _dbConnection ? . Execute ( SqlUpdate , data , tran ) ;
303- tran . Commit ( ) ;
304- }
305- catch ( Exception e )
306- {
307- SimpleLogHelper . Fatal ( e ) ;
308- // Not needed any rollback, if you don't call Complete
309- // a rollback is automatic exiting from the using block
310- //tran.Rollback();
311- return false ;
312- }
313-
314- return true ;
315- }
316- }
317-
318317 public override void SetDataUpdateTimestamp ( long time = - 1 )
319318 {
320319 lock ( this )
0 commit comments