| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { Column } from "./column.js";
- import { entityKind, is } from "./entity.js";
- import { SQL, sql } from "./sql/sql.js";
- import { Table } from "./table.js";
- import { ViewBaseConfig } from "./view-common.js";
- class ColumnAliasProxyHandler {
- constructor(table) {
- this.table = table;
- }
- static [entityKind] = "ColumnAliasProxyHandler";
- get(columnObj, prop) {
- if (prop === "table") {
- return this.table;
- }
- return columnObj[prop];
- }
- }
- class TableAliasProxyHandler {
- constructor(alias, replaceOriginalName) {
- this.alias = alias;
- this.replaceOriginalName = replaceOriginalName;
- }
- static [entityKind] = "TableAliasProxyHandler";
- get(target, prop) {
- if (prop === Table.Symbol.IsAlias) {
- return true;
- }
- if (prop === Table.Symbol.Name) {
- return this.alias;
- }
- if (this.replaceOriginalName && prop === Table.Symbol.OriginalName) {
- return this.alias;
- }
- if (prop === ViewBaseConfig) {
- return {
- ...target[ViewBaseConfig],
- name: this.alias,
- isAlias: true
- };
- }
- if (prop === Table.Symbol.Columns) {
- const columns = target[Table.Symbol.Columns];
- if (!columns) {
- return columns;
- }
- const proxiedColumns = {};
- Object.keys(columns).map((key) => {
- proxiedColumns[key] = new Proxy(
- columns[key],
- new ColumnAliasProxyHandler(new Proxy(target, this))
- );
- });
- return proxiedColumns;
- }
- const value = target[prop];
- if (is(value, Column)) {
- return new Proxy(value, new ColumnAliasProxyHandler(new Proxy(target, this)));
- }
- return value;
- }
- }
- class RelationTableAliasProxyHandler {
- constructor(alias) {
- this.alias = alias;
- }
- static [entityKind] = "RelationTableAliasProxyHandler";
- get(target, prop) {
- if (prop === "sourceTable") {
- return aliasedTable(target.sourceTable, this.alias);
- }
- return target[prop];
- }
- }
- function aliasedTable(table, tableAlias) {
- return new Proxy(table, new TableAliasProxyHandler(tableAlias, false));
- }
- function aliasedRelation(relation, tableAlias) {
- return new Proxy(relation, new RelationTableAliasProxyHandler(tableAlias));
- }
- function aliasedTableColumn(column, tableAlias) {
- return new Proxy(
- column,
- new ColumnAliasProxyHandler(new Proxy(column.table, new TableAliasProxyHandler(tableAlias, false)))
- );
- }
- function mapColumnsInAliasedSQLToAlias(query, alias) {
- return new SQL.Aliased(mapColumnsInSQLToAlias(query.sql, alias), query.fieldAlias);
- }
- function mapColumnsInSQLToAlias(query, alias) {
- return sql.join(query.queryChunks.map((c) => {
- if (is(c, Column)) {
- return aliasedTableColumn(c, alias);
- }
- if (is(c, SQL)) {
- return mapColumnsInSQLToAlias(c, alias);
- }
- if (is(c, SQL.Aliased)) {
- return mapColumnsInAliasedSQLToAlias(c, alias);
- }
- return c;
- }));
- }
- export {
- ColumnAliasProxyHandler,
- RelationTableAliasProxyHandler,
- TableAliasProxyHandler,
- aliasedRelation,
- aliasedTable,
- aliasedTableColumn,
- mapColumnsInAliasedSQLToAlias,
- mapColumnsInSQLToAlias
- };
- //# sourceMappingURL=alias.js.map
|