package DADA::App::DBIHandle;   

use lib qw( ../ ../DADA ../perllib

/sw/lib/perl5/5.8.6/darwin-thread-multi-2level
/sw/lib/perl5

);

use DADA::Config;  


my $t = $DADA::Config::DEBUG_TRACE->{DADA_App_DBIHandle}; 


use Carp qw(carp croak); 
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'; # DEV: This isn't set in the Config.pm (?!?!)
my $dbtype    = $DADA::Config::SQL_PARAMS{dbtype};




sub new {

	carp "creating DBIHandle Object"
		if $t; 
		
	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}; 
    
	# We usin' this at all?
    if(
	   $DADA::Config::SUBSCRIBER_DB_TYPE =~ m/SQL/i || 
	   $DADA::Config::ARCHIVE_DB_TYPE    =~ m/SQL/i || 
	   $DADA::Config::SETTINGS_DB_TYPE   =~ m/SQL/i ||
	   $DADA::Config::SESSION_DB_TYPE    =~ m/SQL/i
	
	){ 
		carp "DBI support enabled"
			if $t; 
			
    	$self->{enabled} = 1; 
    } 
	else { 
    	$self->{enabled} = undef; 

		carp "DBI support is disabled"
			if $t;
			
	}
    
	$self->{connect_tries} = 0; 
    $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){ 
		
		carp "Not connected. Connecting..."
			if $t;
			
		$self->connectdb; 
	} else{ 
		# ... ?
	}
	
	return $self->{dbh}; 
}




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

	carp "Connecting to DB..."
		if $t;
		
  my $data_source;
    
	if($self->{connect_tries} >= 10){ 
		croak "We've attempted to connect 10 times, what's going on!? Dying!"; 
	}


  if($dbtype eq 'SQLite'){ 
    
      $data_source = 'dbi:' . $dbtype . ':' . $DADA::Config::FILES . '/' . $database;
      $self->{dbh} = DBI->connect("$data_source", "", "") || croak("can't connect to db: $!");
  
  }
  else { 
  
      $data_source = "dbi:$dbtype:dbname=$database;host=$dbserver;port=$port";
      $self->{dbh} = DBI->connect("$data_source", $user, $pass) || croak("can't connect to db: $!");


    }
    
	foreach(keys %{$DADA::Config::DBI_PARAMS}){ 
	      $self->{dbh}->{$_} = $DADA::Config::DBI_PARAMS->{$_};     
    }

	carp "Connected."
		if $t;
		
      $self->{is_connected} = 1; 
      
	$self->{connect_tries}++; 

}




sub disconnectdb {
  my $self = shift; 
  	return undef unless $self->{enabled}; 
  
	carp "Diconnecting from DB..."
		if $t;
		
  $self->{dbh}->disconnect;
  $self->{is_connected} = 0; 

	carp "Disconnected."
		if $t;
		
}




sub DESTROY { 

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

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


=pod

=head1 COPYRIGHT 

Copyright (c) 1999-2008 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;
