Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

OOP Data extractor

by kazak (Beadle)
on Nov 03, 2012 at 16:24 UTC ( [id://1002116]=perlquestion: print w/replies, xml ) Need Help??

kazak has asked for the wisdom of the Perl Monks concerning the following question:

Hi 2 all. I'm trying to wrap into OOP form some chunks of my code. I mean to make a module that will be able to perform different complex operations MySQL DB (based on custom logics) and return data in a simple form (hash or scalar). For example something like:

1. FetchDataset($label_id,$role_id,'dataset') should return a hash where key\value pares will be created from table column names\values returned by query.

2. GetValue('log_file') will check if "dataset" exists and in case of success will return this value.

So this where I got stuck. I'm not sure if I understand how to store data returned from a DB without pre defined keys in "$self" reference like $self = {label_id => shift, role_id => shift,....} which is not to good for me because I'm trying to make this module to be able to retrieve data from different tables.
#!/usr/bin/perl -w package Conf; use DBI; my $dbh = DBI->connect("DBI:mysql:database=framework;host=192.168.1.1; +port=3306;", "test", "test", {'RaiseError' => 1}); sub new { $class = shift; my $self = {}; die "ERROR: Connection to DB failed.Dying." if !$dbh; bless $self, $class; return $self; } sub FetchDataset { my ( $self,$label_id,$role_id,$data_type ) = @_; print "$self\n"; print "$label_id\n"; print "$role_id\n"; print "$data_type\n"; if ($data_type eq "dataset") { $sth = $dbh->prepare(qq{ SELECT `cfg_role_attrs`.`attr +_key` , `cfg_datasets`.`attr_value` FROM `cfg_datasets` LEFT JOIN `c +fg_role_attrs` ON `cfg_datasets`.`attr_id` = `cfg_role_attrs`.`attr_i +d` WHERE `cfg_datasets`.`role_id` = $role_id}); $rv = $sth->execute(); if (!$rv) { die "Building 'global' dataset for: |$label_i +d - $role_id : FAILED\n"; } $self = $sth->fetchrow_hashref(); return $self; } } sub GetValue { my ($key) = @_; return $self->{$key}; } 1;

And this is example how I'm planning to use it:

#!/usr/bin/perl -w use warnings; use strict; use Conf; my $object = Conf->new(); my $d_set = $object->FetchDataset('1','1','dataset'); my $value = $object->GetValue('log_file'); print "Coresponding value is : $value\n";
Thanks in advance for any help.

Replies are listed 'Best First'.
Re: OOP Data extractor
by Anonymous Monk on Nov 03, 2012 at 16:46 UTC
      You see, I'm trying to make this module to move often used functions into one custom module, (in order avoid of repeating them many times in various scripts) so it's not like I'm trying to re invent something but making a wrapper around DBI class with my logics.

        You see, I'm trying to make this module to move often used functions into one custom module, (in order avoid of repeating them many times in various scripts)

        good goal :)

        so it's not like I'm trying to re invent something but making a wrapper around DBI class with my logics.

        See DBIx::Class::Manual::Intro, its like 90% of the wrapper you're creating

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1002116]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found