@@ -68,10 +68,15 @@ function mapHeaderType(column: ColumnDefinition): QueryResultHeader {
6868export default class MySQLConnection extends SQLLikeConnection {
6969 protected connectionConfig : DatabaseConnectionConfig ;
7070 protected connection : Connection | undefined ;
71+ protected onStateChangedCallback : ( state : string ) => void ;
7172
72- constructor ( connectionConfig : DatabaseConnectionConfig ) {
73+ constructor (
74+ connectionConfig : DatabaseConnectionConfig ,
75+ statusChanged : ( state : string ) => void
76+ ) {
7377 super ( ) ;
7478 this . connectionConfig = connectionConfig ;
79+ this . onStateChangedCallback = statusChanged ;
7580 }
7681
7782 protected async getConnection ( ) {
@@ -81,7 +86,20 @@ export default class MySQLConnection extends SQLLikeConnection {
8186 dateStrings : true ,
8287 namedPlaceholders : true ,
8388 } ) ;
89+
90+ console . log ( 'connected' ) ;
91+ this . onStateChangedCallback ( 'Connected' ) ;
92+
93+ this . connection . on ( 'error' , ( ) => {
94+ if ( this . connection ) {
95+ this . connection . removeAllListeners ( ) ;
96+ this . connection . destroy ( ) ;
97+ this . connection = undefined ;
98+ this . onStateChangedCallback ( 'Disconnected' ) ;
99+ }
100+ } ) ;
84101 }
102+
85103 return this . connection ;
86104 }
87105
@@ -131,7 +149,9 @@ export default class MySQLConnection extends SQLLikeConnection {
131149 }
132150
133151 async close ( ) {
134- const conn = await this . getConnection ( ) ;
135- conn . destroy ( ) ;
152+ if ( this . connection ) {
153+ const conn = await this . getConnection ( ) ;
154+ conn . destroy ( ) ;
155+ }
136156 }
137157}
0 commit comments