mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 06:32:15 +08:00
Escape database names in MSSQL datasource (#99754)
Valid MSSQL database names can contain characters like `-`, which need to be escaped when used in queries. This PR wraps database names in `[]`, and fixes Grafana issue #58757.
This commit is contained in:
10
public/app/plugins/datasource/mssql/MSSqlMetaQuery.test.ts
Normal file
10
public/app/plugins/datasource/mssql/MSSqlMetaQuery.test.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { getSchema } from './MSSqlMetaQuery';
|
||||
|
||||
describe('getSchema', () => {
|
||||
const database = 'foo';
|
||||
const table = 'bar';
|
||||
const schema = getSchema(database, table);
|
||||
it('should escapte database names', () => {
|
||||
expect(schema).toContain(`USE [${database}]`);
|
||||
});
|
||||
});
|
@ -10,7 +10,7 @@ export function getSchemaAndName(database?: string) {
|
||||
|
||||
export function getSchema(database?: string, table?: string) {
|
||||
return `
|
||||
USE ${database}
|
||||
USE [${database}]
|
||||
SELECT COLUMN_NAME as 'column',DATA_TYPE as 'type'
|
||||
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='${table}';`;
|
||||
}
|
||||
|
18
public/app/plugins/datasource/mssql/sqlUtil.test.ts
Normal file
18
public/app/plugins/datasource/mssql/sqlUtil.test.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { SQLQuery, QueryEditorExpressionType } from '@grafana/sql';
|
||||
|
||||
import { toRawSql } from './sqlUtil';
|
||||
|
||||
describe('toRawSql should escape database names', () => {
|
||||
const query: SQLQuery = {
|
||||
dataset: 'foo',
|
||||
sql: {
|
||||
columns: [{ name: 'a', alias: 'lol', type: QueryEditorExpressionType.Function }],
|
||||
},
|
||||
refId: 'lolsob',
|
||||
table: 'table',
|
||||
};
|
||||
const queryString = toRawSql(query);
|
||||
it('should escapte database names', () => {
|
||||
expect(queryString).toContain(`FROM [${query.dataset}].${query.table}`);
|
||||
});
|
||||
});
|
@ -89,7 +89,7 @@ export function toRawSql({ sql, dataset, table }: SQLQuery): string {
|
||||
rawQuery += createSelectClause(sql.columns, sql.limit);
|
||||
|
||||
if (dataset && table) {
|
||||
rawQuery += `FROM ${dataset}.${table} `;
|
||||
rawQuery += `FROM [${dataset}].${table} `;
|
||||
}
|
||||
|
||||
if (sql.whereString) {
|
||||
|
Reference in New Issue
Block a user