-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftable2mysqlimport
executable file
·128 lines (102 loc) · 2.41 KB
/
ftable2mysqlimport
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use Cwd;
use File::Basename;
use Pod::Usage;
=head1 NAME
ftable2mysqlimport
=head1 SYNOPSIS
ftable2mysqlimport [options] <table-file>
-h help
table-file data file with headers
example: ftable2mysqlimport pscalare.sa
Generates <table-file>.txt and <table-file>.sql that allows for import into mySQL.
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $tableFile;
my $sqlFile;
my $dataFile;
my $colNo;
my $headerProcessed;
my @labels;
my @maxFieldLength;
my $addRankFieldName;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help)
|| scalar(@ARGV) != 1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$tableFile = $ARGV[0];
my ($name, $path, $ext) = fileparse($tableFile, '\..*');
$name=~s/-/_/g;
$dataFile = "$name$ext.txt";
open(DATA, ">$dataFile") || die "Cannot open $dataFile";
open(TABLE, $tableFile) || die "Cannot open $tableFile";
$headerProcessed = 0;
while(<TABLE>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
for my $col (0 .. $#fields)
{
push(@labels, $fields[$col]);
push(@maxFieldLength, 1);
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
my $fieldLength;
for my $col (0 .. $#fields-1)
{
if ($fields[$col] eq 'n/a')
{
$fields[$col] = '\\N';
}
$fieldLength = length($fields[$col]);
$maxFieldLength[$col] = max($maxFieldLength[$col], $fieldLength);
print DATA "$fields[$col]\t";
}
if ($fields[$#fields] eq 'n/a')
{
$fields[$#fields] = '\\N';
}
$fieldLength = length($fields[$#fields]);
$maxFieldLength[$#fields] = max($maxFieldLength[$#fields], $fieldLength);
print DATA "$fields[$#fields]\n";
}
}
close(TABLE);
close(DATA);
$sqlFile = "$name$ext.sql";
open(SQL, ">$sqlFile") || die "Cannot open $sqlFile\n";
print SQL "drop table if exists $name;\n";
print SQL "create table $name (\n";
for my $col (0 .. $#labels-1)
{
$labels[$col] =~ s/-/_/g;
#print SQL "\t`$labels[$col]` varchar($maxFieldLength[$col]),\n";
print SQL "\t`$labels[$col]` double,\n";
}
$labels[$#labels] =~ s/-/_/g;
#print SQL "\t`$labels[$#labels]` varchar($maxFieldLength[$#labels]));\n";
print SQL "\t`$labels[$#labels]` double);\n";
close(SQL);