11<script setup lang="ts">
22import type { StatementToExecute } from ' @/composables/useSqlStatements'
3- import type { DatabaseType } from ' @/store/connectionStore'
43import type { TableInfo } from ' @/store/databaseStore'
54import type { ApiResponse } from ' @/types/api'
65import { invoke } from ' @tauri-apps/api/core'
@@ -22,6 +21,7 @@ import { usePlatform } from '@/composables/usePlatform'
2221import { useSqlFormatter } from ' @/composables/useSqlFormatter'
2322import { browseApi , loadQueryFile , saveQueryFile , saveQueryFileAs , saveQueryMetadata } from ' @/datasources'
2423import { ConnectionStatus , useAppStore , useConnectionStore , useDatabaseStore , useTabStore } from ' @/store'
24+ import { DatabaseType } from ' @/store/connectionStore'
2525import { isApiSuccess } from ' @/types/api'
2626
2727const { t } = useI18n ()
@@ -313,6 +313,14 @@ function handleNewTab() {
313313 tabStore .createTab (connId ?? undefined , db )
314314}
315315
316+ function handleOpenQueryTab(database : string ) {
317+ const connId = getActiveConnectionId ()
318+ if (! connId )
319+ return
320+ const tab = tabStore .createTab (connId , database )
321+ tabStore .setActiveTab (tab .id )
322+ }
323+
316324function handleTabSelect(tabId : string ) {
317325 const tab = tabStore .tabById (tabId )
318326 if (tab ?.orphanFromConnectionId ) {
@@ -388,11 +396,48 @@ CREATE TABLE ${schemaPrefix}"${table.name}" (
388396 }
389397}
390398
391- function handleSelectTopN(table : TableInfo , database : string , schema ? : string , n = 100 ) {
392- const schemaPrefix = schema ? ` "${schema }". ` : ' '
393- const query = ` SELECT * FROM ${schemaPrefix }"${table .name }" LIMIT ${n }; `
399+ // Databases speaking the MySQL dialect quote identifiers with backticks.
400+ const MYSQL_LIKE_DB_TYPES = new Set <DatabaseType >([
401+ DatabaseType .MYSQL ,
402+ DatabaseType .MARIADB ,
403+ DatabaseType .TIDB ,
404+ DatabaseType .OCEANBASE ,
405+ DatabaseType .TDSQL ,
406+ DatabaseType .POLARDB ,
407+ DatabaseType .DORIS ,
408+ DatabaseType .SELECTDB ,
409+ DatabaseType .STARROCKS ,
410+ DatabaseType .DATABEND ,
411+ DatabaseType .GOLDENDB ,
412+ DatabaseType .MANTICORESEARCH ,
413+ DatabaseType .SINGLESTOREMEMSQL ,
414+ DatabaseType .CLOUDSQLMYSQL ,
415+ ])
416+
417+ function quoteIdentifier(name : string , dbType : DatabaseType | undefined ): string {
418+ if (dbType === DatabaseType .SQLSERVER )
419+ return ` [${name .replace (/ \] / g , ' ]]' )}] `
420+ if (dbType && MYSQL_LIKE_DB_TYPES .has (dbType ))
421+ return ` \` ${name .replace (/ `/ g , ' ``' )}\` `
422+ return ` "${name .replace (/ "/ g , ' ""' )}" `
423+ }
424+
425+ function buildSelectTopN(table : TableInfo , schema : string | undefined , n : number , dbType : DatabaseType | undefined ): string {
426+ const tableRef = schema
427+ ? ` ${quoteIdentifier (schema , dbType )}.${quoteIdentifier (table .name , dbType )} `
428+ : quoteIdentifier (table .name , dbType )
429+ if (dbType === DatabaseType .SQLSERVER )
430+ return ` SELECT TOP ${n } * FROM ${tableRef }; `
431+ if (dbType === DatabaseType .ORACLE || dbType === DatabaseType .OCEANBASE_ORACLE )
432+ return ` SELECT * FROM ${tableRef } FETCH FIRST ${n } ROWS ONLY; `
433+ return ` SELECT * FROM ${tableRef } LIMIT ${n }; `
434+ }
394435
436+ function handleSelectTopN(table : TableInfo , database : string , schema ? : string , n = 100 ) {
395437 const connId = getActiveConnectionId ()
438+ const dbType = connId ? connectionStore .getConnectionById (connId )?.type : undefined
439+ const query = buildSelectTopN (table , schema , n , dbType )
440+
396441 if (connId ) {
397442 const tab = tabStore .createTab (connId , database , schema )
398443 tabStore .updateTabContent (tab .id , query )
@@ -975,6 +1020,7 @@ function closeResultPanel() {
9751020 @truncate-table =" handleTruncateTable "
9761021 @open-listing-tab =" handleOpenListingTab "
9771022 @open-ddl-tab =" handleOpenDdlTab "
1023+ @open-query-tab =" handleOpenQueryTab "
9781024 />
9791025 </template >
9801026 <template #bottom >
0 commit comments