@@ -111,26 +111,55 @@ module.exports = class TDatabase {
111
111
///
112
112
/// Constructor
113
113
///
114
- constructor ( config ) {
114
+ constructor ( config , params ) {
115
115
this . config = config ;
116
+ this . params = params ;
116
117
this . connection = undefined ;
117
118
this . last_identity = 0 ;
118
119
this . last_fields = { }
119
120
if ( this . config . hasOwnProperty ( 'connectionLimit' ) )
120
121
this . pool = mysql . createPool ( this . config ) ;
121
122
}
122
123
124
+ ///
125
+ /// Log Error
126
+ ///
127
+ logError ( message ) {
128
+ if ( this . params && this . params . logger )
129
+ this . params . logger . error ( message ) ;
130
+ }
131
+
132
+ ///
133
+ /// Log Info
134
+ ///
135
+ logInfo ( message ) {
136
+ if ( this . params && this . params . logger )
137
+ this . params . logger . info ( message ) ;
138
+ }
139
+
140
+ ///
141
+ /// Log Debug
142
+ ///
143
+ logDebug ( message ) {
144
+ if ( this . params && this . params . logger )
145
+ this . params . logger . debug ( message ) ;
146
+ }
147
+
123
148
///
124
149
/// Connect to the database
125
150
///
126
151
connect ( ) {
127
152
return new Promise ( ( resolve , reject ) => {
128
153
this . connection = mysql . createConnection ( this . config ) ;
129
154
this . connection . connect ( ( err ) => {
130
- if ( err )
155
+ if ( err ) {
156
+ this . logError ( err . message ) ;
131
157
reject ( err ) ;
132
- else
158
+ }
159
+ else {
160
+ this . logInfo ( 'TDatabase.Connected' ) ;
133
161
resolve ( ) ;
162
+ }
134
163
} ) ;
135
164
} ) ;
136
165
}
@@ -171,6 +200,10 @@ module.exports = class TDatabase {
171
200
else {
172
201
if ( this . connection ) {
173
202
this . connection . end ( ( err ) => {
203
+ if ( err )
204
+ this . logError ( err . message ) ;
205
+ else
206
+ this . logDebug ( 'TDatabase.Disconnected' )
174
207
} ) ;
175
208
}
176
209
this . connection = undefined ;
@@ -198,13 +231,20 @@ module.exports = class TDatabase {
198
231
/// Query
199
232
///
200
233
query ( sql , params ) {
201
- var self = this ;
202
- return new Promise ( function ( resolve , reject ) {
203
- self . connection . query ( sql , params , function ( err , results , fields ) {
204
- if ( err )
234
+ this . logDebug ( 'TDatabase.query: ' + sql ) ;
235
+ if ( params )
236
+ params . forEach ( ( param ) => this . logDebug ( ` - ${ param } ` ) ) ;
237
+ let hrstart = process . hrtime ( ) ;
238
+ return new Promise ( ( resolve , reject ) => {
239
+ this . connection . query ( sql , params , ( err , results , fields ) => {
240
+ if ( err ) {
241
+ this . logError ( err . message ) ;
205
242
reject ( err ) ;
243
+ }
206
244
else {
207
- self . last_fields = fields ;
245
+ let hrend = process . hrtime ( hrstart ) ;
246
+ this . logDebug ( `Count: ${ results . length } , (${ hrend [ 0 ] } s ${ hrend [ 1 ] / 1000000 | 0 } ms)` ) ;
247
+ this . last_fields = fields ;
208
248
resolve ( results ) ;
209
249
}
210
250
} ) ;
0 commit comments