+12Mar12=======================================================================
+1. Check database operations on MySQL/SQlite3.
+2. Add ipaddr field to spot table if not present.
+3. Add ipaddr field from PC61 to database stored spots.
09Mar12=======================================================================
1. Try to trap unblessed references on receipt of PC34s
15Feb12=======================================================================
my $dbh;
eval {
no strict 'refs';
- $dbh = DBI->connect($dsn, $user, $passwd, {AutoCommit => 0});
+ $dbh = DBI->connect($dsn, $user, $passwd);
};
unless ($dbh) {
$active = 0;
eval { $self->{dbh}->do($s); };
}
+sub begin_work
+{
+ $_[0]->{dbh}->begin_work;
+}
+
sub commit
{
$_[0]->{dbh}->commit;
- $_[0]->{dbh}->{AutoCommit} = 0;
}
sub rollback
{
$_[0]->{dbh}->rollback;
- $_[0]->{dbh}->{AutoCommit} = 0;
}
sub quote
sub spot_insert_prepare
{
my $self = shift;
- return $self->prepare('insert into spot values(?' . ',?' x 14 . ')');
+ return $self->prepare('insert into spot values(?' . ',?' x 15 . ')');
}
sub spot_insert
my $sth = shift;
if ($sth) {
+ push @$spot, undef while @$spot < 15;
+ pop @$spot while @$spot > 15;
eval {$sth->execute(undef, @$spot)};
} else {
my $s = "insert into spot values(NULL,";
$s .= $spot->[10] . ',';
$s .= $spot->[11] . ',';
$s .= (length $spot->[12] ? $self->quote($spot->[12]) : 'NULL') . ',';
- $s .= (length $spot->[13] ? $self->quote($spot->[13]) : 'NULL') . ')';
+ $s .= (length $spot->[13] ? $self->quote($spot->[13]) : 'NULL') . ',';
+ $s .= (length $spot->[14] ? $self->quote($spot->[14]) : 'NULL') . ')';
eval {$self->do($s)};
}
}
my $sth = $self->prepare($s);
$sth->execute;
my @out;
- push @out, $sth->fetchrow_array;
+ while (my @t = $sth->fetchrow_array) {
+ push @out, @t;
+ }
$sth->finish;
return @out;
}
+sub has_ipaddr
+{
+ my $self = shift;
+ my $s = q(PRAGMA table_info(spot));
+ my $sth = $self->prepare($s);
+ $sth->execute;
+ while (my @t = $sth->fetchrow_array) {
+ if ($t[0] eq 'ipaddr') {
+ $sth->finish;
+ return 1;
+ }
+ }
+ $sth->finish;
+ return undef;
+}
+
+sub add_ipaddr
+{
+ my $self = shift;
+ my $s = q(alter table spot add column ipaddr varchar(40));
+ $self->do($s);
+}
+
sub spot_create_table
{
my $self = shift;
spotteritu int,
spottercq int,
spotstate text,
-spotterstate text
+spotterstate text,
+ipaddr text
)};
$self->do($s);
}
return @out;
}
+sub has_ipaddr
+{
+ my $self = shift;
+ my $s = q(describe spot);
+ my $sth = $self->prepare($s);
+ $sth->execute;
+ while (my @t = $sth->fetchrow_array) {
+ if ($t[0] eq 'ipaddr') {
+ $sth->finish;
+ return 1;
+ }
+ }
+ $sth->finish;
+ return undef;
+}
+
+sub add_ipaddr
+{
+ my $self = shift;
+ my $s = q(alter table spot add column ipaddr varchar(40));
+ $self->do($s);
+}
+
sub spot_create_table
{
my $self = shift;
spotteritu tinyint,
spottercq tinyint,
spotstate char(2),
-spotterstate char(2)
+spotterstate char(2),
+ipaddr varchar(40)
)};
$self->do($s);
}
my $now = Julian::Day->alloc(1995, 0);
my $today = Julian::Day->new(time);
my $sth = $main::dbh->spot_insert_prepare;
- $main::dbh->{RaiseError} = 0;
while ($now->cmp($today) <= 0) {
my $fh = $fp->open($now);
if ($fh) {
+# $main::dbh->{RaiseError} = 0;
+ $main::dbh->begin_work;
my $count = 0;
while (<$fh>) {
chomp;
push @s, $b[1] if @s < 7;
push @s, '' if @s < 8;
push @s, @a[0,1], @b[0,1] if @s < 12;
- push @s, $a[2], $a[2] if @s < 14;
+ push @s, $a[2], $a[2] if @s < 14;
}
- push @s, undef while @s < 14;
- pop @s while @s > 14;
-
$main::dbh->spot_insert(\@s, $sth);
$count++;
}
- $main::dbh->commit if $count;
- $main::dbh->{RaiseError} = 0;
+ $main::dbh->commit;
dbg("inserted $count spots from $now->[0] $now->[1]");
$fh->close;
$total += $count;
}
$now = $now->add(1);
}
+ $main::dbh->begin_work;
$main::dbh->spot_add_indexes;
$main::dbh->commit;
- $main::dbh->{RaiseError} = 1;
+# $main::dbh->{RaiseError} = 1;
$t = time - $t;
my $min = int($t / 60);
my $sec = $t % 60;
dbg("$total spots converted in $min:$sec");
}
+ unless ($main::dbh->has_ipaddr) {
+ $main::dbh->add_ipaddr;
+ dbg("added ipaddr field to spot table");
+ }
}
}
my $buf = join('^', @_);
$fp->writeunix($_[2], $buf);
if ($main::dbh) {
+ $main::dbh->begin_work;
$main::dbh->spot_insert(\@_);
$main::dbh->commit;
}