MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / ResolveExisting

Method ResolveExisting

pkg/sql/sem/tree/name_resolution.go:270–329  ·  view source on GitHub ↗

ResolveExisting performs name resolution for a table name when the target object is expected to exist already.

(
	ctx context.Context,
	r TableNameExistingResolver,
	lookupFlags ObjectLookupFlags,
	curDb string,
	searchPath sessiondata.SearchPath,
)

Source from the content-addressed store, hash-verified

268// ResolveExisting performs name resolution for a table name when
269// the target object is expected to exist already.
270func (t *TableName) ResolveExisting(
271 ctx context.Context,
272 r TableNameExistingResolver,
273 lookupFlags ObjectLookupFlags,
274 curDb string,
275 searchPath sessiondata.SearchPath,
276) (bool, NameResolutionResult, error) {
277 if t.ExplicitSchema {
278 // pg_temp can be used as an alias for the current sessions temporary schema.
279 // We must perform this resolution before looking up the object. This
280 // resolution only succeeds if the session already has a temporary schema.
281 scName, err := searchPath.MaybeResolveTemporarySchema(t.Schema())
282 if err != nil {
283 return false, nil, err
284 }
285 if t.ExplicitCatalog {
286 // Already 3 parts: nothing to search. Delegate to the resolver.
287 return r.LookupObject(ctx, lookupFlags, t.Catalog(), scName, t.Table())
288 }
289 // Two parts: D.T.
290 // Try to use the current database, and be satisfied if it's sufficient to find the object.
291 //
292 // Note: we test this even if curDb == "", because CockroachDB
293 // supports querying virtual schemas even when the current
294 // database is not set. For example, `select * from
295 // pg_catalog.pg_tables` is meant to show all tables across all
296 // databases when there is no current database set.
297
298 if found, objMeta, err := r.LookupObject(ctx, lookupFlags, curDb, scName, t.Table()); found || err != nil {
299 if err == nil {
300 t.CatalogName = Name(curDb)
301 }
302 return found, objMeta, err
303 }
304 // No luck so far. Compatibility with CockroachDB v1.1: try D.public.T instead.
305 if found, objMeta, err := r.LookupObject(ctx, lookupFlags, t.Schema(), PublicSchema, t.Table()); found || err != nil {
306 if err == nil {
307 t.CatalogName = t.SchemaName
308 t.SchemaName = PublicSchemaName
309 t.ExplicitCatalog = true
310 }
311 return found, objMeta, err
312 }
313 // Welp, really haven't found anything.
314 return false, nil, nil
315 }
316
317 // This is a naked table name. Use the search path.
318 iter := searchPath.Iter()
319 for next, ok := iter.Next(); ok; next, ok = iter.Next() {
320 if found, objMeta, err := r.LookupObject(ctx, lookupFlags, curDb, next, t.Table()); found || err != nil {
321 if err == nil {
322 t.CatalogName = Name(curDb)
323 t.SchemaName = Name(next)
324 }
325 return found, objMeta, err
326 }
327 }

Callers

nothing calls this directly

Calls 8

TableMethod · 0.95
NameTypeAlias · 0.85
SchemaMethod · 0.80
LookupObjectMethod · 0.80
CatalogMethod · 0.80
IterMethod · 0.80
NextMethod · 0.65

Tested by

no test coverage detected