add DXSql/Pg.pm and CTY-2206 Prefix changes
[spider.git] / perl / DXSql / Pg.pm
1 #
2 # Module for SQLite DXSql variants
3 #
4 # Stuff like table creates and (later) alters
5 #
6 # Copyright (c) 2005 Dirk Koopman G1TLH
7 # Modifications made for Pg, Copyright (c) 2012 Wijnand Modderman-Lenstra PD0MZ
8 #
9
10 package DXSql::Pg;
11
12 use DXDebug;
13
14 use vars qw(@ISA);
15 @ISA = qw{DXSql};
16
17 sub show_tables
18 {
19         my $self = shift;
20         #my $s = q(show tables);
21     my $s = q(SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';);
22         my $sth = $self->prepare($s);
23         $sth->execute;
24         my @out;
25         while (my @t = $sth->fetchrow_array) {
26                 push @out, @t;
27         }
28         $sth->finish;
29         return @out;
30 }
31
32 sub has_ipaddr
33 {
34         my $self = shift;
35         my $s = q(SELECT column_name FROM information_schema.columns WHERE table_name = 'spot');
36         my $sth = $self->prepare($s);
37         $sth->execute;
38         while (my @t = $sth->fetchrow_array) {
39                 if ($t[0] eq 'ipaddr') {
40                         $sth->finish;
41                         return 1;
42                 }
43         }
44         $sth->finish;
45         return undef;
46 }
47
48 sub add_ipaddr
49 {
50         my $self = shift;
51         my $s = q(alter table spot add column ipaddr varchar(40));
52         $self->do($s);
53 }
54
55 sub spot_create_table
56 {
57         my $self = shift;
58     my $s;
59     $s = q{create sequence spot_rowid_seq};
60     $self->do($s);
61         $s = q{create table spot (
62 rowid sequence primary key ,
63 freq real not null,
64 spotcall varchar(14) not null,
65 time int not null,
66 comment varchar(255),
67 spotter varchar(14) not null,
68 spotdxcc smallint,
69 spotterdxcc smallint,
70 origin varchar(14),
71 spotitu tinyint,
72 spotcq tinyint,
73 spotteritu tinyint,
74 spottercq tinyint,
75 spotstate char(2),
76 spotterstate char(2),
77 ipaddr varchar(40)
78 )};
79         $self->do($s);
80     $s = q{alter table spot alter column rowid set default nextval('spot_rowid_seq');};
81     $self->do($s);
82 }
83
84 sub spot_add_indexes
85 {
86         my $self = shift;
87         #dbg('adding spot index ix1');
88         #$self->do('create index spot_ix1 on spot(time desc)');
89         #dbg('adding spot index ix2');
90         #$self->do('create index spot_ix2 on spot(spotcall asc)');
91 }
92
93 sub spot_insert
94 {
95         my $self = shift;
96         my $spot = shift;
97         my $sth = shift;
98         
99         if ($sth) {
100                 push @$spot, undef while  @$spot < 15;
101                 pop @$spot while @$spot > 15;
102                 eval {$sth->execute(undef, @$spot)};
103         } else {
104                 my $s = "insert into spot values(NEXTVAL('spot_rowid_seq'),";
105                 $s .= sprintf("%.1f,", $spot->[0]);
106                 $s .= $self->quote($spot->[1]) . "," ;
107                 $s .= $spot->[2] . ',';
108                 $s .= (length $spot->[3] ? $self->quote($spot->[3]) : 'NULL') . ',';
109                 $s .= $self->quote($spot->[4]) . ',';
110                 $s .= $spot->[5] . ',';
111                 $s .= $spot->[6] . ',';
112                 $s .= (length $spot->[7] ? $self->quote($spot->[7]) : 'NULL') . ',';
113                 $s .= $spot->[8] . ',';
114                 $s .= $spot->[9] . ',';
115                 $s .= $spot->[10] . ',';
116                 $s .= $spot->[11] . ',';
117                 $s .= (length $spot->[12] ? $self->quote($spot->[12]) : 'NULL') . ',';
118                 $s .= (length $spot->[13] ? $self->quote($spot->[13]) : 'NULL') . ',';
119                 $s .= (length $spot->[14] ? $self->quote($spot->[14]) : 'NULL') . ')';
120                 eval {$self->do($s)};
121         }
122 }
123
124
125
126 1;