| 1 | package PGObject::Util::Catalog::Types; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Memoize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | 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.03 | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | 3 | our $VERSION = '0.03'; | |||||||||
| 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 | 0 | ▶ | sub get_attributes { | ||||||||
|
Subroutine
get_attributes: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 90 | 0 | my %args = @_; | |||||||||
| 91 | 0 | ▶ | croak 'No DB Handle Provided' unless $args{dbh}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | ▶ | croak 'No schema provided' unless $args{typeschema}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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')){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | push @cols, $ref; | |||||||||
| 108 | } | ||||||||||
| 109 | 0 | return grep {defined $_->{attname}} @cols; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | =head1 AUTHOR | ||||||||||
| 113 | |||||||||||
| 114 | Chris Travers, C<< <chris.travers at gmail.com> >> | ||||||||||
| 115 | |||||||||||
| 116 | =head1 BUGS | ||||||||||
| 117 | |||||||||||
| 118 | Please report any bugs or feature requests to C<bug-pgobject-util-catalog-types at rt.cpan.org>, or through | ||||||||||
| 119 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Util-Catalog-Types>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Util-Catalog-Types> | ||||||||||
| 139 | |||||||||||
| 140 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 141 | |||||||||||
| 142 | L<http://annocpan.org/dist/PGObject-Util-Catalog-Types> | ||||||||||
| 143 | |||||||||||
| 144 | =item * CPAN Ratings | ||||||||||
| 145 | |||||||||||
| 146 | L<http://cpanratings.perl.org/d/PGObject-Util-Catalog-Types> | ||||||||||
| 147 | |||||||||||
| 148 | =item * Search CPAN | ||||||||||
| 149 | |||||||||||
| 150 | L<http://search.cpan.org/dist/PGObject-Util-Catalog-Types/> | ||||||||||
| 151 | |||||||||||
| 152 | =back | ||||||||||
| 153 | |||||||||||
| 154 | |||||||||||
| 155 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 159 | |||||||||||
| 160 | Copyright 2014-2017 Chris Travers. | ||||||||||
| 161 | |||||||||||
| 162 | This program is distributed under the (Revised) BSD License: | ||||||||||
| 163 | L<http://www.opensource.org/licenses/BSD-3-Clause> | ||||||||||
| 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 | 3 | 1; # End of PGObject::Util::Catalog::Types | |||||||||