Skip to content

Commit

Permalink
Merge pull request #1314 from tediousjs/arthur/track-database-collation
Browse files Browse the repository at this point in the history
Improve collation handling
  • Loading branch information
arthurschreiber authored Aug 28, 2021
2 parents 93c4bc6 + c4fc65e commit 1189b4a
Show file tree
Hide file tree
Showing 24 changed files with 751 additions and 290 deletions.
2 changes: 1 addition & 1 deletion src/always-encrypted/get-parameter-encryption-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ export const getParameterEncryptionMetadata = (connection: Connection, request:
resultRows.push(columns);
});

connection.makeRequest(metadataRequest, TYPE.RPC_REQUEST, new RpcRequestPayload(metadataRequest.sqlTextOrProcedure!, metadataRequest.parameters, connection.currentTransactionDescriptor(), connection.config.options));
connection.makeRequest(metadataRequest, TYPE.RPC_REQUEST, new RpcRequestPayload(metadataRequest.sqlTextOrProcedure!, metadataRequest.parameters, connection.currentTransactionDescriptor(), connection.config.options, connection.databaseCollation));
};
21 changes: 14 additions & 7 deletions src/bulk-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Transform } from 'stream';
import { TYPE as TOKEN_TYPE } from './token/token';

import { DataType, Parameter } from './data-type';
import { Collation } from './collation';

/**
* @private
Expand Down Expand Up @@ -88,6 +89,7 @@ export type Callback =

interface Column extends Parameter {
objName: string;
collation: Collation | undefined;
}

interface ColumnOptions {
Expand Down Expand Up @@ -180,7 +182,7 @@ class RowTransform extends Transform {
let value = Array.isArray(row) ? row[i] : row[c.objName];

try {
value = c.type.validate(value);
value = c.type.validate(value, c.collation);
} catch (error) {
return callback(error);
}
Expand Down Expand Up @@ -320,12 +322,14 @@ class BulkLoad extends EventEmitter {
/**
* @private
*/
rowCount?: number;;
rowCount?: number;

collation: Collation | undefined;

/**
* @private
*/
constructor(table: string, connectionOptions: InternalConnectionOptions, {
constructor(table: string, collation: Collation | undefined, connectionOptions: InternalConnectionOptions, {
checkConstraints = false,
fireTriggers = false,
keepNulls = false,
Expand Down Expand Up @@ -364,6 +368,8 @@ class BulkLoad extends EventEmitter {
this.canceled = false;
this.executionStarted = false;

this.collation = collation;

this.table = table;
this.options = connectionOptions;
this.callback = callback;
Expand Down Expand Up @@ -404,7 +410,7 @@ class BulkLoad extends EventEmitter {
throw new Error('Columns cannot be added to bulk insert after execution has started.');
}

const column = {
const column: Column = {
type: type,
name: name,
value: null,
Expand All @@ -413,7 +419,8 @@ class BulkLoad extends EventEmitter {
precision: precision,
scale: scale,
objName: objName,
nullable: nullable
nullable: nullable,
collation: this.collation
};

if ((type.id & 0x30) === 0x20) {
Expand Down Expand Up @@ -491,11 +498,11 @@ class BulkLoad extends EventEmitter {
// write each column
if (Array.isArray(row)) {
this.rowToPacketTransform.write(this.columns.map((column, i) => {
return column.type.validate(row[i]);
return column.type.validate(row[i], column.collation);
}));
} else {
this.rowToPacketTransform.write(this.columns.map((column) => {
return column.type.validate(row[column.objName]);
return column.type.validate(row[column.objName], column.collation);
}));
}
}
Expand Down
Loading

0 comments on commit 1189b4a

Please sign in to comment.