Skip to content

Commit 636c015

Browse files
committed
fix(schema-compiler): move create table name check to underlying driver for Postgres, MySQL, Oracle
1 parent 6d75c60 commit 636c015

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

packages/cubejs-mysql-driver/src/MySqlDriver.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
IndexesSQL,
2323
DownloadTableMemoryData,
2424
DriverCapabilities,
25+
TableColumn,
2526
} from '@cubejs-backend/base-driver';
2627

2728
const GenericTypeToMySql: Record<GenericDataBaseType, string> = {
@@ -180,7 +181,7 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
180181

181182
protected primaryKeysQuery(conditionString?: string): string | null {
182183
return `SELECT
183-
TABLE_SCHEMA as ${this.quoteIdentifier('table_schema')},
184+
TABLE_SCHEMA as ${this.quoteIdentifier('table_schema')},
184185
TABLE_NAME as ${this.quoteIdentifier('table_name')},
185186
COLUMN_NAME as ${this.quoteIdentifier('column_name')}
186187
FROM
@@ -262,6 +263,14 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
262263
}
263264
}
264265

266+
public async createTable(quotedTableName: string, columns: TableColumn[]): Promise<void> {
267+
if (quotedTableName.length > 64) {
268+
throw new Error('MySQL can not work with table names longer than 64 symbols. ' +
269+
`Consider using the 'sqlAlias' attribute in your cube definition for ${quotedTableName}.`);
270+
}
271+
return super.createTable(quotedTableName, columns);
272+
}
273+
265274
public async query(query: string, values: unknown[]) {
266275
return this.withConnection(async (conn) => {
267276
await this.setTimeZone(conn);

packages/cubejs-oracle-driver/driver/OracleDriver.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
getEnv,
99
assertDataSource,
1010
} = require('@cubejs-backend/shared');
11-
const { BaseDriver } = require('@cubejs-backend/base-driver');
11+
const { BaseDriver, TableColumn } = require('@cubejs-backend/base-driver');
1212
const oracledb = require('oracledb');
1313
const { reduce } = require('ramda');
1414

@@ -95,13 +95,13 @@ class OracleDriver extends BaseDriver {
9595
, tc.data_type "data_type"
9696
, c.constraint_type "key_type"
9797
from all_tab_columns tc
98-
left join all_cons_columns cc
99-
on (tc.owner, tc.table_name, tc.column_name)
98+
left join all_cons_columns cc
99+
on (tc.owner, tc.table_name, tc.column_name)
100100
in ((cc.owner, cc.table_name, cc.column_name))
101-
left join all_constraints c
102-
on (tc.owner, tc.table_name, cc.constraint_name)
103-
in ((c.owner, c.table_name, c.constraint_name))
104-
and c.constraint_type
101+
left join all_constraints c
102+
on (tc.owner, tc.table_name, cc.constraint_name)
103+
in ((c.owner, c.table_name, c.constraint_name))
104+
and c.constraint_type
105105
in ('P','U')
106106
where tc.owner = user
107107
`);
@@ -121,6 +121,14 @@ class OracleDriver extends BaseDriver {
121121
await this.query('SELECT 1 FROM DUAL', {});
122122
}
123123

124+
async createTable(quotedTableName, columns) {
125+
if (quotedTableName.length > 128) {
126+
throw new Error('Oracle can not work with table names longer than 128 symbols. ' +
127+
`Consider using the 'sqlAlias' attribute in your cube definition for ${quotedTableName}.`);
128+
}
129+
return super.createTable(quotedTableName, columns);
130+
}
131+
124132
async query(query, values) {
125133
const conn = await this.getConnectionFromPool();
126134

packages/cubejs-postgres-driver/src/PostgresDriver.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
BaseDriver,
1717
DownloadQueryResultsOptions, DownloadTableMemoryData, DriverInterface,
1818
GenericDataBaseType, IndexesSQL, TableStructure, StreamOptions,
19-
StreamTableDataWithTypes, QueryOptions, DownloadQueryResultsResult, DriverCapabilities,
19+
StreamTableDataWithTypes, QueryOptions, DownloadQueryResultsResult, DriverCapabilities, TableColumn,
2020
} from '@cubejs-backend/base-driver';
2121
import { QueryStream } from './QueryStream';
2222

@@ -118,7 +118,7 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre
118118
const dataSource =
119119
config.dataSource ||
120120
assertDataSource('default');
121-
121+
122122
this.pool = new Pool({
123123
idleTimeoutMillis: 30000,
124124
max:
@@ -146,9 +146,9 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre
146146
}
147147

148148
protected primaryKeysQuery(conditionString?: string): string | null {
149-
return `SELECT
149+
return `SELECT
150150
columns.table_schema as ${this.quoteIdentifier('table_schema')},
151-
columns.table_name as ${this.quoteIdentifier('table_name')},
151+
columns.table_name as ${this.quoteIdentifier('table_name')},
152152
columns.column_name as ${this.quoteIdentifier('column_name')}
153153
FROM information_schema.table_constraints tc
154154
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
@@ -360,6 +360,14 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre
360360
}
361361
}
362362

363+
public async createTable(quotedTableName: string, columns: TableColumn[]): Promise<void> {
364+
if (quotedTableName.length > 63) {
365+
throw new Error('PostgreSQL can not work with table names longer than 63 symbols. ' +
366+
`Consider using the 'sqlAlias' attribute in your cube definition for ${quotedTableName}.`);
367+
}
368+
return super.createTable(quotedTableName, columns);
369+
}
370+
363371
// eslint-disable-next-line @typescript-eslint/no-unused-vars
364372
public async query<R = unknown>(query: string, values: unknown[], options?: QueryOptions): Promise<R[]> {
365373
const result = await this.queryResponse(query, values);

0 commit comments

Comments
 (0)