@@ -58,6 +58,30 @@ export default function useQueryCode() {
5858 } )
5959 }
6060
61+ // Parse SQL command to extract operation details
62+ const parseSqlCommand = ( sql : string ) => {
63+ const regex =
64+ / \b S H O W \s + C R E A T E \s + T A B L E \b | (?: C R E A T E | D R O P | A L T E R ) \s + ( D A T A B A S E | T A B L E ) \s + (?: I F \s + (?: N O T \s + ) ? E X I S T S \s + ) ? [ ` " ] ? ( [ \w . - ] + ) [ ` " ] ? / gi
65+
66+ // Reset regex state to ensure proper matching
67+ regex . lastIndex = 0
68+ const match = regex . exec ( sql )
69+
70+ // Return immediately after finding the first match
71+ if ( match && match [ 1 ] ) {
72+ // Extract action from the full match
73+ const fullMatch = match [ 0 ]
74+ const action = fullMatch . split ( / \s + / ) [ 0 ] . toUpperCase ( ) // First word is the action
75+ return {
76+ action,
77+ object : match [ 1 ] . toUpperCase ( ) ,
78+ name : match [ 2 ] ,
79+ }
80+ }
81+
82+ return null
83+ }
84+
6185 const runQuery = async (
6286 code : string ,
6387 type = queryType . value ,
@@ -72,45 +96,36 @@ export default function useQueryCode() {
7296 }
7397 if ( ! res . error && type === 'sql' ) {
7498 const sql = sqlFormatter ( code )
99+ const command = parseSqlCommand ( sql )
75100
76- const regex =
77- / C R E A T E T A B L E I F N O T E X I S T S ( \s ) + ( \S ) + | C R E A T E T A B L E ( \s ) + ( \S ) + | D R O P T A B L E ( \s ) + ( \S ) + | A L T E R T A B L E ( \s ) + ( \S ) + / g
78- const matchString = sql . match ( regex ) ?. [ 0 ] || ''
101+ if ( command ) {
102+ const { refreshTables, loadMoreColumns } = useSiderTabs ( )
103+ const { originTablesTree } = storeToRefs ( useDataBaseStore ( ) )
104+ const { fetchDatabases } = useAppStore ( )
79105
80- const { refreshTables, loadMoreColumns, loadMoreDetails } = useSiderTabs ( )
81- const { originTablesTree } = storeToRefs ( useDataBaseStore ( ) )
106+ const commandType = `${ command . action } _${ command . object } `
82107
83- if ( matchString !== '' ) {
84- const matchArray = matchString . split ( ' ' )
85- let tableName = matchArray [ matchArray . length - 1 ]
86- if ( tableName . startsWith ( `'` ) || tableName . startsWith ( `"` ) ) {
87- tableName = tableName . slice ( 1 , tableName . length - 1 )
88- }
89- // TODO: What about `show create table`?
90- if (
91- ( matchString . includes ( 'CREATE TABLE' ) && ! matchString . includes ( 'IF NOT EXISTS' ) ) ||
92- matchString . includes ( 'DROP TABLE' )
93- ) {
94- // CREATE NEW TABLE OR DROP TABLE
95- refreshTables ( )
96- } else if ( ! matchString . includes ( 'ALTER TABLE' ) ) {
97- // CREATE TABLE IF NOT EXIST
98- const isNewTable =
99- originTablesTree . value . findIndex ( ( item : TableTreeParent ) => item . title === tableName ) === - 1
100- if ( isNewTable ) {
108+ switch ( commandType ) {
109+ case 'CREATE_DATABASE' :
110+ case 'DROP_DATABASE' :
111+ await fetchDatabases ( )
112+ break
113+ case 'CREATE_TABLE' :
114+ case 'DROP_TABLE' :
101115 refreshTables ( )
116+ break
117+ case 'ALTER_TABLE' : {
118+ const tableNodeData = originTablesTree . value . find ( ( item : TableTreeParent ) => item . title === command . name )
119+ if ( tableNodeData ) {
120+ await loadMoreColumns ( tableNodeData , true )
121+ // Update new children
122+ originTablesTree . value [ tableNodeData . key ] . children =
123+ originTablesTree . value [ tableNodeData . key ] [ tableNodeData . childrenType ]
124+ }
125+ break
102126 }
103- } else {
104- // ALTER TABLE
105- const tableNodeData = originTablesTree . value . find ( ( item : TableTreeParent ) => item . title === tableName )
106- if ( tableNodeData ) {
107- // Silently load more
108- await loadMoreColumns ( tableNodeData , true )
109- // await loadMoreDetails(tableNodeData, true)
110- // Update new children
111- originTablesTree . value [ tableNodeData . key ] . children =
112- originTablesTree . value [ tableNodeData . key ] [ tableNodeData . childrenType ]
113- }
127+ default :
128+ break
114129 }
115130 }
116131 }
@@ -145,6 +160,7 @@ export default function useQueryCode() {
145160 throw enhancedError
146161 }
147162 }
163+
148164 return {
149165 getResultsByType,
150166 runQuery,
0 commit comments