is_auto_increment for Firebird, refactor _extra_column_info
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / InterBase.pm
CommitLineData
4cbddf8d 1package DBIx::Class::Schema::Loader::DBI::InterBase;
2
3use strict;
4use warnings;
4cbddf8d 5use Class::C3;
6use base qw/DBIx::Class::Schema::Loader::DBI/;
7use Carp::Clan qw/^DBIx::Class/;
8
9our $VERSION = '0.05003';
10
11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBI::InterBase - DBIx::Class::Schema::Loader::DBI
14Firebird Implementation.
15
16=head1 DESCRIPTION
17
18See L<DBIx::Class::Schema::Loader::Base>.
19
20=cut
21
22sub _table_pk_info {
23 my ($self, $table) = @_;
24
25 my $dbh = $self->schema->storage->dbh;
26 my $sth = $dbh->prepare(<<'EOF');
27SELECT iseg.rdb$field_name
28FROM rdb$relation_constraints rc
29JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
30WHERE rc.rdb$constraint_type = 'PRIMARY KEY' and rc.rdb$relation_name = ?
31ORDER BY iseg.rdb$field_position
32EOF
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
46sub _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');
52SELECT rc.rdb$constraint_name fk, iseg.rdb$field_name local_col, ri.rdb$relation_name remote_tab, riseg.rdb$field_name remote_col
53FROM rdb$relation_constraints rc
54JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
55JOIN rdb$indices li ON rc.rdb$index_name = li.rdb$index_name
56JOIN rdb$indices ri ON li.rdb$foreign_key = ri.rdb$index_name
57JOIN rdb$index_segments riseg ON iseg.rdb$field_position = riseg.rdb$field_position and ri.rdb$index_name = riseg.rdb$index_name
58WHERE rc.rdb$constraint_type = 'FOREIGN KEY' and rc.rdb$relation_name = ?
59ORDER BY iseg.rdb$field_position
60EOF
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
81sub _table_uniq_info {
82 my ($self, $table) = @_;
83
84 my $dbh = $self->schema->storage->dbh;
85 my $sth = $dbh->prepare(<<'EOF');
86SELECT rc.rdb$constraint_name, iseg.rdb$field_name
87FROM rdb$relation_constraints rc
88JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
89WHERE rc.rdb$constraint_type = 'UNIQUE' and rc.rdb$relation_name = ?
90ORDER BY iseg.rdb$field_position
91EOF
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
45be2ce7 105sub _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');
115SELECT t.rdb$trigger_source
116FROM rdb$triggers t
117WHERE t.rdb$relation_name = ?
118EOF
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
4cbddf8d 135=head1 SEE ALSO
136
137L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
138L<DBIx::Class::Schema::Loader::DBI>
139
140=head1 AUTHOR
141
142See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
143
144=head1 LICENSE
145
146This library is free software; you can redistribute it and/or modify it under
147the same terms as Perl itself.
148
149=cut
150
1511;