-
Notifications
You must be signed in to change notification settings - Fork 92
/
query.ts
121 lines (101 loc) · 2.85 KB
/
query.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
import type { RowDescription } from "./connection.ts";
import type { Connection } from "./connection.ts";
import { encode, EncodedArg } from "./encode.ts";
import { decode } from "./decode.ts";
const commandTagRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/;
type CommandType = (
| "INSERT"
| "DELETE"
| "UPDATE"
| "SELECT"
| "MOVE"
| "FETCH"
| "COPY"
);
export interface QueryConfig {
text: string;
args?: Array<unknown>;
name?: string;
encoder?: (arg: unknown) => EncodedArg;
}
export class QueryResult {
public rowDescription!: RowDescription;
private _done = false;
// deno-lint-ignore no-explicit-any
public rows: any[] = []; // actual results
public rowCount?: number;
public command!: CommandType;
constructor(public query: Query) {}
handleRowDescription(description: RowDescription) {
this.rowDescription = description;
}
// deno-lint-ignore no-explicit-any
private _parseDataRow(dataRow: any[]): any[] {
const parsedRow = [];
for (let i = 0, len = dataRow.length; i < len; i++) {
const column = this.rowDescription.columns[i];
const rawValue = dataRow[i];
if (rawValue === null) {
parsedRow.push(null);
} else {
parsedRow.push(decode(rawValue, column));
}
}
return parsedRow;
}
// deno-lint-ignore no-explicit-any
handleDataRow(dataRow: any[]): void {
if (this._done) {
throw new Error("New data row, after result if done.");
}
const parsedRow = this._parseDataRow(dataRow);
this.rows.push(parsedRow);
}
handleCommandComplete(commandTag: string): void {
const match = commandTagRegexp.exec(commandTag);
if (match) {
this.command = match[1] as CommandType;
if (match[3]) {
// COMMAND OID ROWS
this.rowCount = parseInt(match[3], 10);
} else {
// COMMAND ROWS
this.rowCount = parseInt(match[2], 10);
}
}
}
rowsOfObjects() {
return this.rows.map((row) => {
// deno-lint-ignore no-explicit-any
const rv: { [key: string]: any } = {};
this.rowDescription.columns.forEach((column, index) => {
rv[column.name] = row[index];
});
return rv;
});
}
done() {
this._done = true;
}
}
export class Query {
public text: string;
public args: EncodedArg[];
public result: QueryResult;
// TODO: can we use more specific type for args?
constructor(text: string | QueryConfig, ...args: unknown[]) {
let config: QueryConfig;
if (typeof text === "string") {
config = { text, args };
} else {
config = text;
}
this.text = config.text;
this.args = this._prepareArgs(config);
this.result = new QueryResult(this);
}
private _prepareArgs(config: QueryConfig): EncodedArg[] {
const encodingFn = config.encoder ? config.encoder : encode;
return (config.args || []).map(encodingFn);
}
}