2 # The master SQL module
6 # Copyright (c) 2006 Dirk Koopman G1TLH
17 use vars qw($VERSION $BRANCH $active);
18 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
19 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
20 $main::build += $VERSION;
21 $main::branch += $BRANCH;
29 return $active if $active;
48 return undef unless $active;
50 my ($style) = $dsn =~ /^dbi:(\w+):/;
51 my $newclass = "DXSql::$style";
52 eval "require $newclass";
57 return bless {}, $newclass;
70 $dbh = DBI->connect($dsn, $user, $passwd, {AutoCommit => 0});
83 $self->{dbh}->disconnect;
91 eval { $self->{dbh}->do($s); };
97 $_[0]->{dbh}->{AutoCommit} = 0;
102 $_[0]->{dbh}->rollback;
103 $_[0]->{dbh}->{AutoCommit} = 0;
108 return $_[0]->{dbh}->quote($_[1]);
113 return $_[0]->{dbh}->prepare($_[1]);
116 sub spot_insert_prepare
119 return $self->prepare('insert into spot values(?' . ',?' x 14 . ')');
129 eval {$sth->execute(undef, @$spot)};
131 my $s = "insert into spot values(NULL,";
132 $s .= sprintf("%.1f,", $spot->[0]);
133 $s .= $self->quote($spot->[1]) . "," ;
134 $s .= $spot->[2] . ',';
135 $s .= (length $spot->[3] ? $self->quote($spot->[3]) : 'NULL') . ',';
136 $s .= $self->quote($spot->[4]) . ',';
137 $s .= $spot->[5] . ',';
138 $s .= $spot->[6] . ',';
139 $s .= (length $spot->[7] ? $self->quote($spot->[7]) : 'NULL') . ',';
140 $s .= $spot->[8] . ',';
141 $s .= $spot->[9] . ',';
142 $s .= $spot->[10] . ',';
143 $s .= $spot->[11] . ',';
144 $s .= (length $spot->[12] ? $self->quote($spot->[12]) : 'NULL') . ',';
145 $s .= (length $spot->[13] ? $self->quote($spot->[13]) : 'NULL') . ')';
146 eval {$self->do($s)};
159 dbg("expr: $expr") if isdbg('search');
160 if ($expr =~ /\$f/) {
161 $expr =~ s/(?:==|eq)/ = /g;
162 $expr =~ s/\$f10/spotteritu/g;
163 $expr =~ s/\$f11/spottercq/g;
164 $expr =~ s/\$f12/spotstate/g;
165 $expr =~ s/\$f13/spotterstate/g;
166 $expr =~ s/\$f0/freq/g;
167 $expr =~ s/\$f1/spotcall/g;
168 $expr =~ s/\$f2/time/g;
169 $expr =~ s/\$f3/comment/g;
170 $expr =~ s/\$f4/spotter/g;
171 $expr =~ s/\$f5/spotdxcc/g;
172 $expr =~ s/\$f6/spotterdxcc/g;
173 $expr =~ s/\$f7/origin/g;
174 $expr =~ s/\$f8/spotitu/g;
175 $expr =~ s/\$f9/spotcq/g;
176 $expr =~ s/\|\|/ or /g;
177 $expr =~ s/\&\&/ and /g;
178 $expr =~ s/=~\s+m\{\^([%\w]+)[^\}]*\}/ like '$1'/g;
182 my $fdays = $dayfrom ? "time <= " . ($main::systime - ($dayfrom * 86400)) : "";
183 my $days = "time >= " . ($main::systime - ($dayto * 86400));
184 my $trange = $fdays ? "($fdays and $days)" : $days;
185 $expr .= $expr ? " and $trange" : $trange;
186 my $s = qq{select freq,spotcall,time,comment,spotter,spotdxcc,spotterdxcc,
187 origin,spotitu,spotcq,spotteritu,spottercq,spotstate,spotterstate from spot
188 where $expr order by time desc limit $n};
189 dbg("sql expr: $s") if isdbg('search');
190 my $ref = $self->{dbh}->selectall_arrayref($s);