-
Notifications
You must be signed in to change notification settings - Fork 4
/
result.ts
101 lines (89 loc) · 3.8 KB
/
result.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
import { ClientError, ProtoError, ResponseError } from "./errors.js";
import type * as proto from "./shared/proto.js";
import type { Value, IntMode } from "./value.js";
import { valueFromProto } from "./value.js";
/** Result of executing a database statement. */
export interface StmtResult {
/** Number of rows that were changed by the statement. This is meaningful only if the statement was an
* INSERT, UPDATE or DELETE, and the value is otherwise undefined. */
affectedRowCount: number;
/** The ROWID of the last successful insert into a rowid table. This is a 64-big signed integer. For other
* statements than INSERTs into a rowid table, the value is not specified. */
lastInsertRowid: bigint | undefined;
/** Names of columns in the result. */
columnNames: Array<string | undefined>;
/** Declared types of columns in the result. */
columnDecltypes: Array<string | undefined>;
}
/** An array of rows returned by a database statement. */
export interface RowsResult extends StmtResult {
/** The returned rows. */
rows: Array<Row>;
}
/** A single row returned by a database statement. */
export interface RowResult extends StmtResult {
/** The returned row. If the query produced zero rows, this is `undefined`. */
row: Row | undefined,
}
/** A single value returned by a database statement. */
export interface ValueResult extends StmtResult {
/** The returned value. If the query produced zero rows or zero columns, this is `undefined`. */
value: Value | undefined,
}
/** Row returned from the database. This is an Array-like object (it has `length` and can be indexed with a
* number), and in addition, it has enumerable properties from the named columns. */
export interface Row {
length: number;
[index: number]: Value;
[name: string]: Value;
}
export function stmtResultFromProto(result: proto.StmtResult): StmtResult {
return {
affectedRowCount: result.affectedRowCount,
lastInsertRowid: result.lastInsertRowid,
columnNames: result.cols.map(col => col.name),
columnDecltypes: result.cols.map(col => col.decltype),
};
}
export function rowsResultFromProto(result: proto.StmtResult, intMode: IntMode): RowsResult {
const stmtResult = stmtResultFromProto(result);
const rows = result.rows.map(row => rowFromProto(stmtResult.columnNames, row, intMode));
return {...stmtResult, rows};
}
export function rowResultFromProto(result: proto.StmtResult, intMode: IntMode): RowResult {
const stmtResult = stmtResultFromProto(result);
let row: Row | undefined;
if (result.rows.length > 0) {
row = rowFromProto(stmtResult.columnNames, result.rows[0], intMode);
}
return {...stmtResult, row};
}
export function valueResultFromProto(result: proto.StmtResult, intMode: IntMode): ValueResult {
const stmtResult = stmtResultFromProto(result);
let value: Value | undefined;
if (result.rows.length > 0 && stmtResult.columnNames.length > 0) {
value = valueFromProto(result.rows[0][0], intMode);
}
return {...stmtResult, value};
}
function rowFromProto(
colNames: Array<string | undefined>,
values: Array<proto.Value>,
intMode: IntMode,
): Row {
const row = {};
// make sure that the "length" property is not enumerable
Object.defineProperty(row, "length", { value: values.length });
for (let i = 0; i < values.length; ++i) {
const value = valueFromProto(values[i], intMode);
Object.defineProperty(row, i, { value });
const colName = colNames[i];
if (colName !== undefined && !Object.hasOwn(row, colName)) {
Object.defineProperty(row, colName, { value, enumerable: true });
}
}
return row as Row;
}
export function errorFromProto(error: proto.Error): ResponseError {
return new ResponseError(error.message, error);
}