Obtaining Datasource Name for Historical Tags at Runtime

Hello all,

I have constructed a Named Query to pull and provide some metadata about historical tags that a user selects in a Perspective Session. This query is configured to accept the database name as an input parameter, but that leads to another issue: how can I obtain the database name?

Currently, I allow the user to see and select from the available databases in a dropdown menu by browsing the available system tags (or system.db.getConnections would also work for this, I know). While this technically works, it means that the user can't obtain information about two or more tags at once unless they store to the same database. Obviously, this could be a problem; often, even tags within the same folder or UDT will store to a different database.

I would prefer to dynamically resolve the database name per tag (likely with some form of in-script caching linking a DB name to each history provider for performance?) and batch the queries if possible, allowing the user to select tags as desired without needing to know from which database they are sourced.

I am aware that I can obtain the name of the associated history provider for each tag. However, while usually not the case, I would like this tool to work in the event that the history provider and database do not have the same name. Is there a scripted way to obtain the exact datasource name for a known history provider? That would be an immediate solution to the problem.

Critically, the entire tool must be portable between projects and Gateways (i.e. no caching of relations to a database lookup table, since there's no guarantee that such a table will be available on another Gateway).

Thanks!