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