#!/usr/bin/perl # $Id: db.pm,v 1.2 2011/07/01 10:59:18 yason Exp $ package db; use DBI; use log; my $DB_SERVER = "pdc"; # from /usr/local/etc/freetds.conf my $DB_USER = "sa"; my $DB_PASS = "aekghbynflvby"; my $dsn = "DBI:Sybase:server=$DB_SERVER"; # exported our $dbh; # functions sub connect() { $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS) or log::critical("Can not connect to database!"); log::main("<<<*** Connected to database server ***>>>"); # select database $dbh->do("use full_print"); } sub disconnect() { $dbh->disconnect(); log::main("<<<*** Disconnected from database ***>>>"); } sub change_status($$) { my $ID = shift; my $value = shift; my $sth = $dbh->prepare("UPDATE upload_files SET Status=$value WHERE ID=$ID"); $sth->execute() or log::error("SQL 'UPDATE upload_files SET Status=$value WHERE Status=0 AND ID=$ID' failed!"); } sub write_size_orig($$) { my $ID = shift; my $size = shift; my $sth = $dbh->prepare("UPDATE upload_files SET size_orig=$size WHERE ID=$ID"); $sth->execute() or log::error("SQL 'UPDATE upload_files SET size_orig=$size WHERE ID=$ID' failed"); } sub write_size_arc($$) { my $ID = shift; my $size = shift; my $sth = $dbh->prepare("UPDATE upload_files SET size_arc=$size WHERE ID=$ID"); $sth->execute() or log::error("SQL 'UPDATE upload_files SET size_arc=$size WHERE ID=$ID' failed"); } sub write_ux_rpath($$) { my $ID = shift; my $value = shift; my $sth = $dbh->prepare("UPDATE upload_files SET ux_rpath='$value' WHERE ID=$ID"); $sth->execute() or log::error("SQL 'UPDATE upload_files SET ux_rpath='$value' WHERE ID=$ID' failed"); } sub get_tx_count($) { my $ID = shift; my $sth = $dbh->prepare("SELECT tx_count FROM upload_files WHERE ID=$ID"); # considering okay to not to exit on sql failure $sth->execute() or log::error("SQL 'SELECT tx_count FROM upload_files WHERE ID=$ID' failed"); # non-scalar context (my $tx_count) = ( $sth->fetchrow_array() ); return($tx_count); } sub increment_tx_count($) { my $ID = shift; my $sth = $dbh->prepare("SELECT tx_count FROM upload_files WHERE ID=$ID"); $sth->execute() or log::error("SQL 'SELECT tx_count FROM upload_files WHERE ID=$ID' failed"); # non-scalar context (my $tx_count) = ( $sth->fetchrow_array() ); $tx_count++; my $sth = $dbh->prepare("UPDATE upload_files SET tx_count=$tx_count WHERE ID=$ID"); $sth->execute() or log::error("SQL 'UPDATE upload_files SET tx_count=$tx_count WHERE ID=$ID' failed"); } return(1);