fix(bi): orderBy bug under MySQL (#2283)

* fix(bi): orderBy bug under MySQL

* fix: compatible with pgsql
This commit is contained in:
YANG QIA 2023-07-19 21:25:49 +08:00 committed by GitHub
parent 1f91ebc65e
commit 4e979bc7b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 51 deletions

View File

@ -41,8 +41,8 @@ describe('api', () => {
const repo = db.getRepository('chart_test');
await repo.create({
values: [
{ price: 1, count: 1, title: 'title1' },
{ price: 2, count: 2, title: 'title2' },
{ price: 1, count: 1, title: 'title1', createdAt: '2023-02-02' },
{ price: 2, count: 2, title: 'title2', createdAt: '2023-01-01' },
],
});
});
@ -51,55 +51,52 @@ describe('api', () => {
await db.close();
});
test('query', () => {
expect.assertions(1);
return expect(
queryData({ db } as any, {
collection: 'chart_test',
measures: [
{
field: ['price'],
alias: 'Price',
},
{
field: ['count'],
alias: 'Count',
},
],
dimensions: [
{
field: ['title'],
alias: 'Title',
},
],
}),
).resolves.toBeDefined();
test('query', async () => {
const result = await queryData({ db } as any, {
collection: 'chart_test',
measures: [
{
field: ['price'],
alias: 'Price',
},
{
field: ['count'],
alias: 'Count',
},
],
dimensions: [
{
field: ['title'],
alias: 'Title',
},
],
});
expect(result).toBeDefined();
});
test('query with sort', () => {
expect.assertions(1);
return expect(
queryData({ db } as any, {
collection: 'chart_test',
measures: [
{
field: ['price'],
aggregation: 'sum',
alias: 'Price',
},
],
dimensions: [
{
field: ['title'],
alias: 'Title',
},
{
field: ['createdAt'],
format: 'YYYY',
},
],
orders: [{ field: 'createdAt', order: 'asc' }],
}),
).resolves.toBeDefined();
test('query with sort', async () => {
const result = await queryData({ db } as any, {
collection: 'chart_test',
measures: [
{
field: ['price'],
aggregation: 'sum',
alias: 'Price',
},
],
dimensions: [
{
field: ['title'],
alias: 'Title',
},
{
field: ['createdAt'],
format: 'YYYY-MM',
},
],
orders: [{ field: 'createdAt', order: 'asc' }],
});
expect(result).toBeDefined();
expect(result).toMatchObject([{ createdAt: '2023-01' }, { createdAt: '2023-02' }]);
});
});

View File

@ -161,7 +161,12 @@ export const parseBuilder = (ctx: Context, builder: QueryParams) => {
});
orders.forEach((item: OrderProps) => {
const name = hasAgg ? sequelize.literal(`"${item.alias}"`) : sequelize.col(item.field as string);
const dialect = sequelize.getDialect();
let alias = `"${item.alias}"`;
if (dialect === 'mysql') {
alias = `\`${item.alias}\``;
}
const name = hasAgg ? sequelize.literal(alias) : sequelize.col(item.field as string);
order.push([name, item.order || 'ASC']);
});