-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreparedStatement.js
More file actions
80 lines (69 loc) · 2.11 KB
/
PreparedStatement.js
File metadata and controls
80 lines (69 loc) · 2.11 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
80
/**
* PreparedStatement — Parameterized SQL execution.
*
* Extends Statement. Parameters are set by 1-based index before execution.
* Driver implementations override _executePreparedQuery and _executePreparedUpdate.
*/
import Statement from './Statement.js';
export default class PreparedStatement extends Statement {
/**
* @param {Connection} connection
* @param {string} sql — SQL with ? placeholders
*/
constructor(connection, sql) {
super(connection);
this._sql = sql;
this._parameters = new Map();
}
/**
* Set a parameter value (auto-detect type).
* @param {number} index — 1-based
* @param {*} value
*/
setParameter(index, value) {
this._checkClosed();
this._parameters.set(index, value);
}
/** Alias for setParameter. */
setString(index, value) { this.setParameter(index, value); }
/** Alias for setParameter. */
setInt(index, value) { this.setParameter(index, value); }
/** Alias for setParameter. */
setFloat(index, value) { this.setParameter(index, value); }
/** Alias for setParameter. */
setNull(index) { this.setParameter(index, null); }
/**
* Get parameters as an ordered array.
* @returns {Array}
*/
_getParameterArray() {
const arr = [];
for (let i = 1; i <= this._parameters.size; i++) {
arr.push(this._parameters.get(i));
}
return arr;
}
/** Clear all parameter values. */
clearParameters() {
this._parameters.clear();
}
/**
* Execute a prepared query returning a ResultSet.
* @returns {Promise<ResultSet>}
*/
async executeQuery() {
this._checkClosed();
return this._executePreparedQuery(this._sql, this._getParameterArray());
}
/**
* Execute a prepared update/insert/delete.
* @returns {Promise<number>} affected row count
*/
async executeUpdate() {
this._checkClosed();
return this._executePreparedUpdate(this._sql, this._getParameterArray());
}
// Override in driver implementations
async _executePreparedQuery(sql, params) { throw new Error('Not implemented'); }
async _executePreparedUpdate(sql, params) { throw new Error('Not implemented'); }
}