Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sql-playground): allow to run selected queries of editor #2381

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions dashboard/src2/components/devtools/database/SQLCodeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
Codemirror
},
props: ['schema'],
emits: ['update:query'],
emits: ['update:query', 'codeSelected', 'codeUnselected'],
computed: {
query: {
get() {
Expand All @@ -33,7 +33,7 @@ export default {
}
}
},
setup(props) {
setup(props, { emit }) {
const extensions = [
sql({
dialect: MySQL,
Expand All @@ -51,6 +51,25 @@ export default {
const view = shallowRef();
const handleReady = payload => {
view.value = payload.view;
view.value.dom.addEventListener('mouseup', handleSelectionChange);
};

const handleSelectionChange = () => {
if (!view.value) return;

const { state } = view.value;
const selection = state.selection.main;

// Get the selected text
if (!selection.empty) {
const selectedText = state.doc.sliceString(
selection.from,
selection.to
);
emit('codeSelected', selectedText);
} else {
emit('codeUnselected');
}
};
return {
extensions,
Expand Down
61 changes: 51 additions & 10 deletions dashboard/src2/pages/devtools/database/DatabaseSQLPlayground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
v-model="query"
v-if="sqlSchemaForAutocompletion"
:schema="sqlSchemaForAutocompletion"
@codeSelected="handleCodeSelected"
@codeUnselected="handleCodeUnselected"
/>
</div>
<div class="mt-2 flex flex-row items-center justify-between">
Expand All @@ -73,13 +75,24 @@
<Button iconLeft="file-text" @click="toggleLogsDialog">Logs</Button>
</div>

<Button
@click="() => runSQLQuery()"
:loading="$resources.runSQLQuery.loading"
iconLeft="play"
variant="solid"
>Run Query</Button
>
<div class="flex gap-2">
<Button
v-if="selectedQuery"
@click="runSelectedSQLQuery"
:loading="$resources.runSQLQuery.loading"
iconLeft="play"
variant="outline"
>
Run Selected Query
</Button>
<Button
@click="() => runSQLQuery()"
:loading="$resources.runSQLQuery.loading"
iconLeft="play"
variant="solid"
>Run Query</Button
>
</div>
</div>
<div
class="mt-4"
Expand Down Expand Up @@ -170,6 +183,7 @@ export default {
site: null,
tabIndex: 0,
query: '',
selectedQuery: null,
commit: false,
execution_successful: null,
data: null,
Expand Down Expand Up @@ -284,6 +298,12 @@ export default {
}
},
methods: {
handleCodeSelected(selectedCode) {
this.selectedQuery = selectedCode;
},
handleCodeUnselected() {
this.selectedQuery = null;
},
fetchTableSchemas({ site_name = null, reload = false } = {}) {
if (!site_name) site_name = this.site;
if (!site_name) return;
Expand All @@ -296,15 +316,15 @@ export default {
}
});
},
runSQLQuery(ignore_validation = false) {
runSQLQuery(ignore_validation = false, run_selected_query = false) {
if (!this.query) return;
if (this.mode === 'read-only' || ignore_validation) {
this.$resources.runSQLQuery.submit({
dt: 'Site',
dn: this.site,
method: 'run_sql_query_in_database',
args: {
query: this.query,
query: run_selected_query ? this.selectedQuery : this.query,
commit: this.mode === 'read-write'
}
});
Expand All @@ -321,7 +341,28 @@ Are you sure you want to run the query?`,
label: 'Run Query',
variant: 'solid',
onClick: ({ hide }) => {
this.runSQLQuery(true);
this.runSQLQuery(true, run_selected_query);
hide();
}
}
});
},
runSelectedSQLQuery() {
if (!this.selectedQuery) {
return;
}
confirmDialog({
title: 'Verify Query',
message: `
Are you sure you want to run the query?
<br>
<pre class="mt-2 max-h-52 overflow-y-auto whitespace-pre-wrap rounded bg-gray-50 px-2 py-1.5 text-sm text-gray-700">${this.selectedQuery}</pre>
`,
primaryAction: {
label: 'Run Query',
variant: 'solid',
onClick: ({ hide }) => {
this.runSQLQuery(false, true);
hide();
}
}
Expand Down
Loading