Skip to content

Commit

Permalink
fix(sql-collection): filtering SQL Collection throws an error when `D…
Browse files Browse the repository at this point in the history
…B_TABLE_PREFIX` is set (nocobase#6156)
  • Loading branch information
2013xile authored Feb 5, 2025
1 parent bae769c commit b0c517a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,74 @@ describe('select query', () => {
);
});
});

describe('select query with DB_TABLE_PREFIX', () => {
const model = class extends SQLModel {};
model.init(null, {
modelName: 'users',
tableName: 'd_users',
sequelize: new Sequelize({
dialect: 'postgres',
}),
});
model.sql = 'SELECT * FROM "d_users"';
model.collection = {
fields: new Map(
Object.entries({
id: {},
name: {},
}),
),
} as any;
const queryGenerator = model.queryInterface.queryGenerator as any;

test('plain sql', () => {
const query = queryGenerator.selectQuery('d_users', {}, model);
expect(query).toBe('SELECT * FROM "d_users";');
});

test('attributes', () => {
const query = queryGenerator.selectQuery('d_users', { attributes: ['id', 'name'] }, model);
expect(query).toBe('SELECT "id", "name" FROM (SELECT * FROM "d_users") AS "users";');
});

test('where', () => {
const query = queryGenerator.selectQuery('d_users', { where: { id: 1 } }, model);
expect(query).toBe('SELECT * FROM (SELECT * FROM "d_users") AS "users" WHERE "users"."id" = 1;');
});

test('group', () => {
const query1 = queryGenerator.selectQuery('d_users', { group: 'id' }, model);
expect(query1).toBe('SELECT * FROM (SELECT * FROM "d_users") AS "users" GROUP BY "id";');
const query2 = queryGenerator.selectQuery('d_users', { group: ['id', 'name'] }, model);
expect(query2).toBe('SELECT * FROM (SELECT * FROM "d_users") AS "users" GROUP BY "id", "name";');
});

test('order', () => {
const query = queryGenerator.selectQuery('d_users', { order: ['id'] }, model);
expect(query).toBe('SELECT * FROM (SELECT * FROM "d_users") AS "users" ORDER BY "users"."id";');
});

test('limit, offset', () => {
const query = queryGenerator.selectQuery('d_users', { limit: 1, offset: 0 }, model);
expect(query).toBe('SELECT * FROM (SELECT * FROM "d_users") AS "users" LIMIT 1 OFFSET 0;');
});

test('complex sql', () => {
const query = queryGenerator.selectQuery(
'd_users',
{
attributes: ['id', 'name'],
where: { id: 1 },
group: 'id',
order: ['id'],
limit: 1,
offset: 0,
},
model,
);
expect(query).toBe(
'SELECT "id", "name" FROM (SELECT * FROM "d_users") AS "users" WHERE "users"."id" = 1 GROUP BY "id" ORDER BY "users"."id" LIMIT 1 OFFSET 0;',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function selectQuery(

// Add WHERE to sub or main query
if (Object.prototype.hasOwnProperty.call(options, 'where')) {
options.where = this.getWhereConditions(options.where, tableName, model, options);
options.where = this.getWhereConditions(options.where, model.name, model, options);
if (options.where) {
queryItems.push(` WHERE ${options.where}`);
}
Expand All @@ -48,8 +48,8 @@ export function selectQuery(
// Add GROUP BY to sub or main query
if (options.group) {
options.group = Array.isArray(options.group)
? options.group.map((t) => this.aliasGrouping(t, model, tableName, options)).join(', ')
: this.aliasGrouping(options.group, model, tableName, options);
? options.group.map((t) => this.aliasGrouping(t, model, model.name, options)).join(', ')
: this.aliasGrouping(options.group, model, model.name, options);

if (options.group) {
queryItems.push(` GROUP BY ${options.group}`);
Expand Down

0 comments on commit b0c517a

Please sign in to comment.