Firebird passes common tests
[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;
4145a6f3 5use namespace::autoclean;
4cbddf8d 6use Class::C3;
7use base qw/DBIx::Class::Schema::Loader::DBI/;
8use Carp::Clan qw/^DBIx::Class/;
4145a6f3 9use List::Util 'first';
4cbddf8d 10
11our $VERSION = '0.05003';
12
13=head1 NAME
14
15DBIx::Class::Schema::Loader::DBI::InterBase - DBIx::Class::Schema::Loader::DBI
16Firebird Implementation.
17
18=head1 DESCRIPTION
19
20See L<DBIx::Class::Schema::Loader::Base>.
21
22=cut
23
24sub _table_pk_info {
25 my ($self, $table) = @_;
26
27 my $dbh = $self->schema->storage->dbh;
28 my $sth = $dbh->prepare(<<'EOF');
29SELECT iseg.rdb$field_name
30FROM rdb$relation_constraints rc
31JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
32WHERE rc.rdb$constraint_type = 'PRIMARY KEY' and rc.rdb$relation_name = ?
33ORDER BY iseg.rdb$field_position
34EOF
35 $sth->execute($table);
36
37 my @keydata;
38
39 while (my ($col) = $sth->fetchrow_array) {
40 s/^\s+//, s/\s+\z// for $col;
41
42 push @keydata, lc $col;
43 }
44
45 return \@keydata;
46}
47
48sub _table_fk_info {
49 my ($self, $table) = @_;
50
51 my ($local_cols, $remote_cols, $remote_table, @rels);
52 my $dbh = $self->schema->storage->dbh;
53 my $sth = $dbh->prepare(<<'EOF');
54SELECT rc.rdb$constraint_name fk, iseg.rdb$field_name local_col, ri.rdb$relation_name remote_tab, riseg.rdb$field_name remote_col
55FROM rdb$relation_constraints rc
56JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
57JOIN rdb$indices li ON rc.rdb$index_name = li.rdb$index_name
58JOIN rdb$indices ri ON li.rdb$foreign_key = ri.rdb$index_name
59JOIN rdb$index_segments riseg ON iseg.rdb$field_position = riseg.rdb$field_position and ri.rdb$index_name = riseg.rdb$index_name
60WHERE rc.rdb$constraint_type = 'FOREIGN KEY' and rc.rdb$relation_name = ?
61ORDER BY iseg.rdb$field_position
62EOF
63 $sth->execute($table);
64
65 while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
66 s/^\s+//, s/\s+\z// for $fk, $local_col, $remote_tab, $remote_col;
67
68 push @{$local_cols->{$fk}}, lc $local_col;
69 push @{$remote_cols->{$fk}}, lc $remote_col;
70 $remote_table->{$fk} = $remote_tab;
71 }
72
73 foreach my $fk (keys %$remote_table) {
74 push @rels, {
75 local_columns => $local_cols->{$fk},
76 remote_columns => $remote_cols->{$fk},
77 remote_table => $remote_table->{$fk},
78 };
79 }
80 return \@rels;
81}
82
83sub _table_uniq_info {
84 my ($self, $table) = @_;
85
86 my $dbh = $self->schema->storage->dbh;
87 my $sth = $dbh->prepare(<<'EOF');
88SELECT rc.rdb$constraint_name, iseg.rdb$field_name
89FROM rdb$relation_constraints rc
90JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
91WHERE rc.rdb$constraint_type = 'UNIQUE' and rc.rdb$relation_name = ?
92ORDER BY iseg.rdb$field_position
93EOF
94 $sth->execute($table);
95
96 my $constraints;
97 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
98 s/^\s+//, s/\s+\z// for $constraint_name, $column;
99
100 push @{$constraints->{$constraint_name}}, lc $column;
101 }
102
103 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
104 return \@uniqs;
105}
106
45be2ce7 107sub _extra_column_info {
108 my ($self, $table, $column, $info, $dbi_info) = @_;
109 my %extra_info;
110
111 my $dbh = $self->schema->storage->dbh;
112
113 local $dbh->{LongReadLen} = 100000;
114 local $dbh->{LongTruncOk} = 1;
115
116 my $sth = $dbh->prepare(<<'EOF');
117SELECT t.rdb$trigger_source
118FROM rdb$triggers t
119WHERE t.rdb$relation_name = ?
120EOF
45be2ce7 121 $sth->execute($table);
122
123 while (my ($trigger) = $sth->fetchrow_array) {
4145a6f3 124 my @trig_cols = $trigger =~ /new\."?(\w+)/ig;
125
126 my ($generator) = $trigger =~
127/(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(\w+)/ix;
45be2ce7 128
4145a6f3 129 if (first { lc($_) eq lc($column) } @trig_cols) {
45be2ce7 130 $extra_info{is_auto_increment} = 1;
131 $extra_info{sequence} = $generator;
132 }
133 }
134
4145a6f3 135# fix up DT types, no idea which other types are fucked
136 if ($info->{data_type} eq '11') {
137 $extra_info{data_type} = 'TIMESTAMP';
138 }
139 elsif ($info->{data_type} eq '9') {
140 $extra_info{data_type} = 'DATE';
141 }
142
143# get default
144 $sth = $dbh->prepare(<<'EOF');
145SELECT rf.rdb$default_source
146FROM rdb$relation_fields rf
147WHERE rf.rdb$relation_name = ?
148AND rf.rdb$field_name = ?
149EOF
150 $sth->execute($table, uc $column);
151 my ($default_src) = $sth->fetchrow_array;
152
153 if ($default_src && (my ($def) = $default_src =~ /^DEFAULT \s+ (\S+)/ix)) {
154 if (my ($quoted) = $def =~ /^'(.*?)'\z/) {
155 $extra_info{default_value} = $quoted;
156 }
157 else {
158 $extra_info{default_value} = $def =~ /^\d/ ? $def : \$def;
159 }
160 }
161
45be2ce7 162 return \%extra_info;
163}
164
4cbddf8d 165=head1 SEE ALSO
166
167L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
168L<DBIx::Class::Schema::Loader::DBI>
169
170=head1 AUTHOR
171
172See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
173
174=head1 LICENSE
175
176This library is free software; you can redistribute it and/or modify it under
177the same terms as Perl itself.
178
179=cut
180
1811;