@malloydata/malloy 0.0.11-dev221202152346 → 0.0.12-dev221202180754

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.
@@ -9,7 +9,7 @@ import { AnonymousQueryContext } from "./MalloyParser";
9
9
  import { NamedSourceContext } from "./MalloyParser";
10
10
  import { TableSourceContext } from "./MalloyParser";
11
11
  import { QuerySourceContext } from "./MalloyParser";
12
- import { SQLSourceContext } from "./MalloyParser";
12
+ import { SQLSourceNameContext } from "./MalloyParser";
13
13
  import { LiteralTimestampContext } from "./MalloyParser";
14
14
  import { LiteralDayContext } from "./MalloyParser";
15
15
  import { LiteralWeekContext } from "./MalloyParser";
@@ -75,7 +75,10 @@ import { ExploreKeywordContext } from "./MalloyParser";
75
75
  import { DefineQueryContext } from "./MalloyParser";
76
76
  import { TopLevelAnonQueryDefContext } from "./MalloyParser";
77
77
  import { DefineSQLStatementContext } from "./MalloyParser";
78
- import { SqlStatementDefContext } from "./MalloyParser";
78
+ import { SqlBlockContext } from "./MalloyParser";
79
+ import { BlockSQLDefContext } from "./MalloyParser";
80
+ import { SqlStringContext } from "./MalloyParser";
81
+ import { SqlInterpolationContext } from "./MalloyParser";
79
82
  import { ImportStatementContext } from "./MalloyParser";
80
83
  import { ImportURLContext } from "./MalloyParser";
81
84
  import { TopLevelQueryDefsContext } from "./MalloyParser";
@@ -167,7 +170,7 @@ import { JsonObjectContext } from "./MalloyParser";
167
170
  import { JsonPropertyContext } from "./MalloyParser";
168
171
  import { JsonArrayContext } from "./MalloyParser";
169
172
  import { SqlExploreNameRefContext } from "./MalloyParser";
170
- import { SqlCommandNameDefContext } from "./MalloyParser";
173
+ import { NameSQLBlockContext } from "./MalloyParser";
171
174
  import { ConnectionNameContext } from "./MalloyParser";
172
175
  /**
173
176
  * This interface defines a complete listener for a parse tree produced by
@@ -295,17 +298,17 @@ export interface MalloyParserListener extends ParseTreeListener {
295
298
  */
296
299
  exitQuerySource?: (ctx: QuerySourceContext) => void;
297
300
  /**
298
- * Enter a parse tree produced by the `SQLSource`
301
+ * Enter a parse tree produced by the `SQLSourceName`
299
302
  * labeled alternative in `MalloyParser.exploreSource`.
300
303
  * @param ctx the parse tree
301
304
  */
302
- enterSQLSource?: (ctx: SQLSourceContext) => void;
305
+ enterSQLSourceName?: (ctx: SQLSourceNameContext) => void;
303
306
  /**
304
- * Exit a parse tree produced by the `SQLSource`
307
+ * Exit a parse tree produced by the `SQLSourceName`
305
308
  * labeled alternative in `MalloyParser.exploreSource`.
306
309
  * @param ctx the parse tree
307
310
  */
308
- exitSQLSource?: (ctx: SQLSourceContext) => void;
311
+ exitSQLSourceName?: (ctx: SQLSourceNameContext) => void;
309
312
  /**
310
313
  * Enter a parse tree produced by the `literalTimestamp`
311
314
  * labeled alternative in `MalloyParser.dateLiteral`.
@@ -1073,15 +1076,45 @@ export interface MalloyParserListener extends ParseTreeListener {
1073
1076
  */
1074
1077
  exitDefineSQLStatement?: (ctx: DefineSQLStatementContext) => void;
1075
1078
  /**
1076
- * Enter a parse tree produced by `MalloyParser.sqlStatementDef`.
1079
+ * Enter a parse tree produced by `MalloyParser.sqlBlock`.
1077
1080
  * @param ctx the parse tree
1078
1081
  */
