Firebird: cleanup trigger parsing code a bit
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / InterBase.pm
index e731182..1d62ef9 100644 (file)
@@ -6,6 +6,7 @@ use namespace::autoclean;
 use Class::C3;
 use base qw/DBIx::Class::Schema::Loader::DBI/;
 use Carp::Clan qw/^DBIx::Class/;
+use List::Util 'first';
 
 our $VERSION = '0.05003';
 
@@ -16,10 +17,19 @@ Firebird Implementation.
 
 =head1 DESCRIPTION
 
-See L<DBIx::Class::Schema::Loader::Base>.
+See L<DBIx::Class::Schema::Loader::Base> for available options.
 
 =cut
 
+sub _is_case_sensitive { 1 }
+
+sub _setup {
+    my $self = shift;
+
+    $self->schema->storage->sql_maker->quote_char('"');
+    $self->schema->storage->sql_maker->name_sep('.');
+}
+
 sub _table_pk_info {
     my ($self, $table) = @_;
 
@@ -38,7 +48,7 @@ EOF
     while (my ($col) = $sth->fetchrow_array) {
         s/^\s+//, s/\s+\z// for $col;
 
-        push @keydata, lc $col;
+        push @keydata, $col;
     }
 
     return \@keydata;
@@ -64,8 +74,8 @@ EOF
     while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
         s/^\s+//, s/\s+\z// for $fk, $local_col, $remote_tab, $remote_col;
 
-        push @{$local_cols->{$fk}},  lc $local_col;
-        push @{$remote_cols->{$fk}}, lc $remote_col;
+        push @{$local_cols->{$fk}},  $local_col;
+        push @{$remote_cols->{$fk}}, $remote_col;
         $remote_table->{$fk} = $remote_tab;
     }
 
@@ -96,13 +106,80 @@ EOF
     while (my ($constraint_name, $column) = $sth->fetchrow_array) {
         s/^\s+//, s/\s+\z// for $constraint_name, $column;
 
-        push @{$constraints->{$constraint_name}}, lc $column;
+        push @{$constraints->{$constraint_name}}, $column;
     }
 
     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
     return \@uniqs;
 }
 
+sub _extra_column_info {
+    my ($self, $table, $column, $info, $dbi_info) = @_;
+    my %extra_info;
+
+    my $dbh = $self->schema->storage->dbh;
+
+    local $dbh->{LongReadLen} = 100000;
+    local $dbh->{LongTruncOk} = 1;
+
+    my $sth = $dbh->prepare(<<'EOF');
+SELECT t.rdb$trigger_source
+FROM rdb$triggers t
+WHERE t.rdb$relation_name = ?
+AND t.rdb$system_flag = 0 -- user defined
+AND t.rdb$trigger_type = 1 -- BEFORE INSERT
+EOF
+    $sth->execute($table);
+
+    while (my ($trigger) = $sth->fetchrow_array) {
+        my @trig_cols = map {
+            /^"([^"]+)/ ? $1 : uc($1)
+        } $trigger =~ /new\.("?\w+"?)/ig;
+
+        my ($quoted, $generator) = $trigger =~
+/(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
+
+        if ($generator) {
+            $generator = uc $generator unless $quoted;
+
+            if (first { $_ eq $column } @trig_cols) {
+                $extra_info{is_auto_increment} = 1;
+                $extra_info{sequence}          = $generator;
+                last;
+            }
+        }
+    }
+
+# fix up DT types, no idea which other types are fucked
+    if ($info->{data_type} eq '11') {
+        $extra_info{data_type} = 'TIMESTAMP';
+    }
+    elsif ($info->{data_type} eq '9') {
+        $extra_info{data_type} = 'DATE';
+    }
+
+# get default
+    $sth = $dbh->prepare(<<'EOF');
+SELECT rf.rdb$default_source
+FROM rdb$relation_fields rf
+WHERE rf.rdb$relation_name = ?
+AND rf.rdb$field_name = ?
+EOF
+    $sth->execute($table, $column);
+    my ($default_src) = $sth->fetchrow_array;
+
+    if ($default_src && (my ($def) = $default_src =~ /^DEFAULT \s+ (\S+)/ix)) {
+        if (my ($quoted) = $def =~ /^'(.*?)'\z/) {
+            $extra_info{default_value} = $quoted;
+        }
+        else {
+            $extra_info{default_value} = $def =~ /^\d/ ? $def : \$def;
+        }
+    }
+
+    return \%extra_info;
+}
+
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,