Skip to content

Commit

Permalink
fix: Unexpected string literal Bigquery
Browse files Browse the repository at this point in the history
Fixes #182
  • Loading branch information
paveltiunov committed Aug 14, 2019
1 parent 840f9d4 commit 8768895
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/cubejs-bigquery-driver/driver/BigQueryDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ class BigQueryDriver extends BaseDriver {
await pause(5000);
}
}

quoteIdentifier(identifier) {
const nestedFields = identifier.split('.');
return nestedFields.map(name => {
if (name.match(/^[a-z0-9_]+$/)) {
return name;
}
return `\`${identifier}\``;
}).join('.');
}
}

module.exports = BigQueryDriver;
17 changes: 13 additions & 4 deletions packages/cubejs-schema-compiler/scaffolding/ScaffoldingTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ class ScaffoldingTemplate {
}

escapeName(name) {
if (name.match(/^[a-z0-9_]+$/)) {
if (this.eligibleIdentifier(name)) {
return name;
}
return this.driver.quoteIdentifier(name);
}

eligibleIdentifier(name) {
return !!name.match(/^[a-z0-9_]+$/);
}

generateFilesByTableNames(tableNames) {
const schemaForTables = this.scaffoldingSchema.generateForTables(tableNames.map(n => this.resolveTableName(n)));
return schemaForTables.map(tableSchema => ({
Expand Down Expand Up @@ -57,7 +61,7 @@ class ScaffoldingTemplate {
})).reduce((a, b) => ({ ...a, ...b }), {}),
measures: tableSchema.measures.map(m => ({
[this.memberName(m)]: {
sql: `${this.escapeName(m.name) !== m.name ? '${CUBE}.' : ''}${this.escapeName(m.name)}`,
sql: this.sqlForMember(m),
type: m.types[0],
title: this.memberTitle(m)
}
Expand All @@ -69,13 +73,18 @@ class ScaffoldingTemplate {
}),
dimensions: tableSchema.dimensions.map(m => ({
[this.memberName(m)]: {
sql: `${this.escapeName(m.name) !== m.name ? '${CUBE}.' : ''}${this.escapeName(m.name)}`,
sql: this.sqlForMember(m),
type: m.types[0],
title: this.memberTitle(m),
primaryKey: m.isPrimaryKey ? true : undefined
}
})).reduce((a, b) => ({ ...a, ...b }), {})
}
};
}

sqlForMember(m) {
// eslint-disable-next-line no-template-curly-in-string
return `${this.escapeName(m.name) !== m.name || !this.eligibleIdentifier(m.name) ? '${CUBE}.' : ''}${this.escapeName(m.name)}`;
}

memberTitle(m) {
Expand Down
65 changes: 65 additions & 0 deletions packages/cubejs-schema-compiler/test/ScaffoldingSchemaTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable quote-props */
/* globals it,describe */
const ScaffoldingSchema = require('../scaffolding/ScaffoldingSchema');
const ScaffoldingTemplate = require('../scaffolding/ScaffoldingTemplate');
require('should');
Expand All @@ -10,6 +12,18 @@ const mySqlDriver = {
quoteIdentifier: (name) => `\`${name}\``
};

const bigQueryDriver = {
quoteIdentifier(identifier) {
const nestedFields = identifier.split('.');
return nestedFields.map(name => {
if (name.match(/^[a-z0-9_]+$/)) {
return name;
}
return `\`${identifier}\``;
}).join('.');
}
};

describe('ScaffoldingSchema', () => {
it('schema', () => {
const schema = new ScaffoldingSchema({
Expand Down Expand Up @@ -461,6 +475,57 @@ describe('ScaffoldingSchema', () => {
}
}
});
`
}
]);
});

it('big query nested fields', () => {
const template = new ScaffoldingTemplate({
public: {
orders: [{
"name": "id",
"type": "integer",
"attributes": []
}, {
"name": "some.dimension.inside",
"nestedName": ['some', 'dimension', 'inside'],
"type": "string",
"attributes": []
}]
}
}, bigQueryDriver);
template.generateFilesByTableNames(['public.orders']).should.be.deepEqual([
{
fileName: 'Orders.js',
content: `cube(\`Orders\`, {
sql: \`SELECT * FROM public.orders\`,
joins: {
},
measures: {
count: {
type: \`count\`,
drillMembers: [id, someDimensionInside]
}
},
dimensions: {
id: {
sql: \`id\`,
type: \`number\`,
primaryKey: true
},
someDimensionInside: {
sql: \`\${CUBE}.some.dimension.inside\`,
type: \`string\`,
title: \`Some.dimension.inside\`
}
}
});
`
}
]);
Expand Down

0 comments on commit 8768895

Please sign in to comment.