1*5a0e240fSJohn Levon#!/usr/bin/perl -w
2*5a0e240fSJohn Levon
3*5a0e240fSJohn Levonuse strict;
4*5a0e240fSJohn Levonuse warnings;
5*5a0e240fSJohn Levonuse File::Basename;
6*5a0e240fSJohn Levonuse DBI;
7*5a0e240fSJohn Levon
8*5a0e240fSJohn Levonmy $bin_dir = dirname($0);
9*5a0e240fSJohn Levonmy $project = shift;
10*5a0e240fSJohn Levonmy $db_file = shift;
11*5a0e240fSJohn Levonif (!defined($db_file)) {
12*5a0e240fSJohn Levon    print "usage:  $0 <project> <db_file>\n";
13*5a0e240fSJohn Levon    exit(1);
14*5a0e240fSJohn Levon}
15*5a0e240fSJohn Levonmy $insertions = "$bin_dir/$project.insert.return_states";
16*5a0e240fSJohn Levon
17*5a0e240fSJohn Levonmy $db = DBI->connect("dbi:SQLite:$db_file", "", "", {AutoCommit => 0});
18*5a0e240fSJohn Levon$db->do("PRAGMA cache_size = 800000");
19*5a0e240fSJohn Levon$db->do("PRAGMA journal_mode = OFF");
20*5a0e240fSJohn Levon$db->do("PRAGMA count_changes = OFF");
21*5a0e240fSJohn Levon$db->do("PRAGMA temp_store = MEMORY");
22*5a0e240fSJohn Levon$db->do("PRAGMA locking = EXCLUSIVE");
23*5a0e240fSJohn Levon
24*5a0e240fSJohn Levonsub insert_record($$$$$$$)
25*5a0e240fSJohn Levon{
26*5a0e240fSJohn Levon    my $file = shift;
27*5a0e240fSJohn Levon    my $func = shift;
28*5a0e240fSJohn Levon    my $ret = shift;
29*5a0e240fSJohn Levon    my $type = shift;
30*5a0e240fSJohn Levon    my $param = shift;
31*5a0e240fSJohn Levon    my $key = shift;
32*5a0e240fSJohn Levon    my $value = shift;
33*5a0e240fSJohn Levon
34*5a0e240fSJohn Levon#    print "file = '$file' func = '$func' ret = $ret\n";
35*5a0e240fSJohn Levon#    print "type = $type param = $param, key = $key, value = '$value'\n";
36*5a0e240fSJohn Levon#    print "select file, return_id, return, static from return_states where function = '$func' and return = '$ret' and type = 0;'\n";
37*5a0e240fSJohn Levon
38*5a0e240fSJohn Levon    my $sth;
39*5a0e240fSJohn Levon    if ($file ne '') {
40*5a0e240fSJohn Levon        $sth = $db->prepare("select file, return_id, static from return_states where file = ? and function = ? and return = ? and type = 0;");
41*5a0e240fSJohn Levon        $sth->execute($file, $func, $ret);
42*5a0e240fSJohn Levon    } else {
43*5a0e240fSJohn Levon        $sth = $db->prepare("select file, return_id, static from return_states where function = ? and return = ? and type = 0;");
44*5a0e240fSJohn Levon        $sth->execute($func, $ret);
45*5a0e240fSJohn Levon    }
46*5a0e240fSJohn Levon
47*5a0e240fSJohn Levon    my $insert = $db->prepare("insert into return_states values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
48*5a0e240fSJohn Levon    while (my @row = $sth->fetchrow_array()) {
49*5a0e240fSJohn Levon        my $file = $row[0];
50*5a0e240fSJohn Levon        my $return_id = $row[1];
51*5a0e240fSJohn Levon        my $static = $row[2];
52*5a0e240fSJohn Levon
53*5a0e240fSJohn Levon        $insert->execute($file, $func, 0, $return_id, $ret, $static, $type, $param, $key, $value);
54*5a0e240fSJohn Levon    }
55*5a0e240fSJohn Levon}
56*5a0e240fSJohn Levon
57*5a0e240fSJohn Levonmy ($ret, $insert, $file, $func, $type, $param, $key, $value);
58*5a0e240fSJohn Levon
59*5a0e240fSJohn Levonopen(FILE, "<$insertions");
60*5a0e240fSJohn Levonwhile (<FILE>) {
61*5a0e240fSJohn Levon
62*5a0e240fSJohn Levon    if ($_ =~ /^\s*#/) {
63*5a0e240fSJohn Levon        next;
64*5a0e240fSJohn Levon    }
65*5a0e240fSJohn Levon
66*5a0e240fSJohn Levon    ($ret, $insert) = split(/\|/, $_);
67*5a0e240fSJohn Levon
68*5a0e240fSJohn Levon    if ($ret =~ /(.+),\W*(.+),\W*"(.*)"/) {
69*5a0e240fSJohn Levon        $file = $1;
70*5a0e240fSJohn Levon        $func = $2;
71*5a0e240fSJohn Levon        $ret = $3;
72*5a0e240fSJohn Levon    } elsif ($ret =~ /(.+),\W*"(.*)"/) {
73*5a0e240fSJohn Levon        $file = "";
74*5a0e240fSJohn Levon        $func = $1;
75*5a0e240fSJohn Levon        $ret = $2;
76*5a0e240fSJohn Levon    } else {
77*5a0e240fSJohn Levon        next;
78*5a0e240fSJohn Levon    }
79*5a0e240fSJohn Levon
80*5a0e240fSJohn Levon    ($type, $param, $key, $value) = split(/,/, $insert);
81*5a0e240fSJohn Levon
82*5a0e240fSJohn Levon    $type = int($type);
83*5a0e240fSJohn Levon    $param = int($param);
84*5a0e240fSJohn Levon    $key =~ s/^["\s]+|["\s]+$//g;
85*5a0e240fSJohn Levon    $value =~ s/^["\s]+|["\s]+$//g;
86*5a0e240fSJohn Levon    chomp($value);
87*5a0e240fSJohn Levon
88*5a0e240fSJohn Levon    insert_record($file, $func, $ret, $type, $param, $key, $value);
89*5a0e240fSJohn Levon}
90*5a0e240fSJohn Levonclose(FILE);
91*5a0e240fSJohn Levon
92*5a0e240fSJohn Levon$db->commit();
93*5a0e240fSJohn Levon$db->disconnect();
94