#! /tools/bin/perl

sub get_admins
{
my $pusher=shift;
my $can_push;

my $query="select users.userid from users where users.type=2 and users.userid = '$pusher' ";
$sth= $dbh->prepare(qq{ $query }) || &leave($DBI::errstr);
$sth->execute() || &leave ($DBI::errstr);
if ($sth->rows) {
	$can_push=1;
} else {
	$can_push=0;
} # End of if
return ($can_push);
} # End of get_admins

sub generic_connect_to_db
{
my $db = shift ;
my ($dsn)="DBI:mysql:${db}:keefer.epinions.com";

DBI->trace(1, "/usr/www/push/dbi_${db}.out");

$dbh = DBI->connect($dsn, "root", "push", {RaiseError => 1 })
            || &choke( "$dbh->errstr - Can't connect to database server: $db");
return $dbh ;
} # End of generic_connect_to_db

sub adduser
{
###
### adds a user and returns the user's id from the users table
### if user exists just retun the user's id
###
my $user = shift ; # user we are working on
my $id ;
my $quoted_user = $dbh->quote($user);

$sth= $dbh->prepare(qq{ select id from users where userid = $quoted_user } ) || &leave($DBI::errstr);
$sth->execute() || &leave ($DBI::errstr);
$id  = $sth->fetchrow_array() ;
$sth->finish;
if (! $id) {
	$sth= $dbh->prepare (qq { insert into users (userid) values ($quoted_user) } ) || &leave ($DBI::errstr);
	$sth->execute() || &leave ($DBI::errstr);
	$sth->finish;
	return $sth->{insertid} ;  
} else {
	return $id ;
} # End of if
} # End of adduser

sub Addfilepath
{
my $path = shift ;
my $id ;
my $qpath= $dbh->quote($path) ;

$sth= $dbh->prepare(qq{ select id from filepaths where path = $qpath } ) || die($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
$id  = $sth->fetchrow_array() ;
$sth->finish;
if (! $id) {
	$sth= $dbh->prepare (qq { insert into filepaths (path) values ($qpath) } ) || die ($sth->errstr);
	$sth->execute() || &leave ($sth->errstr);
	$sth->finish;
	return $sth->{insertid} ;
} else {
	return $id ;
}
} # End of Addfilepath

sub pushhistory
{
my $dbh=shift;
my $fileid=shift;
my $version=shift;
my $uid=shift;
my $unix_time=shift;
my $dest=shift ;
my $commentid=shift;
my $insert;
my $sth;

print "$fileid-$version-$uid-$unix_time-$dest-$commentid<br>";
$insert="insert into pushhistory values (NULL,FROM_UNIXTIME($unix_time),'$uid','$fileid','$version','$commentid','$dest',NULL)";
print "<br>$insert<br>";
$sth= $dbh->prepare (qq { $insert } ) || die ($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
$sth->finish;
} # End of pushhistory

#########################################################################
#### This fills in the pushcomments database. 
#########################################################################

sub addcomment
{
my $comment = shift ;
   $comment = $dbh->quote($comment);

$sth= $dbh->prepare (qq { select id from pushcomments where  comment = $comment limit 1 } ) || die ($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
if ($sth->rows) { 
	my $id = $sth->fetchrow_array() ; # we are only going to get one ...
	return $id ;
}else{ 
	$sth= $dbh->prepare (qq { insert into pushcomments (comment)  values ($comment) } ) || die ($sth->errstr);
	$sth->execute() || &leave ($sth->errstr);
	$sth->finish;
	return $sth->{insertid} ;
} # End of if
} # End of addcomment

#########################################################################
#### This returns the value of a query.
#########################################################################

sub query_db_one
{
my $dbh=shift;
my $query=shift;
my $sth;

$sth= $dbh->prepare (qq { $query } ) || die ($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
my ($value) = $sth->fetchrow_array();
$sth->finish;
return($value);
} # End of query_db_one

#########################################################################
#### This returns the value of a query.
#########################################################################

sub query_db_two
{
my $dbh=shift;
my $query=shift;
my $sth;

$sth= $dbh->prepare (qq { $query } ) || die ($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
my ($value1,$value2) = $sth->fetchrow_array();
$sth->finish;
return($value1,$value2);
} # End of query_db_two

#########################################################################
#### This returns the value of a query.
#########################################################################

sub query_db_array
{
my $dbh=shift;
my $query=shift;
my @array;
my $sth;

$sth= $dbh->prepare (qq { $query } ) || die ($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
while (my ($value) = $sth->fetchrow_array()) {
	push(@array,$value);
} # End of while
$sth->finish;
return(@array);
} # End of query_db_array

#########################################################################
#### This executes a database procedure
#########################################################################

sub sql_exec
{
my $dbh=shift;
my $statement=shift;
my $sth;

$sth= $dbh->prepare (qq { $statement } ) || die ($sth->errstr);
$sth->execute() || &leave ($sth->errstr);
$sth->finish;
} # End of sql_exec

1;
