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 | |
9990e58f |
12 | our $VERSION = '0.07000'; |
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(@_); |
bc1cb85e |
38 | |
996be9ee |
39 | $self->{db_schema} ||= 'public'; |
996be9ee |
40 | |
bc1cb85e |
41 | if (not defined $self->preserve_case) { |
42 | $self->preserve_case(0); |
43 | } |
44 | } |
fbcfebdd |
45 | |
996be9ee |
46 | sub _table_uniq_info { |
47 | my ($self, $table) = @_; |
48 | |
fd589700 |
49 | # Use the default support if available |
50 | return $self->next::method($table) |
79fe0081 |
51 | if $DBD::Pg::VERSION >= 1.50; |
fd589700 |
52 | |
996be9ee |
53 | my @uniqs; |
54 | my $dbh = $self->schema->storage->dbh; |
55 | |
5223f24a |
56 | # Most of the SQL here is mostly based on |
57 | # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from |
58 | # John Siracusa to use his superior SQL code :) |
59 | |
60 | my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare( |
61 | q{SELECT attname FROM pg_catalog.pg_attribute |
62 | WHERE attrelid = ? AND attnum = ?} |
63 | ); |
64 | |
65 | my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare( |
66 | q{SELECT x.indrelid, i.relname, x.indkey |
67 | FROM |
68 | pg_catalog.pg_index x |
69 | JOIN pg_catalog.pg_class c ON c.oid = x.indrelid |
70 | JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid |
71 | JOIN pg_catalog.pg_constraint con ON con.conname = i.relname |
72 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
73 | WHERE |
74 | x.indisunique = 't' AND |
75 | c.relkind = 'r' AND |
76 | i.relkind = 'i' AND |
77 | con.contype = 'u' AND |
78 | n.nspname = ? AND |
79 | c.relname = ?} |
80 | ); |
81 | |
82 | $uniq_sth->execute($self->db_schema, $table); |
83 | while(my $row = $uniq_sth->fetchrow_arrayref) { |
84 | my ($tableid, $indexname, $col_nums) = @$row; |
85 | $col_nums =~ s/^\s+//; |
86 | my @col_nums = split(/\s+/, $col_nums); |
87 | my @col_names; |
88 | |
89 | foreach (@col_nums) { |
90 | $attr_sth->execute($tableid, $_); |
91 | my $name_aref = $attr_sth->fetchrow_arrayref; |
92 | push(@col_names, $name_aref->[0]) if $name_aref; |
996be9ee |
93 | } |
5223f24a |
94 | |
95 | if(!@col_names) { |
8f9d7ce5 |
96 | warn "Failed to parse UNIQUE constraint $indexname on $table"; |
996be9ee |
97 | } |
98 | else { |
5223f24a |
99 | push(@uniqs, [ $indexname => \@col_names ]); |
996be9ee |
100 | } |
101 | } |
102 | |
103 | return \@uniqs; |
104 | } |
105 | |
fbcfebdd |
106 | sub _table_comment { |
107 | my ( $self, $table ) = @_; |
108 | my ($table_comment) = $self->schema->storage->dbh->selectrow_array( |
109 | q{SELECT obj_description(oid) |
110 | FROM pg_class |
111 | WHERE relname=? AND relnamespace=( |
112 | SELECT oid FROM pg_namespace WHERE nspname=?) |
113 | }, undef, $table, $self->db_schema |
114 | ); |
115 | return $table_comment |
116 | } |
117 | |
118 | |
119 | sub _column_comment { |
120 | my ( $self, $table, $column_number ) = @_; |
121 | my ($table_oid) = $self->schema->storage->dbh->selectrow_array( |
122 | q{SELECT oid |
123 | FROM pg_class |
124 | WHERE relname=? AND relnamespace=( |
125 | SELECT oid FROM pg_namespace WHERE nspname=?) |
126 | }, undef, $table, $self->db_schema |
127 | ); |
128 | return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid, |
129 | $column_number ); |
130 | } |
131 | |
baff904e |
132 | # Make sure data_type's that don't need it don't have a 'size' column_info, and |
afb4c5bc |
133 | # set the correct precision for datetime and varbit types. |
baff904e |
134 | sub _columns_info_for { |
135 | my $self = shift; |
136 | my ($table) = @_; |
137 | |
138 | my $result = $self->next::method(@_); |
139 | |
f80b0ea7 |
140 | while (my ($col, $info) = each %$result) { |
141 | my $data_type = $info->{data_type}; |
baff904e |
142 | |
143 | # these types are fixed size |
144 | if ($data_type =~ |
f80b0ea7 |
145 | /^(?:bigint|int8|bigserial|serial8|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) { |
146 | delete $info->{size}; |
baff904e |
147 | } |
afb4c5bc |
148 | # for datetime types, check if it has a precision or not |
43b982ea |
149 | elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) { |
f80b0ea7 |
150 | if (lc($data_type) eq 'timestamp without time zone') { |
151 | $info->{data_type} = 'timestamp'; |
152 | } |
8e030521 |
153 | elsif (lc($data_type) eq 'time without time zone') { |
154 | $info->{data_type} = 'time'; |
155 | } |
f80b0ea7 |
156 | |
baff904e |
157 | my ($precision) = $self->schema->storage->dbh |
158 | ->selectrow_array(<<EOF, {}, $table, $col); |
159 | SELECT datetime_precision |
160 | FROM information_schema.columns |
161 | WHERE table_name = ? and column_name = ? |
162 | EOF |
163 | |
5f85388e |
164 | if ($data_type =~ /^time\b/i) { |
165 | if ((not $precision) || $precision !~ /^\d/) { |
f80b0ea7 |
166 | delete $info->{size}; |
5f85388e |
167 | } |
168 | else { |
169 | my ($integer_datetimes) = $self->schema->storage->dbh |
170 | ->selectrow_array('show integer_datetimes'); |
171 | |
172 | my $max_precision = |
173 | $integer_datetimes =~ /^on\z/i ? 6 : 10; |
174 | |
175 | if ($precision == $max_precision) { |
f80b0ea7 |
176 | delete $info->{size}; |
5f85388e |
177 | } |
178 | else { |
f80b0ea7 |
179 | $info->{size} = $precision; |
5f85388e |
180 | } |
181 | } |
182 | } |
183 | elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) { |
f80b0ea7 |
184 | delete $info->{size}; |
baff904e |
185 | } |
186 | else { |
f80b0ea7 |
187 | $info->{size} = $precision; |
baff904e |
188 | } |
189 | } |
f80b0ea7 |
190 | elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) { |
191 | $info->{data_type} = 'varbit' if $data_type =~ /var/i; |
192 | |
afb4c5bc |
193 | my ($precision) = $self->schema->storage->dbh |
194 | ->selectrow_array(<<EOF, {}, $table, $col); |
195 | SELECT character_maximum_length |
196 | FROM information_schema.columns |
197 | WHERE table_name = ? and column_name = ? |
198 | EOF |
199 | |
f80b0ea7 |
200 | $info->{size} = $precision if $precision; |
201 | |
202 | $info->{size} = 1 if (not $precision) && lc($data_type) eq 'bit'; |
afb4c5bc |
203 | } |
f80b0ea7 |
204 | elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $info->{size})) { |
d4d1a665 |
205 | $size =~ s/\s*//g; |
206 | |
207 | my ($scale, $precision) = split /,/, $size; |
208 | |
f80b0ea7 |
209 | $info->{size} = [ $precision, $scale ]; |
210 | } |
211 | elsif (lc($data_type) eq 'character varying') { |
212 | $info->{data_type} = 'varchar'; |
213 | |
8e030521 |
214 | if (not $info->{size}) { |
215 | $info->{data_type} = 'text'; |
216 | $info->{original}{data_type} = 'varchar'; |
217 | } |
f80b0ea7 |
218 | } |
219 | elsif (lc($data_type) eq 'character') { |
220 | $info->{data_type} = 'char'; |
d4d1a665 |
221 | } |
baff904e |
222 | |
df956aad |
223 | # process SERIAL columns |
f80b0ea7 |
224 | if (ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } =~ /\bnextval\(['"](\w+)/i) { |
225 | $info->{is_auto_increment} = 1; |
226 | $info->{sequence} = $1; |
227 | delete $info->{default_value}; |
df956aad |
228 | } |
8e64075f |
229 | |
6e566cc4 |
230 | # alias now() to current_timestamp for deploying to other DBs |
f80b0ea7 |
231 | if (eval { lc ${ $info->{default_value} }||'' eq 'now()' }) { |
8a64178e |
232 | # do not use a ref to a constant, that breaks Data::Dump output |
f80b0ea7 |
233 | ${$info->{default_value}} = 'current_timestamp'; |
8e64075f |
234 | } |
a8df0345 |
235 | } |
236 | |
df956aad |
237 | return $result; |
78b7ccaa |
238 | } |
239 | |
996be9ee |
240 | =head1 SEE ALSO |
241 | |
242 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
243 | L<DBIx::Class::Schema::Loader::DBI> |
244 | |
be80bba7 |
245 | =head1 AUTHOR |
246 | |
9cc8e7e1 |
247 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
248 | |
249 | =head1 LICENSE |
250 | |
251 | This library is free software; you can redistribute it and/or modify it under |
252 | the same terms as Perl itself. |
253 | |
996be9ee |
254 | =cut |
255 | |
256 | 1; |