Skip to content

Commit

Permalink
Merge pull request #974 from IanChokS/more-ts
Browse files Browse the repository at this point in the history
More Conversion
  • Loading branch information
arthurschreiber committed Sep 30, 2019
2 parents c5eae51 + c119f18 commit c4511cd
Show file tree
Hide file tree
Showing 37 changed files with 169 additions and 101 deletions.
6 changes: 3 additions & 3 deletions src/bulk-load.ts
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import WritableTrackingBuffer from './tracking-buffer/writable-tracking-buffer';
import { ConnectionOptions } from './connection';
import { InternalConnectionOptions } from './connection';

const Transform = require('readable-stream').Transform;
const TOKEN_TYPE = require('./token/token').TYPE;
Expand Down Expand Up @@ -69,7 +69,7 @@ class BulkLoad extends EventEmitter {
table: string;
timeout?: number

options: ConnectionOptions;
options: InternalConnectionOptions;
callback: (err: Error | undefined | null, rowCount: number) => void;

columns: Array<Column>;
Expand All @@ -80,7 +80,7 @@ class BulkLoad extends EventEmitter {

bulkOptions: InternalOptions;

constructor(table: string, connectionOptions: ConnectionOptions, {
constructor(table: string, connectionOptions: InternalConnectionOptions, {
checkConstraints = false,
fireTriggers = false,
keepNulls = false,
Expand Down
6 changes: 3 additions & 3 deletions src/connection.d.ts
Expand Up @@ -2,12 +2,12 @@ import Request from './request';
import BulkLoad from './bulk-load';
import { Metadata } from './metadata-parser';

export type ConnectionOptions = {
tdsVersion: string,
export type InternalConnectionOptions = {
camelCaseColumns: boolean,
columnNameReplacer?: (colName: string, index: number, metadata: Metadata) => string,
tdsVersion: string,
useColumnNames: boolean,
useUTC: boolean,
columnNameReplacer?: (colName: string, index: number, metadata: Metadata) => string
};

declare class Connection {
Expand Down
8 changes: 4 additions & 4 deletions src/data-type.ts
Expand Up @@ -37,7 +37,7 @@ const DateTimeOffset = require('./data-types/datetimeoffset');
const UDT = require('./data-types/udt');
const TVP = require('./data-types/tvp');
const Variant = require('./data-types/sql-variant');
import { ConnectionOptions } from './connection';
import { InternalConnectionOptions } from './connection';

export type Parameter = {
type: DataType,
Expand Down Expand Up @@ -67,11 +67,11 @@ export interface DataType {
name: string,

declaration(parameter: Parameter) : string,
writeTypeInfo(buf: any, data: ParameterData, options: ConnectionOptions) : void,
writeParameterData(buf: any, data: ParameterData, options: ConnectionOptions, callback: () => void) : void,
writeTypeInfo(buf: any, data: ParameterData, options: InternalConnectionOptions) : void,
writeParameterData(buf: any, data: ParameterData, options: InternalConnectionOptions, callback: () => void) : void,
validate(value: any) : any, // TODO: Refactor 'any' and replace with more specific type.

dataLengthFromScale?: (scale: number) => number,
dataLengthFromScale?: (scale: number) => (number | undefined),
fixedDataLength?: number,
dataLengthLength?: number,

Expand Down
4 changes: 2 additions & 2 deletions src/data-types/bit.ts
@@ -1,4 +1,4 @@
import { DataType, ParameterData } from '../data-type';
import { DataType } from '../data-type';
const BitN = require('./bitn');

const Bit: DataType = {
Expand All @@ -15,7 +15,7 @@ const Bit: DataType = {
buffer.writeUInt8(1);
},

writeParameterData: function(buffer, parameter: ParameterData<any>, options, cb) {
writeParameterData: function(buffer, parameter, options, cb) {
if (typeof parameter.value === 'undefined' || parameter.value === null) {
buffer.writeUInt8(0);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/data-types/date.ts
@@ -1,4 +1,4 @@
import { DataType, ParameterData } from '../data-type';
import { DataType } from '../data-type';

// globalDate is to be used for JavaScript's global 'Date' object to avoid name clashing with the 'Date' constant below
const globalDate = global.Date;
Expand All @@ -21,7 +21,7 @@ const Date : DataType = {
},

// ParameterData<any> is temporary solution. TODO: need to understand what type ParameterData<...> can be.
writeParameterData: function(buffer, parameter: ParameterData<any>, options, cb) {
writeParameterData: function(buffer, parameter, options, cb) {
if (parameter.value != null) {
buffer.writeUInt8(3);
if (options.useUTC) {
Expand Down
12 changes: 9 additions & 3 deletions src/data-types/datetime.js → src/data-types/datetime.ts
@@ -1,9 +1,11 @@
import { DataType } from '../data-type';

const DateTimeN = require('./datetimen');

const EPOCH_DATE = new Date(1900, 0, 1);
const UTC_EPOCH_DATE = new Date(Date.UTC(1900, 0, 1));

module.exports = {
const DateTime: DataType = {
id: 0x3D,
type: 'DATETIME',
name: 'DateTime',
Expand All @@ -16,7 +18,7 @@ module.exports = {
buffer.writeUInt8(DateTimeN.id);
buffer.writeUInt8(8);
},

// ParameterData<any> is temporary solution. TODO: need to understand what type ParameterData<...> can be.
writeParameterData: function(buffer, parameter, options, cb) {
if (parameter.value != null) {
let days, dstDiff, milliseconds, seconds, threeHundredthsOfSecond;
Expand Down Expand Up @@ -48,7 +50,8 @@ module.exports = {
cb();
},

validate: function(value) {
// TODO: type 'any' needs to be revisited.
validate: function(value): null | TypeError | Buffer {
if (value == null) {
return null;
}
Expand All @@ -61,3 +64,6 @@ module.exports = {
return value;
}
};

export default DateTime;
module.exports = DateTime;
14 changes: 9 additions & 5 deletions src/data-types/datetime2.js → src/data-types/datetime2.ts
@@ -1,7 +1,9 @@
import { DataType } from '../data-type';

const YEAR_ONE = new Date(2000, 0, -730118);
const UTC_YEAR_ONE = Date.UTC(2000, 0, -730118);

module.exports = {
const DateTime2: DataType & { resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x2A,
type: 'DATETIME2N',
name: 'DateTime2',
Expand Down Expand Up @@ -53,9 +55,8 @@ module.exports = {
} else {
timestamp = ((time.getHours() * 60 + time.getMinutes()) * 60 + time.getSeconds()) * 1000 + time.getMilliseconds();
}

timestamp = timestamp * Math.pow(10, parameter.scale - 3);
timestamp += (parameter.value.nanosecondDelta != null ? parameter.value.nanosecondDelta : 0) * Math.pow(10, parameter.scale);
timestamp = timestamp * Math.pow(10, parameter.scale! - 3);
timestamp += (parameter.value.nanosecondDelta != null ? parameter.value.nanosecondDelta : 0) * Math.pow(10, parameter.scale!);
timestamp = Math.round(timestamp);

switch (parameter.scale) {
Expand All @@ -80,7 +81,7 @@ module.exports = {
buffer.writeUInt24LE(Math.floor((+parameter.value - UTC_YEAR_ONE) / 86400000));
} else {
const dstDiff = -(parameter.value.getTimezoneOffset() - YEAR_ONE.getTimezoneOffset()) * 60 * 1000;
buffer.writeUInt24LE(Math.floor((+parameter.value - YEAR_ONE + dstDiff) / 86400000));
buffer.writeUInt24LE(Math.floor((+parameter.value - +YEAR_ONE + dstDiff) / 86400000));
}
} else {
buffer.writeUInt8(0);
Expand All @@ -101,3 +102,6 @@ module.exports = {
return value;
}
};

export default DateTime2;
module.exports = DateTime2;
5 changes: 4 additions & 1 deletion src/data-types/datetimen.js → src/data-types/datetimen.ts
@@ -1,6 +1,9 @@
module.exports = {
const DateTimeN = {
id: 0x6F,
type: 'DATETIMN',
name: 'DateTimeN',
dataLengthLength: 1
};

export default DateTimeN;
module.exports = DateTimeN;
@@ -1,6 +1,8 @@
import { DataType } from '../data-type';

const UTC_YEAR_ONE = Date.UTC(2000, 0, -730118);

module.exports = {
const DateTimeOffset: DataType & { resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x2B,
type: 'DATETIMEOFFSETN',
name: 'DateTimeOffset',
Expand Down Expand Up @@ -44,8 +46,9 @@ module.exports = {
time.setUTCMonth(0);
time.setUTCDate(1);

let timestamp = time * Math.pow(10, parameter.scale - 3);
timestamp += (parameter.value.nanosecondDelta != null ? parameter.value.nanosecondDelta : 0) * Math.pow(10, parameter.scale);
let timestamp;
timestamp = +time * Math.pow(10, parameter.scale! - 3);
timestamp += (parameter.value.nanosecondDelta != null ? parameter.value.nanosecondDelta : 0) * Math.pow(10, parameter.scale!);
timestamp = Math.round(timestamp);

const offset = -parameter.value.getTimezoneOffset();
Expand Down Expand Up @@ -87,3 +90,6 @@ module.exports = {
return value;
}
};

export default DateTimeOffset;
module.exports = DateTimeOffset;
23 changes: 14 additions & 9 deletions src/data-types/decimal.js → src/data-types/decimal.ts
@@ -1,6 +1,8 @@
import { DataType } from '../data-type';

const DecimalN = require('./decimaln');

module.exports = {
const Decimal: DataType & { resolvePrecision: NonNullable<DataType['resolvePrecision']>, resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x37,
type: 'DECIMAL',
name: 'Decimal',
Expand Down Expand Up @@ -31,11 +33,11 @@ module.exports = {

writeTypeInfo: function(buffer, parameter) {
buffer.writeUInt8(DecimalN.id);
if (parameter.precision <= 9) {
if (parameter.precision! <= 9) {
buffer.writeUInt8(5);
} else if (parameter.precision <= 19) {
} else if (parameter.precision! <= 19) {
buffer.writeUInt8(9);
} else if (parameter.precision <= 28) {
} else if (parameter.precision! <= 28) {
buffer.writeUInt8(13);
} else {
buffer.writeUInt8(17);
Expand All @@ -47,16 +49,16 @@ module.exports = {
writeParameterData: function(buffer, parameter, options, cb) {
if (parameter.value != null) {
const sign = parameter.value < 0 ? 0 : 1;
const value = Math.round(Math.abs(parameter.value * Math.pow(10, parameter.scale)));
if (parameter.precision <= 9) {
const value = Math.round(Math.abs(parameter.value * Math.pow(10, parameter.scale!)));
if (parameter.precision! <= 9) {
buffer.writeUInt8(5);
buffer.writeUInt8(sign);
buffer.writeUInt32LE(value);
} else if (parameter.precision <= 19) {
} else if (parameter.precision! <= 19) {
buffer.writeUInt8(9);
buffer.writeUInt8(sign);
buffer.writeUInt64LE(value);
} else if (parameter.precision <= 28) {
} else if (parameter.precision! <= 28) {
buffer.writeUInt8(13);
buffer.writeUInt8(sign);
buffer.writeUInt64LE(value);
Expand All @@ -74,7 +76,7 @@ module.exports = {
cb();
},

validate: function(value) {
validate: function(value): null | TypeError | Buffer {
if (value == null) {
return null;
}
Expand All @@ -85,3 +87,6 @@ module.exports = {
return value;
}
};

export default Decimal;
module.exports = Decimal;
5 changes: 4 additions & 1 deletion src/data-types/decimaln.js → src/data-types/decimaln.ts
@@ -1,8 +1,11 @@
module.exports = {
const DecimalN = {
id: 0x6A,
type: 'DECIMALN',
name: 'DecimalN',
dataLengthLength: 1,
hasPrecision: true,
hasScale: true
};

export default DecimalN;
module.exports = DecimalN;
9 changes: 7 additions & 2 deletions src/data-types/float.js → src/data-types/float.ts
@@ -1,6 +1,8 @@
import { DataType } from '../data-type';

const FloatN = require('./floatn');

module.exports = {
const Float: DataType = {
id: 0x3E,
type: 'FLT8',
name: 'Float',
Expand All @@ -24,7 +26,7 @@ module.exports = {
cb();
},

validate: function(value) {
validate: function(value): null | TypeError | Buffer {
if (value == null) {
return null;
}
Expand All @@ -35,3 +37,6 @@ module.exports = {
return value;
}
};

export default Float;
module.exports = Float;
5 changes: 4 additions & 1 deletion src/data-types/floatn.js → src/data-types/floatn.ts
@@ -1,6 +1,9 @@
module.exports = {
const FloatN = {
id: 0x6D,
type: 'FLTN',
name: 'FloatN',
dataLengthLength: 1
};

export default FloatN;
module.exports = FloatN;
12 changes: 9 additions & 3 deletions src/data-types/image.js → src/data-types/image.ts
@@ -1,4 +1,6 @@
module.exports = {
import { DataType } from '../data-type';

const Image: DataType & { hasTextPointerAndTimestamp: boolean } = {
id: 0x22,
type: 'IMAGE',
name: 'Image',
Expand All @@ -12,7 +14,8 @@ module.exports = {

resolveLength: function(parameter) {
if (parameter.value != null) {
return parameter.value.length;
const value = parameter.value as any; // TODO: Temporary solution. Replace 'any' more with specific type;
return value.length;
} else {
return -1;
}
Expand All @@ -33,7 +36,7 @@ module.exports = {
cb();
},

validate: function(value) {
validate: function(value): null | TypeError | Buffer {
if (value == null) {
return null;
}
Expand All @@ -43,3 +46,6 @@ module.exports = {
return value;
}
};

export default Image;
module.exports = Image;
9 changes: 7 additions & 2 deletions src/data-types/int.js → src/data-types/int.ts
@@ -1,6 +1,8 @@
import { DataType } from '../data-type';

const IntN = require('./intn');

module.exports = {
const Int: DataType = {
id: 0x38,
type: 'INT4',
name: 'Int',
Expand All @@ -24,7 +26,7 @@ module.exports = {
cb();
},

validate: function(value) {
validate: function(value): null | TypeError | Buffer {
if (value == null) {
return null;
}
Expand All @@ -38,3 +40,6 @@ module.exports = {
return value;
}
};

export default Int;
module.exports = Int;
5 changes: 4 additions & 1 deletion src/data-types/intn.js → src/data-types/intn.ts
@@ -1,6 +1,9 @@
module.exports = {
const IntN = {
id: 0x26,
type: 'INTN',
name: 'IntN',
dataLengthLength: 1
};

export default IntN;
module.exports = IntN;

0 comments on commit c4511cd

Please sign in to comment.