Skip to content

Commit

Permalink
use parameterized query for new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeNitsenko committed Aug 29, 2023
1 parent 5773564 commit 806e7f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
59 changes: 30 additions & 29 deletions packages/cubejs-base-driver/src/BaseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,6 @@ export abstract class BaseDriver implements DriverInterface {
`;
}

protected informationSchemaGetSchemasQuery() {
return `
SELECT table_schema as ${this.quoteIdentifier('schema_name')}
FROM information_schema.tables
WHERE table_schema NOT IN (${this.getIgnoredSchemas()})
GROUP BY table_schema
ORDER BY table_schema
`;
}

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 @@ -334,32 +313,54 @@ export abstract class BaseDriver implements DriverInterface {
}

public getSchemas() {
const query = this.informationSchemaGetSchemasQuery();
const query = `

Check warning on line 316 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L315-L316

Added lines #L315 - L316 were not covered by tests
SELECT table_schema as ${this.quoteIdentifier('schema_name')}
FROM information_schema.tables
WHERE table_schema NOT IN (${this.getIgnoredSchemas()})
GROUP BY table_schema
`;
return this.query<QuerySchemasResult>(query);

Check warning on line 322 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L322

Added line #L322 was not covered by tests
}

public getTables(schemaNames?: string[]) {
public getTablesForSpecificSchemas(schemaNames?: string[]) {

Check warning on line 325 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L325

Added line #L325 was not covered by tests
if (!schemaNames) {
throw new Error('getTables without schemaNames is not supported');

Check warning on line 327 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L327

Added line #L327 was not covered by tests
}

const query = this.informationSchemaGetTablesQuery(schemaNames);
return this.query<QueryTablesResult>(query);
const schemas = schemaNames.map((_, idx) => `$${idx + 1}`).join(', ');

Check warning on line 330 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L330

Added line #L330 was not covered by tests

const query = `

Check warning on line 332 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L332

Added line #L332 was not covered by tests
SELECT table_schema as ${this.quoteIdentifier('schema_name')},
table_name as ${this.quoteIdentifier('table_name')}
FROM information_schema.tables
WHERE table_schema IN (${schemas})
`;
return this.query<QueryTablesResult>(query, schemaNames);

Check warning on line 338 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L338

Added line #L338 was not covered by tests
}

public getColumns(tables: QueryTablesResult[]) {
const conditions = tables.map(t => `(table_schema='${t.schema_name}' AND table_name='${t.table_name}')`).join(' OR ');
public getColumnsForSpecificTables(tables: QueryTablesResult[]) {
const conditions: string[] = [];
const parameters: string[] = [];

Check warning on line 343 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L341-L343

Added lines #L341 - L343 were not covered by tests

tables.forEach((t, idx) => {
const schemaPlaceholder = `$${2 * idx + 1}`;
const tablePlaceholder = `$${2 * idx + 2}`;
conditions.push(`(table_schema=${schemaPlaceholder} AND table_name=${tablePlaceholder})`);
parameters.push(t.schema_name, t.table_name);

Check warning on line 349 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L345-L349

Added lines #L345 - L349 were not covered by tests
});

const conditionString = conditions.join(' OR ');

Check warning on line 352 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L352

Added line #L352 was not covered by tests

const query = `

Check warning on line 354 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L354

Added line #L354 was not covered by tests
SELECT columns.column_name as ${this.quoteIdentifier('column_name')},
columns.table_name as ${this.quoteIdentifier('table_name')},
columns.table_schema as ${this.quoteIdentifier('table_schema')},
columns.data_type as ${this.quoteIdentifier('data_type')}
FROM information_schema.columns
WHERE ${conditions}
WHERE ${conditionString}
`;

return this.query<QueryColumnsResult>(query);
return this.query<QueryColumnsResult>(query, parameters);

Check warning on line 363 in packages/cubejs-base-driver/src/BaseDriver.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-base-driver/src/BaseDriver.ts#L363

Added line #L363 was not covered by tests
}

public getTablesQuery(schemaName: string) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cubejs-base-driver/src/driver.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ export interface DriverInterface {
//
tableColumnTypes: (table: string) => Promise<TableStructure>;
queryColumnTypes: (sql: string, params?: unknown[]) => Promise<{ name: any; type: string; }[]>;
// Returns all schemas for current database
//
getSchemas: () => Promise<QuerySchemasResult[]>;
// Returns all tables for defined schemas
getTables: (schemaNames: string[]) => Promise<QueryTablesResult[]>;
getTablesForSpecificSchemas: (schemaNames: string[]) => Promise<QueryTablesResult[]>;
getColumnsForSpecificTables: (tables: QueryTablesResult[]) => Promise<QueryColumnsResult[]>;
// eslint-disable-next-line camelcase
getTablesQuery: (schemaName: string) => Promise<TableQueryResult[]>;
// Remove table from database
Expand Down
4 changes: 2 additions & 2 deletions packages/cubejs-server-core/src/core/DevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class DevServer {
requestId: getRequestIdFromRequest(req),
});

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

Check warning on line 135 in packages/cubejs-server-core/src/core/DevServer.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-server-core/src/core/DevServer.ts#L134-L135

Added lines #L134 - L135 were not covered by tests
}));

Expand All @@ -149,7 +149,7 @@ export class DevServer {
requestId: getRequestIdFromRequest(req),
});

const columns = await driver.getColumns(req.body.tables);
const columns = await driver.getColumnsForSpecificTables(req.body.tables);
res.json({ columns });

Check warning on line 153 in packages/cubejs-server-core/src/core/DevServer.ts

View check run for this annotation

Codecov / codecov/patch

packages/cubejs-server-core/src/core/DevServer.ts#L152-L153

Added lines #L152 - L153 were not covered by tests
}));

Expand Down

0 comments on commit 806e7f1

Please sign in to comment.