16 |
bless($self, $class); |
bless($self, $class); |
17 |
|
|
18 |
# FIX: config params |
# FIX: config params |
19 |
$self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","","") || die $DBI::errstr; |
$self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr; |
20 |
# begin transaction |
# begin transaction |
21 |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
22 |
|
|
58 |
|
|
59 |
my $field = shift; |
my $field = shift; |
60 |
my $index_data = shift; |
my $index_data = shift; |
61 |
my $ident = shift; # e.g. Library ID |
my $ident = shift; # e.g. library id |
62 |
|
|
63 |
if (! $index_data) { |
if (! $index_data) { |
64 |
print STDERR "\$index->insert() -- no value to insert\n"; |
print STDERR "\$index->insert() -- no value to insert\n"; |
72 |
|
|
73 |
my $sql = "select item from $field where upper(item)=upper(?)"; |
my $sql = "select item from $field where upper(item)=upper(?)"; |
74 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
75 |
$sth->execute($index_data) || die "SQL: $sql; ".$self->{dbh}->errstr(); |
$sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr(); |
76 |
if (! $sth->fetchrow_hashref) { |
if (! $sth->fetchrow_hashref) { |
77 |
my $sql = "insert into $field (item,ident,count) values (?,?,?)"; |
my $sql = "insert into $field (item,ident,count) values (?,?,?)"; |
78 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
79 |
$sth->execute($index_data,$ident,1) || die "SQL: $sql; ".$self->{dbh}->errstr(); |
$sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr(); |
80 |
#print STDERR "in index: $index_data\n"; |
#print stderr "in index: $index_data\n"; |
81 |
} else { |
} else { |
82 |
my $sql = "update $field set count = count + 1 where item = ? and ident = ?"; |
my $sql = "update $field set count = count + 1 where item = ? and ident = ?"; |
83 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
84 |
$sth->execute($index_data,$ident) || die "SQL: $sql; ".$self->{dbh}->errstr(); |
$sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr(); |
85 |
} |
} |
86 |
} |
} |
87 |
|
|
88 |
|
sub check { |
89 |
|
my $self = shift; |
90 |
|
|
91 |
|
my $field = shift; |
92 |
|
|
93 |
|
my $sql = "select count(*) from $field"; |
94 |
|
return $self->{dbh}->do($sql); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
sub fetch { |
99 |
|
my $self = shift; |
100 |
|
|
101 |
|
my $field = shift; |
102 |
|
my $what = shift || 'item'; # 'item,ident' |
103 |
|
my $where = shift; |
104 |
|
|
105 |
|
my @sql_args; |
106 |
|
|
107 |
|
my $sql = "select $what from $field"; |
108 |
|
if ($where) { |
109 |
|
$sql .= " where upper(item) like upper(?)||'%'"; |
110 |
|
push @sql_args,$where; |
111 |
|
} |
112 |
|
$sql .= " order by item"; |
113 |
|
|
114 |
|
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
115 |
|
$sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr(); |
116 |
|
my @arr; |
117 |
|
while (my $row = $sth->fetchrow_hashref) { |
118 |
|
push @arr,$row; |
119 |
|
} |
120 |
|
return @arr; |
121 |
|
} |
122 |
|
|
123 |
sub close { |
sub close { |
124 |
my $self = shift; |
my $self = shift; |
125 |
|
|
132 |
|
|
133 |
END { |
END { |
134 |
$Count--; |
$Count--; |
135 |
print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count != 0); |
print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0); |
136 |
# FIX: debug output |
# FIX: debug output |
137 |
print STDERR "usage\ttable\n"; |
# print STDERR "usage\ttable\n"; |
138 |
foreach (keys %Table) { |
# foreach (keys %Table) { |
139 |
print STDERR $Table{$_},"\t$_\n"; |
# print STDERR $Table{$_},"\t$_\n"; |
140 |
} |
# } |
141 |
} |
} |
142 |
|
|
143 |
1; |
1; |