mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
fix(sql-collection): filtering SQL Collection throws an error when DB_TABLE_PREFIX
is set (#6156)
This commit is contained in:
parent
bae769c6bf
commit
b0c517a222
@ -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;',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -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}`);
|
||||
}
|
||||
@ -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}`);
|
||||
|
Loading…
x
Reference in New Issue
Block a user