File: pg2cdbi_viaTT.pl

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (67 lines) | stat: -rwxr-xr-x 1,661 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env perl
# vim: set ft=perl ts=2 expandtab:

use strict;
use SQL::Translator;
use lib './bin';
use Skip_tables qw( @skip_tables );

unless ( scalar @ARGV > 3 ) {
  die "USAGE: $0 <dbname> <username> <password> <sql file> [<sql files>]\n";
}

my $db_name     = shift @ARGV;
my $db_username = shift @ARGV;
my $db_password = shift @ARGV;
my $dsn         = "dbi:Pg:dbname=$db_name";
my $translator  = SQL::Translator->new(
  from          => 'PostgreSQL',
  to            => 'TTSchema',
  filename      => \@ARGV,
  producer_args => {
    tt_vars => {
      db_user     => $db_username,
      db_pass     => $db_password,
      db_dsn      => $dsn,
      baseclass   => 'Bio::Chado::DBI',
      format_fk   => \&generate_file_y,
      format_node => \&format_table_name,
      format_refers => \&format_refers,
    },
    ttfile    => "./bin/dbi.tt2",
  },
  filters       => [
                   sub {
                     my $schema = shift;
                     foreach (@skip_tables) {
                       $schema->drop_table($_);
                     }  
                   },
  ],
);


my $output = $translator->translate or die $translator->error;
print $output;

#stolen from turnkey_generate script
sub generate_file_y {
  my $table_name = shift;
  my $field_name = shift;
  $field_name =~ s/_id$//;
  return $field_name;
}

sub format_table_name {
  my $table_name = shift;
  my $first_char = substr($table_name,0,1);
  substr($table_name,0,1,uc($first_char));
  $table_name =~ s/_(\w)/'_'.uc($1)/eg;
  return $table_name;
}

sub format_refers {
  my $table_name = shift;
  my $field_name = shift;
  return "--$table_name--$field_name--";
}