File: dbi.tt2

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (201 lines) | stat: -rw-r--r-- 7,762 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
[% MACRO printPackage(node) BLOCK %]
########Bio::Chado::CDBI::[% format_node(node.name) %]########

package Bio::Chado::CDBI::[% format_node(node.name) %];
use base '[% baseclass %]';
use Class::DBI::Pager;
no warnings qw(redefine);

Bio::Chado::CDBI::[% format_node(node.name) %]->set_up_table('[% node.table.name %]');
[% printPKAccessors(node.primary_key, node.table.name) %]
[% printHasA(node.edges, node) %]
[% printHasMany(node.edges, node) %]
[% printHasCompound(node.compoundedges, node.hyperedges, node.name) %]

1;
[% END %]
[% MACRO printPKAccessors(array, name) BLOCK %]
#
# Primary key accessors
#
[% FOREACH item = array %]
[% IF item != "id" %]sub id { shift->[% item %] }[% END %]
[% IF item != name %]sub [% name %] { shift->[% item %] }[% END %]
[% END %] [% END %] [% MACRO printHasA(edges, name) BLOCK %]
[% FOREACH edge = edges %]
[% IF loop.first() %]
#
# Has A
#
[% END %] [% IF edge.type == 'import' %]
Bio::Chado::CDBI::[% format_node(node.name) %]->has_a([% edge.thisfield.name %] => 'Bio::Chado::CDBI::[% format_node(edge.thatnode.name) %]');
    [% IF node.has(edge.thatnode.name) < 2 %]
sub Bio::Chado::CDBI::[% format_node(node.name) %]::[% edge.thatnode.table.name %] { return shift->[% edge.thisfield.name %] }
    [% ELSE %]
sub [% format_fk(edge.thisnode.table.name,edge.thisfield.name) %] { return shift->[% edge.thisfield.name %] }
    [% END %] [% END %] [% END %] [% END %]
[%- MACRO printHasMany(edges, node) BLOCK -%]
[%- FOREACH edge = edges -%]
[% IF loop.first() %]
#
# Has Many
#
[% END %] [% IF edge.type == 'export' %]
Bio::Chado::CDBI::[% format_node(node.name) %]->has_many('[% edge.thatnode.table.name %]_[% edge.thatfield.name %]', 'Bio::Chado::CDBI::[% format_node(edge.thatnode.name) %]' => '[% edge.thatfield.name %]');
  [% IF node.via(edge.thatnode.name) >= 1 %]
sub [% edge.thatnode.table.name %]_[% format_fk(edge.thatnode.table.name,edge.thatfield.name) %]s { return shift->[% edge.thatnode.table.name %]_[% edge.thatfield.name %] }
#sub [% format_refers(edge.thatnode.table.name,edge.thatfield.name) %] {}
  [% ELSIF edge.thatnode.table.is_data %]
    [% IF node.edgecount(edge.thatnode.name) > 1 %]
sub [% edge.thatnode.table.name %]_[% format_fk(edge.thatnode.name,edge.thatfield.name) %]_ids { my $self = shift; return $self->[% edge.thatnode.table.name %]_[% edge.thatfield.name %](@_) }
    [% ELSE %]
sub [% edge.thatnode.table.name %]s { return shift->[% edge.thatnode.table.name %]_[% edge.thatfield.name %] }
    [% END %] [% END %] [%- END -%] [%- END -%] [%- END -%]
[% MACRO printHasCompound(cedges,hedges,name) BLOCK %]
[% FOREACH cedge = cedges %]
[% IF loop.first() %]
#
# Has Compound Many (many to many relationships in all their flavors)
#
[% END %] [% FOREACH edge = cedge.edges %]
  [% NEXT IF edge.thisnode.name != name %]
sub [% cedge.via.table.name %]_[% format_fk(edge.thatnode.table.name,edge.thatfield.name) %]s { return shift->[% cedge.via.table.name %]_[% edge.thatfield.name %] }
[% END %] [% END %]
# one to many to one
[% seen = 0 %] [% FOREACH h = hedges %]
  [% NEXT UNLESS h.type == 'one2one' %]
[% IF seen == 0 ; seen = 1 %]# one2one #[% END %]
sub [% h.thatnode.table.name %]s { my $self = shift; return map $_->[% h.thatviafield.name %], $self->[% h.vianode.table.name %]_[% h.thisviafield.name %] }
[% END %]
# one to many to many
[% seen = 0 %] [% FOREACH h = hedges %]
  [% NEXT UNLESS h.type == 'one2many' %]
[% IF seen == 0 ; seen = 1 %]# one2many #[% END %]
  [% thisnode = h.thisnode_index(0) %]
  [% i = 0 %]
  [% FOREACH thatnode = h.thatnode %]
    [% NEXT UNLESS h.thisviafield_index(i).name %]
sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thatviafield_index(0).name) %]s { my $self = shift; return map $_->[% h.thatviafield_index(0).name %], $self->[% h.vianode.table.name %]_[% h.thisviafield_index(i).name %] }
    [% i = i + 1 %]
  [% END %] [% END %]
#many to many to one
[% seen = 0 %] [% FOREACH h = hedges %]
  [% NEXT UNLESS h.type == 'many2one' %]
[% IF seen == 0 ; seen = 1 %]# many2one #[% END %]
  [% i = 0 %]
  [% FOREACH thisnode = h.thisnode %]
sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thisviafield_index(i).name) %]_[% format_fk(h.vianode,h.thatviafield_index(0).name) %]s { my $self = shift; return map $_->[% h.thatviafield_index(0).name %], $self->[% h.vianode.table.name %]_[% h.thisviafield_index(i).name %] }
    [% i = i + 1 %]
  [% END %] [% END %]
#many to many to many
[% seen = 0 %]
[% FOREACH h = hedges %]
  [% NEXT UNLESS h.type == 'many2many' %]
[% IF seen == 0 ; seen = 1 %]# many2many #[% END %]
  [% i = 0 %]
  [% FOREACH thisnode = h.thisnode %]
    [% j = 0 %]
    [% FOREACH thatnode = h.thatnode %]
sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thisviafield_index(i).name) %]_[% format_fk(h.vianode,h.thatviafield_index(j).name) %]s { my $self = shift; return map $_->[% h.vianode.table.name %]_[% format_fk(h.vianode,h.thatviafield_index(j).name) %]s, $self->[% h.vianode.table.name %]_[% format_fk(h.vianode,h.thisviafield_index(i).name) %]s }
      [% j = j + 1 %]
    [% END %]
    [% i = i + 1 %]
  [% END %] [% END %] [% END %]
########DBI########
package [% baseclass %];

# Created by SQL::Translator::Producer::TTSchema
# Template used: dbi.tt2

use strict;
use Data::Dumper;
use Bio::GMOD::Config;
use Bio::GMOD::DB::Config;
no warnings 'redefine';
use base qw(Class::DBI::Pg);

# This is how you normally connect with Class DBI's connection pooling but
# its very fragile for me on FC2.  I'm replacing it with the db_Main method below
#[% baseclass %]->set_db('Main', '[% db_dsn  %]', '[% db_user %]', '[% db_pass %]');

my $db_options = { __PACKAGE__->_default_attributes };
__PACKAGE__->_remember_handle('Main'); # so dbi_commit works
$db_options->{AutoCommit} = 0;

sub db_Main {
  my $DBPROFILE ||= 'default';   #might want to allow passing this in somehow
  my $gmod_conf = Bio::GMOD::Config->new();
  my $db_conf = Bio::GMOD::DB::Config->new( $gmod_conf, $DBPROFILE );

  my $dbname = $db_conf->name;
  my $dbhost = $db_conf->host;
  my $dbport = $db_conf->port;
  my $dbuser = $db_conf->user;
  my $dbpass = $db_conf->password;
 
  my $dbh;
  $dbh = DBI->connect_cached( 
      "dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport", 
      $dbuser, 
      $dbpass, 
      $db_options );
  # clear the connection cache if can't ping
  if ($dbh->ping() < 1) {
    my $CachedKids_hashref = $dbh->{Driver}->{CachedKids};
    %$CachedKids_hashref = () if $CachedKids_hashref;
    $dbh = DBI->connect_cached(
       "dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport",
       $dbuser, 
       $dbpass, 
       $db_options );
       warn("Database handle reset!: ".$dbh." ping: ".$dbh->ping());
  }
  return($dbh);
}

sub search_ilike { shift->_do_search(ILIKE => [% "\@\_" %] ) }
sub search_lower {
   my $c = shift;
   my %q = @_;
   my %t;
   foreach my $k (keys %q){
     $t{"lower($k)"} = lc($q{$k});
   }
   $c->_do_search(LIKE => %t);
}


# debug method
sub dump {
  my $self = shift;
  my %arg  = %{shift @_};
  $arg{'indent'} ||= 1;
  $arg{'depth'} ||= 3;
  $Data::Dumper::Maxdepth = $arg{'depth'} if defined $arg{'depth'};
  $Data::Dumper::Indent = $arg{'indent'} if defined $arg{'indent'};
  return(Dumper($arg{'object'}));
}

#
#
# NOT PART OF THE API, but useful function which returns a single row
#  and throws an error if more than one is returned
#
# Added as a utility function for modware
#
sub get_single_row {
   my ($proto, @args) = @_;
   my $class = ref $proto || $proto;

   my @rows  = $class->search( @args );

   my $count = @rows;
   die "only one row expected, @rows returned" if @rows > 1;

   return $rows[0];
}


1;
[% graph = schema.as_graph %]
[% FOREACH n = graph.node %][% NEXT UNLESS n.isa('UNIVERSAL') %][% printPackage(n) %][% END %]