From 6dfcd631bb6ec3f6165f2c21e138ad2ea29b5543 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 12 Sep 2012 22:04:15 -0700 Subject: [PATCH] first pass at instance tagging --- example/schemas/dbr_schema_sqlite.sql | 1 + lib/DBR.pm | 6 ++++++ lib/DBR/Config/Instance.pm | 22 +++++++++++----------- lib/DBR/Misc/Session.pm | 9 +++++++++ sql/dbr_schema_mysql.sql | 1 + sql/dbr_schema_sqlite.sql | 1 + 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/example/schemas/dbr_schema_sqlite.sql b/example/schemas/dbr_schema_sqlite.sql index c6059d2..c853f70 100644 --- a/example/schemas/dbr_schema_sqlite.sql +++ b/example/schemas/dbr_schema_sqlite.sql @@ -11,6 +11,7 @@ CREATE TABLE dbr_instances ( schema_id int(10) NOT NULL, handle varchar(50) NOT NULL, class varchar(50) NOT NULL, + tag varchar(250), dbname varchar(250), username varchar(250), password varchar(250), diff --git a/lib/DBR.pm b/lib/DBR.pm index 6964ae0..32f80df 100644 --- a/lib/DBR.pm +++ b/lib/DBR.pm @@ -70,6 +70,12 @@ sub import { $dbr->get_instance(@_); }; + *{"${callpack}::dbr_session"} = + sub { + shift if blessed($_[0]) || $_[0]->isa( [caller]->[0] ); + $dbr->session; + }; + } sub new { diff --git a/lib/DBR/Config/Instance.pm b/lib/DBR/Config/Instance.pm index b140678..429e9ff 100644 --- a/lib/DBR/Config/Instance.pm +++ b/lib/DBR/Config/Instance.pm @@ -56,10 +56,12 @@ sub lookup{ }else{ my $handle = $params{handle} || return $self->_error('handle is required'); my $class = $params{class} || 'master'; - - $self->{guid} = $INSTANCE_MAP{$handle}->{$class} || $INSTANCE_MAP{$handle}->{'*'} or # handle aliases if there's no exact match - return $self->_error("No DB instance found for '$handle','$class'"); - + my $tag = $params{tag} || $self->{session}->tag; + + my $h = $INSTANCE_MAP{$handle} or return $self->_error("No DB instance found for handle '$handle'"); + + $self->{guid} = $h->{$tag}{$class} || $h->{$tag}{'*'} || $h->{''}{$class} || $h->{''}{'*'} or # handle aliases if there's no exact match + return $self->_error("No DB instance found for handle '$handle', class '$class', tag '$tag'"); } $INSTANCES_BY_GUID{ $self->{guid} } or return $self->_error('no such guid'); @@ -84,7 +86,7 @@ sub load_from_db{ return $self->_error('Failed to select instances') unless my $instrows = $dbh->select( -table => 'dbr_instances', - -fields => 'instance_id schema_id class dbname username password host dbfile module handle readonly' + -fields => 'instance_id schema_id class dbname username password host dbfile module handle readonly tag' ); my @instances; @@ -123,6 +125,7 @@ sub register { # basically the same as a new hostname => $spec->{hostname} || $spec->{host}, user => $spec->{username} || $spec->{user}, dbfile => $spec->{dbfile}, + tag => $spec->{tag} || '', password => $spec->{password}, class => $spec->{class} || 'master', # default to master instance_id => $spec->{instance_id} || '', @@ -153,18 +156,15 @@ sub register { # basically the same as a new $config->{connectstring} =~ s/-$key-/$config->{$key}/; } - #Reuse the guid if we are being reloaded - my $guid = $INSTANCE_MAP{ $config->{handle} }->{ $config->{class} } || $GUID++; - - # Register this instance in the global repository - $INSTANCE_MAP{ $config->{handle} }->{ $config->{class} } ||= $guid; + # Register or Reuse the guid + my $guid = $INSTANCE_MAP{ $config->{handle} }{ $config->{tag} }{ $config->{class} } ||= $GUID++; $INSTANCES_BY_GUID{ $guid } = $config; $self->{guid} = $config->{guid} = $guid; # Now we are cool to start calling accessors if ($spec->{alias}) { - $INSTANCE_MAP{ $spec->{alias} }->{'*'} = $guid; + $INSTANCE_MAP{ $spec->{alias} }{ $config->{tag} }{'*'} = $guid; } if ($config->{schema_id}){ diff --git a/lib/DBR/Misc/Session.pm b/lib/DBR/Misc/Session.pm index cb9e03e..f9085f3 100644 --- a/lib/DBR/Misc/Session.pm +++ b/lib/DBR/Misc/Session.pm @@ -14,6 +14,7 @@ sub new { admin => $params{admin} ? 1 : 0, fudge_tz => $params{fudge_tz}, use_exceptions => $params{use_exceptions} ? 1 : 0, + tag => defined($params{tag}) ? $params{tag} : '' }; bless( $self, $package ); @@ -27,6 +28,14 @@ sub new { return $self; } +sub tag{ + my $self = shift; + if(exists $_[0]){ + my $set = shift; + return $self->{tag} = defined($set) ? $set : ''; + } + return $self->{tag}; +} sub timezone { my $self = shift; diff --git a/sql/dbr_schema_mysql.sql b/sql/dbr_schema_mysql.sql index f813fd8..8f17416 100644 --- a/sql/dbr_schema_mysql.sql +++ b/sql/dbr_schema_mysql.sql @@ -12,6 +12,7 @@ CREATE TABLE dbr_instances ( schema_id int(10) NOT NULL, handle varchar(50) NOT NULL, class varchar(50) NOT NULL COMMENT 'query, master, etc...', + tag varchar(250), dbname varchar(250), username varchar(250), password varchar(250), diff --git a/sql/dbr_schema_sqlite.sql b/sql/dbr_schema_sqlite.sql index c7a94e8..5316bfc 100644 --- a/sql/dbr_schema_sqlite.sql +++ b/sql/dbr_schema_sqlite.sql @@ -11,6 +11,7 @@ CREATE TABLE dbr_instances ( schema_id int(10) NOT NULL, handle varchar(50) NOT NULL, class varchar(50) NOT NULL, + tag varchar(250), dbname varchar(250), username varchar(250), password varchar(250),