5dd588482da9238d392500fb7b0a1d946c03a466
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Component / QuotedDefault.pm
1 package DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault;
2
3 use strict;
4 use warnings;
5 use Class::C3;
6
7 our $VERSION = '0.07001';
8
9 =head1 NAME
10
11 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault -- Loader::DBI
12 Component to parse quoted default constants and functions
13
14 =head1 DESCRIPTION
15
16 If C<COLUMN_DEF> from L<DBI/column_info> returns character constants quoted,
17 then we need to remove the quotes. This also allows distinguishing between
18 default functions without information schema introspection.
19
20 =cut
21
22 sub _columns_info_for {
23     my $self    = shift;
24     my ($table) = @_;
25
26     my $result = $self->next::method(@_);
27
28     while (my ($col, $info) = each %$result) {
29         if (my $def = $info->{default_value}) {
30             $def =~ s/^\s+//;
31             $def =~ s/\s+\z//;
32
33 # remove Pg typecasts (e.g. 'foo'::character varying) too
34             if ($def =~ /^["'](.*?)['"](?:::[\w\s]+)?\z/) {
35                 $info->{default_value} = $1;
36             }
37             else {
38                 # Some DBs (eg. Pg) put brackets around negative number defaults
39                 $info->{default_value} = $def =~ /^\(?(-?\d.*?)\)?$/ ? $1 : \$def;
40             }
41         }
42     }
43
44     return $result;
45 }
46
47 1;
48
49 =head1 SEE ALSO
50
51 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
52 L<DBIx::Class::Schema::Loader::DBI>
53
54 =head1 AUTHOR
55
56 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
57
58 =head1 LICENSE
59
60 This library is free software; you can redistribute it and/or modify it under
61 the same terms as Perl itself.
62
63 =cut