1079
- enterSqlStatementDef?: (ctx: SqlStatementDefContext) => void;
1082
+ enterSqlBlock?: (ctx: SqlBlockContext) => void;
1080
1083
  /**
1081
- * Exit a parse tree produced by `MalloyParser.sqlStatementDef`.
1084
+ * Exit a parse tree produced by `MalloyParser.sqlBlock`.
1082
1085
  * @param ctx the parse tree
1083
1086
  */
1084
- exitSqlStatementDef?: (ctx: SqlStatementDefContext) => void;
1087
+ exitSqlBlock?: (ctx: SqlBlockContext) => void;
1088
+ /**
1089
+ * Enter a parse tree produced by `MalloyParser.blockSQLDef`.
1090
+ * @param ctx the parse tree
1091
+ */
1092
+ enterBlockSQLDef?: (ctx: BlockSQLDefContext) => void;
1093
+ /**
1094
+ * Exit a parse tree produced by `MalloyParser.blockSQLDef`.
1095
+ * @param ctx the parse tree
1096
+ */
1097
+ exitBlockSQLDef?: (ctx: BlockSQLDefContext) => void;
1098
+ /**
1099
+ * Enter a parse tree produced by `MalloyParser.sqlString`.
1100
+ * @param ctx the parse tree
1101
+ */
1102
+ enterSqlString?: (ctx: SqlStringContext) => void;
1103
+ /**
1104
+ * Exit a parse tree produced by `MalloyParser.sqlString`.
1105
+ * @param ctx the parse tree
1106
+ */
1107
+ exitSqlString?: (ctx: SqlStringContext) => void;
1108
+ /**
1109
+ * Enter a parse tree produced by `MalloyParser.sqlInterpolation`.
1110
+ * @param ctx the parse tree
1111
+ */
1112
+ enterSqlInterpolation?: (ctx: SqlInterpolationContext) => void;
1113
+ /**
1114
+ * Exit a parse tree produced by `MalloyParser.sqlInterpolation`.
1115
+ * @param ctx the parse tree
1116
+ */
1117
+ exitSqlInterpolation?: (ctx: SqlInterpolationContext) => void;
1085
1118
  /**
1086
1119
  * Enter a parse tree produced by `MalloyParser.importStatement`.
1087
1120
  * @param ctx the parse tree
@@ -1993,15 +2026,15 @@ export interface MalloyParserListener extends ParseTreeListener {
1993
2026
  */
1994
2027
  exitSqlExploreNameRef?: (ctx: SqlExploreNameRefContext) => void;
1995
2028
  /**
1996
- * Enter a parse tree produced by `MalloyParser.sqlCommandNameDef`.
2029
+ * Enter a parse tree produced by `MalloyParser.nameSQLBlock`.
1997
2030
  * @param ctx the parse tree
1998
2031
  */
1999
- enterSqlCommandNameDef?: (ctx: SqlCommandNameDefContext) => void;
2032
+ enterNameSQLBlock?: (ctx: NameSQLBlockContext) => void;
2000
2033
  /**
2001
- * Exit a parse tree produced by `MalloyParser.sqlCommandNameDef`.
2034
+ * Exit a parse tree produced by `MalloyParser.nameSQLBlock`.
2002
2035
  * @param ctx the parse tree
2003
2036
  */
