Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::DBI::Pg; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base 'DBIx::Class::Schema::Loader::DBI'; |
fa994d3c |
6 | use Carp::Clan qw/^DBIx::Class/; |
996be9ee |
7 | use Class::C3; |
8 | |
04e60ed2 |
9 | our $VERSION = '0.04999_14'; |
32f784fc |
10 | |
996be9ee |
11 | =head1 NAME |
12 | |
8f9d7ce5 |
13 | DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI |
14 | PostgreSQL Implementation. |
996be9ee |
15 | |
16 | =head1 SYNOPSIS |
17 | |
18 | package My::Schema; |
19 | use base qw/DBIx::Class::Schema::Loader/; |
20 | |
59cfa251 |
21 | __PACKAGE__->loader_options( debug => 1 ); |
996be9ee |
22 | |
23 | 1; |
24 | |
25 | =head1 DESCRIPTION |
26 | |
27 | See L<DBIx::Class::Schema::Loader::Base>. |
28 | |
29 | =cut |
30 | |
31 | sub _setup { |
32 | my $self = shift; |
33 | |
34 | $self->next::method(@_); |
35 | $self->{db_schema} ||= 'public'; |
36 | } |
37 | |
fbcfebdd |
38 | |
996be9ee |
39 | sub _table_uniq_info { |
40 | my ($self, $table) = @_; |
41 | |
fd589700 |
42 | # Use the default support if available |
43 | return $self->next::method($table) |
79fe0081 |
44 | if $DBD::Pg::VERSION >= 1.50; |
fd589700 |
45 | |
996be9ee |
46 | my @uniqs; |
47 | my $dbh = $self->schema->storage->dbh; |
48 | |
5223f24a |
49 | # Most of the SQL here is mostly based on |
50 | # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from |
51 | # John Siracusa to use his superior SQL code :) |
52 | |
53 | my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare( |
54 | q{SELECT attname FROM pg_catalog.pg_attribute |
55 | WHERE attrelid = ? AND attnum = ?} |
56 | ); |
57 | |
58 | my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare( |
59 | q{SELECT x.indrelid, i.relname, x.indkey |
60 | FROM |
61 | pg_catalog.pg_index x |
62 | JOIN pg_catalog.pg_class c ON c.oid = x.indrelid |
63 | JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid |
64 | JOIN pg_catalog.pg_constraint con ON con.conname = i.relname |
65 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
66 | WHERE |
67 | x.indisunique = 't' AND |
68 | c.relkind = 'r' AND |
69 | i.relkind = 'i' AND |
70 | con.contype = 'u' AND |
71 | n.nspname = ? AND |
72 | c.relname = ?} |
73 | ); |
74 | |
75 | $uniq_sth->execute($self->db_schema, $table); |
76 | while(my $row = $uniq_sth->fetchrow_arrayref) { |
77 | my ($tableid, $indexname, $col_nums) = @$row; |
78 | $col_nums =~ s/^\s+//; |
79 | my @col_nums = split(/\s+/, $col_nums); |
80 | my @col_names; |
81 | |
82 | foreach (@col_nums) { |
83 | $attr_sth->execute($tableid, $_); |
84 | my $name_aref = $attr_sth->fetchrow_arrayref; |
85 | push(@col_names, $name_aref->[0]) if $name_aref; |
996be9ee |
86 | } |
5223f24a |
87 | |
88 | if(!@col_names) { |
8f9d7ce5 |
89 | warn "Failed to parse UNIQUE constraint $indexname on $table"; |
996be9ee |
90 | } |
91 | else { |
5223f24a |
92 | push(@uniqs, [ $indexname => \@col_names ]); |
996be9ee |
93 | } |
94 | } |
95 | |
96 | return \@uniqs; |
97 | } |
98 | |
fbcfebdd |
99 | sub _table_comment { |
100 | my ( $self, $table ) = @_; |
101 | my ($table_comment) = $self->schema->storage->dbh->selectrow_array( |
102 | q{SELECT obj_description(oid) |
103 | FROM pg_class |
104 | WHERE relname=? AND relnamespace=( |
105 | SELECT oid FROM pg_namespace WHERE nspname=?) |
106 | }, undef, $table, $self->db_schema |
107 | ); |
108 | return $table_comment |
109 | } |
110 | |
111 | |
112 | sub _column_comment { |
113 | my ( $self, $table, $column_number ) = @_; |
114 | my ($table_oid) = $self->schema->storage->dbh->selectrow_array( |
115 | q{SELECT oid |
116 | FROM pg_class |
117 | WHERE relname=? AND relnamespace=( |
118 | SELECT oid FROM pg_namespace WHERE nspname=?) |
119 | }, undef, $table, $self->db_schema |
120 | ); |
121 | return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid, |
122 | $column_number ); |
123 | } |
124 | |
baff904e |
125 | # Make sure data_type's that don't need it don't have a 'size' column_info, and |
afb4c5bc |
126 | # set the correct precision for datetime and varbit types. |
baff904e |
127 | sub _columns_info_for { |
128 | my $self = shift; |
129 | my ($table) = @_; |
130 | |
131 | my $result = $self->next::method(@_); |
132 | |
133 | foreach my $col (keys %$result) { |
134 | my $data_type = $result->{$col}{data_type}; |
135 | |
136 | # these types are fixed size |
137 | if ($data_type =~ |
138 | /^(?:bigint|int8|bigserial|serial8|bit|boolean|bool|box|bytea|cidr|circle|date|double precision|float8|inet|integer|int|int4|line|lseg|macaddr|money|path|point|polygon|real|float4|smallint|int2|serial|serial4|text)\z/i) { |
139 | delete $result->{$col}{size}; |
140 | } |
afb4c5bc |
141 | # for datetime types, check if it has a precision or not |
43b982ea |
142 | elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) { |
baff904e |
143 | my ($precision) = $self->schema->storage->dbh |
144 | ->selectrow_array(<<EOF, {}, $table, $col); |
145 | SELECT datetime_precision |
146 | FROM information_schema.columns |
147 | WHERE table_name = ? and column_name = ? |
148 | EOF |
149 | |
5f85388e |
150 | if ($data_type =~ /^time\b/i) { |
151 | if ((not $precision) || $precision !~ /^\d/) { |
152 | delete $result->{$col}{size}; |
153 | } |
154 | else { |
155 | my ($integer_datetimes) = $self->schema->storage->dbh |
156 | ->selectrow_array('show integer_datetimes'); |
157 | |
158 | my $max_precision = |
159 | $integer_datetimes =~ /^on\z/i ? 6 : 10; |
160 | |
161 | if ($precision == $max_precision) { |
162 | delete $result->{$col}{size}; |
163 | } |
164 | else { |
165 | $result->{$col}{size} = $precision; |
166 | } |
167 | } |
168 | } |
169 | elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) { |
baff904e |
170 | delete $result->{$col}{size}; |
171 | } |
172 | else { |
173 | $result->{$col}{size} = $precision; |
174 | } |
175 | } |
afb4c5bc |
176 | elsif ($data_type =~ /^(?:bit varying|varbit)\z/i) { |
177 | my ($precision) = $self->schema->storage->dbh |
178 | ->selectrow_array(<<EOF, {}, $table, $col); |
179 | SELECT character_maximum_length |
180 | FROM information_schema.columns |
181 | WHERE table_name = ? and column_name = ? |
182 | EOF |
183 | |
184 | $result->{$col}{size} = $precision; |
185 | } |
d4d1a665 |
186 | elsif ($data_type =~ /^(?:numeric|decimal)\z/i) { |
187 | my $size = $result->{$col}{size}; |
188 | $size =~ s/\s*//g; |
189 | |
190 | my ($scale, $precision) = split /,/, $size; |
191 | |
192 | $result->{$col}{size} = [ $precision, $scale ]; |
193 | } |
baff904e |
194 | } |
195 | |
196 | return $result; |
197 | } |
198 | |
a8df0345 |
199 | sub _extra_column_info { |
78b7ccaa |
200 | my ($self, $info) = @_; |
a8df0345 |
201 | my %extra_info; |
78b7ccaa |
202 | |
a8df0345 |
203 | if ($info->{COLUMN_DEF} && $info->{COLUMN_DEF} =~ /\bnextval\(/i) { |
204 | $extra_info{is_auto_increment} = 1; |
205 | } |
206 | |
207 | return \%extra_info; |
78b7ccaa |
208 | } |
209 | |
996be9ee |
210 | =head1 SEE ALSO |
211 | |
212 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
213 | L<DBIx::Class::Schema::Loader::DBI> |
214 | |
be80bba7 |
215 | =head1 AUTHOR |
216 | |
9cc8e7e1 |
217 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
218 | |
219 | =head1 LICENSE |
220 | |
221 | This library is free software; you can redistribute it and/or modify it under |
222 | the same terms as Perl itself. |
223 | |
996be9ee |
224 | =cut |
225 | |
226 | 1; |