You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.6 KiB
68 lines
1.6 KiB
3 years ago
|
import {Event} from '../../../event/Event'
|
||
|
import {Inject, Injectable} from '../../../di'
|
||
|
import {InvalidJSONStateError, JSONState} from '../../../util'
|
||
|
import {Connection} from '../Connection'
|
||
|
import {DatabaseService} from '../../DatabaseService'
|
||
|
|
||
|
/**
|
||
|
* Event fired when a query is executed.
|
||
|
*/
|
||
|
@Injectable()
|
||
|
export class QueryExecutedEvent extends Event {
|
||
|
@Inject()
|
||
|
protected database!: DatabaseService
|
||
|
|
||
|
/**
|
||
|
* The name of the connection where the query was executed.
|
||
|
* @protected
|
||
|
*/
|
||
|
public connectionName!: string
|
||
|
|
||
|
/**
|
||
|
* The connection where the query was executed.
|
||
|
*/
|
||
|
public connection!: Connection
|
||
|
|
||
|
/**
|
||
|
* The query that was executed.
|
||
|
*/
|
||
|
public query!: string
|
||
|
|
||
|
constructor(
|
||
|
connectionName?: string,
|
||
|
connection?: Connection,
|
||
|
query?: string,
|
||
|
) {
|
||
|
super()
|
||
|
if ( connectionName ) {
|
||
|
this.connectionName = connectionName
|
||
|
}
|
||
|
|
||
|
if ( connection ) {
|
||
|
this.connection = connection
|
||
|
}
|
||
|
|
||
|
if ( query ) {
|
||
|
this.query = query
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async dehydrate(): Promise<JSONState> {
|
||
|
return {
|
||
|
connectionName: this.connectionName,
|
||
|
query: this.query,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
rehydrate(state: JSONState): void {
|
||
|
if ( !state.connectionName || !state.query ) {
|
||
|
throw new InvalidJSONStateError('Missing connectionName or query from QueryExecutedEvent state.')
|
||
|
}
|
||
|
|
||
|
this.query = String(state.query)
|
||
|
this.connectionName = String(state.connectionName)
|
||
|
this.connection = this.database.get(this.connectionName)
|
||
|
}
|
||
|
|
||
|
}
|