2004
- exitSqlCommandNameDef?: (ctx: SqlCommandNameDefContext) => void;
2037
+ exitNameSQLBlock?: (ctx: NameSQLBlockContext) => void;
2005
2038
  /**
2006
2039
  * Enter a parse tree produced by `MalloyParser.connectionName`.
2007
2040
  * @param ctx the parse tree
@@ -9,7 +9,7 @@ import { AnonymousQueryContext } from "./MalloyParser";
9
9
  import { NamedSourceContext } from "./MalloyParser";
10
10
  import { TableSourceContext } from "./MalloyParser";
11
11
  import { QuerySourceContext } from "./MalloyParser";
12
- import { SQLSourceContext } from "./MalloyParser";
12
+ import { SQLSourceNameContext } from "./MalloyParser";
13
13
  import { LiteralTimestampContext } from "./MalloyParser";
14
14
  import { LiteralDayContext } from "./MalloyParser";
15
15
  import { LiteralWeekContext } from "./MalloyParser";
@@ -75,7 +75,10 @@ import { ExploreKeywordContext } from "./MalloyParser";
75
75
  import { DefineQueryContext } from "./MalloyParser";
76
76
  import { TopLevelAnonQueryDefContext } from "./MalloyParser";
77
77
  import { DefineSQLStatementContext } from "./MalloyParser";
78
- import { SqlStatementDefContext } from "./MalloyParser";
78
+ import { SqlBlockContext } from "./MalloyParser";
79
+ import { BlockSQLDefContext } from "./MalloyParser";
80
+ import { SqlStringContext } from "./MalloyParser";
81
+ import { SqlInterpolationContext } from "./MalloyParser";
79
82
  import { ImportStatementContext } from "./MalloyParser";
80
83
  import { ImportURLContext } from "./MalloyParser";
81
84
  import { TopLevelQueryDefsContext } from "./MalloyParser";
@@ -167,7 +170,7 @@ import { JsonObjectContext } from "./MalloyParser";
167
170
  import { JsonPropertyContext } from "./MalloyParser";
168
171
  import { JsonArrayContext } from "./MalloyParser";
169
172
  import { SqlExploreNameRefContext } from "./MalloyParser";
170
- import { SqlCommandNameDefContext } from "./MalloyParser";
173
+ import { NameSQLBlockContext } from "./MalloyParser";
171
174
  import { ConnectionNameContext } from "./MalloyParser";
172
175
  /**
173
176
  * This interface defines a complete generic visitor for a parse tree produced
@@ -248,12 +251,12 @@ export interface MalloyParserVisitor<Result> extends ParseTreeVisitor<Result> {
248
251
  */
249
252
  visitQuerySource?: (ctx: QuerySourceContext) => Result;
250
253
  /**
251
- * Visit a parse tree produced by the `SQLSource`
254
+ * Visit a parse tree produced by the `SQLSourceName`
252
255
  * labeled alternative in `MalloyParser.exploreSource`.
253
256
  * @param ctx the parse tree
254
257
  * @return the visitor result
255
258
  */
256
- visitSQLSource?: (ctx: SQLSourceContext) => Result;
259
+ visitSQLSourceName?: (ctx: SQLSourceNameContext) => Result;
257
260
  /**
258
261
  * Visit a parse tree produced by the `literalTimestamp`
259
262
  * labeled alternative in `MalloyParser.dateLiteral`.
@@ -703,11 +706,29 @@ export interface MalloyParserVisitor<Result> extends ParseTreeVisitor<Result> {
703
706
  */
704
707
  visitDefineSQLStatement?: (ctx: DefineSQLStatementContext) => Result;
705
708
  /**
706
- * Visit a parse tree produced by `MalloyParser.sqlStatementDef`.
709
+ * Visit a parse tree produced by `MalloyParser.sqlBlock`.
707
710
  * @param ctx the parse tree
708
711
  * @return the visitor result
709
712
  */
710
- visitSqlStatementDef?: (ctx: SqlStatementDefContext) => Result;
713
+ visitSqlBlock?: (ctx: SqlBlockContext) => Result;
714
+ /**
715
+ * Visit a parse tree produced by `MalloyParser.blockSQLDef`.
716
+ * @param ctx the parse tree
717
+ * @return the visitor result
718
+ */
719
+ visitBlockSQLDef?: (ctx: BlockSQLDefContext) => Result;
720
+ /**
721
+ * Visit a parse tree produced by `MalloyParser.sqlString`.
722
+ * @param ctx the parse tree
723
+ * @return the visitor result
724
+ */
725
+ visitSqlString?: (ctx: SqlStringContext) => Result;
726
+ /**
727
+ * Visit a parse tree produced by `MalloyParser.sqlInterpolation`.
728
+ * @param ctx the parse tree
729
+ * @return the visitor result
730
+ */
731
+ visitSqlInterpolation?: (ctx: SqlInterpolationContext) => Result;
711
732
  /**
712
733
  * Visit a parse tree produced by `MalloyParser.importStatement`.
713
734
  * @param ctx the parse tree
@@ -1255,11 +1276,11 @@ export interface MalloyParserVisitor<Result> extends ParseTreeVisitor<Result> {
1255
1276
  */
1256
1277
  visitSqlExploreNameRef?: (ctx: SqlExploreNameRefContext) => Result;
