blib/lib/Class/DBI/Pg/More.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 138 CC 10 SCAR 25.6
CC
10
Cov
0%
_handle_pg_datetime
30.0
set_up_table
20.0
_do_execute
12.0
CRAP 13.0
1 =head1 NAME
2  
3 Class::DBI::Pg::More - Enhances Class::DBI::Pg with more goodies.
4  
5 =head1 SYNOPSIS
6  
7 package MyClass;
8 use base 'Class::DBI::Pg::More';
9
10 __PACKAGE__->set_up_table("my_table");
11  
12 # a_date is a date column in my_table.
13 # Class::DBI::Plugin::DateFormat::Pg->has_date has been
14 # called for a_date implicitly.
15 my $a_date_info = __PACKAGE__->pg_column_info('a_date')
16 print $a_date_info->{type}; # prints "date"
17  
18 # an_important is an important column in my_table set to not null
19 print $a_date_info->{is_nullable} ? "TRUE" : "FALSE"; # prints FALSE
20  
21 =head1 DESCRIPTION
22  
23 This class overrides Class::DBI::Pg C<set_up_table> method to setup more
24 things from the database.
25  
26 It recognizes date, timestamp etc. columns and calls
27 C<Class::DBI::Plugin::DateTime::Pg> has_* methods on them.
28  
29 It also fetches some constraint information (currently C<not null>).
30  
31 =cut
32  
33 use strict;
34 use warnings FATAL => 'all';
35  
36 package Class::DBI::Pg::More;
37 use base 'Class::DBI';
38  
39 our $VERSION = '0.05';
40 $Class::DBI::Weaken_Is_Available = 0;
41  
42 sub find_primary_key {
43 my ($class, $dbh, $table) = @_;
44 return $dbh->selectcol_arrayref(<<'ENDS', undef, $table);
45 SELECT a.attname FROM pg_class c, pg_attribute a, pg_index i, pg_namespace n,
46 generate_series(0, current_setting('max_index_keys')::integer ) idx(n)
47 WHERE c.oid = a.attrelid AND c.oid = i.indrelid AND i.indisprimary
48 AND a.attnum = i.indkey[idx.n] AND NOT a.attisdropped
49 AND has_schema_privilege(n.oid, 'USAGE'::text)
50 AND n.nspname NOT LIKE 'pg!_%' ESCAPE '!'
51 AND has_table_privilege(c.oid, 'SELECT'::text)
52 AND c.relnamespace = n.oid and c.relname = ? and nspname = 'public';
53 ENDS
54 }
55  
56 sub find_columns {
57 my ($class, $dbh, $table) = @_;
58 return $dbh->selectall_arrayref(<<ENDS, undef, $table);
59 SELECT column_name, data_type, is_nullable FROM information_schema.columns
60 WHERE table_name = ? and table_schema = 'public'
61 ENDS
62 }
63  
64 sub _handle_pg_datetime {
65 my ($class, $col, $type) = @_;
66 my $func;
67 if ($type eq 'date') {
68 $func = "has_$type";
69 } elsif ($type =~ /^(time\w*)/) {
70 $func = "has_$1";
71 $func .= "tz" unless $type =~ /without time zone/;
72 } else {
73 return;
74 }
75 eval "use Class::DBI::Plugin::DateTime::Pg";
76 die "Unable to use CDBIP::DT::Pg: $@" if $@;
77 $class->$func($col);
78 }
79  
80 =head1 METHODS
81  
82 =head2 $class->set_up_table($table, $args)
83  
84 This is main entry point to the module. Please see C<Class::DBI::Pg>
85 documentation for its description.
86  
87 This class automagically uses Class::DBI::Plugin::DateTime::Pg for date/time
88 fields, so you should use DateTime values with them.
89  
90 =cut
91 sub set_up_table {
92 my ( $class, $table, $opts) = @_;
93 $opts ||= {};
94  
95 my $dbh = $class->db_Main;
96 my @primary = @{ $opts->{Primary} || $class->find_primary_key($dbh, $table) }
97 or die "$table has no primary key";
98  
99 my %infos;
100 my $arr = $class->find_columns($dbh, $table);
101 $class->table($table);
102 $class->columns(Primary => @primary);
103 $class->columns(($opts->{ColumnGroup} || 'Essential') => map { $_->[0] } @$arr);
104  
105 my $def = $dbh->selectcol_arrayref(q{ select column_default
106 from information_schema.columns where column_name = ?
107 and table_name = ? and table_schema = 'public' }
108 , undef, $table, $primary[0])->[0];
109 my ($seq) = ($def =~ /nextval\(\'(\w+)/) if $def;
110 $class->sequence($seq) if $seq;
111  
112 for my $a (@$arr) {
113 my $i = { type => $a->[1] };
114 $class->_handle_pg_datetime($a->[0], $a->[1]);
115 $i->{is_nullable} = 1 if $a->[2] eq 'YES';
116 $infos{ $a->[0] } = $i;
117 }
118 $class->mk_classdata("Pg_Column_Infos", \%infos);
119 }
120  
121 sub _do_execute {
122 my ($self, $sql, $arg_map, @rest) = @_;
123 my @args;
124 if (!(ref($self) && @$arg_map)) {
125 @args = @rest;
126 goto OUT;
127 }
128 for (my $i = 0; $i < @$arg_map; $i++) {
129 my $a = $arg_map->[$i];
130 push @args, $a ? $self->$a : shift @rest;
131 }
132 push @args, @rest;
133 OUT:
134 my $sth = $self->$sql;
135 $sth->execute(@args);
136 return $sth;
137 }
138  
139 sub _do_set_sql {
140 my ($class, $name, $sql, $ex, $cb, @arg_map) = @_;
141 $class->set_sql($name, $sql);
142 my $f = "sql_$name";
143 no strict 'refs';
144 *{ "$class\::$ex\_$name" } = sub {
145 return $cb->(shift()->_do_execute($f, \@arg_map, @_));
146 };
147 }
148  
149 =head2 $class->set_exec_sql($name, $sql, @arg_map)
150  
151 Wraps C<Ima::DBI> C<set_sql> methods to create C<exec_$name> function
152 which basically calls C<execute> on C<sql_$name> handle.
153  
154 C<@arg_map> provides mapping of the arguments to the exec function. It can
155 be used to call instance methods to get execution parameters.
156  
157 For example given "update __TABLE__ set col = ? where id = ?" statement
158 argument map (undef, "id") tells to substitute last parameter by results of the
159 $self->id function.
160  
161 =cut
162 sub set_exec_sql {
163 my ($class, $name, $sql, @arg_map) = @_;
164 $class->_do_set_sql($name, $sql, "exec"
165 , sub { return $_[0]->rows; }, @arg_map);
166 }
167  
168 =head2 $class->set_exec_sql($name, $sql, $slice, @arg_map)
169  
170 Wraps C<Ima::DBI> C<set_sql> methods to create C<fetch_$name> function
171 which basically calls C<execute> and C<fetchall_arrayref> on C<sql_$name>
172 handle.
173  
174 For description of C<$slice> parameter see DBI C<fetchall_arrayref> function.
175  
176 C<@arg_map> is described above.
177  
178 =cut
179 sub set_fetch_sql {
180 my ($class, $name, $sql, $slice, @arg_map) = @_;
181 $class->_do_set_sql($name, $sql, "fetch", sub {
182 return $_[0]->fetchall_arrayref($slice);
183 }, @arg_map);
184 }
185  
186 =head2 $class->pg_column_info($column)
187  
188 Returns column information as HASHREF. Currently supported flags are:
189  
190 =over
191  
192 =item type - Returns data type of the column (e.g. integer, text, date etc.).
193  
194 =item is_nullable - Indicates whether the C<$column> can be null.
195  
196 =back
197  
198 =cut
199 sub pg_column_info {
200 my ($class, $col) = @_;
201 return $class->Pg_Column_Infos->{ $col };
202 }
203  
204 1;
205  
206 =head1 AUTHOR
207  
208 Boris Sukholitko
209 CPAN ID: BOSU
210
211 boriss@gmail.com
212
213  
214 =head1 COPYRIGHT
215  
216 This program is free software; you can redistribute
217 it and/or modify it under the same terms as Perl itself.
218  
219 The full text of the license can be found in the
220 LICENSE file included with this module.
221  
222  
223 =head1 SEE ALSO
224  
225 C<Class::DBI::Pg>, C<Class::DBI::Plugin::DateTime::Pg>.
226  
227 =cut
228