user2840647
user2840647

Reputation: 1314

How to query multiple databases using Sequel

I have a project which contains multiple databases in a directory structure like this:

folder1
|_ sub1
  |_ db1
  |_ db2
|_ sub2
  |_ db1
  |_ d2

I would like to run one query against all the databases.

Is there a way to do this using Sequel, or would I need to traverse the directory and create a new database connection for each database, then run the query?

Help appreciated.

Upvotes: 0

Views: 241

Answers (1)

the Tin Man
the Tin Man

Reputation: 160601

You can do it a couple ways but I'd do it like this untested code:

%w[
  sqlite://folder1/sub1/db1.db
  sqlite://folder1/sub1/db2.db
  sqlite://folder1/sub2/db1.db
  sqlite://folder1/sub2/db2.db
].map{ |dsn|
  DB = Sequel.connect(dsn)
  DB[:some_table].where('some' => 'value').all
}

This iterates over the DSNs for each database, opens a connection and returns the result as an array of results.

What those are is undefined since we have no idea what your table schemas are.

Upvotes: 1

Related Questions