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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Samuel Hawksby-Robinson
  • 2,652
  • 4
  • 24
  • 25
  • Partial duplicate: https://stackoverflow.com/q/51952530/13860 – Jonathan Hall Dec 21 '21 at 11:46
  • This may be an XY Problem. Can you explain what you're using these buffered channels for? – Jonathan Hall Dec 21 '21 at 11:47
  • If your goal is to know which database you're connected to, this answer should help: https://stackoverflow.com/questions/18072554/sqlite-get-name-of-attached-databases But note: There's a potential many-to-one relationship between *sql.DB instances and sqlite databases. – Jonathan Hall Dec 21 '21 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 – Samuel Hawksby-Robinson Dec 21 '21 at 15:33
  • 1
    For that, then `PRAGMA database_list` is probably the best answer. – Jonathan Hall Dec 21 '21 at 15:55

0 Answers0