release 0.07018
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Pg.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::Pg;
2
3use strict;
4use warnings;
41968729 5use base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
942bd5e0 9use mro 'c3';
996be9ee 10
18eb280f 11our $VERSION = '0.07018';
32f784fc 12
996be9ee 13=head1 NAME
14
8f9d7ce5 15DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
16PostgreSQL Implementation.
996be9ee 17
996be9ee 18=head1 DESCRIPTION
19
c4a69b87 20See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
996be9ee 21
22=cut
23
24sub _setup {
25 my $self = shift;
26
27 $self->next::method(@_);
bc1cb85e 28
c4a69b87 29 $self->{db_schema} ||= ['public'];
996be9ee 30
bc1cb85e 31 if (not defined $self->preserve_case) {
32 $self->preserve_case(0);
33 }
c930f78b 34 elsif ($self->preserve_case) {
35 $self->schema->storage->sql_maker->quote_char('"');
36 $self->schema->storage->sql_maker->name_sep('.');
37 }
bc1cb85e 38}
fbcfebdd 39
c4a69b87 40sub _system_schemas {
41 my $self = shift;
12b86f07 42
c4a69b87 43 return ($self->next::method(@_), 'pg_catalog');
12b86f07 44}
45
996be9ee 46sub _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;
996be9ee 54
5223f24a 55 # Most of the SQL here is mostly based on
56 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
57 # John Siracusa to use his superior SQL code :)
58
c4a69b87 59 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $self->dbh->prepare(
5223f24a 60 q{SELECT attname FROM pg_catalog.pg_attribute
61 WHERE attrelid = ? AND attnum = ?}
62 );
63
c4a69b87 64 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $self->dbh->prepare(
5223f24a 65 q{SELECT x.indrelid, i.relname, x.indkey
66 FROM
67 pg_catalog.pg_index x
68 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
69 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
70 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
71 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
72 WHERE
73 x.indisunique = 't' AND
74 c.relkind = 'r' AND
75 i.relkind = 'i' AND
76 con.contype = 'u' AND
77 n.nspname = ? AND
78 c.relname = ?}
79 );
80
c4a69b87 81 $uniq_sth->execute($table->schema, $table);
5223f24a 82 while(my $row = $uniq_sth->fetchrow_arrayref) {
83 my ($tableid, $indexname, $col_nums) = @$row;
84 $col_nums =~ s/^\s+//;
85 my @col_nums = split(/\s+/, $col_nums);
86 my @col_names;
87
88 foreach (@col_nums) {
89 $attr_sth->execute($tableid, $_);
90 my $name_aref = $attr_sth->fetchrow_arrayref;
91 push(@col_names, $name_aref->[0]) if $name_aref;
996be9ee 92 }
5223f24a 93
94 if(!@col_names) {
8f9d7ce5 95 warn "Failed to parse UNIQUE constraint $indexname on $table";
996be9ee 96 }
97 else {
5223f24a 98 push(@uniqs, [ $indexname => \@col_names ]);
996be9ee 99 }
100 }
101
102 return \@uniqs;
103}
104
fbcfebdd 105sub _table_comment {
c4a69b87 106 my $self = shift;
107 my ($table) = @_;
108
109 my $table_comment = $self->next::method(@_);
110
111 return $table_comment if $table_comment;
112
113 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema);
114SELECT obj_description(oid)
115FROM pg_class
116WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?)
117EOF
118
fbcfebdd 119 return $table_comment
120}
121
122
123sub _column_comment {
c4a69b87 124 my $self = shift;
125 my ($table, $column_number, $column_name) = @_;
126
127 my $column_comment = $self->next::method(@_);
128
129 return $column_comment if $column_comment;
130
131 my ($table_oid) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema);
132SELECT oid
133FROM pg_class
134WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?)
135EOF
136
137 return $self->dbh->selectrow_array('SELECT col_description(?,?)', {}, $table_oid, $column_number);
fbcfebdd 138}
139
baff904e 140# Make sure data_type's that don't need it don't have a 'size' column_info, and
afb4c5bc 141# set the correct precision for datetime and varbit types.
baff904e 142sub _columns_info_for {
143 my $self = shift;
144 my ($table) = @_;
145
146 my $result = $self->next::method(@_);
147
f80b0ea7 148 while (my ($col, $info) = each %$result) {
149 my $data_type = $info->{data_type};
baff904e 150
151 # these types are fixed size
7640ef4b 152 # XXX should this be a negative match?
baff904e 153 if ($data_type =~
96336646 154/^(?:bigint|int8|bigserial|serial8|bool(?:ean)?|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) {
f80b0ea7 155 delete $info->{size};
baff904e 156 }
afb4c5bc 157# for datetime types, check if it has a precision or not
43b982ea 158 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
f80b0ea7 159 if (lc($data_type) eq 'timestamp without time zone') {
160 $info->{data_type} = 'timestamp';
161 }
8e030521 162 elsif (lc($data_type) eq 'time without time zone') {
163 $info->{data_type} = 'time';
164 }
f80b0ea7 165
baff904e 166 my ($precision) = $self->schema->storage->dbh
167 ->selectrow_array(<<EOF, {}, $table, $col);
168SELECT datetime_precision
169FROM information_schema.columns
170WHERE table_name = ? and column_name = ?
171EOF
172
5f85388e 173 if ($data_type =~ /^time\b/i) {
174 if ((not $precision) || $precision !~ /^\d/) {
f80b0ea7 175 delete $info->{size};
5f85388e 176 }
177 else {
c4a69b87 178 my ($integer_datetimes) = $self->dbh
5f85388e 179 ->selectrow_array('show integer_datetimes');
180
181 my $max_precision =
182 $integer_datetimes =~ /^on\z/i ? 6 : 10;
183
184 if ($precision == $max_precision) {
f80b0ea7 185 delete $info->{size};
5f85388e 186 }
187 else {
f80b0ea7 188 $info->{size} = $precision;
5f85388e 189 }
190 }
191 }
192 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
f80b0ea7 193 delete $info->{size};
baff904e 194 }
195 else {
f80b0ea7 196 $info->{size} = $precision;
baff904e 197 }
198 }
f80b0ea7 199 elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) {
200 $info->{data_type} = 'varbit' if $data_type =~ /var/i;
201
c4a69b87 202 my ($precision) = $self->dbh->selectrow_array(<<EOF, {}, $table, $col);
afb4c5bc 203SELECT character_maximum_length
204FROM information_schema.columns
205WHERE table_name = ? and column_name = ?
206EOF
207
f80b0ea7 208 $info->{size} = $precision if $precision;
209
210 $info->{size} = 1 if (not $precision) && lc($data_type) eq 'bit';
afb4c5bc 211 }
f80b0ea7 212 elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $info->{size})) {
d4d1a665 213 $size =~ s/\s*//g;
214
215 my ($scale, $precision) = split /,/, $size;
216
f80b0ea7 217 $info->{size} = [ $precision, $scale ];
218 }
219 elsif (lc($data_type) eq 'character varying') {
220 $info->{data_type} = 'varchar';
221
8e030521 222 if (not $info->{size}) {
223 $info->{data_type} = 'text';
224 $info->{original}{data_type} = 'varchar';
225 }
f80b0ea7 226 }
227 elsif (lc($data_type) eq 'character') {
228 $info->{data_type} = 'char';
d4d1a665 229 }
12333562 230 else {
231 my ($typetype) = $self->schema->storage->dbh
232 ->selectrow_array(<<EOF, {}, $data_type);
233SELECT typtype
234FROM pg_catalog.pg_type
235WHERE typname = ?
236EOF
c4a69b87 237 if ($typetype && $typetype eq 'e') {
463c86fb 238 # The following will extract a list of allowed values for the
239 # enum.
c4a69b87 240 my $typevalues = $self->dbh
463c86fb 241 ->selectall_arrayref(<<EOF, {}, $info->{data_type});
242SELECT e.enumlabel
243FROM pg_catalog.pg_enum e
244JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
245WHERE t.typname = ?
246EOF
247
248 $info->{extra}{list} = [ map { $_->[0] } @$typevalues ];
249
250 # Store its original name in extra for SQLT to pick up.
251 $info->{extra}{custom_type_name} = $info->{data_type};
252
12333562 253 $info->{data_type} = 'enum';
463c86fb 254
255 delete $info->{size};
12333562 256 }
257 }
258
df956aad 259# process SERIAL columns
12b86f07 260 if (ref($info->{default_value}) eq 'SCALAR'
261 && ${ $info->{default_value} } =~ /\bnextval\('([^:]+)'/i) {
f80b0ea7 262 $info->{is_auto_increment} = 1;
263 $info->{sequence} = $1;
264 delete $info->{default_value};
df956aad 265 }
8e64075f 266
6e566cc4 267# alias now() to current_timestamp for deploying to other DBs
45321eda 268 if ((eval { lc ${ $info->{default_value} } }||'') eq 'now()') {
8a64178e 269 # do not use a ref to a constant, that breaks Data::Dump output
f80b0ea7 270 ${$info->{default_value}} = 'current_timestamp';
701cd3e3 271
272 my $now = 'now()';
273 $info->{original}{default_value} = \$now;
8e64075f 274 }
96336646 275
276# detect 0/1 for booleans and rewrite
277 if ($data_type =~ /^bool/i && exists $info->{default_value}) {
278 if ($info->{default_value} eq '0') {
279 my $false = 'false';
280 $info->{default_value} = \$false;
281 }
282 elsif ($info->{default_value} eq '1') {
283 my $true = 'true';
284 $info->{default_value} = \$true;
285 }
286 }
a8df0345 287 }
288
df956aad 289 return $result;
78b7ccaa 290}
291
996be9ee 292=head1 SEE ALSO
293
294L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
295L<DBIx::Class::Schema::Loader::DBI>
296
be80bba7 297=head1 AUTHOR
298
9cc8e7e1 299See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
be80bba7 300
301=head1 LICENSE
302
303This library is free software; you can redistribute it and/or modify it under
304the same terms as Perl itself.
305
996be9ee 306=cut
307
3081;