2 # The master SQL module
6 # Copyright (c) 2006 Dirk Koopman G1TLH
15 use vars qw($VERSION $BRANCH);
16 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
17 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
18 $main::build += $VERSION;
19 $main::branch += $BRANCH;
25 return $active if $active;
43 return undef unless $active;
45 my ($style) = $dsn =~ /^dbi:(\w+):/;
46 my $newclass = "DXSql::$style";
47 eval "require $newclass";
52 return bless {}, $newclass;
65 $dbh = DBI->connect($dsn, $user, $passwd, {AutoCommit => 0});
78 $self->{dbh}->disconnect;
86 eval { $self->{dbh}->do($s); };
92 $_[0]->{dbh}->{AutoCommit} = 0;
97 $_[0]->{dbh}->rollback;
98 $_[0]->{dbh}->{AutoCommit} = 0;
103 return $_[0]->{dbh}->quote($_[1]);
108 return $_[0]->{dbh}->prepare($_[1]);
111 sub spot_insert_prepare
114 return $self->prepare('insert into spot values(?' . ',?' x 14 . ')');
124 eval {$sth->execute(undef, @$spot)};
126 my $s = "insert into spot values(NULL,";
127 $s .= sprintf("%.1f,", $spot->[0]);
128 $s .= $self->quote($spot->[1]) . "," ;
129 $s .= $spot->[2] . ',';
130 $s .= (length $spot->[3] ? $self->quote($spot->[3]) : 'NULL') . ',';
131 $s .= $self->quote($spot->[4]) . ',';
132 $s .= $spot->[5] . ',';
133 $s .= $spot->[6] . ',';
134 $s .= (length $spot->[7] ? $self->quote($spot->[7]) : 'NULL') . ',';
135 $s .= $spot->[8] . ',';
136 $s .= $spot->[9] . ',';
137 $s .= $spot->[10] . ',';
138 $s .= $spot->[11] . ',';
139 $s .= (length $spot->[12] ? $self->quote($spot->[12]) : 'NULL') . ',';
140 $s .= (length $spot->[13] ? $self->quote($spot->[13]) : 'NULL') . ')';
141 eval {$self->do($s)};
154 dbg("expr: $expr") if isdbg('search');
155 if ($expr =~ /\$f/) {
156 $expr =~ s/(?:==|eq)/ = /g;
157 $expr =~ s/\$f10/spotteritu/g;
158 $expr =~ s/\$f11/spottercq/g;
159 $expr =~ s/\$f12/spotstate/g;
160 $expr =~ s/\$f13/spotterstate/g;
161 $expr =~ s/\$f0/freq/g;
162 $expr =~ s/\$f1/spotcall/g;
163 $expr =~ s/\$f2/time/g;
164 $expr =~ s/\$f3/comment/g;
165 $expr =~ s/\$f4/spotter/g;
166 $expr =~ s/\$f5/spotdxcc/g;
167 $expr =~ s/\$f6/spotterdxcc/g;
168 $expr =~ s/\$f7/origin/g;
169 $expr =~ s/\$f8/spotitu/g;
170 $expr =~ s/\$f9/spotcq/g;
171 $expr =~ s/\|\|/ or /g;
172 $expr =~ s/\&\&/ and /g;
173 $expr =~ s/=~\s+m\{\^([%\w]+)[^\}]*\}/ like '$1'/g;
177 my $fdays = $dayfrom ? "time <= " . ($main::systime - ($dayfrom * 86400)) : "";
178 my $days = "time >= " . ($main::systime - ($dayto * 86400));
179 my $trange = $fdays ? "($fdays and $days)" : $days;
180 $expr .= $expr ? " and $trange" : $trange;
181 my $s = qq{select freq,spotcall,time,comment,spotter,spotdxcc,spotterdxcc,
182 origin,spotitu,spotcq,spotteritu,spottercq,spotstate,spotterstate from spot
183 where $expr order by time desc limit $n};
184 dbg("sql expr: $s") if isdbg('search');
185 my $ref = $self->{dbh}->selectall_arrayref($s);