Skip to content

Commit

Permalink
get tables by schema names
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeNitsenko committed Aug 23, 2023
1 parent 4f11e46 commit d0e7866
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
27 changes: 24 additions & 3 deletions packages/cubejs-base-driver/src/BaseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
TableQueryResult,
TableStructure,
DriverCapabilities,
SchemaQueryResult
QuerySchemasResult,
QueryTablesResult
} from './driver.interface';

const sortByKeys = (unordered: any) => {
Expand Down Expand Up @@ -172,6 +173,17 @@ export abstract class BaseDriver implements DriverInterface {
`;
}

protected informationSchemaGetTablesQuery(schemaNames: string[]) {
const schemas = schemaNames.map(s => this.singleQuoteIdentifier(s)).join(', ');
return `
SELECT table_schema as ${this.quoteIdentifier('schema_name')},
table_name as ${this.quoteIdentifier('table_name')}
FROM information_schema.tables
WHERE table_schema IN (${schemas})
ORDER BY table_schema, table_name
`;
}

protected getSslOptions(dataSource: string): TLSConnectionOptions | undefined {
if (
getEnv('dbSsl', { dataSource }) ||
Expand Down Expand Up @@ -320,9 +332,18 @@ export abstract class BaseDriver implements DriverInterface {
}
}

public getSchemasQuery() {
public getSchemas() {
const query = this.informationSchemaGetSchemasQuery();
return this.query<SchemaQueryResult>(query);
return this.query<QuerySchemasResult>(query);
}

public getTables(schemaNames?: string[]) {
if (!schemaNames) {
throw new Error('getTables without schemaNames is not supported');
}

const query = this.informationSchemaGetTablesQuery(schemaNames);
return this.query<QueryTablesResult>(query);
}

public getTablesQuery(schemaName: string) {
Expand Down
8 changes: 6 additions & 2 deletions packages/cubejs-base-driver/src/driver.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ export type DownloadQueryResultsResult = DownloadQueryResultsBase & (DownloadTab
// eslint-disable-next-line camelcase
export type TableQueryResult = { table_name?: string, TABLE_NAME?: string };

export type SchemaQueryResult = { schemaName: string };
export type QuerySchemasResult = { schemaName: string };

export type QueryTablesResult = { schemaName: string, tableName: string };

export interface DriverInterface {
createSchemaIfNotExists(schemaName: string): Promise<void>;
Expand All @@ -180,7 +182,9 @@ export interface DriverInterface {
tableColumnTypes: (table: string) => Promise<TableStructure>;
queryColumnTypes: (sql: string, params?: unknown[]) => Promise<{ name: any; type: string; }[]>;
// Returns all schemas for current database
getSchemasQuery: () => Promise<SchemaQueryResult[]>;
getSchemas: () => Promise<QuerySchemasResult[]>;
// Returns all tables for defined schemas
getTables: (schemaNames: string[]) => Promise<QueryTablesResult[]>;
// eslint-disable-next-line camelcase
getTablesQuery: (schemaName: string) => Promise<TableQueryResult[]>;
// Remove table from database
Expand Down
20 changes: 19 additions & 1 deletion packages/cubejs-server-core/src/core/DevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,28 @@ export class DevServer {
requestId: getRequestIdFromRequest(req),
});

const schemas = await driver.getSchemasQuery();
const schemas = await driver.getSchemas();
res.json({ schemas });
}));

/** WIP */
app.post('/playground/list-tables', catchErrors(async (req, res) => {
if (!req.body.schemaNames) {
throw new Error('schemaNames parameter is required');
}

// this.cubejsServer.event('Dev Server List Tables');
const driver = await this.cubejsServer.getDriver({
dataSource: req.body.dataSource || 'default',
authInfo: null,
securityContext: null,
requestId: getRequestIdFromRequest(req),
});

const tables = await driver.getTables(req.body.schemaNames);
res.json({ tables });
}));

app.get('/playground/db-schema', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server DB Schema Load');
const driver = await this.cubejsServer.getDriver({
Expand Down

0 comments on commit d0e7866

Please sign in to comment.