View Single Post
  #3 (permalink)  
Old 04-13-10, 10:57
Gorums Gorums is offline
Registered User
 
Join Date: Mar 2010
Posts: 2
Assigning an object to a variable of instance of a class

This one is an extract of my code:

Code:
use DBI;

require './wacid_constant.pl';
require './wacid_conf.pl';

package WacidCon;

sub new
{
	my($class, $type) = @_;    
	
    bless {
            		"DB"  			=> undef,
			"DB_type"  		=> $type,
            		"DB_host" 		=> undef,
			"DB_port" 		=> undef,
			"DB_username" 		=> undef,
			"lastSQL" 		=> undef,
			"version" 		=> undef,
			"sql_trace" 		=> undef,
        }, $class;
}
  
 sub wacidDBConnect
 {	
	my($self, $database, $host, $port, $username, $password) =@_;
	
	$self->wacidConnectMySql($database, $host, $port, $username, $password) if($self->{DB_type} eq $_DRIVER_MYSQL);
	$self->wacidConnectPg($database, $host, $port, $username, $password) 	if($self->{DB_type} eq $_DRIVER_POSTGRES);	 
}
 
 sub wacidConnectMySql
 {		
	my($self, $database, $host, $port, $username, $password) = @_;
	
    	$self->{DB_name} = $database;
    	$self->{DB_host} = $host;
    	$self->{DB_port} = $port;
    	$self->{DB_username} = $username;
	 
	$datasource = "dbi:mysql:$database:$host:$port";
	
	$self->{DB} = DBI->connect($datasource, $username, $password,  { PrintError => 0})
 || &snort_error(&text('alert_err_connect_mysql', '<font color="red">'.$DBI::errstr.'</font>'));
	print ref($self->{DB});
	$self->{version} = $self->wacidDBGetVersionSchema;
 }
 
 sub wacidConnectPg
 {	
	my($self, $database, $host, $port, $username, $password) = @_;
	
    	$self->{DB_name} = $database;
    	$self->{DB_host} = $host;
    	$self->{DB_port} = $port;
    	$self->{DB_username} = $username;
	
	$datasource = "dbi:pg:$conf_alert_dbname:$conf_alert_host:$conf_alert_port";
	
	$self->{DB} = DBI->connect($datasource, $conf_alert_user, $conf_alert_password, { PrintError => 0}) 
|| &snort_error(&text('alert_err_connect_postgre', '<font color="red">'.$DBI::errstr.'</font>'));
	
	$self->{version} = $self->wacidDBGetVersionSchema;	
 }
 
  
 #Verifica que el driver a usar este disponible por DBI ("mysql", "pg")
 sub wacidDBIsAvilityDriverDBI
 {
	my($self) = shift;
	my @drivers = DBI->available_drivers();
	foreach my $driver (@drivers)
	{
	 	if($driver eq $self->{DB_type})
		{
			return $_TRUE;
		}
	}
	return $_FALSE;
 }
 
 sub wacidDBGetVersionSchema
 {	
	my ($self) = shift;
	
	$sql = "SELECT vseq FROM `schema`" if($self->{DB_type} eq $_DRIVER_MYSQL);		
	$sql = "SELECT vseq FROM schema" if($self->{DB_type} eq $_DRIVER_POSTGRES);		
	#Probar
	$sth = %{$self->{DB}}->prepare($sql);
	$sth->execute || &snort_error(&text('alert_err_execute','<font color="red">'.$DBI::err.':'.$DBI::errstr.'</font>'));
	
	my @array = $sth->fetchrow_array;
	$sth->finish(); 
		
	return $array[0];
 }
 
 sub wacidDBClose
 {
	my($self) = shift;
    	$self->{DB}->disconnect;
 }

Now if there is facilitated he to see my mistake?

I am going to prove what you say to me

Thank you.
Reply With Quote