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 | |
ffb03c96 |
26 | sub _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 |
33 | sub _table_pk_info { |
34 | my ($self, $table) = @_; |
35 | |
36 | my $dbh = $self->schema->storage->dbh; |
37 | my $sth = $dbh->prepare(<<'EOF'); |
38 | SELECT iseg.rdb$field_name |
39 | FROM rdb$relation_constraints rc |
40 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name |
41 | WHERE rc.rdb$constraint_type = 'PRIMARY KEY' and rc.rdb$relation_name = ? |
42 | ORDER BY iseg.rdb$field_position |
43 | EOF |
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 | |
57 | sub _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'); |
63 | SELECT rc.rdb$constraint_name fk, iseg.rdb$field_name local_col, ri.rdb$relation_name remote_tab, riseg.rdb$field_name remote_col |
64 | FROM rdb$relation_constraints rc |
65 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name |
66 | JOIN rdb$indices li ON rc.rdb$index_name = li.rdb$index_name |
67 | JOIN rdb$indices ri ON li.rdb$foreign_key = ri.rdb$index_name |
68 | JOIN rdb$index_segments riseg ON iseg.rdb$field_position = riseg.rdb$field_position and ri.rdb$index_name = riseg.rdb$index_name |
69 | WHERE rc.rdb$constraint_type = 'FOREIGN KEY' and rc.rdb$relation_name = ? |
70 | ORDER BY iseg.rdb$field_position |
71 | EOF |
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 | |
92 | sub _table_uniq_info { |
93 | my ($self, $table) = @_; |
94 | |
95 | my $dbh = $self->schema->storage->dbh; |
96 | my $sth = $dbh->prepare(<<'EOF'); |
97 | SELECT rc.rdb$constraint_name, iseg.rdb$field_name |
98 | FROM rdb$relation_constraints rc |
99 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name |
100 | WHERE rc.rdb$constraint_type = 'UNIQUE' and rc.rdb$relation_name = ? |
101 | ORDER BY iseg.rdb$field_position |
102 | EOF |
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 |
116 | sub _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'); |
126 | SELECT t.rdb$trigger_source |
127 | FROM rdb$triggers t |
128 | WHERE t.rdb$relation_name = ? |
93e8c513 |
129 | AND t.rdb$system_flag = 0 -- user defined |
130 | AND t.rdb$trigger_type = 1 -- BEFORE INSERT |
45be2ce7 |
131 | EOF |
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'); |
160 | SELECT rf.rdb$default_source |
161 | FROM rdb$relation_fields rf |
162 | WHERE rf.rdb$relation_name = ? |
163 | AND rf.rdb$field_name = ? |
164 | EOF |
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 | |
182 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
183 | L<DBIx::Class::Schema::Loader::DBI> |
184 | |
185 | =head1 AUTHOR |
186 | |
187 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
188 | |
189 | =head1 LICENSE |
190 | |
191 | This library is free software; you can redistribute it and/or modify it under |
192 | the same terms as Perl itself. |
193 | |
194 | =cut |
195 | |
196 | 1; |