Skip to content

Commit

Permalink
feat: track the current database collation on the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber authored Aug 16, 2021
1 parent b8cfb08 commit d27a7e0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { MemoryCache } from 'adal-node';
import AbortController, { AbortSignal } from 'node-abort-controller';
import { Parameter, TYPES } from './data-type';
import { BulkLoadPayload } from './bulk-load-payload';
import { Collation } from './collation';

import { version } from '../package.json';

Expand Down Expand Up @@ -1021,6 +1022,11 @@ class Connection extends EventEmitter {
*/
_cancelAfterRequestSent: () => void;

/**
* @private
*/
databaseCollation: Collation | undefined;

/**
* Note: be aware of the different options field:
* 1. config.authentication.options
Expand Down Expand Up @@ -2023,6 +2029,10 @@ class Connection extends EventEmitter {
this.emit('charsetChange', token.newValue);
});

tokenStreamParser.on('sqlCollationChange', (token) => {
this.databaseCollation = token.newValue;
});

tokenStreamParser.on('fedAuthInfo', (token) => {
this.dispatchEvent('fedAuthInfo', token);
});
Expand Down
9 changes: 7 additions & 2 deletions src/token/env-change-token-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Parser from './stream-parser';
import { InternalConnectionOptions } from '../connection';
import { Collation } from '../collation';

import {
DatabaseEnvChangeToken,
Expand Down Expand Up @@ -114,8 +115,12 @@ function readNewAndOldValue(parser: Parser, length: number, type: { name: string
return parser.readBVarByte((newValue) => {
parser.readBVarByte((oldValue) => {
switch (type.name) {
case 'SQL_COLLATION':
return callback(new CollationChangeToken(newValue, oldValue));
case 'SQL_COLLATION': {
const newCollation = newValue.length ? Collation.fromBuffer(newValue) : undefined;
const oldCollation = oldValue.length ? Collation.fromBuffer(oldValue) : undefined;

return callback(new CollationChangeToken(newCollation, oldCollation));
}

case 'BEGIN_TXN':
return callback(new BeginTransactionEnvChangeToken(newValue, oldValue));
Expand Down
4 changes: 3 additions & 1 deletion src/token/token-stream-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
ReturnValueToken,
DoneInProcToken,
DoneProcToken,
DoneToken
DoneToken,
CollationChangeToken
} from './token';
import { Readable } from 'stream';
import Message from '../message';
Expand Down Expand Up @@ -65,6 +66,7 @@ export class Parser extends EventEmitter {
((event: 'databaseChange', listener: (token: DatabaseEnvChangeToken) => void) => this) &
((event: 'languageChange', listener: (token: LanguageEnvChangeToken) => void) => this) &
((event: 'charsetChange', listener: (token: CharsetEnvChangeToken) => void) => this) &
((event: 'sqlCollationChange', listener: (token: CollationChangeToken) => void) => this) &
((event: 'fedAuthInfo', listener: (token: FedAuthInfoToken) => void) => this) &
((event: 'featureExtAck', listener: (token: FeatureExtAckToken) => void) => this) &
((event: 'loginack', listener: (token: LoginAckToken) => void) => this) &
Expand Down
7 changes: 4 additions & 3 deletions src/token/token.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Collation } from '../collation';
import { Metadata } from '../metadata-parser';
import { ColumnMetadata } from './colmetadata-token-parser';

Expand Down Expand Up @@ -281,10 +282,10 @@ export class CollationChangeToken extends Token {
declare event: 'sqlCollationChange';

type: 'SQL_COLLATION';
oldValue: Buffer;
newValue: Buffer;
oldValue: Collation | undefined;
newValue: Collation | undefined;

constructor(newValue: Buffer, oldValue: Buffer) {
constructor(newValue: Collation | undefined, oldValue: Collation | undefined) {
super('ENVCHANGE', 'sqlCollationChange');

this.type = 'SQL_COLLATION';
Expand Down

0 comments on commit d27a7e0

Please sign in to comment.