Brian Candler
10/26/2004 1:15:00 PM
> [foo.rbx]
> require 'dbi'
> $dbh ||= DBI.connect('dbi:Pg:foo',...)
>
> [bar.rbx]
> require 'dbi'
> $dbh ||= DBI.connect('dbi:Pg:bar',...)
>
> $dbh conflicts. This is not cool way.
Well OK, but if you can't choose a unique global variable name, you could
instead write
module ::MyPrivateStuff
def self.dbh
@dbh ||= DBI.connect('dbi:Pg:foo',...)
end
end
dbh = ::MyPrivateStuff.dbh
> Apache::DBI's advantage is transparent & simple.
I'm guessing now. Does your module keep a separate database instance for
each unique value of the DBI string? Or each unique combination of all the
arguments inside DBI.connect(...) ?
In other words, does it do something like
$dbh ||= {}
$dbh['dbi:Pg:bar'] ||= DBI.connect('dbi:Pg:bar')
Help me out here :-) I'm just trying to understand what it's doing, so I can
see whether I can make use of it.
If my guess is correct, then perhaps the pattern can be abstracted: e.g.
dbh = Persistent[DBI, :connect, 'dbi:Pg:bar', ...]
and the implementation would be:
module Persistent
@things = {}
def self.[](*args)
@things[args] ||= args[0].send(*args[1..-1])
end
end
Regards,
Brian.