-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-to-haskell.pl
80 lines (69 loc) · 1.63 KB
/
mysql-to-haskell.pl
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
# Convert MySQL to Haskell structures
$db = "SIGPLAN-2014-09-04T17-20-27.mysql";
#$db = "X.mysql";
$field_re = '(\'(\\\\.|[^\'\\\\]+)*\'|null)';
# '
#print "$field_re\n"; exit(0);
open(F,$db);
sub process {
my $entry = $_[0];
my @res = ();
while($entry =~ /^($field_re),?/) {
# print "field = $1\n";
my $field = $1;
$entry = $'; # '
# now decode the single field
if ($field =~ /^\'(.*)\'/) {
$field = "\"$1\"";
} elsif ($field =~ /^\-?\d+(.\d+)+$/) {
# okay, leave it alone
} elsif ($field =~ /^null$/) {
$field = "\"null\"";
} else {
die "Opps ($field)\n";
}
push(@res,"$field");
}
die "process: $entry\n" if ($entry ne "");
return ("(" . join (",",@res) .")");
}
sub insert {
my $table = $_[0];
my $entries = $_[1];
# print "($table)\n";
# print "$entries\n\n";;
$_ = $entries;
while (/^,?\(($field_re(,$field_re)*)\)/) {
# print "## $` $& [$1]\n";
$entry = process($1);
if (defined $tables{$table}) {
$tables{$table} .= "\n$entry";
} else {
$tables{$table} = "$entry";
}
$_ = $'; # '
}
die "die insert: $_\n" if ($_ ne "");
}
foreach (<F>) {
if (/^INSERT INTO `(\S+)` VALUES (.*);$/) {
next if (/watchdog/ || /cache/); # has funny unicode chars
# next if ($1 ne "dr_node_revisions");
# print "insert $1 $2\n";
insert($1,$2);
# exit (0);
}
}
print "module X where\n";
foreach(keys %tables) {
# print "(($_))\n";
my $ch = '[';
print "$_ =\n";
foreach(reverse(split(/\n/,$tables{$_}))) {
$_ =~ s/\t/\\t/g;
print " $ch $_\n";
$ch = ",";
}
print " ] -- end of $_\n";
print "\n\n";
}