1 package DBIx::Class::Schema::Loader::DBI::Pg;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
10 use DBIx::Class::Schema::Loader::Table ();
12 our $VERSION = '0.07010';
16 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
17 PostgreSQL Implementation.
21 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
28 $self->next::method(@_);
30 $self->{db_schema} ||= ['public'];
32 if (not defined $self->preserve_case) {
33 $self->preserve_case(0);
35 elsif ($self->preserve_case) {
36 $self->schema->storage->sql_maker->quote_char('"');
37 $self->schema->storage->sql_maker->name_sep('.');
42 my ($self, $opts) = @_;
44 my $dbh = $self->schema->storage->dbh;
48 foreach my $schema (@{ $self->db_schema }) {
49 my @raw_tables = $dbh->tables(undef, $schema, '%', '%');
51 foreach my $table_name (@raw_tables) {
52 my $schema_quoted = $table_name =~ /^"/;
55 $table_name =~ s/^"([^"]+)"\.//;
58 $table_name =~ s/^([^.]+)\.//;
60 my $table = DBIx::Class::Schema::Loader::Table->new(schema => $1);
62 $table_name =~ s/^"([^"]+)"\z/$1/;
64 $table->name($table_name);
70 return $self->_filter_tables(\@tables, $opts);
73 sub _table_uniq_info {
74 my ($self, $table) = @_;
76 # Use the default support if available
77 return $self->next::method($table)
78 if $DBD::Pg::VERSION >= 1.50;
81 my $dbh = $self->schema->storage->dbh;
83 # Most of the SQL here is mostly based on
84 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
85 # John Siracusa to use his superior SQL code :)
87 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
88 q{SELECT attname FROM pg_catalog.pg_attribute
89 WHERE attrelid = ? AND attnum = ?}
92 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
93 q{SELECT x.indrelid, i.relname, x.indkey
96 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
97 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
98 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
99 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
101 x.indisunique = 't' AND
104 con.contype = 'u' AND
109 $uniq_sth->execute($table->schema, $table);
110 while(my $row = $uniq_sth->fetchrow_arrayref) {
111 my ($tableid, $indexname, $col_nums) = @$row;
112 $col_nums =~ s/^\s+//;
113 my @col_nums = split(/\s+/, $col_nums);
116 foreach (@col_nums) {
117 $attr_sth->execute($tableid, $_);
118 my $name_aref = $attr_sth->fetchrow_arrayref;
119 push(@col_names, $name_aref->[0]) if $name_aref;
123 warn "Failed to parse UNIQUE constraint $indexname on $table";
126 push(@uniqs, [ $indexname => \@col_names ]);
134 my ( $self, $table ) = @_;
135 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
136 q{SELECT obj_description(oid)
138 WHERE relname=? AND relnamespace=(
139 SELECT oid FROM pg_namespace WHERE nspname=?)
140 }, undef, $table, $table->schema
142 return $table_comment
146 sub _column_comment {
147 my ( $self, $table, $column_number ) = @_;
148 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
151 WHERE relname=? AND relnamespace=(
152 SELECT oid FROM pg_namespace WHERE nspname=?)
153 }, undef, $table, $table->schema
155 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
159 # Make sure data_type's that don't need it don't have a 'size' column_info, and
160 # set the correct precision for datetime and varbit types.
161 sub _columns_info_for {
165 my $result = $self->next::method(@_);
167 while (my ($col, $info) = each %$result) {
168 my $data_type = $info->{data_type};
170 # these types are fixed size
171 # XXX should this be a negative match?
173 /^(?: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) {
174 delete $info->{size};
176 # for datetime types, check if it has a precision or not
177 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
178 if (lc($data_type) eq 'timestamp without time zone') {
179 $info->{data_type} = 'timestamp';
181 elsif (lc($data_type) eq 'time without time zone') {
182 $info->{data_type} = 'time';
185 my ($precision) = $self->schema->storage->dbh
186 ->selectrow_array(<<EOF, {}, $table, $col);
187 SELECT datetime_precision
188 FROM information_schema.columns
189 WHERE table_name = ? and column_name = ?
192 if ($data_type =~ /^time\b/i) {
193 if ((not $precision) || $precision !~ /^\d/) {
194 delete $info->{size};
197 my ($integer_datetimes) = $self->schema->storage->dbh
198 ->selectrow_array('show integer_datetimes');
201 $integer_datetimes =~ /^on\z/i ? 6 : 10;
203 if ($precision == $max_precision) {
204 delete $info->{size};
207 $info->{size} = $precision;
211 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
212 delete $info->{size};
215 $info->{size} = $precision;
218 elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) {
219 $info->{data_type} = 'varbit' if $data_type =~ /var/i;
221 my ($precision) = $self->schema->storage->dbh
222 ->selectrow_array(<<EOF, {}, $table, $col);
223 SELECT character_maximum_length
224 FROM information_schema.columns
225 WHERE table_name = ? and column_name = ?
228 $info->{size} = $precision if $precision;
230 $info->{size} = 1 if (not $precision) && lc($data_type) eq 'bit';
232 elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $info->{size})) {
235 my ($scale, $precision) = split /,/, $size;
237 $info->{size} = [ $precision, $scale ];
239 elsif (lc($data_type) eq 'character varying') {
240 $info->{data_type} = 'varchar';
242 if (not $info->{size}) {
243 $info->{data_type} = 'text';
244 $info->{original}{data_type} = 'varchar';
247 elsif (lc($data_type) eq 'character') {
248 $info->{data_type} = 'char';
251 my ($typetype) = $self->schema->storage->dbh
252 ->selectrow_array(<<EOF, {}, $data_type);
254 FROM pg_catalog.pg_type
257 if ($typetype && $typetype eq 'e') {
258 # The following will extract a list of allowed values for the
260 my $typevalues = $self->schema->storage->dbh
261 ->selectall_arrayref(<<EOF, {}, $info->{data_type});
263 FROM pg_catalog.pg_enum e
264 JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
268 $info->{extra}{list} = [ map { $_->[0] } @$typevalues ];
270 # Store its original name in extra for SQLT to pick up.
271 $info->{extra}{custom_type_name} = $info->{data_type};
273 $info->{data_type} = 'enum';
275 delete $info->{size};
279 # process SERIAL columns
280 if (ref($info->{default_value}) eq 'SCALAR'
281 && ${ $info->{default_value} } =~ /\bnextval\('([^:]+)'/i) {
282 $info->{is_auto_increment} = 1;
283 $info->{sequence} = $1;
284 delete $info->{default_value};
287 # alias now() to current_timestamp for deploying to other DBs
288 if ((eval { lc ${ $info->{default_value} } }||'') eq 'now()') {
289 # do not use a ref to a constant, that breaks Data::Dump output
290 ${$info->{default_value}} = 'current_timestamp';
293 $info->{original}{default_value} = \$now;
302 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
303 L<DBIx::Class::Schema::Loader::DBI>
307 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
311 This library is free software; you can redistribute it and/or modify it under
312 the same terms as Perl itself.