| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

03-11-10, 15:28
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 2
|
|
|
Assigning an object to a variable of instance of a class
|
|
Hola, people I have a great problem, since I do not speak Englishman am going to be concise.
I have a class
Code:
package WacidCon;
sub new
{
my($class, $type) = @_;
bless {
"DB" => undef,
"DB_type" => $type,
"DB_host" => undef,
"DB_port" => undef,
"DB_port" => undef,
"DB_username" => undef,
"lastSQL" => undef,
"version" => undef,
"sql_trace" => undef,
}, $class;
}
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;
}
Error: Function prepare not Defined
$self->{DB}->prepare($sql);
That I want to solve:
To be able refenrenciar to the object returned by the function DBI -> connect () across a variable of instance(authority) defined with the anonymous hash by function new()
Thank you .
|
|

04-09-10, 18:34
|
|
Registered User
|
|
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 696
|
|
You're not showing all your code, so I can only guess what's going wrong.
Where is your statement? Is the DBI->connect actually returning a proper value? If not, perhaps the statement needs to be moved after the package statement. If it's before, it would be importing DBI into the wrong package space.
|
|

04-13-10, 11:57
|
|
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.
|
|

04-28-10, 20:36
|
|
Registered User
|
|
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 696
|
|
Sorry, I haven't been on dbforums in a while.
Yeah, move the package statement to the beginning if you want DBI to be visible to your classes. What's happening is that at the top of the file, the namespace is automatically "main", so use DBI loads the DBI class into main:: DBI. But then package WacidCon changes the default namespace to WacidCon. So now when you say DBI->connect, it looks for WacidCon:: DBI, but that is undefined.
A small issue, most of the time it's preferable to "use" a module rather than "require" it.
So I'd change the first few lines to look like this:
Code:
package WacidCon;
use DBI;
# You can keep the require statements, but this is a better style
# for most modules.
use lib '.'; # Tells Perl to look in the current directory for modules
use wacid_constant; # You'll need to rename it .pm
use wacid_conf;
And rename those .pl files to .pm. See perldoc lib and perldoc perlmod for more information.
|
Last edited by sco08y; 04-28-10 at 20:38.
Reason: Eliminate random smilies.
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|