remove all traces of Aranea.
[spider.git] / perl / DXSql.pm
1 #
2 # The master SQL module
3 #
4 # $Id$
5 #
6 # Copyright (c) 2006 Dirk Koopman G1TLH
7 #
8
9 package DXSql;
10
11 use strict;
12
13 use DXDebug;
14
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;
20
21 our $active = 0;
22
23 sub init
24 {
25         my $dsn = shift;
26         return unless $dsn;
27         return $active if $active;
28         
29         eval { 
30                 require DBI;
31         };
32         unless ($@) {
33                 import DBI;
34                 $active++;
35         }
36         undef $@;
37         return $active;
38
39
40 sub new
41 {
42         my $class = shift;
43         my $dsn = shift;
44         my $self;
45         
46         return undef unless $active;
47         my $dbh;
48         my ($style) = $dsn =~ /^dbi:(\w+):/;
49         my $newclass = "DXSql::$style";
50         eval "require $newclass";
51         if ($@) {
52                 $active = 0;
53                 return undef;
54         }
55         return bless {}, $newclass;
56 }
57
58 sub connect
59 {
60         my $self = shift; 
61         my $dsn = shift;
62         my $user = shift;
63         my $passwd = shift;
64         
65         my $dbh;
66         eval {
67                 no strict 'refs';
68                 $dbh = DBI->connect($dsn, $user, $passwd, {AutoCommit => 0}); 
69         };
70         unless ($dbh) {
71                 $active = 0;
72                 return undef;
73         }
74         $self->{dbh} = $dbh;
75         return $self;
76 }
77
78 sub finish
79 {
80         my $self = shift;
81         $self->{dbh}->disconnect;
82
83
84 sub do
85 {
86         my $self = shift;
87         my $s = shift;
88         
89         eval { $self->{dbh}->do($s); }; 
90 }
91
92 sub commit
93 {
94         $_[0]->{dbh}->commit;
95         $_[0]->{dbh}->{AutoCommit} = 0;
96 }
97
98 sub rollback
99 {
100         $_[0]->{dbh}->rollback;
101         $_[0]->{dbh}->{AutoCommit} = 0;
102 }
103
104 sub quote
105 {
106         return $_[0]->{dbh}->quote($_[1]);
107 }
108
109 sub prepare
110 {
111         return $_[0]->{dbh}->prepare($_[1]);
112 }
113
114 sub spot_insert_prepare
115 {
116         my $self = shift;
117         return $self->prepare('insert into spot values(?' . ',?' x 14 . ')');
118 }
119
120 sub spot_insert
121 {
122         my $self = shift;
123         my $spot = shift;
124         my $sth = shift;
125         
126         if ($sth) {
127                 eval {$sth->execute(undef, @$spot)};
128         } else {
129                 my $s = "insert into spot values(NULL,";
130                 $s .= sprintf("%.1f,", $spot->[0]);
131                 $s .= $self->quote($spot->[1]) . "," ;
132                 $s .= $spot->[2] . ',';
133                 $s .= (length $spot->[3] ? $self->quote($spot->[3]) : 'NULL') . ',';
134                 $s .= $self->quote($spot->[4]) . ',';
135                 $s .= $spot->[5] . ',';
136                 $s .= $spot->[6] . ',';
137                 $s .= (length $spot->[7] ? $self->quote($spot->[7]) : 'NULL') . ',';
138                 $s .= $spot->[8] . ',';
139                 $s .= $spot->[9] . ',';
140                 $s .= $spot->[10] . ',';
141                 $s .= $spot->[11] . ',';
142                 $s .= (length $spot->[12] ? $self->quote($spot->[12]) : 'NULL') . ',';
143                 $s .= (length $spot->[13] ? $self->quote($spot->[13]) : 'NULL') . ')';
144                 eval {$self->do($s)};
145         }
146 }
147
148 sub spot_search
149 {
150         my $self = shift;
151         my $expr = shift;
152         my $dayfrom = shift;
153         my $dayto = shift;
154         my $n = shift;
155         my $dxchan = shift;
156         
157         dbg("expr: $expr") if isdbg('search');
158         if ($expr =~ /\$f/) {
159                 $expr =~ s/(?:==|eq)/ = /g;
160                 $expr =~ s/\$f10/spotteritu/g;
161                 $expr =~ s/\$f11/spottercq/g;
162                 $expr =~ s/\$f12/spotstate/g;
163                 $expr =~ s/\$f13/spotterstate/g;
164                 $expr =~ s/\$f0/freq/g;
165                 $expr =~ s/\$f1/spotcall/g;
166                 $expr =~ s/\$f2/time/g;
167                 $expr =~ s/\$f3/comment/g;
168                 $expr =~ s/\$f4/spotter/g;
169                 $expr =~ s/\$f5/spotdxcc/g;
170                 $expr =~ s/\$f6/spotterdxcc/g;
171                 $expr =~ s/\$f7/origin/g;
172                 $expr =~ s/\$f8/spotitu/g;
173                 $expr =~ s/\$f9/spotcq/g;
174                 $expr =~ s/\|\|/ or /g;
175                 $expr =~ s/\&\&/ and /g;
176                 $expr =~ s/=~\s+m\{\^([%\w]+)[^\}]*\}/ like '$1'/g;
177         } else {
178                 $expr = '';
179         }  
180         my $fdays = $dayfrom ? "time <= " . ($main::systime - ($dayfrom * 86400)) : "";
181         my $days = "time >= " . ($main::systime - ($dayto * 86400));
182         my $trange = $fdays ? "($fdays and $days)" : $days;
183         $expr .= $expr ? " and $trange" : $trange;
184     my $s = qq{select freq,spotcall,time,comment,spotter,spotdxcc,spotterdxcc,
185 origin,spotitu,spotcq,spotteritu,spottercq,spotstate,spotterstate from spot
186 where $expr order by time desc limit $n};
187     dbg("sql expr: $s") if isdbg('search');
188         my $ref = $self->{dbh}->selectall_arrayref($s);
189         return @$ref;
190 }
191
192 1;
193