File Coverage

blib/lib/PGObject/Util/Catalog/Types.pm
Criterion Covered Total %
statement 18 29 62.0
branch 0 4 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 41 60.9


line stmt bran cond sub pod time code
1             package PGObject::Util::Catalog::Types;
2              
3 2     2   57973 use 5.008;
  2         10  
  2         134  
4 2     2   15 use strict;
  2         3  
  2         95  
5 2     2   12 use warnings FATAL => 'all';
  2         10  
  2         119  
6              
7 2     2   11 use Carp;
  2         4  
  2         203  
8 2     2   3643 use Memoize;
  2         6813  
  2         108  
9              
10 2     2   13 use Exporter 'import';
  2         4  
  2         452  
11             our @EXPORT_OK = qw(get_attributes);
12              
13             =head1 NAME
14              
15             PGObject::Util::Catalog::Types - Utilities for working with Composite types
16              
17             =head1 VERSION
18              
19             Version 0.02
20              
21             =cut
22              
23             our $VERSION = '0.02';
24              
25              
26              
27             =head1 SYNOPSIS
28              
29             To get the attributes of a composite type:
30              
31             use PGObject::Util::Catalog::Types;
32             my $dbh = DBI->connect(...);
33             my @attributes = get_attributes(
34             typename => 'foo',
35             typeschema => 'public',
36             dbh => $dbh);
37              
38             =head1 OPTIONALLY EXPORTED FUNCTIONS
39              
40             =over
41              
42             =item get_attributes
43              
44             =back
45              
46             =head1 SUBROUTINES/METHODS
47              
48             =head2 get_attributes
49              
50             This takes a list of args as a hash and returns a list of hashrefs, one for
51             each attribute in order. This function may be memoized, so it is not safe to
52             alter any part of the returned data structure in any way.
53              
54             Arguments are:
55              
56             =over
57              
58             =item typename
59              
60             Name of type
61              
62             =item typeschema
63              
64             Schema to look in. This is required
65              
66             =item dbh
67              
68             Database handle to use. This allows different dbs to be used simultaneously
69             with different type definitions.
70              
71             =back
72              
73             Each hashref includes the following fields:
74              
75             =over
76              
77             =item attname
78              
79             The attribute name
80              
81             =item atttype
82              
83             The name of the type
84              
85             =back
86              
87             =cut
88              
89             sub get_attributes {
90 0     0 1   my %args = @_;
91 0 0         croak 'No DB Handle Provided' unless $args{dbh};
92 0 0         croak 'No schema provided' unless $args{typeschema};
93              
94 0           my $query = "
95             SELECT attname, typname as atttype
96             FROM pg_attribute a
97             JOIN pg_class r ON a.attrelid = r.oid
98             JOIN pg_type t ON t.oid = a.atttypid
99             JOIN pg_namespace n ON r.relnamespace = n.oid
100             WHERE relname = ? AND nspname = ? and attnum > 0
101             ORDER BY a.attnum
102             ";
103 0           my $sth = $args{dbh}->prepare($query);
104 0           $sth->execute($args{typename}, $args{typeschema});
105 0           my @cols;
106 0           while (my $ref = $sth->fetchrow_hashref('NAME_lc')){
107 0           push @cols, $ref;
108             }
109 0           return grep {defined $_->{attname}} @cols;
  0            
110             }
111              
112             =head1 AUTHOR
113              
114             Chris Travers, C<< >>
115              
116             =head1 BUGS
117              
118             Please report any bugs or feature requests to C, or through
119             the web interface at L. I will be notified, and then you'll
120             automatically be notified of progress on your bug as I make changes.
121              
122              
123              
124              
125             =head1 SUPPORT
126              
127             You can find documentation for this module with the perldoc command.
128              
129             perldoc PGObject::Util::Catalog::Types
130              
131              
132             You can also look for information at:
133              
134             =over
135              
136             =item * RT: CPAN's request tracker (report bugs here)
137              
138             L
139              
140             =item * AnnoCPAN: Annotated CPAN documentation
141              
142             L
143              
144             =item * CPAN Ratings
145              
146             L
147              
148             =item * Search CPAN
149              
150             L
151              
152             =back
153              
154              
155             =head1 ACKNOWLEDGEMENTS
156              
157              
158             =head1 LICENSE AND COPYRIGHT
159              
160             Copyright 2014 Chris Travers.
161              
162             This program is distributed under the (Revised) BSD License:
163             L
164              
165             Redistribution and use in source and binary forms, with or without
166             modification, are permitted provided that the following conditions
167             are met:
168              
169             * Redistributions of source code must retain the above copyright
170             notice, this list of conditions and the following disclaimer.
171              
172             * Redistributions in binary form must reproduce the above copyright
173             notice, this list of conditions and the following disclaimer in the
174             documentation and/or other materials provided with the distribution.
175              
176             * Neither the name of Chris Travers's Organization
177             nor the names of its contributors may be used to endorse or promote
178             products derived from this software without specific prior written
179             permission.
180              
181             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182             "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183             LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
184             A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
185             OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
186             SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
187             LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
188             DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
189             THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
190             (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
191             OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
192              
193              
194             =cut
195              
196             1; # End of PGObject::Util::Catalog::Types