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