UNPKG

@arcgis/coding-components

Version:
256 lines (255 loc) • 6.99 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */ import { fetchT9nStringsBundle as v } from "@arcgis/toolkit/intl"; import { g as C } from "./runtime.js"; import { a as w, D as T, b as M, q as F, n as L, c as V, e as N, L as $, h as k, E as _, i as x } from "./language-defaults-base.js"; import A from "@arcgis/core/portal/Portal.js"; import { l as B } from "./utils.js"; import { substitute as E } from "@arcgis/core/intl.js"; function b(t, e, a) { return t?.[e] ?? a; } function y(t, e, a, o) { return E(b(t, e, a), o); } function z(t, e) { return b(t, e ? "nullable" : "notnull", e ? "nullable" : "not nullable"); } function I(t, e) { return b(t, e ? "yes" : "no", e ? "yes" : "no"); } function h(t) { for (const e of t) { if (e instanceof M) return e; if (e.isCollection && "variables" in e) { const a = h( e.variables.filter( (o) => o.type !== "group" ) ); if (a) return a; } } } function P(t, e) { return `${t}:${e}`; } function W(t) { if (t) { for (const e of t) if (e.type === "dataCatalogDatastore") return e.definition; } } async function U(t) { const e = new A({ url: t.portal.url }); return await e.load(), new T({ itemId: t.id, serviceUrl: t.serviceUrl, portal: e }); } async function K(t) { return B(t) ? await U(t) : t; } function j(t, e, a, o) { let r = `**${t}**`; return e.type && (r += ` ${e.type}`), a && (r += ` ${y(o, "datastoreformat", "Datastore: {datastoreName}", { datastoreName: a })}`), { value: r }; } function Q(t, e, a) { let o = `**${t.name}**`; return t.serverType && (o += ` ${t.serverType}`), o += ` ${y(a, "nullableformat", "Nullable: {nullableValue}", { nullableValue: I(a, t.nullable) })}`, o += ` ${y(a, "tableformat", "Table: {tableName}", { tableName: e })}`, { value: o }; } function H(t, e, a, o) { return t.map((r) => { const i = F(r.name, a); return { name: r.name, normalizedName: L(r.name), quotedName: i === r.name ? void 0 : i, dataType: r.serverType, nullable: r.nullable, completionLabel: r.name, insertText: i, detail: [r.serverType, z(o, r.nullable)].filter(Boolean).join(" | "), documentation: Q(r, e, o) }; }); } function O(t, e, a, o, r) { const i = V(t.name, o), n = t.name.split("."), s = N(t.name); return { id: P(e, t.name), name: t.name, normalizedName: L(t.name), quotedName: i === t.name ? void 0 : i, description: t.type, schema: n.length >= 2 ? n.at(-2) : void 0, catalog: n.length >= 3 ? n.at(-3) : void 0, completionLabel: t.name, insertText: i, detail: [t.type, a].filter(Boolean).join(" | "), documentation: j(s, t, a, r), columns: [] }; } async function G(t, e) { const a = h(t.dictionaryVariables); if (!a) return { tables: [] }; await a.loadSource(); const o = a.datastoreInfo, r = o?.datastoreId; if (!r) return { tables: [] }; const i = a.tableVariables.map( (n) => O( n.datasetInfo, r, o?.name, e, t.intlStrings ) ); return { datastore: { id: r, displayName: o?.name, databasePlatform: o?.databasePlatform }, tables: i }; } async function ae(t) { const e = W(t?.variables); if (!e) return w(void 0); const o = await (await K(e)).fetchInfo(); return w(o.databasePlatform); } class J extends $ { /** * Per-model update chaining to serialize profile updates. see arcade-defaults for rationale. */ #e = /* @__PURE__ */ new Map(); /** * Per-model, per-table column load caching to avoid redundant loads when multiple requests for the same table(s) are made before the first load completes. */ #t = /* @__PURE__ */ new Map(); #a; constructor(e) { super(e, { apiPath: "./assets/sql-layer/api", apiPrefix: "sql-layer-api.t9n." }); const a = x(e); this.dialectId = a; } getWorkerHostExtension() { return { getSqlLayerDialectSpec: async () => await this.getDialectSpec(), getSqlLayerColumnsForModel: async (e, a) => await this._loadSqlLayerColumnsForModel(e, a) }; } async getDialectSpec() { return this.#a ??= k(this.languageId), await this.#a; } disposeForModel(e) { this.#t.delete(this._getApiKey(e)), super.disposeForModel(e); } async setProfileForModel(e, a, o) { const r = this._getApiKey(e), n = (this.#e.get(r) || Promise.resolve()).then(async () => { const d = await v( o.locale, C("./assets/editor-profile/t9n"), "messages." ); if (!d) throw new Error(`Failed to load the language bundle for ${o.locale}`); this.disposeForModel(e); const c = new _(a, d, o.locale); this._modelToProfileMap.set(r, c); const g = await G(c, await this.getDialectSpec()); this.updateApiContextForModel(e, { locale: o.locale, snippets: o.snippets, sqlProfile: g }); }), s = n.finally(() => { this.#e.get(r) === s && this.#e.delete(r); }); return this.#e.set(r, s), await n; } async _loadSqlLayerColumnsForModel(e, a) { if (!a.length) return []; const o = this._getApiKey(e), r = this.getApiContextForModel(e), i = r.sqlProfile, n = this._modelToProfileMap.get(o), s = n ? h(n.dictionaryVariables) : void 0; if (!i || !s) return []; await s.loadSource(); const c = s.datastoreInfo?.datastoreId; if (!c) return []; const g = new Map( s.tableVariables.map((l) => [ P(c, l.datasetInfo.name), l ]) ); let f = this.#t.get(o); f || (f = /* @__PURE__ */ new Map(), this.#t.set(o, f)); const D = await Promise.all( a.map(async (l) => { const S = i.tables.find((u) => u.id === l); if (S?.columns.length) return { tableId: l, columns: S.columns }; let m = f.get(l); return m || (m = (async () => { const u = g.get(l); return u ? (await u.loadSource(), H( u.fieldInfos, u.datasetInfo.name, await this.getDialectSpec(), n?.intlStrings )) : []; })(), f.set(l, m)), { tableId: l, columns: await m }; }) ), q = this._mergeLoadedColumns(i, D); return q !== i && this.updateApiContextForModel(e, { ...r, sqlProfile: q }), D; } _mergeLoadedColumns(e, a) { const o = new Map(a.map((n) => [n.tableId, n.columns])); let r = !1; const i = e.tables.map((n) => n.columns.length || !o.has(n.id) ? n : (r = !0, { ...n, columns: o.get(n.id) ?? n.columns })); return r ? { ...e, tables: i } : e; } } const p = /* @__PURE__ */ new Map(); function oe(t) { let e = p.get(t); return e || (e = new J(t), p.set(t, e)), e; } function re(t) { return p.get(t); } function ne() { return [...p.values()]; } export { ae as a, re as b, oe as c, ne as g };