How to wrap DBI $sth object?

Discussion in 'Perl Misc' started by Vito Corleone, Mar 11, 2005.

  1. Hi,

    I want to make a module that execute the query and return the $sth in
    wrapped form.

    If you don't use the wrapper it would be like this:
    ## no_wrapper.pl
    my $sth = $dbh->prepare("SELECT member_id, item_name FROM item");
    $sth->execute();
    while (my $row = $sth->fetchrow_arrayref()) {
    print $row->[0];
    pirnt $row->[1];
    }

    I want to wrap that, so I can write it like below:
    ## with_wrapper.pl
    use Item;
    my $it = Item::->new();
    my $obj = $it->get_items(); ## This return $sth in wrapped form
    while (my $item = $obj->each()) {
    print $item->member_id();
    print $item->item_name();
    ## etc
    }

    How should I write Item.pm, so it can be used as the wrapper above?
    Could you please give some simple example, as I don't know where to
    start. And thank you in advance.


    Regards,
    Vito
    Vito Corleone, Mar 11, 2005
    #1
    1. Advertising

  2. * Vito Corleone wrote:
    >
    > I want to make a module that execute the query and return the $sth in
    > wrapped form.
    >
    > If you don't use the wrapper it would be like this:
    > ## no_wrapper.pl
    > my $sth = $dbh->prepare("SELECT member_id, item_name FROM item");
    > $sth->execute();
    > while (my $row = $sth->fetchrow_arrayref()) {
    > print $row->[0];
    > pirnt $row->[1];
    > }
    >
    > I want to wrap that, so I can write it like below:
    > ## with_wrapper.pl
    > use Item;
    > my $it = Item::->new();
    > my $obj = $it->get_items(); ## This return $sth in wrapped form
    > while (my $item = $obj->each()) {
    > print $item->member_id();
    > print $item->item_name();
    > ## etc
    > }


    Eh, what should be your "item": the object which Item->new() returned or
    each element *within* this object. Certainly you can find better names
    for your variables -- or bundle all these functions in only one wrapper
    object (in your example there are three: $it, $obj and $item).

    >
    > How should I write Item.pm, so it can be used as the wrapper above?
    > Could you please give some simple example, as I don't know where to
    > start. And thank you in advance.


    The following code was simply hacked into my newsreader (at the moment I
    have neither a database nor DBI installed). Therefore it is untested.


    #!/usr/bin/perl -w
    use strict;

    # -- Item.pm --

    package Item;

    sub new {
    my $class = shift;
    my $dbh = shift;
    bless { dbh => $dbh }, $class;
    }

    sub get_items {
    my $self = shift;
    my $sql = 'SELECT member_id, item_name FROM item';
    $self->{sth} = $self->{dbh}->prepare( $sql );
    $self->{sth}->execute;
    1;
    }

    sub each {
    my $self = shift;
    my $row = $self->{sth}->fetchrow_arrayref;
    if ( $row ) {
    $self->{ member_id } = $row->[0];
    $self->{ item_name } = $row->[1];
    return 1;
    }
    else {
    $self->{ member_id } = undef;
    $self->{ item_name } = undef;
    return undef;
    }
    }

    sub member_id {
    my $self = shift;
    return $self->{ member_id };
    }

    sub item_name {
    my $self = shift;
    return $self->{ item_name };
    }

    1;
    __END__


    With that, you can fetch your data without cryptic calls now. However,
    this should be a little different from these in your predefined example.


    #!/usr/bin/perl -w
    use strict;
    use DBI;
    use Item;

    my $dbh = DBI->connect( ... );

    my $it = Item->new( $dbh );
    $it->get_items();
    while ( $it->each() ) {
    print $it->member_id();
    print $it->item_name();
    # ...
    }
    __END__


    regards,
    fabian
    Fabian Pilkowski, Mar 11, 2005
    #2
    1. Advertising

  3. Vito Corleone <> wrote in
    news::

    > I want to wrap that, so I can write it like below:
    > ## with_wrapper.pl
    > use Item;
    > my $it = Item::->new();
    > my $obj = $it->get_items(); ## This return $sth in wrapped form
    > while (my $item = $obj->each()) {
    > print $item->member_id();
    > print $item->item_name();
    > ## etc
    > }
    >
    > How should I write Item.pm, so it can be used as the wrapper above?


    I think you are looking for Class:DBI. Search CPAN for it.

    Sinan
    A. Sinan Unur, Mar 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. **ham

    Can i do sth like that in .NET?

    **ham, Dec 1, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    662
  2. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    696
    Chronic Philharmonic
    May 10, 2008
  3. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    228
    Art Werschulz
    Sep 22, 2003
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    245
    Anno Siegel
    Sep 25, 2003
  5. elodie

    $sth->errstr; and $sth->execute;

    elodie, Nov 7, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    207
    elodie
    Nov 7, 2011
Loading...

Share This Page