Refactor value column SQL generation

This commit is contained in:
Sven Klemm
2018-07-11 13:03:06 +02:00
parent 9ba42f63f1
commit 2784a2df6b
2 changed files with 20 additions and 18 deletions

View File

@ -86,8 +86,8 @@ export default class PostgresQuery {
} }
addGroupBy(partType, value) { addGroupBy(partType, value) {
var partModel = sqlPart.create({ type: partType, params: [value] }); let partModel = sqlPart.create({ type: partType, params: [value] });
var partCount = this.target.groupBy.length; let partCount = this.target.groupBy.length;
if (partCount === 0) { if (partCount === 0) {
this.target.groupBy.push(partModel.part); this.target.groupBy.push(partModel.part);
@ -137,11 +137,11 @@ export default class PostgresQuery {
// if we remove the field remove the whole statement // if we remove the field remove the whole statement
if (part.def.type === 'column') { if (part.def.type === 'column') {
if (this.selectModels.length > 1) { if (this.selectModels.length > 1) {
var modelsIndex = _.indexOf(this.selectModels, selectParts); let modelsIndex = _.indexOf(this.selectModels, selectParts);
this.selectModels.splice(modelsIndex, 1); this.selectModels.splice(modelsIndex, 1);
} }
} else { } else {
var partIndex = _.indexOf(selectParts, part); let partIndex = _.indexOf(selectParts, part);
selectParts.splice(partIndex, 1); selectParts.splice(partIndex, 1);
} }
@ -149,7 +149,7 @@ export default class PostgresQuery {
} }
addSelectPart(selectParts, type) { addSelectPart(selectParts, type) {
var partModel = sqlPart.create({ type: type }); let partModel = sqlPart.create({ type: type });
partModel.def.addStrategy(selectParts, partModel, this); partModel.def.addStrategy(selectParts, partModel, this);
this.updatePersistedParts(); this.updatePersistedParts();
} }
@ -164,7 +164,7 @@ export default class PostgresQuery {
return this.quoteLiteral(value); return this.quoteLiteral(value);
} }
var escapedValues = _.map(value, this.quoteLiteral); let escapedValues = _.map(value, this.quoteLiteral);
return '(' + escapedValues.join(',') + ')'; return '(' + escapedValues.join(',') + ')';
} }
@ -193,7 +193,7 @@ export default class PostgresQuery {
let query; let query;
if (timeGroup) { if (timeGroup) {
var args; let args;
if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') { if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') {
args = timeGroup.params.join(','); args = timeGroup.params.join(',');
} else { } else {
@ -220,22 +220,24 @@ export default class PostgresQuery {
buildValueColumns(target) { buildValueColumns(target) {
let query = ''; let query = '';
for (let i = 0; i < this.selectModels.length; i++) { for (let i = 0; i < this.selectModels.length; i++) {
let parts = this.selectModels[i]; query += ', ' + this.buildValueColumn(target, this.selectModels[i]);
var selectText = '';
for (let y = 0; y < parts.length; y++) {
let part = parts[y];
selectText = part.render(selectText);
}
query += ', ' + selectText;
} }
return query; return query;
} }
buildValueColumn(target, column) {
let selectText = '';
for (let i = 0; i < column.length; i++) {
let part = column[i];
selectText = part.render(selectText);
}
return selectText;
}
buildWhereClause(target) { buildWhereClause(target) {
let query = ''; let query = '';
var conditions = _.map(target.where, (tag, index) => { let conditions = _.map(target.where, (tag, index) => {
switch (tag.type) { switch (tag.type) {
case 'macro': case 'macro':
return tag.name + '(' + target.timeColumn + ')'; return tag.name + '(' + target.timeColumn + ')';
@ -258,7 +260,7 @@ export default class PostgresQuery {
let groupBySection = ''; let groupBySection = '';
for (let i = 0; i < this.groupByParts.length; i++) { for (let i = 0; i < this.groupByParts.length; i++) {
var part = this.groupByParts[i]; let part = this.groupByParts[i];
if (i > 0) { if (i > 0) {
groupBySection += ', '; groupBySection += ', ';
} }

View File

@ -78,7 +78,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.selectMenu = [ this.selectMenu = [
{ text: 'Aggregate', value: 'aggregate' }, { text: 'Aggregate', value: 'aggregate' },
{ text: 'Math', value: 'math' }, { text: 'Math', value: 'math' },
// { text: 'Special', value: 'special' }, { text: 'Special', value: 'special' },
{ text: 'Alias', value: 'alias' }, { text: 'Alias', value: 'alias' },
{ text: 'Column', value: 'column' }, { text: 'Column', value: 'column' },
]; ];