grafana/public/app/plugins/datasource/mysql/MySqlDatasource.ts
Jev Forsberg c0a1fc2cbd
SQL Datasources: Reinstate SQL data source behavior around database selection when default configured databases already exist (#65659)
* badlm0mma/sql_datasource_update/ initial notes

* baldm0mma/sql_datasource_update/ solution

* baldm0mma/sql_datasource_update/ clean datasetSelector

* baldm0mma/sql_datasource_update/ clean up queryEditor.tsx

* baldm0mma/sql_datasource_update/ clewan up queryHeader.tsx

* baldm0mma/sql_datasource_update/ clean up tableSelector.tsx

* baldm0mma/sql_datasource_update/ clean up mysqlDatasource.ts

* baldm0mma/sql_datasource_update/ clean up configurationEditor.tsx

* baldm0mma/sql_datasource_update/ rem conlog from queryEditor.tsx

* baldm0mma/sql_datasource_update/ rem conlog from queryEditor.tsx

* baldm0mma/sql_datasource_update/ remove conlog in tableSelector.tsx

* baldm0mma/sql_datasource_update/ rem conlog in sqlDatasource.ts

* baldm0mma/sql_datasource_update/ update deafult database value in sqlDatasource.ts

* baldm0mma/sql_datasource_update/ update logic to accomidate no preconfig

* baldm0mma/sql_datasource_update/ update props

* baldm0mma/sql_datasource_update/ update prop names

* baldm0mma/sql_datasource_update/ update prop names in tableSelector

* baldm0mma/sql_datasource_update/ update annos in datasetSelector

* baldm0mma/sql_datasource_update/ update naming

* baldm0mma/sql_datasource_update/ update to standard langauae

* baldm0mma/sql_datasource_update/ update prop names

* baldm0mma/sql_datasource_update/ update annos in datasetSelector

* baldm0mma/sql_datasource_update/ remove unused import in tableSelector.tsx

* baldm0mma/sql_datasource_update/ remove addDefaultdataset

* baldm0mma/sql_datasource_update/ reset query when needed

* baldm0mma/sql_datasource_update/ update asymc return val

* baldm0mma/sql_datasource_update/ remove psql query editor

* baldm0mma/sql_datasource_update/ remove con logs in defaults.ts

* baldm0mma/sql_datasource_update/ revert postgres changes

* baldm0mma/sql_datasource_update/ update postgres naming

* baldm0mma/sql_datasource_update/ add altert

* baldm0mma/sql_datasource_update/ update annos and add alerts and alert logic

* baldm0mma/sql_datasource_update/ update postgres nomenclature

* baldm0mma/sql_datasource_update/ update annos and remove con logs

* baldm0mma/sql_datasource_update/ update nomenclature

* baldm0mma/sql_datasource_update/ drone fix

* baldm0mma/sql_datasource_update/ export and format

* baldm0mma/sql_datasource_update/ update docs for mysql datasource with dataset dropdown disable info

* baldm0mma/sql_datasource_update/ update mssql docs

* baldm0mma/sql_datasource_update/ update postgres docs and alert logic

* baldm0mma/sql_datasource_update/ add tests

* baldm0mma/sql_datasource_update/ update docs

* baldm0mma/sql_datasource_update/ update test names

* baldm0mma/sql_datasource_update/ update alert language

* baldm0mma/sql_datasource_update/ correct spelling

* baldm0mma/sql_datasource_update/ update postgres query builder image

* baldm0mma/sql_datasource_update/ update annos

* baldm0mma/sql_datasource_update/ update tests

* baldm0mma/sql_datasource_update/ docs updated

* baldm0mma/sql_datsource_update/ refactor determinePlaceholder

* Update public/app/features/plugins/sql/components/QueryEditor.tsx

spelling

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>

* Update public/app/features/plugins/sql/components/QueryEditor.tsx

spelling

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>

* baldm0mma/sql_datasource_update/ remove superfluous cleanup data from tests

* baldm0mma/sql_datasource_update/ update spelling

* Update public/app/features/plugins/sql/components/SqlComponents.test.tsx

spelling

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>

* baldm0mma/sql_datasource_update/ add logic to prevent db call for tables if dataset it nullish

* baldm0mma/sql_datasource_update/ update alert tests

* baldm0mma/sql_datasource_update/ update button text

* baldm0mma/sql_datasource_update/ update tests to preferred standard

* baldm0mma/sql_datasource_update/add feature flag

* baldm0mma/sql_datasource_update/ update mssql docs

* baldm0mma/sql_datasource_update/ add feature flag to registry

* baldm0mma/sql_datasource_update/ adjust table and dataset dropdown logic

* baldm0mma/sql_datasource_update/ update testing to deal with feature flag

* baldm0mma/sql_datasource_update/ update wioth cascadeDisable

* baldm0mma/sql_datasource_update/ update naming

* baldm0mma/sql_datasource_update/ update tests to reflect enabled feature flag

* baldm0mma/sql_datasource_update/ update annotations

* baldm0mma/sql_datasource_update/ update annos in queryEd

* baldm0mma/sql_datasource_update/ update test names

* baldm0mma/sql_datasource_update/ update anno issues

* baldm0mma/slq_datasource_update/ add query to sqlDatasource with error

* baldm0mma/sql_datasource_update/ update docs language

* baldm0mma/sql_datasource_update/ remove notes to self

* baldm0mma/sql_datasource_update/ add QueryEditorFeatureFlag.utils.ts

* baldm0mma/sql_datasource_update/ update database into json

* baldm0mma/sql_datasource_update/ found file

* Update docs/sources/datasources/mssql/query-editor/index.md

Co-authored-by: lwandz13 <126723338+lwandz13@users.noreply.github.com>

* baldm0mma/sql_datasource_update/ update feature flag and toggles

* baldm0mma/sql_datasource_update/ add hasConfigIssue, update annos, rethink disable logic

* baldm0mma/sql_datasource_update/ update warning language

* baldm0mma/sql_datasource_update/ update button content

* baldm0mma/sql_datasource_update/ update jsonData logic in frontendsettings.go

* baldm0mma/sql_datasource_update/ update jsonData logic

* baldm0mma/sql_datasource_update/ update annos in frontendsettings.go

* baldm0mma/sql_datasource_update/ update sql editor docs

* baldm0mma/sql_datasource_update/ update mysql docs

* baldm0mma/sql_datasource_update/ update postgres docs

* baldm0mma/sql_datasource_update/ remove unused code in datasetSelector.tsx

* baldm0mma/sql_datasource_update/ update syntax conventions

* baldm0mma/sql_datasource_update/ add logs

* baldm0mma/sql_datasource_update/ remove unused code

* baldm0mma/sql_datasource_update/ remove conlogs

* baldm0mma/sql_datasource_update/ update tests

* baldm0mma/sql_datasource_update/ add second error to query

* baldm0mma/sql_datasource_update/ run make gen-cue

* baldm0mma/sql_database_update/ lint registry

* baldm0mma/sql_datasource_update/ update registry

* baldm0mma/sql_datasource_update/ upate datasource logic

* baldm0mma/sql_datasource_update/ add logs

* baldm0mma/sql_datasource_update/ add comms to self

* baldm0mma/sql_datasource_update/ comment out false pos tests, and add investigatory comments

* baldm0mma/sql_database_update/ update query error to only test for config change if query was made in "builder" mode

* baldm0mma/sql_datasource_update/ update annos in frontendsettings.go

* baldm0mma/sql_datasource_update/ update error logic to datasource

* baldm0mma/sql_datasource_update/ remove alerts from query editor

* baldm0mma/sql_datasource_update/ remove unused imports

* baldm0mma/sql_datasource_update/ update tests

* baldm0mma/sql_datasource_update/ remove comments

* baldm0mma/sql_datasource_update/ remove logs in queryGroup.tsx

* baldm0mma/sql_datasource_update/ remove outdated annotation in datasetSelector.tsx

* baldm0mma/sql_datasource_update/ remove superfluous test and update test description

* baldm0mma/sql_datasource_update/ remove feature flag

* baldm0mma/sql_datasource_update/ add back feature flag

* baldm0mma/sql_datasource_update/ update to enums

* baldm0mma/sql_datasource_update/ update panel caps

* baldm0mma/sql_datasource_update/ update dataset selector to default update the database correctly

* baldm0mma/sql_datasource_update/ move onChange into conditional

* baldm0mma/sql_datasource_update/ add logic for previous datasets choice

* baldm0mma/sql_datasource_update/ add back previous logic for assigning default datasets

* baldm0mma/sql_datasource_update/ update useEffect dep array

* baldm0mma/sql_datasource_update/ remove feature toggle

* baldm0mma/sql_datasource_update/ add feature toggle

---------

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
Co-authored-by: lwandz13 <126723338+lwandz13@users.noreply.github.com>
2023-06-06 10:28:52 -06:00

107 lines
3.9 KiB
TypeScript

import { DataSourceInstanceSettings, TimeRange } from '@grafana/data';
import { CompletionItemKind, LanguageDefinition, TableIdentifier } from '@grafana/experimental';
import { SqlDatasource } from 'app/features/plugins/sql/datasource/SqlDatasource';
import { DB, SQLQuery } from 'app/features/plugins/sql/types';
import { formatSQL } from 'app/features/plugins/sql/utils/formatSQL';
import { mapFieldsToTypes } from './fields';
import { buildColumnQuery, buildTableQuery, showDatabases } from './mySqlMetaQuery';
import { getSqlCompletionProvider } from './sqlCompletionProvider';
import { quoteIdentifierIfNecessary, quoteLiteral, toRawSql } from './sqlUtil';
import { MySQLOptions } from './types';
export class MySqlDatasource extends SqlDatasource {
sqlLanguageDefinition: LanguageDefinition | undefined;
constructor(private instanceSettings: DataSourceInstanceSettings<MySQLOptions>) {
super(instanceSettings);
}
getQueryModel() {
return { quoteLiteral };
}
getSqlLanguageDefinition(): LanguageDefinition {
if (this.sqlLanguageDefinition !== undefined) {
return this.sqlLanguageDefinition;
}
const args = {
getMeta: (identifier?: TableIdentifier) => this.fetchMeta(identifier),
};
this.sqlLanguageDefinition = {
id: 'mysql',
completionProvider: getSqlCompletionProvider(args),
formatter: formatSQL,
};
return this.sqlLanguageDefinition;
}
async fetchDatasets(): Promise<string[]> {
const datasets = await this.runSql<string[]>(showDatabases(), { refId: 'datasets' });
return datasets.map((t) => quoteIdentifierIfNecessary(t[0]));
}
async fetchTables(dataset?: string): Promise<string[]> {
const tables = await this.runSql<string[]>(buildTableQuery(dataset), { refId: 'tables' });
return tables.map((t) => quoteIdentifierIfNecessary(t[0]));
}
async fetchFields(query: Partial<SQLQuery>) {
if (!query.dataset || !query.table) {
return [];
}
const queryString = buildColumnQuery(query.table, query.dataset);
const frame = await this.runSql<string[]>(queryString, { refId: 'fields' });
const fields = frame.map((f) => ({
name: f[0],
text: f[0],
value: quoteIdentifierIfNecessary(f[0]),
type: f[1],
label: f[0],
}));
return mapFieldsToTypes(fields);
}
async fetchMeta(identifier?: TableIdentifier) {
const defaultDB = this.instanceSettings.jsonData.database;
if (!identifier?.schema && defaultDB) {
const tables = await this.fetchTables(defaultDB);
return tables.map((t) => ({ name: t, completion: `${defaultDB}.${t}`, kind: CompletionItemKind.Class }));
} else if (!identifier?.schema && !defaultDB) {
const datasets = await this.fetchDatasets();
return datasets.map((d) => ({ name: d, completion: `${d}.`, kind: CompletionItemKind.Module }));
} else {
if (!identifier?.table && (!defaultDB || identifier?.schema)) {
const tables = await this.fetchTables(identifier?.schema);
return tables.map((t) => ({ name: t, completion: t, kind: CompletionItemKind.Class }));
} else if (identifier?.table && identifier.schema) {
const fields = await this.fetchFields({ dataset: identifier.schema, table: identifier.table });
return fields.map((t) => ({ name: t.name, completion: t.value, kind: CompletionItemKind.Field }));
} else {
return [];
}
}
}
getDB(): DB {
if (this.db !== undefined) {
return this.db;
}
return {
datasets: () => this.fetchDatasets(),
tables: (dataset?: string) => this.fetchTables(dataset),
fields: (query: SQLQuery) => this.fetchFields(query),
validateQuery: (query: SQLQuery, _range?: TimeRange) =>
Promise.resolve({ query, error: '', isError: false, isValid: true }),
dsID: () => this.id,
toRawSql,
functions: () => ['VARIANCE', 'STDDEV'],
getEditorLanguageDefinition: () => this.getSqlLanguageDefinition(),
};
}
}