1717 * under the License.
1818 */
1919import { sqlLab as sqlLabApi } from '@apache-superset/core' ;
20+ import { nanoid } from 'nanoid' ;
2021import {
2122 ADD_QUERY_EDITOR ,
2223 QUERY_FAILED ,
@@ -38,8 +39,9 @@ import {
3839 queryEditorSetDb ,
3940 queryEditorSetCatalog ,
4041 queryEditorSetSchema ,
41- runQueryFromSqlEditor ,
42+ runQuery as runQueryAction ,
4243 postStopQuery ,
44+ Query ,
4345} from 'src/SqlLab/actions/sqlLab' ;
4446import { RootState , store } from 'src/views/store' ;
4547import { AnyListenerPredicate } from '@reduxjs/toolkit' ;
@@ -156,16 +158,16 @@ const makeTab = (
156158 id : string ,
157159 name : string ,
158160 dbId : number ,
159- catalog ? : string | null ,
160- schema ? : string | null ,
161+ catalog : string | null = null ,
162+ schema : string | null = null ,
161163) : Tab => {
162164 const panels : Panel [ ] = [ ] ; // TODO: Populate panels
163165 return new Tab (
164166 id ,
165167 name ,
166168 dbId ,
167- catalog ?? null ,
168- schema ?? null ,
169+ catalog ,
170+ schema ,
169171 ( ) => getEditorAsync ( id ) ,
170172 panels ,
171173 ) ;
@@ -591,7 +593,7 @@ const setActiveTab: typeof sqlLabApi.setActiveTab = async (tabId: string) => {
591593 }
592594} ;
593595
594- const runQuery : typeof sqlLabApi . runQuery = async ( ) => {
596+ const executeQuery : typeof sqlLabApi . executeQuery = async options => {
595597 const state = store . getState ( ) as SqlLabRootState ;
596598 const editorId = activeEditorId ( ) ;
597599 const queryEditor = findQueryEditor ( editorId ) ;
@@ -609,12 +611,58 @@ const runQuery: typeof sqlLabApi.runQuery = async () => {
609611 const database = qe . dbId ? databases [ qe . dbId ] : null ;
610612 const defaultLimit = state . common ?. conf ?. DEFAULT_SQLLAB_LIMIT ?? 1000 ;
611613
612- store . dispatch ( runQueryFromSqlEditor ( database , qe , defaultLimit ) as any ) ;
614+ // Determine SQL to execute
615+ let sql : string ;
616+ let updateTabState = true ;
617+
618+ if ( options ?. sql !== undefined ) {
619+ // Custom SQL provided - don't update tab state
620+ ( { sql } = options ) ;
621+ updateTabState = false ;
622+ } else if ( options ?. selectedOnly && qe . selectedText ) {
623+ // Run selected text only
624+ sql = qe . selectedText ;
625+ updateTabState = false ;
626+ } else {
627+ // Default: use editor content (selected text takes precedence)
628+ sql = qe . selectedText || qe . sql ;
629+ updateTabState = ! qe . selectedText ;
630+ }
631+
632+ // Merge template params
633+ const templateParams = options ?. templateParams
634+ ? JSON . stringify ( {
635+ ...JSON . parse ( qe . templateParams || '{}' ) ,
636+ ...options . templateParams ,
637+ } )
638+ : qe . templateParams ;
639+
640+ const queryId = nanoid ( 11 ) ;
641+
642+ const query : Query = {
643+ id : queryId ,
644+ dbId : qe . dbId ,
645+ sql,
646+ sqlEditorId : qe . tabViewId ?? qe . id ,
647+ sqlEditorImmutableId : qe . immutableId ,
648+ tab : qe . name ,
649+ catalog : qe . catalog ,
650+ schema : qe . schema ,
651+ tempTable : options ?. ctas ?. tableName ,
652+ templateParams,
653+ queryLimit : options ?. limit ?? qe . queryLimit ?? defaultLimit ,
654+ runAsync : database ? database . allow_run_async : false ,
655+ ctas : ! ! options ?. ctas ,
656+ ctas_method : options ?. ctas ?. method ,
657+ updateTabState,
658+ } ;
659+
660+ store . dispatch ( runQueryAction ( query ) ) ;
613661
614- return qe . id ;
662+ return queryId ;
615663} ;
616664
617- const stopQuery : typeof sqlLabApi . stopQuery = async ( queryId : string ) => {
665+ const cancelQuery : typeof sqlLabApi . cancelQuery = async ( queryId : string ) => {
618666 const state = store . getState ( ) as SqlLabRootState ;
619667 const query = state . sqlLab . queries [ queryId ] ;
620668
@@ -664,8 +712,8 @@ export const sqlLab: typeof sqlLabApi = {
664712 createTab,
665713 closeTab,
666714 setActiveTab,
667- runQuery ,
668- stopQuery ,
715+ executeQuery ,
716+ cancelQuery ,
669717 setDatabase,
670718 setCatalog,
671719 setSchema,
0 commit comments