|
| 1 | +import { CompilerInterface } from './PrepareCompiler'; |
| 2 | +import { CubeDefinitionExtended, CubeSymbols, JoinDefinition } from './CubeSymbols'; |
| 3 | +import type { ErrorReporter } from './ErrorReporter'; |
| 4 | + |
| 5 | +export class CubeJoinsResolver extends CubeSymbols implements CompilerInterface { |
| 6 | + // key: cubeName with joins defined |
| 7 | + private cubeJoins: Record<string, JoinDefinition[]>; |
| 8 | + |
| 9 | + // key: cubeName with joins defined |
| 10 | + // 1st level value: join alias |
| 11 | + // 2nd level value: join definition |
| 12 | + private cubeJoinAliases: Record<string, Record<string, JoinDefinition>>; |
| 13 | + |
| 14 | + // key: cubeName with joins defined |
| 15 | + // 1st level value: target cube name |
| 16 | + // 2nd level value: join definition |
| 17 | + private cubeJoinTargets: Record<string, Record<string, JoinDefinition>>; |
| 18 | + |
| 19 | + public constructor() { |
| 20 | + super(false); // It seems that we don't need to evaluate views |
| 21 | + this.cubeJoins = {}; |
| 22 | + this.cubeJoinAliases = {}; |
| 23 | + this.cubeJoinTargets = {}; |
| 24 | + } |
| 25 | + |
| 26 | + public compile(cubes: CubeDefinitionExtended[], errorReporter: ErrorReporter) { |
| 27 | + super.compile(cubes, errorReporter); |
| 28 | + |
| 29 | + this.cubeList.forEach(cube => { |
| 30 | + if (!cube.joins) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + this.cubeJoins[cube.name] = this.cubeJoins[cube.name] || []; |
| 35 | + this.cubeJoinAliases[cube.name] = this.cubeJoinAliases[cube.name] || {}; |
| 36 | + this.cubeJoinTargets[cube.name] = this.cubeJoinTargets[cube.name] || {}; |
| 37 | + |
| 38 | + const er = errorReporter.inContext(`${cube.name} cube`); |
| 39 | + |
| 40 | + cube.joins.forEach(join => { |
| 41 | + this.cubeJoins[cube.name].push(join); |
| 42 | + this.cubeJoinTargets[cube.name][join.name] = join; |
| 43 | + |
| 44 | + if (join.alias) { |
| 45 | + if (this.cubeJoinAliases[cube.name][join.alias]) { |
| 46 | + er.error( |
| 47 | + `Join alias "${join.alias}" is already defined in cube "${cube.name}".`, |
| 48 | + cube.fileName |
| 49 | + ); |
| 50 | + |
| 51 | + return; |
| 52 | + } |
| 53 | + this.cubeJoinAliases[cube.name][join.alias] = join; |
| 54 | + } |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
0 commit comments