@@ -26,6 +26,7 @@ struct PersistedTab: Codable {
2626 let tableName : String ?
2727 var isView : Bool = false
2828 var databaseName : String = " "
29+ var schemaName : String ?
2930 var sourceFileURL : URL ?
3031}
3132
@@ -340,6 +341,7 @@ struct QueryTab: Identifiable, Equatable {
340341 var isEditable : Bool
341342 var isView : Bool // True for database views (read-only)
342343 var databaseName : String // Database this tab was opened in (for multi-database restore)
344+ var schemaName : String ? // Schema this tab was opened in (for multi-schema restore, e.g. PostgreSQL)
343345 var showStructure : Bool // Toggle to show structure view instead of data
344346 var explainText : String ?
345347 var explainExecutionTime : TimeInterval ?
@@ -426,6 +428,7 @@ struct QueryTab: Identifiable, Equatable {
426428 self . isEditable = tabType == . table
427429 self . isView = false
428430 self . databaseName = " "
431+ self . schemaName = nil
429432 self . showStructure = false
430433 self . pendingChanges = TabPendingChanges ( )
431434 self . selectedRowIndices = [ ]
@@ -460,6 +463,7 @@ struct QueryTab: Identifiable, Equatable {
460463 self . isEditable = persisted. tabType == . table && !persisted. isView
461464 self . isView = persisted. isView
462465 self . databaseName = persisted. databaseName
466+ self . schemaName = persisted. schemaName
463467 self . showStructure = false
464468 self . pendingChanges = TabPendingChanges ( )
465469 self . selectedRowIndices = [ ]
@@ -479,6 +483,7 @@ struct QueryTab: Identifiable, Equatable {
479483 @MainActor static func buildBaseTableQuery(
480484 tableName: String ,
481485 databaseType: DatabaseType ,
486+ schemaName: String ? = nil ,
482487 quoteIdentifier: ( ( String ) -> String ) ? = nil
483488 ) -> String {
484489 let quote = quoteIdentifier ?? quoteIdentifierFromDialect ( PluginManager . shared. sqlDialect ( for: databaseType) )
@@ -499,13 +504,18 @@ struct QueryTab: Identifiable, Equatable {
499504 case . bash:
500505 return " SCAN 0 MATCH * COUNT \( pageSize) "
501506 default :
502- let quotedName = quote ( tableName)
507+ let qualifiedName : String
508+ if let schema = schemaName, !schema. isEmpty {
509+ qualifiedName = " \( quote ( schema) ) . \( quote ( tableName) ) "
510+ } else {
511+ qualifiedName = quote ( tableName)
512+ }
503513 switch PluginManager . shared. paginationStyle ( for: databaseType) {
504514 case . offsetFetch:
505515 let orderBy = PluginManager . shared. offsetFetchOrderBy ( for: databaseType)
506- return " SELECT * FROM \( quotedName ) \( orderBy) OFFSET 0 ROWS FETCH NEXT \( pageSize) ROWS ONLY; "
516+ return " SELECT * FROM \( qualifiedName ) \( orderBy) OFFSET 0 ROWS FETCH NEXT \( pageSize) ROWS ONLY; "
507517 case . limit:
508- return " SELECT * FROM \( quotedName ) LIMIT \( pageSize) ; "
518+ return " SELECT * FROM \( qualifiedName ) LIMIT \( pageSize) ; "
509519 }
510520 }
511521 }
@@ -532,6 +542,7 @@ struct QueryTab: Identifiable, Equatable {
532542 tableName: tableName,
533543 isView: isView,
534544 databaseName: databaseName,
545+ schemaName: schemaName,
535546 sourceFileURL: sourceFileURL
536547 )
537548 }
@@ -695,7 +706,7 @@ final class QueryTabManager {
695706 func replaceTabContent(
696707 tableName: String , databaseType: DatabaseType = . mysql,
697708 isView: Bool = false , databaseName: String = " " ,
698- isPreview: Bool = false ,
709+ schemaName : String ? = nil , isPreview: Bool = false ,
699710 quoteIdentifier: ( ( String ) -> String ) ? = nil
700711 ) -> Bool {
701712 guard let selectedId = selectedTabId,
@@ -707,6 +718,7 @@ final class QueryTabManager {
707718 let query = QueryTab . buildBaseTableQuery (
708719 tableName: tableName,
709720 databaseType: databaseType,
721+ schemaName: schemaName,
710722 quoteIdentifier: quoteIdentifier
711723 )
712724 let pageSize = AppSettingsManager . shared. dataGrid. defaultPageSize
@@ -733,6 +745,7 @@ final class QueryTabManager {
733745 tab. columnLayout = ColumnLayoutState ( )
734746 tab. pagination = PaginationState ( pageSize: pageSize)
735747 tab. databaseName = databaseName
748+ tab. schemaName = schemaName
736749 tab. isPreview = isPreview
737750 tabs [ selectedIndex] = tab
738751 return true
0 commit comments