mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
feat(sqllab): save query parameters in database (#21682)
This commit is contained in:
@@ -111,8 +111,8 @@ const ERR_MSG_CANT_LOAD_QUERY = t("The query couldn't be loaded");
|
||||
const queryClientMapping = {
|
||||
id: 'remoteId',
|
||||
db_id: 'dbId',
|
||||
client_id: 'id',
|
||||
label: 'name',
|
||||
template_parameters: 'templateParams',
|
||||
};
|
||||
const queryServerMapping = invert(queryClientMapping);
|
||||
|
||||
@@ -120,8 +120,8 @@ const queryServerMapping = invert(queryClientMapping);
|
||||
const fieldConverter = mapping => obj =>
|
||||
mapKeys(obj, (value, key) => (key in mapping ? mapping[key] : key));
|
||||
|
||||
const convertQueryToServer = fieldConverter(queryServerMapping);
|
||||
const convertQueryToClient = fieldConverter(queryClientMapping);
|
||||
export const convertQueryToServer = fieldConverter(queryServerMapping);
|
||||
export const convertQueryToClient = fieldConverter(queryClientMapping);
|
||||
|
||||
export function getUpToDateQuery(rootState, queryEditor, key) {
|
||||
const {
|
||||
@@ -903,17 +903,23 @@ export function queryEditorSetAutorun(queryEditor, autorun) {
|
||||
};
|
||||
}
|
||||
|
||||
export function queryEditorSetTitle(queryEditor, name) {
|
||||
export function queryEditorSetTitle(queryEditor, name, id) {
|
||||
return function (dispatch) {
|
||||
const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)
|
||||
? SupersetClient.put({
|
||||
endpoint: encodeURI(`/tabstateview/${queryEditor.id}`),
|
||||
endpoint: encodeURI(`/tabstateview/${id}`),
|
||||
postPayload: { label: name },
|
||||
})
|
||||
: Promise.resolve();
|
||||
|
||||
return sync
|
||||
.then(() => dispatch({ type: QUERY_EDITOR_SET_TITLE, queryEditor, name }))
|
||||
.then(() =>
|
||||
dispatch({
|
||||
type: QUERY_EDITOR_SET_TITLE,
|
||||
queryEditor: { ...queryEditor, id },
|
||||
name,
|
||||
}),
|
||||
)
|
||||
.catch(() =>
|
||||
dispatch(
|
||||
addDangerToast(
|
||||
@@ -926,21 +932,26 @@ export function queryEditorSetTitle(queryEditor, name) {
|
||||
};
|
||||
}
|
||||
|
||||
export function saveQuery(query) {
|
||||
export function saveQuery(query, clientId) {
|
||||
const { id, ...payload } = convertQueryToServer(query);
|
||||
|
||||
return dispatch =>
|
||||
SupersetClient.post({
|
||||
endpoint: '/savedqueryviewapi/api/create',
|
||||
postPayload: convertQueryToServer(query),
|
||||
stringify: false,
|
||||
endpoint: '/api/v1/saved_query/',
|
||||
jsonPayload: convertQueryToServer(payload),
|
||||
})
|
||||
.then(result => {
|
||||
const savedQuery = convertQueryToClient(result.json.item);
|
||||
const savedQuery = convertQueryToClient({
|
||||
id: result.json.id,
|
||||
...result.json.result,
|
||||
});
|
||||
dispatch({
|
||||
type: QUERY_EDITOR_SAVED,
|
||||
query,
|
||||
clientId,
|
||||
result: savedQuery,
|
||||
});
|
||||
dispatch(queryEditorSetTitle(query, query.name));
|
||||
dispatch(queryEditorSetTitle(query, query.name, clientId));
|
||||
return savedQuery;
|
||||
})
|
||||
.catch(() =>
|
||||
@@ -966,16 +977,17 @@ export const addSavedQueryToTabState =
|
||||
});
|
||||
};
|
||||
|
||||
export function updateSavedQuery(query) {
|
||||
export function updateSavedQuery(query, clientId) {
|
||||
const { id, ...payload } = convertQueryToServer(query);
|
||||
|
||||
return dispatch =>
|
||||
SupersetClient.put({
|
||||
endpoint: `/savedqueryviewapi/api/update/${query.remoteId}`,
|
||||
postPayload: convertQueryToServer(query),
|
||||
stringify: false,
|
||||
endpoint: `/api/v1/saved_query/${query.remoteId}`,
|
||||
jsonPayload: convertQueryToServer(payload),
|
||||
})
|
||||
.then(() => {
|
||||
dispatch(addSuccessToast(t('Your query was updated')));
|
||||
dispatch(queryEditorSetTitle(query, query.name));
|
||||
dispatch(queryEditorSetTitle(query, query.name, clientId));
|
||||
})
|
||||
.catch(e => {
|
||||
const message = t('Your query could not be updated');
|
||||
@@ -1350,11 +1362,12 @@ export function popStoredQuery(urlId) {
|
||||
export function popSavedQuery(saveQueryId) {
|
||||
return function (dispatch) {
|
||||
return SupersetClient.get({
|
||||
endpoint: `/savedqueryviewapi/api/get/${saveQueryId}`,
|
||||
endpoint: `/api/v1/saved_query/${saveQueryId}`,
|
||||
})
|
||||
.then(({ json }) => {
|
||||
const queryEditorProps = {
|
||||
...convertQueryToClient(json.result),
|
||||
dbId: json.result?.database?.id,
|
||||
loaded: true,
|
||||
autorun: false,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user