Skip to content

Commit

Permalink
whatIfExpresion qbSystems
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Alloza committed Jan 14, 2024
1 parent 07fcf56 commit ef0f5f4
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,13 @@ export class BigQueryBuilderService extends QueryBuilderService {

el.display_name = el.display_name.replace(/ /g, "_");

if (!el.hasOwnProperty('minimumFractionDigits')) {
el.minimumFractionDigits = 0;
}
const table_column = `\`${el.table_id}\`.\`${el.column_name}\``;

let whatIfExpression = '';
if (el.whatif_column) whatIfExpression = `${el.whatif.operator} ${el.whatif.value}`;

el.minimumFractionDigits = el.minimumFractionDigits || 0;

// Aqui se manejan las columnas calculadas
if (el.computed_column === 'computed') {
if(el.column_type=='text'){
Expand Down Expand Up @@ -233,47 +237,47 @@ export class BigQueryBuilderService extends QueryBuilderService {

if (el.aggregation_type !== 'none') {
if (el.aggregation_type === 'count_distinct') {
columns.push(`ROUND( count( distinct \`${el.table_id}\`.\`${el.column_name}\`), ${el.minimumFractionDigits||0}) as \`${el.display_name}\``);
columns.push(`ROUND(count(distinct ${table_column}), ${el.minimumFractionDigits}) ${whatIfExpression} as \`${el.display_name}\``);
} else {
columns.push(`ROUND(${el.aggregation_type}(\`${el.table_id}\`.\`${el.column_name}\`), ${el.minimumFractionDigits||0}) as \`${el.display_name}\``);
columns.push(`ROUND(${el.aggregation_type}(${table_column}), ${el.minimumFractionDigits}) ${whatIfExpression} as \`${el.display_name}\``);
}
} else {
if (el.column_type === 'numeric') {
columns.push(`ROUND(\`${el.table_id}\`.\`${el.column_name}\`, ${el.minimumFractionDigits}) as \`${el.display_name}\``);
columns.push(`ROUND(${table_column}, ${el.minimumFractionDigits}) ${whatIfExpression} as \`${el.display_name}\``);
} else if (el.column_type === 'date') {
if (el.format) {
if (_.isEqual(el.format, 'year')) {
columns.push(`FORMAT_DATETIME('%Y',\`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME('%Y',${table_column}) as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'quarter')) {
columns.push(`FORMAT_DATETIME('%Y-%Q', \`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME('%Y-%Q', ${table_column}) as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'month')) {
columns.push(`FORMAT_DATETIME('%Y-%m', \`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME('%Y-%m', ${table_column}) as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'week')) {
columns.push(`EXTRACT(WEEK FROM \`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`EXTRACT(WEEK FROM ${table_column}) as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'week_day')) {
columns.push(`EXTRACT(DAYOFWEEK FROM \`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`EXTRACT(DAYOFWEEK FROM ${table_column}) as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'day')) {
columns.push(`FORMAT_DATETIME( '%Y-%m-%d',\`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME( '%Y-%m-%d',${table_column}) as \`${el.display_name}\``);
}else if (_.isEqual(el.format, 'day_hour')) {
columns.push(`FORMAT_DATETIME( '%Y-%m-%d %H',\`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME( '%Y-%m-%d %H',${table_column}) as \`${el.display_name}\``);
}else if (_.isEqual(el.format, 'day_hour_minute')) {
columns.push(`FORMAT_DATETIME( '%Y-%m-%d %H:%i',\`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME( '%Y-%m-%d %H:%i',${table_column}) as \`${el.display_name}\``);
}else if (_.isEqual(el.format, 'timestamp')) {
columns.push(`FORMAT_DATETIME( '%Y-%m-%d %H:%i:%s',\`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME( '%Y-%m-%d %H:%i:%s',${table_column}) as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'No')) {
columns.push(`FORMAT_DATETIME('%Y-%m-%d', \`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME('%Y-%m-%d', ${table_column}) as \`${el.display_name}\``);
}
} else {
columns.push(`FORMAT_DATETIME( '%Y-%m-%d', \`${el.table_id}\`.\`${el.column_name}\`) as \`${el.display_name}\``);
columns.push(`FORMAT_DATETIME( '%Y-%m-%d', ${table_column}) as \`${el.display_name}\``);
}
} else {

columns.push(`\`${el.table_id}\`.\`${el.column_name}\` as \`${el.display_name}\``);
columns.push(`${table_column} as \`${el.display_name}\``);

}
// GROUP BY
// Si es una única columna numérica no se agrega.
if( this.queryTODO.fields.length > 1 || el.column_type != 'numeric' ){
if(this.queryTODO.fields.length > 1 || el.column_type != 'numeric'){
grouping.push(`\`${el.display_name}\``);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ export class OracleBuilderService extends QueryBuilderService {
this.queryTODO.fields.forEach(el => {
el.order !== 0 && el.table_id !== origin && !dest.includes(el.table_id) ? dest.push(el.table_id) : false;

if (!el.hasOwnProperty('minimumFractionDigits')) {
el.minimumFractionDigits = 0;
}
const table_column = `"${el.table_id}"."${el.column_name}"`;

let whatIfExpression = '';
if (el.whatif_column) whatIfExpression = `${el.whatif.operator} ${el.whatif.value}`;

el.minimumFractionDigits = el.minimumFractionDigits || 0;

// Aqui se manejan las columnas calculadas
if (el.computed_column === 'computed') {
Expand Down Expand Up @@ -220,70 +223,70 @@ export class OracleBuilderService extends QueryBuilderService {

if (el.aggregation_type !== 'none') {
if (el.aggregation_type === 'count_distinct') {
columns.push(`ROUND( count( distinct "${el.table_id}"."${el.column_name}"), ${el.minimumFractionDigits||0}) as "${el.display_name}"`);
columns.push(`ROUND(count(distinct ${table_column}), ${el.minimumFractionDigits}) ${whatIfExpression} as "${el.display_name}"`);
} else {
columns.push(`ROUND(${el.aggregation_type}("${el.table_id}"."${el.column_name}"), ${el.minimumFractionDigits||0}) as "${el.display_name}"`);
columns.push(`ROUND(${el.aggregation_type}(${table_column}), ${el.minimumFractionDigits}) ${whatIfExpression} as "${el.display_name}"`);
}
} else {
if (el.column_type === 'numeric') {
columns.push(`ROUND("${el.table_id}"."${el.column_name}", ${el.minimumFractionDigits}) as "${el.display_name}"`);
columns.push(`ROUND(${table_column}, ${el.minimumFractionDigits}) ${whatIfExpression} as "${el.display_name}"`);
} else if (el.column_type === 'date') {
if (el.format) {
if (_.isEqual(el.format, 'year')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY') as "${el.display_name}"`);
} else if (_.isEqual(el.format, 'quarter')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-"Q"Q') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-"Q"Q') as "${el.display_name}"`);
} else if (_.isEqual(el.format, 'month')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM') as "${el.display_name}"`);
} else if (_.isEqual(el.format, 'week')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'IYYY-IW') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'IYYY-IW') as "${el.display_name}"`);
} else if (_.isEqual(el.format, 'week_day')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'D') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'D') as "${el.display_name}"`);
} else if (_.isEqual(el.format, 'day')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM-DD') as "${el.display_name}"`);
}else if (_.isEqual(el.format, 'day_hour')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD HH24') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM-DD HH24') as "${el.display_name}"`);
}else if (_.isEqual(el.format, 'day_hour_minute')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD HH24:MI') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM-DD HH24:MI') as "${el.display_name}"`);
}else if (_.isEqual(el.format, 'timestamp')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD HH24:MI:SS') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM-DD HH24:MI:SS') as "${el.display_name}"`);
} else if (_.isEqual(el.format, 'No')) {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM-DD') as "${el.display_name}"`);
}
} else {
columns.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD') as "${el.display_name}"`);
columns.push(`to_char(${table_column}, 'YYYY-MM-DD') as "${el.display_name}"`);
}
} else {
columns.push(`"${el.table_id}"."${el.column_name}" as "${el.display_name}"`);
columns.push(`${table_column} as "${el.display_name}"`);
}
// GROUP BY
if (el.format) {
if (_.isEqual(el.format, 'year')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY')`);
grouping.push(`to_char(${table_column}, 'YYYY')`);
} else if (_.isEqual(el.format, 'quarter')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-"Q"Q')`);
grouping.push(`to_char(${table_column}, 'YYYY-"Q"Q')`);
} else if (_.isEqual(el.format, 'month')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM')`);
grouping.push(`to_char(${table_column}, 'YYYY-MM')`);
} else if (_.isEqual(el.format, 'week')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'IYYY-IW')`);
grouping.push(`to_char(${table_column}, 'IYYY-IW')`);
} else if (_.isEqual(el.format, 'week_day')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'D')`);
grouping.push(`to_char(${table_column}, 'D')`);
} else if (_.isEqual(el.format, 'day')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD')`);
grouping.push(`to_char(${table_column}, 'YYYY-MM-DD')`);
}else if (_.isEqual(el.format, 'day_hour')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD HH24') `);
grouping.push(`to_char(${table_column}, 'YYYY-MM-DD HH24') `);
}else if (_.isEqual(el.format, 'day_hour_minute')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD HH24:MI') `);
grouping.push(`to_char(${table_column}, 'YYYY-MM-DD HH24:MI') `);
} else if (_.isEqual(el.format, 'timestamp')) {
grouping.push(`to_char("${el.table_id}"."${el.column_name}", 'YYYY-MM-DD HH24:MI:SS')`);
grouping.push(`to_char(${table_column}, 'YYYY-MM-DD HH24:MI:SS')`);
} else if (_.isEqual(el.format, 'No')) {
grouping.push(`"${el.table_id}"."${el.column_name}"`);
grouping.push(`${table_column}`);
}
} else {

// Si es una única columna numérica no se agrega.
if( this.queryTODO.fields.length > 1 || el.column_type != 'numeric' ){
grouping.push(`"${el.table_id}"."${el.column_name}"`);
grouping.push(`${table_column}`);
}

}
Expand Down
Loading

0 comments on commit ef0f5f4

Please sign in to comment.