-
Notifications
You must be signed in to change notification settings - Fork 4
/
stream.ts
123 lines (107 loc) · 4.43 KB
/
stream.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Batch } from "./batch.js";
import type { Client } from "./client.js";
import type { Cursor } from "./cursor.js";
import type { DescribeResult } from "./describe.js";
import { describeResultFromProto } from "./describe.js";
import type { RowsResult, RowResult, ValueResult, StmtResult } from "./result.js";
import {
stmtResultFromProto, rowsResultFromProto,
rowResultFromProto, valueResultFromProto,
} from "./result.js";
import type * as proto from "./shared/proto.js";
import type { InSql, SqlOwner, ProtoSql } from "./sql.js";
import { sqlToProto } from "./sql.js";
import type { InStmt } from "./stmt.js";
import { stmtToProto } from "./stmt.js";
import type { IntMode } from "./value.js";
/** A stream for executing SQL statements (a "database connection"). */
export abstract class Stream {
/** @private */
constructor(intMode: IntMode) {
this.intMode = intMode;
}
/** Get the client object that this stream belongs to. */
abstract client(): Client;
/** @private */
abstract _sqlOwner(): SqlOwner;
/** @private */
abstract _execute(stmt: proto.Stmt): Promise<proto.StmtResult>;
/** @private */
abstract _batch(batch: proto.Batch): Promise<proto.BatchResult>;
/** @private */
abstract _describe(protoSql: ProtoSql): Promise<proto.DescribeResult>;
/** @private */
abstract _sequence(protoSql: ProtoSql): Promise<void>;
/** @private */
abstract _openCursor(batch: proto.Batch): Promise<Cursor>;
/** Execute a statement and return rows. */
query(stmt: InStmt): Promise<RowsResult> {
return this.#execute(stmt, true, rowsResultFromProto);
}
/** Execute a statement and return at most a single row. */
queryRow(stmt: InStmt): Promise<RowResult> {
return this.#execute(stmt, true, rowResultFromProto);
}
/** Execute a statement and return at most a single value. */
queryValue(stmt: InStmt): Promise<ValueResult> {
return this.#execute(stmt, true, valueResultFromProto);
}
/** Execute a statement without returning rows. */
run(stmt: InStmt): Promise<StmtResult> {
return this.#execute(stmt, false, stmtResultFromProto);
}
#execute<T>(
inStmt: InStmt,
wantRows: boolean,
fromProto: (result: proto.StmtResult, intMode: IntMode) => T,
): Promise<T> {
const stmt = stmtToProto(this._sqlOwner(), inStmt, wantRows);
return this._execute(stmt).then((r) => fromProto(r, this.intMode));
}
/** Return a builder for creating and executing a batch.
*
* If `useCursor` is true, the batch will be executed using a Hrana cursor, which will stream results from
* the server to the client, which consumes less memory on the server. This requires protocol version 3 or
* higher.
*/
batch(useCursor: boolean = false): Batch {
return new Batch(this, useCursor);
}
/** Parse and analyze a statement. This requires protocol version 2 or higher. */
describe(inSql: InSql): Promise<DescribeResult> {
const protoSql = sqlToProto(this._sqlOwner(), inSql);
return this._describe(protoSql).then(describeResultFromProto);
}
/** Execute a sequence of statements separated by semicolons. This requires protocol version 2 or higher.
* */
sequence(inSql: InSql): Promise<void> {
const protoSql = sqlToProto(this._sqlOwner(), inSql);
return this._sequence(protoSql);
}
/** Check whether the SQL connection underlying this stream is in autocommit state (i.e., outside of an
* explicit transaction). This requires protocol version 3 or higher.
*/
abstract getAutocommit(): Promise<boolean>;
/** Immediately close the stream.
*
* This closes the stream immediately, aborting any pending operations.
*/
abstract close(): void;
/** Gracefully close the stream.
*
* After calling this method, you will not be able to start new operations, but existing operations will
* complete.
*/
abstract closeGracefully(): void;
/** True if the stream is closed or closing.
*
* If you call {@link closeGracefully}, this will become true immediately, even if the underlying stream
* is not physically closed yet.
*/
abstract get closed(): boolean;
/** Representation of integers returned from the database. See {@link IntMode}.
*
* This value affects the results of all operations on this stream.
*/
intMode: IntMode;
}