1257
1278
  /**
1258
- * Visit a parse tree produced by `MalloyParser.sqlCommandNameDef`.
1279
+ * Visit a parse tree produced by `MalloyParser.nameSQLBlock`.
1259
1280
  * @param ctx the parse tree
1260
1281
  * @return the visitor result
1261
1282
  */
1262
- visitSqlCommandNameDef?: (ctx: SqlCommandNameDefContext) => Result;
1283
+ visitNameSQLBlock?: (ctx: NameSQLBlockContext) => Result;
1263
1284
  /**
1264
1285
  * Visit a parse tree produced by `MalloyParser.connectionName`.
1265
1286
  * @param ctx the parse tree
@@ -1,6 +1,6 @@
1
1
  import { Token, CommonTokenStream, ParserRuleContext, CodePointCharStream } from "antlr4ts";
2
2
  import type { ParseTree } from "antlr4ts/tree";
3
- import { Query, NamedStructDefs, StructDef, ModelDef, SQLBlock, DocumentReference, DocumentPosition, DocumentLocation, DocumentRange } from "../model/malloy_types";
3
+ import { Query, NamedStructDefs, StructDef, ModelDef, SQLBlockSource, DocumentReference, DocumentPosition, DocumentLocation, DocumentRange, SQLBlockStructDef } from "../model/malloy_types";
4
4
  import * as ast from "./ast";
5
5
  import { LogMessage, MessageLog } from "./parse-log";
6
6
  import { Zone, ZoneData } from "./zone";
@@ -26,24 +26,14 @@ export interface NeedSchemaData {
26
26
  export interface NeedURLData {
27
27
  urls: string[];
28
28
  }
29
- /**
30
- * An SQL Block contains a unique key inside it, and we use that to
31
- * reference and define blocks, since Zone really wants keys to be strings.
32
- *
33
- * If I had SQLBlocks when Zones were defined, this would not be
34
- * a one off class, it would be Zone<keyType,valueType>
35
- */
36
- export declare class SQLExploreZone extends Zone<StructDef> {
37
- keyed: Record<string, SQLBlock>;
38
- referenceBlock(from: ast.SQLStatement, at: DocumentLocation): void;
39
- getUndefinedBlocks(): SQLBlock[] | undefined;
40
- }
41
- export interface NeedSQLStruct {
42
- sqlStructs: SQLBlock[];
29
+ export interface NeedCompileSQL {
30
+ compileSQL: SQLBlockSource;
31
+ partialModel: ModelDef | undefined;
43
32
  }
44
- interface NeededData extends NeedURLData, NeedSchemaData, NeedSQLStruct {
33
+ interface NeededData extends NeedURLData, NeedSchemaData, NeedCompileSQL {
45
34
  }
46
35
  export declare type DataRequestResponse = Partial<NeededData> | null;
36
+ export declare type ModelDataRequest = NeedCompileSQL | undefined;
47
37
  interface ASTData extends ErrorResponse, NeededData, FinalResponse {
48
38
  ast: ast.MalloyElement;
49
39
  }
@@ -65,7 +55,7 @@ interface TranslatedResponseData extends NeededData, ErrorResponse, FinalRespons
65
55
  translated: {
66
56
  modelDef: ModelDef;
67
57
  queryList: Query[];
68
- sqlBlocks: SQLBlock[];
58
+ sqlBlocks: SQLBlockStructDef[];
69
59
  };
70
60
  }
71
61
  export declare type TranslateResponse = Partial<TranslatedResponseData>;
@@ -166,7 +156,7 @@ export declare abstract class MalloyTranslation {
166
156
  childTranslators: Map<string, MalloyTranslation>;
167
157
  urlIsFullPath?: boolean;
168
158
  queryList: Query[];
169
- sqlBlocks: SQLBlock[];
159
+ sqlBlocks: SQLBlockStructDef[];
170
160
  modelDef: ModelDef;
171
161
  readonly parseStep: ParseStep;
172
162
  readonly importsAndTablesStep: ImportsAndTablesStep;
@@ -188,6 +178,7 @@ export declare abstract class MalloyTranslation {
188
178
  errors(): ErrorResponse;
189
179
  getLineMap(url: string): string[] | undefined;
190
180
  prettyErrors(): string;
181
+ childRequest(importURL: string): ModelDataRequest;
191
182
  getChildExports(importURL: string): NamedStructDefs;
192
183
  private finalAnswer?;
193
184
  translate(extendingModel?: ModelDef): TranslateResponse;
@@ -218,7 +209,7 @@ export declare abstract class MalloyTranslation {
218
209
  export declare class MalloyTranslator extends MalloyTranslation {
219
210
  schemaZone: Zone<StructDef>;
220
211
  importZone: Zone<string>;
221
- sqlQueryZone: SQLExploreZone;
212
+ sqlQueryZone: Zone<SQLBlockStructDef>;
222
213
  logger: MessageLog;
223
214
  readonly root: MalloyTranslator;
224
215
  constructor(rootURL: string, preload?: ParseUpdate | null);
@@ -227,7 +218,7 @@ export declare class MalloyTranslator extends MalloyTranslation {
227
218
  interface ErrorData {
228
219
  tables: Record<string, string>;
229
220
  urls: Record<string, string>;
230
- sqlStructs: Record<string, string>;
221
+ compileSQL: Record<string, string>;
231
222
  }
232
223
  export interface URLData {
233
224
  urls: ZoneData<string>;
@@ -235,10 +226,10 @@ export interface URLData {
235
226
  export interface SchemaData {
236
227
  tables: ZoneData<StructDef>;
237
228
  }
238
- export interface SQLStructData {
239
- sqlStructs: ZoneData<StructDef>;
229
+ export interface SQLBlockData {
230
+ compileSQL: ZoneData<SQLBlockStructDef>;
240
231
  }
241
- export interface UpdateData extends URLData, SchemaData, SQLStructData {
232
+ export interface UpdateData extends URLData, SchemaData, SQLBlockData {
242
233
  errors: Partial<ErrorData>;
243
234
  }
244
235
  export declare type ParseUpdate = Partial<UpdateData>;
@@ -35,7 +35,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
35
35
  return result;
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.MalloyTranslator = exports.MalloyTranslation = exports.SQLExploreZone = void 0;
38
+ exports.MalloyTranslator = exports.MalloyTranslation = void 0;
39
39
  const antlr4ts_1 = require("antlr4ts");
40
40
  const MalloyLexer_1 = require("./lib/Malloy/MalloyLexer");
41
41
  const MalloyParser_1 = require("./lib/Malloy/MalloyParser");
@@ -66,34 +66,8 @@ class MalloyParserErrorHandler {
66
66
  this.messages.log(error);
67
67
  }
68
68
  }
69
- /**
70
- * An SQL Block contains a unique key inside it, and we use that to
71
- * reference and define blocks, since Zone really wants keys to be strings.
72
- *
73
- * If I had SQLBlocks when Zones were defined, this would not be
74
- * a one off class, it would be Zone<keyType,valueType>
75
- */
76
- class SQLExploreZone extends zone_1.Zone {
77
- constructor() {
78
- super(...arguments);
79
- this.keyed = {};
80
- }
81
- referenceBlock(from, at) {
82
- const sql = from.sqlBlock();
83
- this.reference(sql.name, at);
84
- this.keyed[sql.name] = sql;
85
- }
86
- getUndefinedBlocks() {
87
- const blockRefs = this.getUndefined();
88
- if (blockRefs) {
89
- return blockRefs.map((ref) => this.keyed[ref]);
90
- }
91
- return undefined;
92
- }
93
- }
94
- exports.SQLExploreZone = SQLExploreZone;
95
69
  function isNeedResponse(dr) {
96
- return !!dr && (dr.tables || dr.urls || dr.sqlStructs) != undefined;
70
+ return !!dr && (dr.tables || dr.urls || dr.compileSQL) != undefined;
97
71
  }
98
72
  class ParseStep {
99
73
  step(that) {
@@ -332,30 +306,21 @@ class ASTStep {
332
306
  this.response = that.fatalErrors();
333
307
  return this.response;
334
308
  }
335
- // Make sure there is a request/reference for all explored-sql entities
336
- const sqlZone = that.root.sqlQueryZone;
337
- for (const sqlExploreRef in sqlExplores) {
338
- const sqlExplore = sqlExplores[sqlExploreRef];
339
- if (sqlExplore.ref && sqlExplore.def) {
340
- const sqlAt = {
341
- url: that.sourceURL,
342
- range: sqlExplore.def.location.range,
343
- };
344
- that.root.sqlQueryZone.referenceBlock(sqlExplore.def, sqlAt);
345
- }
346
- }
347
- // Now make sure that every child also has all sql blocks resolved
309
+ /*
310
+ TODO we used to scna the AST for SQL blocks here, don't need to do that
311
+ becausae that happens at the translate step, but the code I need to
312
+ understand is how a root translate can return the need of a chiold
313
+ which has an SQL block ...
314
+
315
+ */
316
+ // Now make sure that every child has fully translated itself
317
+ // before this tree is ready to also translate ...
348
318
  for (const child of that.childTranslators.values()) {
349
319
  const kidNeeds = child.astStep.step(child);
350
320
  if (isNeedResponse(kidNeeds)) {
351
321
  return kidNeeds;
352
322
  }
353
323
  }
354
- // TODO report errors from here!
355
- const missingSqlStructs = sqlZone.getUndefinedBlocks();
356
- if (missingSqlStructs) {
357
- return { sqlStructs: missingSqlStructs };
358
- }
359
324
  newAst.setTranslator(that);
360
325
  this.response = {
361
326
  ...that.errors(),
@@ -478,9 +443,19 @@ class TranslateStep {
478
443
  if (that.grammarRule === "malloyDocument") {
479
444
  if (astResponse.ast instanceof ast.Document) {
480
445
  const doc = astResponse.ast;
481
- that.modelDef = doc.getModelDef(extendingModel);
482
- that.queryList = doc.queryList;
483
- that.sqlBlocks = doc.sqlBlocks;
446
+ doc.initModelDef(extendingModel);
447
+ for (;;) {
448
+ const docCompile = doc.compile();
449
+ if (docCompile.needs) {
450
+ return docCompile.needs;
451
+ }
452
+ else {
453
+ that.modelDef = docCompile.modelDef;
454
+ that.queryList = docCompile.queryList;
455
+ that.sqlBlocks = docCompile.sqlBlocks;
456
+ break;
457
+ }
458
+ }
484
459
  }
485
460
  else {
486
461
  that.root.logger.log({
@@ -604,6 +579,17 @@ class MalloyTranslation {
604
579
  }
605
580
  return lovely;
606
581
  }
582
+ childRequest(importURL) {
583
+ var _a;
584
+ const childURL = decodeURI(new URL(importURL, this.sourceURL).toString());
585
+ const ret = (_a = this.childTranslators.get(childURL)) === null || _a === void 0 ? void 0 : _a.translate();
586
+ if (ret === null || ret === void 0 ? void 0 : ret.compileSQL) {
587
+ return {
588
+ compileSQL: ret.compileSQL,
589
+ partialModel: ret.partialModel,
590
+ };
591
+ }
592
+ }
607
593
  getChildExports(importURL) {
608
594
  const exports = {};
609
595
  const childURL = decodeURI(new URL(importURL, this.sourceURL).toString());
@@ -711,7 +697,7 @@ class MalloyTranslator extends MalloyTranslation {
711
697
  super(rootURL);
712
698
  this.schemaZone = new zone_1.Zone();
713
699
  this.importZone = new zone_1.Zone();
714
- this.sqlQueryZone = new SQLExploreZone();
700
+ this.sqlQueryZone = new zone_1.Zone();
715
701
  this.logger = new parse_log_1.MessageLog();
716
702
  this.root = this;
717
703
  if (preload) {
@@ -722,7 +708,7 @@ class MalloyTranslator extends MalloyTranslation {
722
708
  var _a, _b, _c;
723
709
  this.schemaZone.updateFrom(dd.tables, (_a = dd.errors) === null || _a === void 0 ? void 0 : _a.tables);
724
710
  this.importZone.updateFrom(dd.urls, (_b = dd.errors) === null || _b === void 0 ? void 0 : _b.urls);
725
- this.sqlQueryZone.updateFrom(dd.sqlStructs, (_c = dd.errors) === null || _c === void 0 ? void 0 : _c.sqlStructs);
711
+ this.sqlQueryZone.updateFrom(dd.compileSQL, (_c = dd.errors) === null || _c === void 0 ? void 0 : _c.compileSQL);
726
712
  }
727
713
  }
728
714
  exports.MalloyTranslator = MalloyTranslator;
@@ -37,6 +37,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
37
37
  protected onlyNestedQueries(els: ast.MalloyElement[]): ast.NestedQuery[];
38
38
  protected onlyQueryRefs(els: ast.MalloyElement[]): ast.QueryItem[];
39
39
  protected getNumber(term: ParseTree): number;
40
+ protected optionalString(fromTerm: ParseTree | undefined): string | undefined;
40
41
  protected getIdText(fromTerm: ParseTree): string;
41
42
  protected getFieldName(cx: ParserRuleContext): ast.FieldName;
42
43
  protected getModelEntryName(cx: ParserRuleContext): ast.ModelEntryReference;
@@ -51,6 +52,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
51
52
  protected getSegments(segments: parse.PipeElementContext[]): ast.QOPDesc[];
52
53
  protected getFilterShortcut(cx: parse.FilterShortcutContext): ast.Filter;
53
54
  protected getExploreSource(pcx: parse.ExploreSourceContext): ast.Mallobj;
55
+ protected makeSqlString(pcx: parse.SqlStringContext, sqlStr: ast.SQLString): void;
54
56
  visitMalloyDocument(pcx: parse.MalloyDocumentContext): ast.Document;
55
57
  visitDefineExploreStatement(pcx: parse.DefineExploreStatementContext): ast.DefineSourceList | ast.DefineExplore;
56
58
  visitExploreDefinition(pcx: parse.ExploreDefinitionContext): ast.DefineExplore;
@@ -58,7 +60,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
58
60
  visitExploreProperties(pcx: parse.ExplorePropertiesContext): ast.ExploreDesc;
59
61
  visitExploreTable(pcx: parse.ExploreTableContext): ast.TableSource;
60
62
  visitTableSource(pcx: parse.TableSourceContext): ast.TableSource;
61
- visitSQLSource(pcx: parse.SQLSourceContext): ast.SQLSource;
63
+ visitSQLSourceName(pcx: parse.SQLSourceNameContext): ast.SQLSource;
62
64
  visitQuerySource(pcx: parse.QuerySourceContext): ast.Mallobj;
63
65
  visitDefJoinMany(pcx: parse.DefJoinManyContext): ast.Joins;
64
66
  visitDefJoinOne(pcx: parse.DefJoinOneContext): ast.Joins;
@@ -150,6 +152,5 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
150
152
  visitImportStatement(pcx: parse.ImportStatementContext): ast.ImportStatement;
151
153
  visitJustExpr(pcx: parse.JustExprContext): ast.ExpressionDef;
152
154
  visitDefineSQLStatement(pcx: parse.DefineSQLStatementContext): ast.SQLStatement;
153
- visitSQLStatementDef(pcx: parse.SqlStatementDefContext): ast.SQLStatement;
154
155
  visitSampleStatement(pcx: parse.SampleStatementContext): ast.SampleProperty;
155
156
  }
@@ -148,6 +148,12 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
148
148
  getNumber(term) {
149
149
  return Number.parseInt(term.text);
150
150
  }
151
+ optionalString(fromTerm) {
152
+ if (fromTerm) {
153
+ return this.stripQuotes(fromTerm.text);
154
+ }
155
+ return undefined;
156
+ }
151
157
  getIdText(fromTerm) {
152
158
  return this.stripQuotes(fromTerm.text);
153
159
  }
@@ -230,6 +236,19 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
230
236
  }
231
237
  throw this.internalError(pcx, `'${element.elementType}': illegal explore source`);
232
238
  }
239
+ makeSqlString(pcx, sqlStr) {
240
+ var _a;
241
+ for (const part of pcx.sqlInterpolation()) {
242
+ const upToOpen = part.OPEN_CODE().text;
243
+ if (upToOpen.length > 2) {
244
+ sqlStr.push(upToOpen.slice(0, upToOpen.length - 2));
245
+ }
246
+ sqlStr.push(this.visit(part.query()));
247
+ }
248
+ const lastChars = (_a = pcx.SQL_END()) === null || _a === void 0 ? void 0 : _a.text.slice(0, -3);
249
+ sqlStr.push(lastChars || "");
250
+ this.astAt(sqlStr, pcx);
251
+ }
233
252
  visitMalloyDocument(pcx) {
234
253
  const stmts = this.onlyDocStatements(pcx.malloyStatement().map((scx) => this.visit(scx)));
235
254
  return new ast.Document(stmts);
@@ -270,7 +289,7 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
270
289
  visitTableSource(pcx) {
271
290
  return this.visitExploreTable(pcx.exploreTable());
272
291
  }
273
- visitSQLSource(pcx) {
292
+ visitSQLSourceName(pcx) {
274
293
  const name = this.getModelEntryName(pcx.sqlExploreNameRef());
275
294
  return this.astAt(new ast.SQLSource(name), pcx);
276
295
  }
@@ -914,19 +933,32 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
914
933
  return this.getFieldExpr(pcx.fieldExpr());
915
934
  }
916
935
  visitDefineSQLStatement(pcx) {
917
- return this.visitSQLStatementDef(pcx.sqlStatementDef());
918
- }
919
- visitSQLStatementDef(pcx) {
920
- const commands = pcx.SQL_STRING().text;
921
- const sqlStmt = new ast.SQLStatement({
922
- select: commands.slice(2, commands.length - 2),
923
- connection: this.optionalText(pcx.connectionName()),
924
- });
925
- const nameCx = pcx.sqlCommandNameDef();
926
- if (nameCx) {
927
- sqlStmt.is = this.getIdText(nameCx);
936
+ var _a;
937
+ const blockName = (_a = pcx.nameSQLBlock()) === null || _a === void 0 ? void 0 : _a.text;
938
+ const blockParts = pcx.sqlBlock().blockSQLDef();
939
+ const sqlStr = new ast.SQLString();
940
+ let connectionName;
941
+ for (const blockEnt of blockParts) {
942
+ const nmCx = blockEnt.connectionName();
943
+ if (nmCx) {
944
+ if (connectionName) {
945
+ this.contextError(nmCx, "Cannot redefine connection");
946
+ }
947
+ else {
948
+ connectionName = this.getIdText(nmCx);
949
+ }
950
+ }
951
+ const selCx = blockEnt.sqlString();
952
+ if (selCx) {
953
+ this.makeSqlString(selCx, sqlStr);
954
+ }
928
955
  }
929
- return this.astAt(sqlStmt, pcx);
956
+ const stmt = new ast.SQLStatement(sqlStr);
957
+ if (connectionName !== undefined) {
958
+ stmt.connection = connectionName;
959
+ }
960
+ stmt.is = blockName;
961
+ return this.astAt(stmt, pcx);
930
962
  }
931
963
  visitSampleStatement(pcx) {
932
964
  const rowCx = pcx.sampleSpec().INTEGER_LITERAL();
@@ -150,9 +150,9 @@ class DocumentSymbolWalker {
150
150
  parent.children.push(symbol);
151
151
  }
152
152
  }
153
- enterSqlStatementDef(pcx) {
153
+ enterDefineSQLStatement(pcx) {
154
154
  var _a;
155
- const name = (_a = pcx.sqlCommandNameDef()) === null || _a === void 0 ? void 0 : _a.id().text;
155
+ const name = (_a = pcx.nameSQLBlock()) === null || _a === void 0 ? void 0 : _a.text;
156
156
  const symbol = {
157
157
  range: this.translator.rangeFromContext(pcx),
158
158
  name: name || "unnamed_sql",
@@ -1,12 +1,14 @@
1
+ import { DocumentLocation } from "../../model";
1
2
  declare global {
2
3
  namespace jest {
3
4
  interface Matchers<R> {
4
- toCompile(): R;
5
+ modelParsed(): R;
5
6
  toBeErrorless(): R;
6
- toTranslate(): R;
7
+ toCompile(): R;
8
+ modelCompiled(): R;
7
9
  toReturnType(tp: string): R;
8
10
  compileToFailWith(...expectedErrors: string[]): R;
11
+ isLocationIn(at: DocumentLocation, txt: string): R;
9
12
  }
10
13
  }
11
14
  }
12
- export {};