Commit | Line | Data |
4cbddf8d |
1 | package DBIx::Class::Schema::Loader::DBI::InterBase; |
2 | |
3 | use strict; |
4 | use warnings; |
4145a6f3 |
5 | use namespace::autoclean; |
4cbddf8d |
6 | use Class::C3; |
7 | use base qw/DBIx::Class::Schema::Loader::DBI/; |
8 | use Carp::Clan qw/^DBIx::Class/; |
4145a6f3 |
9 | use List::Util 'first'; |
4cbddf8d |
10 | |
11 | our $VERSION = '0.05003'; |
12 | |
13 | =head1 NAME |
14 | |
15 | DBIx::Class::Schema::Loader::DBI::InterBase - DBIx::Class::Schema::Loader::DBI |
16 | Firebird Implementation. |
17 | |
18 | =head1 DESCRIPTION |
19 | |
243c6ebc |
20 | See L<DBIx::Class::Schema::Loader::Base> for available options. |
4cbddf8d |
21 | |
22 | =cut |
23 | |
243c6ebc |
24 | sub _is_case_sensitive { 1 } |
25 | |
4cbddf8d |
26 | sub _table_pk_info { |
27 | my ($self, $table) = @_; |
28 | |
29 | my $dbh = $self->schema->storage->dbh; |
30 | my $sth = $dbh->prepare(<<'EOF'); |
31 | SELECT iseg.rdb$field_name |
32 | FROM rdb$relation_constraints rc |
33 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name |
34 | WHERE rc.rdb$constraint_type = 'PRIMARY KEY' and rc.rdb$relation_name = ? |
35 | ORDER BY iseg.rdb$field_position |
36 | EOF |
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 | |
50 | sub _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'); |
56 | SELECT rc.rdb$constraint_name fk, iseg.rdb$field_name local_col, ri.rdb$relation_name remote_tab, riseg.rdb$field_name remote_col |
57 | FROM rdb$relation_constraints rc |
58 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name |
59 | JOIN rdb$indices li ON rc.rdb$index_name = li.rdb$index_name |
60 | JOIN rdb$indices ri ON li.rdb$foreign_key = ri.rdb$index_name |
61 | JOIN rdb$index_segments riseg ON iseg.rdb$field_position = riseg.rdb$field_position and ri.rdb$index_name = riseg.rdb$index_name |
62 | WHERE rc.rdb$constraint_type = 'FOREIGN KEY' and rc.rdb$relation_name = ? |
63 | ORDER BY iseg.rdb$field_position |
64 | EOF |
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 | |
85 | sub _table_uniq_info { |
86 | my ($self, $table) = @_; |
87 | |
88 | my $dbh = $self->schema->storage->dbh; |
89 | my $sth = $dbh->prepare(<<'EOF'); |
90 | SELECT rc.rdb$constraint_name, iseg.rdb$field_name |
91 | FROM rdb$relation_constraints rc |
92 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name |
93 | WHERE rc.rdb$constraint_type = 'UNIQUE' and rc.rdb$relation_name = ? |
94 | ORDER BY iseg.rdb$field_position |
95 | EOF |
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 |
109 | sub _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'); |
119 | SELECT t.rdb$trigger_source |
120 | FROM rdb$triggers t |
121 | WHERE t.rdb$relation_name = ? |
122 | EOF |
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'); |
151 | SELECT rf.rdb$default_source |
152 | FROM rdb$relation_fields rf |
153 | WHERE rf.rdb$relation_name = ? |
154 | AND rf.rdb$field_name = ? |
155 | EOF |
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 | |
173 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
174 | L<DBIx::Class::Schema::Loader::DBI> |
175 | |
176 | =head1 AUTHOR |
177 | |
178 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
179 | |
180 | =head1 LICENSE |
181 | |
182 | This library is free software; you can redistribute it and/or modify it under |
183 | the same terms as Perl itself. |
184 | |
185 | =cut |
186 | |
187 | 1; |