package DADA::App::DBIHandle;   
use lib qw(../ ../DADA ../perllib);

use DADA::Config qw(!:DEFAULT);  
use DBI; 

if($DADA::Config::CPAN_DEBUG_SETTINGS{DBI} > 0){  
    DBI->trace($DADA::Config::CPAN_DEBUG_SETTINGS{DBI}, $PROGRAM_ERROR_LOG);
}

my $database         = $DADA::Config::SQL_PARAMS{database};
my $dbserver         = $DADA::Config::SQL_PARAMS{dbserver};    	  
my $port             = $DADA::Config::SQL_PARAMS{port};     	  
my $user             = $DADA::Config::SQL_PARAMS{user};         
my $pass             = $DADA::Config::SQL_PARAMS{pass};
my $email_id         = $DADA::Config::SQL_PARAMS{id_column} || 'email_id';

if(!$dbtype){ 
	
	$dbtype = 'mysql' if $DADA::Config::SUBSCRIBER_DB_TYPE eq 'MySQL';
	$dbtype = 'Pg'    if $DADA::Config::SUBSCRIBER_DB_TYPE eq 'PostgreSQL';
	
	$dbtype = 'mysql' if $DADA::Config::ARCHIVE_DB_TYPE eq 'MySQL';
	$dbtype = 'Pg'    if $DADA::Config::ARCHIVE_DB_TYPE eq 'PostgreSQL';
	
	$dbtype = 'mysql' if $DADA::Config::SETTINGS_DB_TYPE eq 'MySQL';
	$dbtype = 'Pg'    if $DADA::Config::SETTINGS_DB_TYPE eq 'PostgreSQL';

 }
 
 
 sub new {

	my $class = shift;
	my %args = (@_); 
	   my $self = {};			
       bless $self, $class;
	   $self->_init(%args); 
	   return $self;

}




sub _init  { 

    my $self = shift; 
    my %args = @_; 
    $self->{sql_params} = {%DADA::Config::SQL_PARAMS}; 
    
    if($DADA::Config::SUBSCRIBER_DB_TYPE =~ m/SQL/i || $DADA::Config::ARCHIVE_DB_TYPE =~ m/SQL/i || $DADA::Config::SETTINGS_DB_TYPE =~ m/SQL/){ 
    	$self->{enabled} = 1; 
    } 
    
    $self->{is_connected} = 0; 
   

}


sub dbh_obj { 

	my $self = shift; 
	# it's all yours, champ.
	
	return undef unless $self->{enabled}; 
	
	if($self->{is_connected} != 1){ 
		$self->connectdb; 
	} else{ 
        # ...
	}
	
	return $self->{dbh}; 
}




sub connectdb {
  
  my $self = shift; 
  
  return undef unless $self->{enabled}; 

  my $data_source = "dbi:$dbtype:dbname=$database;host=$dbserver;port=$port";
  $self->{dbh} = DBI->connect("$data_source", $user, $pass) || die("can't connect to db: $!");
  $self->{is_connected} = 1; 
}




sub disconnectdb {
  my $self = shift; 
  	return undef unless $self->{enabled}; 


  
  $self->{dbh}->disconnect;
  $self->{is_connected} = 0; 

}




sub DESTROY { 

	my $self = shift;
		return undef unless $self->{enabled}; 

	if($self->{dbh}){ 
		$self->disconnectdb ; 
	}else{ 
	}
}


=pod

=head1 COPYRIGHT 

Copyright (c) 1999-2007 Justin Simoni All rights reserved. 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, 
Boston, MA  02111-1307, USA.

=cut 




1;
