Skip to content

Commit f4cba30

Browse files
authored
refactor(editor): auto refresh if 'drop/create database' (#547)
* refactor: auto refresh if 'drop/create database' * style: remove * refactor: simplified
1 parent e8f289e commit f4cba30

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

src/hooks/query-code.ts

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
/\bSHOW\s+CREATE\s+TABLE\b|(?:CREATE|DROP|ALTER)\s+(DATABASE|TABLE)\s+(?:IF\s+(?:NOT\s+)?EXISTS\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-
/CREATE TABLE IF NOT EXISTS(\s)+(\S)+|CREATE TABLE(\s)+(\S)+|DROP TABLE(\s)+(\S)+|ALTER TABLE(\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

Comments
 (0)