From 880cf8dc1025a81bb4d5cde3f696f766d6b27ebb Mon Sep 17 00:00:00 2001 From: Juanjo Ortilles Date: Wed, 14 Aug 2024 12:11:20 +0200 Subject: [PATCH] EDA_QUERY_2 patos --- .../qb-systems/bigquery-builder.service.ts | 3 +- .../qb-systems/oracle-builder.service.ts | 78 ++++++++++++++++- .../qb-systems/snowflake-builder.service.ts | 85 ++++++++++++++++++- .../qb-systems/sqlserver-builder-service.ts | 85 ++++++++++++++++++- 4 files changed, 246 insertions(+), 5 deletions(-) diff --git a/eda/eda_api/lib/services/query-builder/qb-systems/bigquery-builder.service.ts b/eda/eda_api/lib/services/query-builder/qb-systems/bigquery-builder.service.ts index 863071e0..6ad8422a 100644 --- a/eda/eda_api/lib/services/query-builder/qb-systems/bigquery-builder.service.ts +++ b/eda/eda_api/lib/services/query-builder/qb-systems/bigquery-builder.service.ts @@ -80,8 +80,7 @@ export class BigQueryBuilderService extends QueryBuilderService { for (const key in alias) { myQuery = myQuery.split(key).join(`\`${alias[key]}\``); } - - + } return myQuery; } diff --git a/eda/eda_api/lib/services/query-builder/qb-systems/oracle-builder.service.ts b/eda/eda_api/lib/services/query-builder/qb-systems/oracle-builder.service.ts index 542c3683..bc89dc10 100644 --- a/eda/eda_api/lib/services/query-builder/qb-systems/oracle-builder.service.ts +++ b/eda/eda_api/lib/services/query-builder/qb-systems/oracle-builder.service.ts @@ -17,7 +17,15 @@ export class OracleBuilderService extends QueryBuilderService { } - const joinString = this.getJoins(joinTree, dest, tables, joinType, valueListJoins); + let joinString: any[]; + let alias: any; + if (this.queryTODO.joined) { + const responseJoins = this.setJoins(joinTree, joinType, valueListJoins); + joinString = responseJoins.joinString; + alias = responseJoins.aliasTables; + } else { + joinString = this.getJoins(joinTree, dest, tables, joinType, valueListJoins ); + } joinString.forEach(x => { @@ -53,6 +61,14 @@ export class OracleBuilderService extends QueryBuilderService { myQuery = `${myQuery}\norder by ${order_columns_string}`; } + if (alias) { + console.log(alias); + for (const key in alias) { + myQuery = myQuery.split(key).join(`"${alias[key]}"`); + } + } + + if (limit) myQuery = `SELECT * FROM (${myQuery})\n WHERE ROWNUM <= ${limit}`; return myQuery; } @@ -165,6 +181,66 @@ export class OracleBuilderService extends QueryBuilderService { return joinString; } + + + public setJoins(joinTree: any[], joinType: string, valueListJoins: string[]) { + + // Inicialización de variables + const joinExists = new Set(); + const aliasTables = {}; + const joinString = []; + const targetTableJoin = []; + + for (const join of joinTree) { + // División de las partes de la join + const [sourceTable, sourceColumn] = join[0].split('.'); + const [targetTable, targetColumn] = join[1].split('.'); + + // Construcción de las partes de la join + const sourceJoin = ` "${sourceTable}"."${sourceColumn}"`; + let targetJoin = ` "${targetTable}"."${targetColumn}"`; + + // Si la join no existe ya, se añade + if (!joinExists.has(`${sourceJoin}=${targetJoin}`)) { + joinExists.add(`${sourceJoin}=${targetJoin}`); + + // Construcción de los alias + const alias = `"${targetTable}.${targetColumn}"`; + aliasTables[alias] = targetTable; + + let aliasTargetTable: string; + if (targetTableJoin.includes(targetTable)) { + aliasTargetTable = `${targetTable}${targetTableJoin.indexOf(targetTable)}`; + aliasTables[alias] = aliasTargetTable; + } + + let joinStr: string; + + joinType = valueListJoins.includes(targetTable) ? 'LEFT' : joinType; + + if (aliasTargetTable) { + targetJoin = `"${aliasTargetTable}"."${targetColumn}"`; + joinStr = `${joinType} JOIN " ${targetTable}" "${aliasTargetTable}" ON ${sourceJoin} = ${targetJoin}`; + } else { + joinStr = `${joinType} JOIN " ${targetTable}" ON ${sourceJoin} = ${targetJoin}`; + } + + // Si la join no se ha incluido ya, se añade al array + if (!joinString.includes(joinStr)) { + targetTableJoin.push(aliasTargetTable || targetTable); + joinString.push(joinStr); + } + } + } + + return { + joinString, + aliasTables + }; + } + + + public getSeparedColumns(origin: string, dest: string[]) { const columns = []; const grouping = []; diff --git a/eda/eda_api/lib/services/query-builder/qb-systems/snowflake-builder.service.ts b/eda/eda_api/lib/services/query-builder/qb-systems/snowflake-builder.service.ts index e863ca79..2c8bfd1e 100644 --- a/eda/eda_api/lib/services/query-builder/qb-systems/snowflake-builder.service.ts +++ b/eda/eda_api/lib/services/query-builder/qb-systems/snowflake-builder.service.ts @@ -25,8 +25,19 @@ export class SnowFlakeBuilderService extends QueryBuilderService { myQuery = `SELECT DISTINCT ${columns.join(', ')} \nFROM ${o}`; } + // JOINS - const joinString = this.getJoins(joinTree, dest, tables, joinType,valueListJoins, SCHEMA); + let joinString: any[]; + let alias: any; + if (this.queryTODO.joined) { + const responseJoins = this.setJoins(joinTree, joinType, SCHEMA, valueListJoins); + joinString = responseJoins.joinString; + alias = responseJoins.aliasTables; + } else { + joinString = this.getJoins(joinTree, dest, tables, joinType, valueListJoins, SCHEMA); + } + + joinString.forEach(x => { myQuery = myQuery + '\n' + x; @@ -60,6 +71,17 @@ export class SnowFlakeBuilderService extends QueryBuilderService { if (order_columns_string.length > 0) { myQuery = `${myQuery}\norder by ${order_columns_string}`; } + + + if (limit) myQuery += `\nlimit ${limit}`; + + if (alias) { + console.log(alias); + for (const key in alias) { + myQuery = myQuery.split(key).join(`"${alias[key]}"`); + } + } + return myQuery; } @@ -164,6 +186,67 @@ export class SnowFlakeBuilderService extends QueryBuilderService { return joinString; } + + public setJoins(joinTree: any[], joinType: string, schema: string, valueListJoins: string[]) { + // Si no se especifica un esquema, se utiliza 'public' por defecto + if (!schema || schema === 'null') { + schema = 'public'; + } + + // Inicialización de variables + const joinExists = new Set(); + const aliasTables = {}; + const joinString = []; + const targetTableJoin = []; + + for (const join of joinTree) { + // División de las partes de la join + const [sourceTable, sourceColumn] = join[0].split('.'); + const [targetTable, targetColumn] = join[1].split('.'); + + // Construcción de las partes de la join + const sourceJoin = `"${schema}"."${sourceTable}"."${sourceColumn}"`; + let targetJoin = `"${schema}"."${targetTable}"."${targetColumn}"`; + + // Si la join no existe ya, se añade + if (!joinExists.has(`${sourceJoin}=${targetJoin}`)) { + joinExists.add(`${sourceJoin}=${targetJoin}`); + + // Construcción de los alias + const alias = `"${targetTable}.${targetColumn}"`; + aliasTables[alias] = targetTable; + + let aliasTargetTable: string; + if (targetTableJoin.includes(targetTable)) { + aliasTargetTable = `${targetTable}${targetTableJoin.indexOf(targetTable)}`; + aliasTables[alias] = aliasTargetTable; + } + + let joinStr: string; + + joinType = valueListJoins.includes(targetTable) ? 'LEFT' : joinType; + + if (aliasTargetTable) { + targetJoin = `"${aliasTargetTable}"."${targetColumn}"`; + joinStr = `${joinType} JOIN "${schema}"."${targetTable}" "${aliasTargetTable}" ON ${sourceJoin} = ${targetJoin}`; + } else { + joinStr = `${joinType} JOIN "${schema}"."${targetTable}" ON ${sourceJoin} = ${targetJoin}`; + } + + // Si la join no se ha incluido ya, se añade al array + if (!joinString.includes(joinStr)) { + targetTableJoin.push(aliasTargetTable || targetTable); + joinString.push(joinStr); + } + } + } + + return { + joinString, + aliasTables + }; + } + public getSeparedColumns(origin: string, dest: string[]) { const columns = []; const grouping = []; diff --git a/eda/eda_api/lib/services/query-builder/qb-systems/sqlserver-builder-service.ts b/eda/eda_api/lib/services/query-builder/qb-systems/sqlserver-builder-service.ts index 40421158..efb90ea8 100644 --- a/eda/eda_api/lib/services/query-builder/qb-systems/sqlserver-builder-service.ts +++ b/eda/eda_api/lib/services/query-builder/qb-systems/sqlserver-builder-service.ts @@ -28,7 +28,16 @@ export class SQLserviceBuilderService extends QueryBuilderService { } // JOINS - const joinString = this.getJoins(joinTree, dest, tables, joinType,valueListJoins, SCHEMA); + let joinString: any[]; + let alias: any; + if (this.queryTODO.joined) { + const responseJoins = this.setJoins(joinTree, joinType, schema, valueListJoins); + joinString = responseJoins.joinString; + alias = responseJoins.aliasTables; + } else { + joinString = this.getJoins(joinTree, dest, tables, joinType, valueListJoins, schema); + } + joinString.forEach(x => { myQuery = myQuery + '\n' + x; @@ -62,6 +71,17 @@ export class SQLserviceBuilderService extends QueryBuilderService { if (order_columns_string.length > 0) { myQuery = `${myQuery}\norder by ${order_columns_string}`; } + + + if (alias) { + console.log(alias); + for (const key in alias) { + myQuery = myQuery.split(key).join(`"${alias[key]}"`); + } + } + + + return myQuery; } @@ -163,6 +183,69 @@ export class SQLserviceBuilderService extends QueryBuilderService { return joinString; } + + + public setJoins(joinTree: any[], joinType: string, schema: string, valueListJoins: string[]) { + // Si no se especifica un esquema, se utiliza 'public' por defecto + if (!schema || schema === 'null') { + schema = 'public'; + } + + // Inicialización de variables + const joinExists = new Set(); + const aliasTables = {}; + const joinString = []; + const targetTableJoin = []; + + for (const join of joinTree) { + // División de las partes de la join + const [sourceTable, sourceColumn] = join[0].split('.'); + const [targetTable, targetColumn] = join[1].split('.'); + + // Construcción de las partes de la join + const sourceJoin = `"${schema}"."${sourceTable}"."${sourceColumn}"`; + let targetJoin = `"${schema}"."${targetTable}"."${targetColumn}"`; + + // Si la join no existe ya, se añade + if (!joinExists.has(`${sourceJoin}=${targetJoin}`)) { + joinExists.add(`${sourceJoin}=${targetJoin}`); + + // Construcción de los alias + const alias = `"${targetTable}.${targetColumn}"`; + aliasTables[alias] = targetTable; + + let aliasTargetTable: string; + if (targetTableJoin.includes(targetTable)) { + aliasTargetTable = `${targetTable}${targetTableJoin.indexOf(targetTable)}`; + aliasTables[alias] = aliasTargetTable; + } + + let joinStr: string; + + joinType = valueListJoins.includes(targetTable) ? 'LEFT' : joinType; + + if (aliasTargetTable) { + targetJoin = `"${aliasTargetTable}"."${targetColumn}"`; + joinStr = `${joinType} JOIN "${schema}"."${targetTable}" "${aliasTargetTable}" ON ${sourceJoin} = ${targetJoin}`; + } else { + joinStr = `${joinType} JOIN "${schema}"."${targetTable}" ON ${sourceJoin} = ${targetJoin}`; + } + + // Si la join no se ha incluido ya, se añade al array + if (!joinString.includes(joinStr)) { + targetTableJoin.push(aliasTargetTable || targetTable); + joinString.push(joinStr); + } + } + } + + return { + joinString, + aliasTables + }; + } + + public getSeparedColumns(origin: string, dest: string[]) { const columns = []; const grouping = [];