| 1 | package PGObject::Type::ByteString; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use DBD::Pg qw(:pg_types); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Ref::Util qw(is_ref is_scalarref); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | PGObject::Type::ByteString - Wrapper for raw strings mapping to BYTEA columns. | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 1.2.3 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '1.2.3'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | PGObject::Type::ByteString->register(); | ||
| 26 | |||
| 27 | Now all BYTEA columns will be returned as ByteString objects. | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This module provides a basic wrapper around Perl strings, mapping them to | ||
| 32 | BYTEA database columns. | ||
| 33 | |||
| 34 | PGObject::Type::ByteString objects have no traditional properties, however | ||
| 35 | dereferencing them will yield the raw string they contain. | ||
| 36 | |||
| 37 | =head1 SUBROUTINES/METHODS | ||
| 38 | |||
| 39 | =head2 register([registry => 'default',] [types => $types_arrayref]) | ||
| 40 | |||
| 41 | By default registers type PG_BYTEA. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub register { | ||
| 46 | my $self = shift @_; | ||
| 47 | croak "Can't pass reference to register \n". | ||
| 48 | "Hint: use the class instead of the object" if ref $self; | ||
| 49 | my %args = @_; | ||
| 50 | my $registry = $args{registry}; | ||
| 51 | $registry ||= 'default'; | ||
| 52 | my $types = $args{types}; | ||
| 53 | $types = [ DBD::Pg::PG_BYTEA, 'bytea' ] unless defined $types and @$types; | ||
| 54 | for my $type (@$types){ | ||
| 55 | if ($PGObject::VERSION =~ /^1\./){ | ||
| 56 | my $ret = | ||
| 57 | PGObject->register_type(registry => $registry, pg_type => $type, | ||
| 58 | perl_class => $self); | ||
| 59 | return $ret unless $ret; | ||
| 60 | } else { | ||
| 61 | PGObject::Type::Registry->register_type( | ||
| 62 | registry => $registry, dbtype => $type, apptype => $self | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | return 1; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | =head2 new | ||
| 71 | |||
| 72 | Instantiates and returns a new object, based on the supplied scalar string, | ||
| 73 | or scalar string reference. | ||
| 74 | |||
| 75 | Dereferencing the returned object will yield the original raw string. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub new { | ||
| 80 | my ($class, $value) = @_; | ||
| 81 | my $self; | ||
| 82 | croak 'Must pass scalar or scalar ref' | ||
| 83 | if is_ref($value) && ! is_scalarref($value); | ||
| 84 | if (is_ref($value)) { | ||
| 85 | $self = $value; | ||
| 86 | } else { | ||
| 87 | $self = \$value; | ||
| 88 | } | ||
| 89 | return bless $self, $class; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | =head2 from_db | ||
| 94 | |||
| 95 | Returns a new PGObject::Type::ByteString object based on the supplied | ||
| 96 | scalar string, or scalar string reference. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub from_db { | ||
| 101 | my ($class, $value) = @_; | ||
| 102 | return $class->new($value); | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 to_db | ||
| 106 | |||
| 107 | Returns the object's binary string value packaged as PG_BYTEA type/value | ||
| 108 | hashref suitable for passing to DBD::Pg. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub to_db { | ||
| 113 | my ($self) = @_; | ||
| 114 | # hashref with value and type allows us to tell DBD::Pg to bind to binary | ||
| 115 | return { value => $$self, type => PG_BYTEA }; | ||
| 116 | } | ||
| 117 | |||
| 118 | =head1 AUTHOR | ||
| 119 | |||
| 120 | Erik Huelsmann, C<< <ehuels at gmail.com> >> | ||
| 121 | |||
| 122 | =head1 BUGS | ||
| 123 | |||
| 124 | Please report any bugs or feature requests to | ||
| 125 | C<bug-pgobject-type-bytestring at rt.cpan.org>, or through | ||
| 126 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Type-ByteString>. I will be notified, and then you'll | ||
| 127 | automatically be notified of progress on your bug as I make changes. | ||
| 128 | |||
| 129 | |||
| 130 | =head1 SUPPORT | ||
| 131 | |||
| 132 | You can find documentation for this module with the perldoc command. | ||
| 133 | |||
| 134 | perldoc PGObject::Type::ByteString | ||
| 135 | |||
| 136 | |||
| 137 | You can also look for information at: | ||
| 138 | |||
| 139 | =over 4 | ||
| 140 | |||
| 141 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 142 | |||
| 143 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Type-ByteString> | ||
| 144 | |||
| 145 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 146 | |||
| 147 | L<http://annocpan.org/dist/PGObject-Type-ByteString> | ||
| 148 | |||
| 149 | =item * CPAN Ratings | ||
| 150 | |||
| 151 | L<http://cpanratings.perl.org/d/PGObject-Type-ByteString> | ||
| 152 | |||
| 153 | =item * Search CPAN | ||
| 154 | |||
| 155 | L<http://search.cpan.org/dist/PGObject-Type-ByteString/> | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | |||
| 160 | =head1 ACKNOWLEDGEMENTS | ||
| 161 | |||
| 162 | |||
| 163 | =head1 LICENSE AND COPYRIGHT | ||
| 164 | |||
| 165 | Copyright 2016-2018 Erik Huelsmann | ||
| 166 | |||
| 167 | This program is released under the following license: BSD | ||
| 168 | |||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | 1; # End of PGObject::Type::DateTime | ||