UNPKG

9.61 kBSource Map (JSON)View Raw
1{"version":3,"file":"transaction.js","names":["_writableTrackingBuffer","_interopRequireDefault","require","_allHeaders","obj","__esModule","default","OPERATION_TYPE","exports","TM_GET_DTC_ADDRESS","TM_PROPAGATE_XACT","TM_BEGIN_XACT","TM_PROMOTE_XACT","TM_COMMIT_XACT","TM_ROLLBACK_XACT","TM_SAVE_XACT","ISOLATION_LEVEL","NO_CHANGE","READ_UNCOMMITTED","READ_COMMITTED","REPEATABLE_READ","SERIALIZABLE","SNAPSHOT","isolationLevelByValue","name","value","assertValidIsolationLevel","isolationLevel","TypeError","includes","Number","isInteger","RangeError","Transaction","constructor","outstandingRequestCount","beginPayload","txnDescriptor","buffer","WritableTrackingBuffer","writeToTrackingBuffer","writeUShort","writeUInt8","length","writeString","Symbol","iterator","data","toString","commitPayload","rollbackPayload","savePayload","isolationLevelToTSQL"],"sources":["../src/transaction.ts"],"sourcesContent":["import WritableTrackingBuffer from './tracking-buffer/writable-tracking-buffer';\nimport { writeToTrackingBuffer } from './all-headers';\n\n/*\n s2.2.6.8\n */\n\nexport const OPERATION_TYPE = {\n TM_GET_DTC_ADDRESS: 0x00,\n TM_PROPAGATE_XACT: 0x01,\n TM_BEGIN_XACT: 0x05,\n TM_PROMOTE_XACT: 0x06,\n TM_COMMIT_XACT: 0x07,\n TM_ROLLBACK_XACT: 0x08,\n TM_SAVE_XACT: 0x09\n};\n\nexport const ISOLATION_LEVEL: { [key: string]: number } = {\n NO_CHANGE: 0x00,\n READ_UNCOMMITTED: 0x01,\n READ_COMMITTED: 0x02,\n REPEATABLE_READ: 0x03,\n SERIALIZABLE: 0x04,\n SNAPSHOT: 0x05\n};\n\nexport const isolationLevelByValue: { [key: number]: string } = {};\nfor (const name in ISOLATION_LEVEL) {\n const value = ISOLATION_LEVEL[name];\n isolationLevelByValue[value] = name;\n}\n\nexport function assertValidIsolationLevel(isolationLevel: any, name: string): asserts isolationLevel is 0 | 1 | 2 | 3 | 4 | 5 {\n if (typeof isolationLevel !== 'number') {\n throw new TypeError(`The \"${name}\" ${name.includes('.') ? 'property' : 'argument'} must be of type number. Received type ${typeof isolationLevel} (${isolationLevel})`);\n }\n\n if (!Number.isInteger(isolationLevel)) {\n throw new RangeError(`The value of \"${name}\" is out of range. It must be an integer. Received: ${isolationLevel}`);\n }\n\n if (!(isolationLevel >= 0 && isolationLevel <= 5)) {\n throw new RangeError(`The value of \"${name}\" is out of range. It must be >= 0 && <= 5. Received: ${isolationLevel}`);\n }\n}\n\nexport class Transaction {\n declare name: string;\n declare isolationLevel: number;\n declare outstandingRequestCount: number;\n\n constructor(name: string, isolationLevel = ISOLATION_LEVEL.NO_CHANGE) {\n this.name = name;\n this.isolationLevel = isolationLevel;\n this.outstandingRequestCount = 1;\n }\n\n beginPayload(txnDescriptor: Buffer) {\n const buffer = new WritableTrackingBuffer(100, 'ucs2');\n writeToTrackingBuffer(buffer, txnDescriptor, this.outstandingRequestCount);\n buffer.writeUShort(OPERATION_TYPE.TM_BEGIN_XACT);\n buffer.writeUInt8(this.isolationLevel);\n buffer.writeUInt8(this.name.length * 2);\n buffer.writeString(this.name, 'ucs2');\n\n return {\n *[Symbol.iterator]() {\n yield buffer.data;\n },\n toString: () => {\n return 'Begin Transaction: name=' + this.name + ', isolationLevel=' + isolationLevelByValue[this.isolationLevel];\n }\n };\n }\n\n commitPayload(txnDescriptor: Buffer) {\n const buffer = new WritableTrackingBuffer(100, 'ascii');\n writeToTrackingBuffer(buffer, txnDescriptor, this.outstandingRequestCount);\n buffer.writeUShort(OPERATION_TYPE.TM_COMMIT_XACT);\n buffer.writeUInt8(this.name.length * 2);\n buffer.writeString(this.name, 'ucs2');\n // No fBeginXact flag, so no new transaction is started.\n buffer.writeUInt8(0);\n\n return {\n *[Symbol.iterator]() {\n yield buffer.data;\n },\n toString: () => {\n return 'Commit Transaction: name=' + this.name;\n }\n };\n }\n\n rollbackPayload(txnDescriptor: Buffer) {\n const buffer = new WritableTrackingBuffer(100, 'ascii');\n writeToTrackingBuffer(buffer, txnDescriptor, this.outstandingRequestCount);\n buffer.writeUShort(OPERATION_TYPE.TM_ROLLBACK_XACT);\n buffer.writeUInt8(this.name.length * 2);\n buffer.writeString(this.name, 'ucs2');\n // No fBeginXact flag, so no new transaction is started.\n buffer.writeUInt8(0);\n\n return {\n *[Symbol.iterator]() {\n yield buffer.data;\n },\n toString: () => {\n return 'Rollback Transaction: name=' + this.name;\n }\n };\n }\n\n savePayload(txnDescriptor: Buffer) {\n const buffer = new WritableTrackingBuffer(100, 'ascii');\n writeToTrackingBuffer(buffer, txnDescriptor, this.outstandingRequestCount);\n buffer.writeUShort(OPERATION_TYPE.TM_SAVE_XACT);\n buffer.writeUInt8(this.name.length * 2);\n buffer.writeString(this.name, 'ucs2');\n\n return {\n *[Symbol.iterator]() {\n yield buffer.data;\n },\n toString: () => {\n return 'Save Transaction: name=' + this.name;\n }\n };\n }\n\n isolationLevelToTSQL() {\n switch (this.isolationLevel) {\n case ISOLATION_LEVEL.READ_UNCOMMITTED:\n return 'READ UNCOMMITTED';\n case ISOLATION_LEVEL.READ_COMMITTED:\n return 'READ COMMITTED';\n case ISOLATION_LEVEL.REPEATABLE_READ:\n return 'REPEATABLE READ';\n case ISOLATION_LEVEL.SERIALIZABLE:\n return 'SERIALIZABLE';\n case ISOLATION_LEVEL.SNAPSHOT:\n return 'SNAPSHOT';\n }\n return '';\n }\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,uBAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAAsD,SAAAD,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAEtD;AACA;AACA;;AAEO,MAAMG,cAAc,GAAAC,OAAA,CAAAD,cAAA,GAAG;EAC5BE,kBAAkB,EAAE,IAAI;EACxBC,iBAAiB,EAAE,IAAI;EACvBC,aAAa,EAAE,IAAI;EACnBC,e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