Merge pull request #5191 from solgenomics/topic/quality_control
[sgn.git] / lib / SGN / Role / Site / DBIC.pm
blob73fa7477130136a576d964468f7f9087faef36b5
1 package SGN::Role::Site::DBIC;
2 use 5.10.0;
4 use Moose::Role;
5 use namespace::autoclean;
7 use Carp;
8 use Data::Dumper;
9 use Class::Load ':all';
11 requires
12 'dbc_profile',
13 'ensure_dbh_search_path_is_set',
17 =head2 dbic_schema
19 Usage: my $schema = $c->dbic_schema( 'Schema::Package', 'connection_name' );
20 Desc : get a L<DBIx::Class::Schema> with the proper connection
21 parameters for the given connection name
22 Args : L<DBIx::Class> schema package name,
23 (optional) connection name to use
24 Ret : schema object
25 Side Effects: dies on failure
27 =cut
29 sub dbic_schema {
30 my ( $class, $schema_name, $profile_name, $sp_person_id) = @_;
31 #my $self = shift;
32 print STDERR "sp_person_id passed to DBIC Schema: $sp_person_id \n";
33 $class = ref $class if ref $class;
34 $schema_name or croak "must provide a schema package name to dbic_schema";
35 #Class::MOP::load_class( $schema_name );
36 load_class( $schema_name );
37 state %schema_cache;
39 return $schema_cache{$class}{$profile_name || ''}{$schema_name} ||= do {
40 my $profile = $class->dbc_profile( $profile_name );
41 #print STDERR "profile: ".Dumper($profile)."\n";
42 #my $sp_person_id = $profile -> user -> get_object() -> get_sp_person_id;
43 $schema_name->connect(
44 @{$profile}{qw| dsn user password attributes |},
45 { on_connect_call => sub { $class->ensure_dbh_search_path_is_set(my $dbh = shift->dbh ) ;
48 #my $delete_old_table_query = "DROP TABLE IF EXISTS logged_in_user";
49 #my $delete = $dbh -> do($delete_old_table_query);
50 my $q = "CREATE temporary table IF NOT EXISTS logged_in_user (sp_person_id bigint)";
51 my $create_handle = $dbh -> do($q);
52 #my $count_q = "select count(*) from logged_in_user";
53 #my $count_h = $dbh -> prepare($count_q);
54 #$count_h -> execute();
55 #my ($count) = $count_h->fetchrow_array();
56 #print STDERR "count: $count \n";
57 my $insert_query = "INSERT INTO logged_in_user (sp_person_id) VALUES (?)";
58 my $insert_handle = $dbh -> prepare($insert_query);
59 $insert_handle -> execute($sp_person_id);
63 }, },
66 #on_connect_do => [
67 # "CREATE temporary table IF NOT EXISTS logged_in_user (sp_person_id bigint)",
68 # "INSERT INTO logged_in_user (sp_person_id) VALUES ($sp_person_id)",
79 #my $dbh = $class->dbc->dbh;
80 #$dbh->do("CREATE temporary table IF NOT EXISTS logged_in_user (sp_person_id bigint)");
81 #my $insert_query = "INSERT INTO logged_in_user (sp_person_id) VALUES (?)";
82 #my $insert_handle = $dbh->prepare($insert_query);
83 #my $sp_person_id = $class -> user -> get_object() -> get_sp_person_id;
84 #$insert_handle->execute($sp_person_id);