1

I want to get the path or even the connection string of a sql.DB instance. The package I'm writing doesn't know what the database path is and the application may have multiple database files. However I need to be able to map a specific database to a specific buffered channel, and ignore any additional calls for a database the package has already seen.

The application uses the github.com/mutecomm/go-sqlcipher driver to instantiate the database, if that makes any difference.

Is it possible to discriminate between instances of sql.DB based on the file path of the source database? If so, how do I do it?

5
  • Partial duplicate: stackoverflow.com/q/51952530/13860 Commented Dec 21, 2021 at 11:46
  • This may be an XY Problem. Can you explain what you're using these buffered channels for? Commented Dec 21, 2021 at 11:47
  • If your goal is to know which database you're connected to, this answer should help: stackoverflow.com/questions/18072554/… But note: There's a potential many-to-one relationship between *sql.DB instances and sqlite databases. Commented Dec 21, 2021 at 11:48
  • Hey @Flimzy thank you for your reply. I'm modifying an application that contains a package that manages settings, this package has this func New(db *sql.DB) (Settings, error) . The package will take any db and return a Settings struct that has settings table related funcs. But the application can have multiple databases each with its own settings table. The application frustratingly instantiates the Settings struct in multiple places by different services that want to access the settings, and I need to trigger events when certain settings are changed. The channel is the event stack Commented Dec 21, 2021 at 15:33
  • 1
    For that, then PRAGMA database_list is probably the best answer. Commented Dec 21, 2021 at 15:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.