@malloydata/db-postgres 0.0.11-dev221202152346 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PersistSQLResults, RunSQLOptions, StreamingConnection, StructDef, MalloyQueryData, QueryData, PooledConnection, SQLBlock, Connection, QueryDataRow } from "@malloydata/malloy";
|
|
2
2
|
import { Client } from "pg";
|
|
3
3
|
interface PostgresQueryConfiguration {
|
|
4
4
|
rowLimit?: number;
|
|
@@ -24,16 +24,17 @@ export declare class PostgresConnection implements Connection, StreamingConnecti
|
|
|
24
24
|
get dialectName(): string;
|
|
25
25
|
isPool(): this is PooledConnection;
|
|
26
26
|
canPersist(): this is PersistSQLResults;
|
|
27
|
-
canFetchSchemaAndRunSimultaneously(): this is FetchSchemaAndRunSimultaneously;
|
|
28
|
-
canFetchSchemaAndRunStreamSimultaneously(): this is FetchSchemaAndRunStreamSimultaneously;
|
|
29
27
|
canStream(): this is StreamingConnection;
|
|
30
28
|
fetchSchemaForTables(missing: string[]): Promise<{
|
|
31
29
|
schemas: Record<string, StructDef>;
|
|
32
30
|
errors: Record<string, string>;
|
|
33
31
|
}>;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
fetchSchemaForSQLBlock(sqlRef: SQLBlock): Promise<{
|
|
33
|
+
structDef: StructDef;
|
|
34
|
+
error?: undefined;
|
|
35
|
+
} | {
|
|
36
|
+
error: string;
|
|
37
|
+
structDef?: undefined;
|
|
37
38
|
}>;
|
|
38
39
|
protected getClient(): Promise<Client>;
|
|
39
40
|
protected runPostgresQuery(sqlCommand: string, _pageSize: number, _rowIndex: number, deJSON: boolean): Promise<MalloyQueryData>;
|
|
@@ -48,6 +48,7 @@ const crypto = __importStar(require("crypto"));
|
|
|
48
48
|
const malloy_1 = require("@malloydata/malloy");
|
|
49
49
|
const pg_1 = require("pg");
|
|
50
50
|
const pg_query_stream_1 = __importDefault(require("pg-query-stream"));
|
|
51
|
+
const crypto_1 = require("crypto");
|
|
51
52
|
const postgresToMalloyTypes = {
|
|
52
53
|
"character varying": "string",
|
|
53
54
|
name: "string",
|
|
@@ -109,13 +110,6 @@ class PostgresConnection {
|
|
|
109
110
|
canPersist() {
|
|
110
111
|
return true;
|
|
111
112
|
}
|
|
112
|
-
canFetchSchemaAndRunSimultaneously() {
|
|
113
|
-
// TODO feature-sql-block Implement FetchSchemaAndRunSimultaneously
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
canFetchSchemaAndRunStreamSimultaneously() {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
113
|
canStream() {
|
|
120
114
|
return true;
|
|
121
115
|
}
|
|
@@ -144,31 +138,21 @@ class PostgresConnection {
|
|
|
144
138
|
}
|
|
145
139
|
return { schemas, errors };
|
|
146
140
|
}
|
|
147
|
-
async
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
inCache = {
|
|
156
|
-
schema: await this.getSQLBlockSchema(sqlRef),
|
|
157
|
-
};
|
|
158
|
-
this.schemaCache.set(key, inCache);
|
|
159
|
-
}
|
|
160
|
-
catch (error) {
|
|
161
|
-
inCache = { error: error.message };
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (inCache.schema !== undefined) {
|
|
165
|
-
schemas[key] = inCache.schema;
|
|
141
|
+
async fetchSchemaForSQLBlock(sqlRef) {
|
|
142
|
+
const key = sqlRef.name;
|
|
143
|
+
let inCache = this.sqlSchemaCache.get(key);
|
|
144
|
+
if (!inCache) {
|
|
145
|
+
try {
|
|
146
|
+
inCache = {
|
|
147
|
+
structDef: await this.getSQLBlockSchema(sqlRef),
|
|
148
|
+
};
|
|
166
149
|
}
|
|
167
|
-
|
|
168
|
-
|
|
150
|
+
catch (error) {
|
|
151
|
+
inCache = { error: error.message };
|
|
169
152
|
}
|
|
153
|
+
this.sqlSchemaCache.set(key, inCache);
|
|
170
154
|
}
|
|
171
|
-
return
|
|
155
|
+
return inCache;
|
|
172
156
|
}
|
|
173
157
|
async getClient() {
|
|
174
158
|
const config = await this.readConfig();
|
|
@@ -211,12 +195,11 @@ class PostgresConnection {
|
|
|
211
195
|
},
|
|
212
196
|
fields: [],
|
|
213
197
|
};
|
|
214
|
-
|
|
215
|
-
const tempTableName = `malloy${Math.floor(Math.random() * 10000000)}`;
|
|
198
|
+
const tempTableName = `tmp${(0, crypto_1.randomUUID)()}`.replace(/-/g, "");
|
|
216
199
|
const infoQuery = `
|
|
217
200
|
drop table if exists ${tempTableName};
|
|
218
201
|
create temp table ${tempTableName} as SELECT * FROM (
|
|
219
|
-
${sqlRef.
|
|
202
|
+
${sqlRef.selectStr}
|
|
220
203
|
) as x where false;
|
|
221
204
|
SELECT column_name, c.data_type, e.data_type as element_type
|
|
222
205
|
FROM information_schema.columns c LEFT JOIN information_schema.element_types e
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloydata/db-postgres",
|
|
3
|
-
"version": "0.0.11
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"license": "GPL-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"prepublishOnly": "npm run build"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@malloydata/malloy": "^0.0.11
|
|
21
|
+
"@malloydata/malloy": "^0.0.11",
|
|
22
22
|
"@types/pg": "^8.6.1",
|
|
23
23
|
"pg": "^8.7.1",
|
|
24
24
|
"pg-query-stream": "4.2.3"
|