MSSQL contexts

In any filter, you can always depend on the following objects being defined.

Keep in mind that JavaScript objects should not be stored beyond the invocation of a filter, except in the thread context (see JavaScript environment for details).


Invocation context (context)

This is the context in which a filter gets invoked. Any variables or functions you define in this context will be thrown away once the filter returns.

Pre-defined variables

  • packet: the packet or token being filtered. The exact nature of this packet depends on the filter's circumstances. You can always get the type of the packet using the packetType property.

  • filterContext: see below

  • threadContext: see below

  • connectionContext: see below

  • projectContext: see below

Example

let pkt = context.packet;

if (pkt.packetType === "Row" || pkt.packetType === "NBCRow") {

let fullName = context.packet.first_name + " " + context.packet.last_name;

}


Filter context (context.filterContext)

This context is shared by all invocations of the filter.

Pre-defined variables

There may be one or more variable defined in this context with a name that starts with an underscore. These are internal variables and should not be used.

You can define additional variables to store data that would be useful to subsequent invocations of this filter.

Example

let startTime = context.filterContext.startTime;
if ( ! startTime) {
startTime = (new Date()).getTime();
context.filterContext.startTime = startTime;
}


Connection context (context.connectionContext)

This context is shared by all logic invoked during the lifetime of the connection from the database client.

Pre-defined variables

  • userIP: the IP address of the client, e.g. /23.45.67.89 or /2600:1701:6920:69a0:17:986a:da7a:c12a

  • userName: the name of the database user. When using SQL Server authentication, this is usually a simple name, e.g. jdoe. When using Windows authentication, the user name is typically prefixed by the domain name, e.g. MYDOMAIN\jdoe

  • lastSQL: the last SQL to be executed on this connection, either as a SQL command, or as a parameter to sp_executesql.

Example

let sql = context.connectionContext.lastSQL;
if ( ! sql.toLowerCase().startsWith("select")) {
log.debug("Non-select SQL statement: " + sql);
}


Project context (context.projectContext)

This context is as close as you can get to "global" variables. It is shared by all filters within a project.

Pre-defined variables

None


Thread context (context.threadContext)

This is a special context that is only shared by invocations of various filters that happen to be on the same Java thread. Anything stored in this context is thrown away when the thread is terminated, which can happen at any time between filter invocations. This context can be useful when dealing with non-thread-safe objects.

Pre-defined variables

None

Example

let startDate = context.threadContext.startDate;
if ( ! startDate) {
startDate = new Date();
// Note that we are storing a JavaScript object (Date), which OK only in threadContext.
context.threadContext.startDate = startDate;
}