is_auto_increment for Firebird, refactor _extra_column_info
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / InterBase.pm
1 package DBIx::Class::Schema::Loader::DBI::InterBase;
2
3 use strict;
4 use warnings;
5 use Class::C3;
6 use base qw/DBIx::Class::Schema::Loader::DBI/;
7 use Carp::Clan qw/^DBIx::Class/;
8
9 our $VERSION = '0.05003';
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBI::InterBase - DBIx::Class::Schema::Loader::DBI
14 Firebird Implementation.
15
16 =head1 DESCRIPTION
17
18 See L<DBIx::Class::Schema::Loader::Base>.
19
20 =cut
21
22 sub _table_pk_info {
23     my ($self, $table) = @_;
24
25     my $dbh = $self->schema->storage->dbh;
26     my $sth = $dbh->prepare(<<'EOF');
27 SELECT iseg.rdb$field_name
28 FROM rdb$relation_constraints rc
29 JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
30 WHERE rc.rdb$constraint_type = 'PRIMARY KEY' and rc.rdb$relation_name = ?
31 ORDER BY iseg.rdb$field_position
32 EOF
33     $sth->execute($table);
34
35     my @keydata;
36
37     while (my ($col) = $sth->fetchrow_array) {
38         s/^\s+//, s/\s+\z// for $col;
39
40         push @keydata, lc $col;
41     }
42
43     return \@keydata;
44 }
45
46 sub _table_fk_info {
47     my ($self, $table) = @_;
48
49     my ($local_cols, $remote_cols, $remote_table, @rels);
50     my $dbh = $self->schema->storage->dbh;
51     my $sth = $dbh->prepare(<<'EOF');
52 SELECT rc.rdb$constraint_name fk, iseg.rdb$field_name local_col, ri.rdb$relation_name remote_tab, riseg.rdb$field_name remote_col
53 FROM rdb$relation_constraints rc
54 JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
55 JOIN rdb$indices li ON rc.rdb$index_name = li.rdb$index_name
56 JOIN rdb$indices ri ON li.rdb$foreign_key = ri.rdb$index_name
57 JOIN rdb$index_segments riseg ON iseg.rdb$field_position = riseg.rdb$field_position and ri.rdb$index_name = riseg.rdb$index_name
58 WHERE rc.rdb$constraint_type = 'FOREIGN KEY' and rc.rdb$relation_name = ?
59 ORDER BY iseg.rdb$field_position
60 EOF
61     $sth->execute($table);
62
63     while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
64         s/^\s+//, s/\s+\z// for $fk, $local_col, $remote_tab, $remote_col;
65
66         push @{$local_cols->{$fk}},  lc $local_col;
67         push @{$remote_cols->{$fk}}, lc $remote_col;
68         $remote_table->{$fk} = $remote_tab;
69     }
70
71     foreach my $fk (keys %$remote_table) {
72         push @rels, {
73             local_columns => $local_cols->{$fk},
74             remote_columns => $remote_cols->{$fk},
75             remote_table => $remote_table->{$fk},
76         };
77     }
78     return \@rels;
79 }
80
81 sub _table_uniq_info {
82     my ($self, $table) = @_;
83
84     my $dbh = $self->schema->storage->dbh;
85     my $sth = $dbh->prepare(<<'EOF');
86 SELECT rc.rdb$constraint_name, iseg.rdb$field_name
87 FROM rdb$relation_constraints rc
88 JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
89 WHERE rc.rdb$constraint_type = 'UNIQUE' and rc.rdb$relation_name = ?
90 ORDER BY iseg.rdb$field_position
91 EOF
92     $sth->execute($table);
93
94     my $constraints;
95     while (my ($constraint_name, $column) = $sth->fetchrow_array) {
96         s/^\s+//, s/\s+\z// for $constraint_name, $column;
97
98         push @{$constraints->{$constraint_name}}, lc $column;
99     }
100
101     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
102     return \@uniqs;
103 }
104
105 sub _extra_column_info {
106     my ($self, $table, $column, $info, $dbi_info) = @_;
107     my %extra_info;
108
109     my $dbh = $self->schema->storage->dbh;
110
111     local $dbh->{LongReadLen} = 100000;
112     local $dbh->{LongTruncOk} = 1;
113
114     my $sth = $dbh->prepare(<<'EOF');
115 SELECT t.rdb$trigger_source
116 FROM rdb$triggers t
117 WHERE t.rdb$relation_name = ?
118 EOF
119
120     $sth->execute($table);
121
122     while (my ($trigger) = $sth->fetchrow_array) {
123         my ($trig_col, $generator) = $trigger =~
124 /new\s*.\s*(\w+) \s* = \s* gen_id\s* \( \s* (\w+)/ix;
125
126         if ($trig_col eq $column) {
127             $extra_info{is_auto_increment} = 1;
128             $extra_info{sequence}          = $generator;
129         }
130     }
131
132     return \%extra_info;
133 }
134
135 =head1 SEE ALSO
136
137 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
138 L<DBIx::Class::Schema::Loader::DBI>
139
140 =head1 AUTHOR
141
142 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
143
144 =head1 LICENSE
145
146 This library is free software; you can redistribute it and/or modify it under
147 the same terms as Perl itself.
148
149 =cut
150
151 1;