Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/cubejs-client-core/src/SqlQuery.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
export type SqlQueryTuple = [string, any[], any];
export type SqlQueryTuple = [string, any[], any?];

export type SqlData = {
aliasNameToMember: Record<string, string>;
cacheKeyQueries: SqlQueryTuple[];
dataSource: boolean;
dataSource: string;
external: boolean;
sql: SqlQueryTuple;
preAggregations: any[];
rollupMatchResults: any[];
};

export type SqlQueryWrapper = { sql: SqlData };

export default class SqlQuery {
private readonly sqlQuery: SqlData;
private readonly sqlQuery: SqlQueryWrapper;

public constructor(sqlQuery: SqlData) {
public constructor(sqlQuery: SqlQueryWrapper) {
this.sqlQuery = sqlQuery;
}

public rawQuery(): SqlData {
return this.sqlQuery;
return this.sqlQuery.sql;
}

public sql(): string {
Expand Down
53 changes: 53 additions & 0 deletions packages/cubejs-client-core/test/SqlQuery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license Apache-2.0
* @copyright Cube Dev, Inc.
* @fileoverview SqlQuery class unit tests.
*/

/* globals describe,it,expect */

import SqlQuery, { SqlQueryTuple, SqlData, SqlQueryWrapper } from '../src/SqlQuery';

describe('SqlQuery', () => {
const mockCacheKeyQueriesTuple: SqlQueryTuple = [
'SELECT FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key',
[],
{
external: false,
renewalThreshold: 60
}
];

const mockSqlTuple: SqlQueryTuple = [
'SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?',
['Europe'],
];

const mockSqlData: SqlData = {
aliasNameToMember: { base_orders__count: 'base_orders.count' },
cacheKeyQueries: [mockCacheKeyQueriesTuple],
dataSource: 'default',
external: false,
sql: mockSqlTuple,
preAggregations: [],
rollupMatchResults: [],
};

const mockWrapper: SqlQueryWrapper = {
sql: mockSqlData,
};

it('should construct without error', () => {
expect(() => new SqlQuery(mockWrapper)).not.toThrow();
});

it('rawQuery should return the original SqlData', () => {
const query = new SqlQuery(mockWrapper);
expect(query.rawQuery()).toEqual(mockSqlData);
});

it('sql should return the first element (SQL string) from the sql tuple', () => {
const query = new SqlQuery(mockWrapper);
expect(query.sql()).toBe('SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?');
});
});
Loading