-
Notifications
You must be signed in to change notification settings - Fork 92
/
query.ts
203 lines (171 loc) · 5.13 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import type { RowDescription } from "./connection.ts";
import { encode, EncodedArg } from "./encode.ts";
import { decode } from "./decode.ts";
import { WarningFields } from "./warning.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 interface QueryObjectConfig extends QueryConfig {
/**
* This parameter superseeds query column names
*
* When specified, this names will be asigned to the results
* of the query in the order they were provided
*
* Fields must be unique (case is not taken into consideration)
*/
fields?: string[];
}
export class QueryResult {
// TODO
// This should be private for real
public _done = false;
public command!: CommandType;
public rowCount?: number;
public rowDescription?: RowDescription;
public warnings: WarningFields[] = [];
constructor(public query: Query) {}
/**
* This function is required to parse each column
* of the results
*/
loadColumnDescriptions(description: RowDescription) {
this.rowDescription = description;
}
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);
}
}
}
insertRow(_row: Uint8Array[]): void {
throw new Error("No implementation for insertRow is defined");
}
done() {
this._done = true;
}
}
export class QueryArrayResult<T extends Array<unknown>> extends QueryResult {
public rows: T[] = [];
// deno-lint-ignore camelcase
insertRow(row_data: Uint8Array[]) {
if (this._done) {
throw new Error(
"Tried to add a new row to the result after the result is done reading",
);
}
if (!this.rowDescription) {
throw new Error(
"The row descriptions required to parse the result data weren't initialized",
);
}
// Row description won't be modified after initialization
const row = row_data.map((raw_value, index) => {
const column = this.rowDescription!.columns[index];
if (raw_value === null) {
return null;
}
return decode(raw_value, column);
}) as T;
this.rows.push(row);
}
}
export class QueryObjectResult<T extends Record<string, unknown>>
extends QueryResult {
public rows: T[] = [];
// deno-lint-ignore camelcase
insertRow(row_data: Uint8Array[]) {
if (this._done) {
throw new Error(
"Tried to add a new row to the result after the result is done reading",
);
}
if (!this.rowDescription) {
throw new Error(
"The row descriptions required to parse the result data weren't initialized",
);
}
if (
this.query.fields &&
this.rowDescription.columns.length !== this.query.fields.length
) {
throw new RangeError(
"The fields provided for the query don't match the ones returned as a result " +
`(${this.rowDescription.columns.length} expected, ${this.query.fields.length} received)`,
);
}
// Row description won't be modified after initialization
const row = row_data.reduce((row, raw_value, index) => {
const column = this.rowDescription!.columns[index];
// Find the field name provided by the user
// default to database provided name
const name = this.query.fields?.[index] ?? column.name;
if (raw_value === null) {
row[name] = null;
} else {
row[name] = decode(raw_value, column);
}
return row;
}, {} as Record<string, unknown>) as T;
this.rows.push(row);
}
}
export class Query {
public text: string;
public args: EncodedArg[];
public fields?: string[];
constructor(config: QueryObjectConfig);
constructor(text: string, ...args: unknown[]);
//deno-lint-ignore camelcase
constructor(config_or_text: string | QueryObjectConfig, ...args: unknown[]) {
let config: QueryConfig;
if (typeof config_or_text === "string") {
config = { text: config_or_text, args };
} else {
const {
fields,
//deno-lint-ignore camelcase
...query_config
} = config_or_text;
config = query_config;
if (fields) {
//deno-lint-ignore camelcase
const clean_fields = fields.map((field) =>
field.toString().toLowerCase()
);
if ((new Set(clean_fields)).size !== clean_fields.length) {
throw new TypeError(
"The fields provided for the query must be unique",
);
}
this.fields = clean_fields;
}
}
this.text = config.text;
this.args = this._prepareArgs(config);
}
private _prepareArgs(config: QueryConfig): EncodedArg[] {
const encodingFn = config.encoder ? config.encoder : encode;
return (config.args || []).map(encodingFn);
}
}