#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

package Vhffs::Robots::Pgsql;

use Vhffs::Services::Pgsql;
use Vhffs::Constants;
use Vhffs::Functions;

sub update_db
{
	my $main = shift;
	my $pg = shift;

	my $db = pgsql_admin_db_connect( $main );

	if( defined $pg )
	{
		$db->do('ALTER USER '.$pg->get_dbusername.' WITH PASSWORD ?', undef, $pg->get_dbpassword);
		$pg->set_status( Vhffs::Constants::ACTIVATED );
		$pg->commit;
		$pg->blank_password;
	}
}



sub create_db
{
	my $main = shift;
	my $pg = shift;

	my $db = pgsql_admin_db_connect( $main );
	
	if( defined $pg )
	{
		$db->do('CREATE USER '.$pg->get_dbusername.' WITH PASSWORD ?', undef, $pg->get_dbpassword);
		$db->do('CREATE DATABASE '.$pg->get_dbname.' OWNER '.$pg->get_dbusername );
		$pg->set_status( Vhffs::Constants::ACTIVATED );	
		$pg->commit;
		$pg->blank_password;
	}
}

sub delete_db
{
	my $main = shift;
	my $pg = shift;

	my $db = pgsql_admin_db_connect( $main );
	
	if( ( defined $pg ) && ( $pg->get_status == Vhffs::Constants::TO_DELETE )  )
	{
		$pg->add_history("Ok, robots will erase all data");
		$db->do("DROP DATABASE ".$pg->get_dbname );
		$db->do("DROP USER " . $pg->get_dbusername );	
		$pg->add_history("All data have been erased");

		if( $pg->delete < 0 )
		{
			$pg->add_history("Cannot be delete from database");
		}
	}
}



sub pgsql_admin_db_connect
{
    use DBI;
    my $vhffs = shift;
    my $pgsqlconfig = $vhffs->get_config->get_service('pgsql');
    my $dbuser = $pgsqlconfig->{'username'};
    my $dbpass = $pgsqlconfig->{'password'};
    my $dbhost = $pgsqlconfig->{'host'};

   my $dbi;
   if( $dbhost eq "localhost" )
   {
    $dbi = DBI->connect( "DBI:Pg:dbname=postgres",$dbuser,$dbpass ) or return -1;
   }
   else
   {
    $dbi = DBI->connect( "DBI:Pg:dbname=postgres;host=$dbhost",$dbuser,$dbpass ) or return -1;
   }

    return $dbi;
}




1;

