Skip to content

Commit

Permalink
Bug Fixes:
Browse files Browse the repository at this point in the history
  - Schema generator bug with case sensitive Table and Column names
  - Column name ambiguity issue during table joins with similar column names fix by forcing the alias for column names
 - Query bug fix to mandatorily include double quotes for the alias when running query against table names with restricted keywords
 such as "case"
  • Loading branch information
dgkm committed Jun 5, 2019
1 parent 2b55e74 commit a0b304e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/cubejs-schema-compiler/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ class BaseQuery {

cubeAlias(cubeName) {
const prefix = this.safeEvaluateSymbolContext().cubeAliasPrefix || this.cubeAliasPrefix;
return this.aliasName(`${prefix ? prefix + '__' : ''}${cubeName}`);
return this.aliasName(`"${prefix ? prefix + '__' : ''}${cubeName}"`);
}

collectCubeNamesFor(fn) {
Expand Down
11 changes: 8 additions & 3 deletions packages/cubejs-schema-compiler/scaffolding/ScaffoldingSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ScaffoldingSchema {
}
if (!this.dbSchema[schema][table]) {
throw new UserError(`Can't resolve ${tableName}: '${table}' does not exist`);
}
}
return this.dbSchema[schema][table];
}

Expand Down Expand Up @@ -148,10 +148,15 @@ class ScaffoldingSchema {
};

joins(tableName, tableDefinition) {
const key = "id";
return R.unnest(tableDefinition
.filter(column => column.name.toLowerCase().indexOf('id') !== -1 && column.name.toLowerCase() !== 'id')
.filter(column => {
const columnName = column.name.toLowerCase();
const columnKeyPosition = columnName.length - columnName.lastIndexOf(key) - key.length;
return (columnKeyPosition === 0 && columnName !== 'id')
})
.map(column => {
const withoutId = column.name.replace('_id', '').replace('id', '');
const withoutId = column.name.replace(new RegExp('_id$', "i"), '').replace(new RegExp('id$', "i"), '');
const tablesToJoin = this.tableNamesToTables[withoutId] || this.tableNamesToTables[inflection.tableize(withoutId)];

if (!tablesToJoin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class ScaffoldingTemplate {
schemaDescriptorForTable(tableSchema) {
return {
cube: tableSchema.cube,
sql: `SELECT * FROM ${tableSchema.schema}.${tableSchema.table}`, // TODO escape
sql: `SELECT * FROM "${tableSchema.schema}"."${tableSchema.table}"`, // TODO escape
joins: tableSchema.joins.map(j => ({
[j.cubeToJoin]: {
sql: `\${CUBE}.${j.thisTableColumn} = \${${j.cubeToJoin}}.${j.columnToJoin}`,
sql: `\${CUBE}."${j.thisTableColumn}" = \${${j.cubeToJoin}}."${j.columnToJoin}"`,
relationship: j.relationship
}
})).reduce((a, b) => ({ ...a, ...b }), {}),
measures: tableSchema.measures.map(m => ({
[this.memberName(m)]: {
sql: m.name,
sql: `\${CUBE}."${m.name}"`,
type: m.types[0],
title: this.memberTitle(m)
}
Expand All @@ -61,7 +61,7 @@ class ScaffoldingTemplate {
}),
dimensions: tableSchema.dimensions.map(m => ({
[this.memberName(m)]: {
sql: m.name,
sql: `\${CUBE}."${m.name}"`,
type: m.types[0],
title: this.memberTitle(m),
primaryKey: m.isPrimaryKey ? true : undefined
Expand Down

0 comments on commit a0b304e

Please sign in to comment.