-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.js
More file actions
79 lines (69 loc) · 1.85 KB
/
Connection.js
File metadata and controls
79 lines (69 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Connection — A session to a database.
*
* Abstract base class. Drivers override the factory methods and
* transaction control.
*/
export default class Connection {
constructor() {
this._closed = false;
this._autoCommit = true;
}
/**
* Create a Statement for executing ad-hoc SQL.
* @returns {Promise<Statement>}
*/
async createStatement() {
this._checkClosed();
return this._createStatement();
}
/**
* Create a PreparedStatement for parameterized SQL.
* @param {string} sql — SQL with ? placeholders
* @returns {Promise<PreparedStatement>}
*/
async prepareStatement(sql) {
this._checkClosed();
return this._prepareStatement(sql);
}
/**
* Set auto-commit mode. When false, explicit commit/rollback is required.
* @param {boolean} autoCommit
*/
async setAutoCommit(autoCommit) {
this._checkClosed();
this._autoCommit = autoCommit;
await this._setAutoCommit(autoCommit);
}
/** @returns {boolean} */
getAutoCommit() {
return this._autoCommit;
}
/** Commit the current transaction. */
async commit() {
this._checkClosed();
await this._commit();
}
/** Rollback the current transaction. */
async rollback() {
this._checkClosed();
await this._rollback();
}
/** Close the connection and release resources. */
async close() {
this._closed = true;
}
/** @returns {boolean} */
isClosed() {
return this._closed;
}
_checkClosed() {
if (this._closed) throw new Error('Connection is closed');
}
// Override in driver implementations
async _createStatement() { throw new Error('Not implemented'); }
async _prepareStatement(sql) { throw new Error('Not implemented'); }
async _setAutoCommit(autoCommit) {}
async _commit() { throw new Error('Not implemented'); }
async _rollback() { throw new Error('Not implemented'); }
}