Files
Zoltán Bedi 62c30dea4d MySQL: Quote identifiers that include special characters (#61135)
* SQL: toRawSQL required and escape table

* Fix autocomplete for MySQL

* Change the way we escape for builder

* Rework escape ident to be smart instead

* Fix A11y for alias

* Add first e2e test

* Add test for code editor

* Add doc

* Review comments

* Move functions to sqlUtil
2023-01-31 18:16:28 +01:00

41 lines
1.4 KiB
TypeScript

import { quoteLiteral, unquoteIdentifier } from './sqlUtil';
export function buildTableQuery(dataset?: string) {
const database = dataset !== undefined ? quoteIdentAsLiteral(dataset) : 'database()';
return `SELECT table_name FROM information_schema.tables WHERE table_schema = ${database} ORDER BY table_name`;
}
export function showDatabases() {
return `SELECT DISTINCT TABLE_SCHEMA from information_schema.TABLES where TABLE_TYPE != 'SYSTEM VIEW' ORDER BY TABLE_SCHEMA`;
}
export function buildColumnQuery(table: string, dbName?: string) {
let query = 'SELECT column_name, data_type FROM information_schema.columns WHERE ';
query += buildTableConstraint(table, dbName);
query += ' ORDER BY column_name';
return query;
}
export function buildTableConstraint(table: string, dbName?: string) {
let query = '';
// check for schema qualified table
if (table.includes('.')) {
const parts = table.split('.');
query = 'table_schema = ' + quoteIdentAsLiteral(parts[0]);
query += ' AND table_name = ' + quoteIdentAsLiteral(parts[1]);
return query;
} else {
const database = dbName !== undefined ? quoteIdentAsLiteral(dbName) : 'database()';
query = `table_schema = ${database} AND table_name = ` + quoteIdentAsLiteral(table);
return query;
}
}
export function quoteIdentAsLiteral(value: string) {
return quoteLiteral(unquoteIdentifier(value));
}