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