1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const DatabaseConnection_1 = require("./DatabaseConnection");
|
4 | const DatabaseQueryError_1 = require("./DatabaseQueryError");
|
5 | const instance_1 = require("./instance");
|
6 | const StartTransactionQuery_1 = require("./private/StartTransactionQuery");
|
7 | const CommitQuery_1 = require("./private/CommitQuery");
|
8 | const RollbackQuery_1 = require("./private/RollbackQuery");
|
9 | const DEFAULT_HIGH_WATERMARK = 512;
|
10 | const startTransactionQuery = new StartTransactionQuery_1.StartTransactionQuery();
|
11 | const commitQuery = new CommitQuery_1.CommitQuery();
|
12 | const rollbackQuery = new RollbackQuery_1.RollbackQuery();
|
13 | class MySQLConnection extends DatabaseConnection_1.DatabaseConnection {
|
14 | constructor(connection, instantiationStack, isReadOnly = true) {
|
15 | super(connection, isReadOnly, instantiationStack);
|
16 | this._opened = true;
|
17 | this.transaction = false;
|
18 | connection.config.queryFormat = function (query, values) {
|
19 | if (!values)
|
20 | return query;
|
21 | return query.replace(/:(\w+)/g, function (txt, key) {
|
22 | if (values.hasOwnProperty(key)) {
|
23 | return this.escape(values[key]);
|
24 | }
|
25 | return txt;
|
26 | }.bind(this));
|
27 | };
|
28 | }
|
29 | isTransaction() {
|
30 | return this.transaction;
|
31 | }
|
32 | isOpen() {
|
33 | return this._opened;
|
34 | }
|
35 | _query(query, params) {
|
36 | return new Promise((resolve, reject) => {
|
37 | let queryObject = this.getAPI().query({
|
38 | sql: query,
|
39 | timeout: this.getTimeout()
|
40 | }, params, (error, results) => {
|
41 | if (error) {
|
42 | return reject(new DatabaseQueryError_1.DatabaseQueryError(queryObject.sql, error));
|
43 | }
|
44 | return resolve(results);
|
45 | });
|
46 | instance_1.getApplicationLogger().trace(queryObject.sql);
|
47 | });
|
48 | }
|
49 | _stream(query, params, streamOptions) {
|
50 | if (!streamOptions) {
|
51 | streamOptions = {};
|
52 | }
|
53 | if (!streamOptions.highWatermark) {
|
54 | streamOptions.highWatermark = DEFAULT_HIGH_WATERMARK;
|
55 | }
|
56 | const queryObject = this.getAPI().query({
|
57 | sql: query,
|
58 | timeout: this.getTimeout()
|
59 | }, params);
|
60 | instance_1.getApplicationLogger().trace(queryObject.sql);
|
61 | return queryObject.stream(streamOptions);
|
62 | }
|
63 | startTransaction() {
|
64 | if (this.isReadOnly()) {
|
65 | return Promise.reject(new Error('A readonly connection cannot start a transaction.'));
|
66 | }
|
67 | if (this.isTransaction()) {
|
68 | return Promise.reject(new Error('Connection is already in a transaction.'));
|
69 | }
|
70 | this.transaction = true;
|
71 | return new Promise((resolve, reject) => {
|
72 | this.query(startTransactionQuery).then(() => {
|
73 | resolve();
|
74 | }).catch((ex) => {
|
75 | this.transaction = false;
|
76 | instance_1.getApplicationLogger().error(ex);
|
77 | reject(ex);
|
78 | });
|
79 | });
|
80 | }
|
81 | endTransaction(requiresRollback = false) {
|
82 | return (requiresRollback) ? this.rollback() : this.commit();
|
83 | }
|
84 | rollback() {
|
85 | if (!this.isTransaction()) {
|
86 | return Promise.reject(new Error('Cannot rollback when there is no active transaction.'));
|
87 | }
|
88 | return new Promise((resolve, reject) => {
|
89 | this.query(rollbackQuery).then(() => {
|
90 | this.transaction = false;
|
91 | resolve();
|
92 | }).catch((ex) => {
|
93 | instance_1.getApplicationLogger().error(ex);
|
94 | reject(ex);
|
95 | });
|
96 | });
|
97 | }
|
98 | commit() {
|
99 | if (!this.isTransaction()) {
|
100 | return Promise.reject(new Error('Cannot commit when there is no active transaction.'));
|
101 | }
|
102 | return new Promise((resolve, reject) => {
|
103 | this.query(commitQuery).then(() => {
|
104 | this.transaction = false;
|
105 | resolve();
|
106 | }).catch((ex) => {
|
107 | instance_1.getApplicationLogger().error(ex);
|
108 | reject(ex);
|
109 | });
|
110 | });
|
111 | }
|
112 | _close(forceClose) {
|
113 | if (!forceClose && this.isTransaction()) {
|
114 | return Promise.reject(new Error('Cannot close a connection while there is an active transaction. Use commit or rollback first.'));
|
115 | }
|
116 | this._opened = false;
|
117 | return new Promise((resolve, reject) => {
|
118 | let rollbackPromise = null;
|
119 | if (forceClose) {
|
120 | if (this.isTransaction()) {
|
121 | rollbackPromise = this.rollback();
|
122 | }
|
123 | else {
|
124 | rollbackPromise = Promise.resolve();
|
125 | }
|
126 | }
|
127 | else {
|
128 | rollbackPromise = Promise.resolve();
|
129 | }
|
130 | rollbackPromise.then(() => {
|
131 | this.getAPI().release();
|
132 | resolve();
|
133 | }).catch((error) => {
|
134 | instance_1.getInstance().getLogger().error(error);
|
135 | this.getAPI().release();
|
136 | resolve();
|
137 | });
|
138 | });
|
139 | }
|
140 | }
|
141 | exports.MySQLConnection = MySQLConnection;
|
142 |
|
\ | No newline at end of file |