|
1 | | -import { AbstractSQLClient } from './AbstractSQLClient.js'; |
| 1 | +import { LinkedQLClient } from './LinkedQLClient.js'; |
2 | 2 | import { RealtimeClient } from '../../proc/realtime/RealtimeClient.js'; |
3 | 3 | import { SchemaInference } from './SchemaInference.js'; |
4 | 4 | import { SyncEngine } from '../../proc/sync/SyncEngine.js'; |
5 | 5 | import { SQLParser } from '../../lang/SQLParser.js'; |
| 6 | +import { registry } from '../../lang/registry.js'; |
6 | 7 | import { normalizeQueryArgs } from './util.js'; |
7 | 8 | import { Result } from '../Result.js'; |
8 | | -import { registry } from '../../lang/registry.js'; |
9 | 9 |
|
10 | | -export class MainstreamSQLClient extends AbstractSQLClient { |
| 10 | +export class MainstreamDBClient extends LinkedQLClient { |
| 11 | + |
| 12 | + // Standard getters: parsers, resolver, sync |
11 | 13 |
|
12 | 14 | #parser; |
13 | 15 | #sync; |
14 | | - #realtimeClient; |
15 | 16 |
|
16 | 17 | get parser() { return this.#parser; } |
| 18 | + get resolver() { |
| 19 | + return super.resolveGetResolver(() => |
| 20 | + new SchemaInference({ client: this })); |
| 21 | + } |
17 | 22 | get sync() { return this.#sync; } |
18 | | - get realtimeClient() { return this.#realtimeClient; } |
19 | 23 |
|
20 | | - async connect() { |
21 | | - if (this.#parser) return; |
| 24 | + // Internal |
| 25 | + |
| 26 | + #realtimeClient; |
| 27 | + |
| 28 | + // ------------ |
| 29 | + |
| 30 | + constructor(options) { |
| 31 | + super(options); |
22 | 32 |
|
23 | 33 | this.#parser = new SQLParser({ dialect: this.dialect }); |
24 | 34 | this.#sync = new SyncEngine({ |
25 | | - dialect: this.dialect, |
26 | 35 | drainMode: 'drain', |
27 | 36 | lifecycleHook: async (status) => { |
28 | 37 | await this.setCapability({ realtime: !!status }); |
29 | 38 | } |
30 | 39 | }); |
31 | 40 |
|
32 | 41 | this.#realtimeClient = new RealtimeClient(this); |
33 | | - await super.connect(); |
34 | 42 | } |
35 | 43 |
|
36 | 44 | async disconnect() { |
37 | | - await super.disconnect(); |
38 | 45 | await this.#sync.close({ destroy: true }); |
| 46 | + await super.disconnect(); |
39 | 47 | } |
40 | 48 |
|
41 | | - async setCapability(capMap) { |
42 | | - capMap = await super.setCapability(capMap); |
43 | | - // realtime? |
44 | | - if (capMap.realtime === false) { |
45 | | - await this._teardownRealtime(); |
46 | | - } else if (capMap.realtime) { |
47 | | - await this._setupRealtime(); |
48 | | - } |
49 | | - return capMap; |
50 | | - } |
51 | | - |
52 | | - #lifetimeSchemaInference; |
53 | | - |
54 | | - createSchemaInference() { |
55 | | - if (this.options.nonDDLMode) { |
56 | | - // We've been promised no DDL operations will |
57 | | - // happen while we're running |
58 | | - if (!this.#lifetimeSchemaInference) |
59 | | - this.#lifetimeSchemaInference = new SchemaInference({ client: this }); |
60 | | - return this.#lifetimeSchemaInference; |
61 | | - } |
62 | | - return new SchemaInference({ client: this }); |
63 | | - } |
| 49 | + // ------------ |
64 | 50 |
|
65 | 51 | async query(...args) { |
66 | 52 | const [_query, options] = normalizeQueryArgs(...args); |
67 | 53 | const query = await this.#parser.parse(_query, options); |
68 | 54 |
|
69 | 55 | const resolveQuery = async (query, tx = null) => { |
70 | | - const schemaInference = this.createSchemaInference(); |
| 56 | + const schemaInference = this.resolver; |
71 | 57 | return await schemaInference.resolveQuery(query, { tx }); |
72 | 58 | }; |
73 | 59 |
|
@@ -104,7 +90,7 @@ export class MainstreamSQLClient extends AbstractSQLClient { |
104 | 90 | const [_query, options] = normalizeQueryArgs(...args); |
105 | 91 | const query = await this.#parser.parse(_query, options); |
106 | 92 |
|
107 | | - const schemaInference = this.createSchemaInference(); |
| 93 | + const schemaInference = this.resolver; |
108 | 94 | const resolvedQuery = await schemaInference.resolveQuery(query, options); |
109 | 95 |
|
110 | 96 | return await this._stream(resolvedQuery, options); |
|
0 commit comments