MCPcopy Create free account
hub / github.com/bytebase/bytebase / buildCatalog

Function buildCatalog

backend/plugin/parser/tidb/completion.go:121–166  ·  view source on GitHub ↗

buildCatalog constructs an omni TiDB catalog from Bytebase metadata by replaying minimal DDL. It fully loads the default database plus any database referenced as a qualifier in the statement (so cross-database qualified completion like `other_db.tbl.col` resolves), and registers every other known da

(ctx context.Context, cCtx base.CompletionContext, statement string)

Source from the content-addressed store, hash-verified

119// backticked so reserved words and special characters parse; tables are created
120// one at a time so a single unparseable column type cannot empty the catalog.
121func buildCatalog(ctx context.Context, cCtx base.CompletionContext, statement string) *catalog.Catalog {
122 cat := catalog.New()
123 allNames := listAllDatabaseNames(ctx, cCtx)
124
125 // Decide which databases to fully load with their objects. This needs a
126 // metadata fetcher and a database to load from: the default database (if one
127 // is selected) plus any database referenced as a qualifier in the statement.
128 seen := map[string]bool{}
129 var loadOrder []string
130 if cCtx.Metadata != nil {
131 if cCtx.DefaultDatabase != "" {
132 loadOrder = append(loadOrder, cCtx.DefaultDatabase)
133 seen[cCtx.DefaultDatabase] = true
134 }
135 for _, name := range allNames {
136 if !seen[name] && statementReferencesDatabase(statement, name) {
137 loadOrder = append(loadOrder, name)
138 seen[name] = true
139 }
140 }
141 }
142
143 // Register every other known database name (name only) so database-name
144 // candidates surface — this works at the instance level even when no database
145 // is selected (DefaultDatabase empty).
146 var reg strings.Builder
147 for _, name := range allNames {
148 if !seen[name] {
149 reg.WriteString("CREATE DATABASE " + backtickIdentifier(name) + "; ")
150 }
151 }
152 if reg.Len() > 0 {
153 _, _ = cat.Exec(reg.String(), &catalog.ExecOptions{ContinueOnError: true})
154 }
155
156 for _, name := range loadOrder {
157 loadDatabaseObjects(ctx, cCtx, cat, name)
158 }
159
160 // Restore the current database to the default so unqualified references
161 // resolve against it.
162 if cCtx.DefaultDatabase != "" {
163 _, _ = cat.Exec("USE "+backtickIdentifier(cCtx.DefaultDatabase)+";", &catalog.ExecOptions{ContinueOnError: true})
164 }
165 return cat
166}
167
168// loadDatabaseObjects fully loads one database's tables and views into the
169// catalog, under that database's namespace.

Callers 1

CompletionFunction · 0.70

Calls 6

listAllDatabaseNamesFunction · 0.85
backtickIdentifierFunction · 0.85
loadDatabaseObjectsFunction · 0.85
LenMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected