Don't know if this is right place for posting this - so you might consider moving it ...

This is some module I'm working on. It's sort of DB abstraction - it (dis)connects and generates insert/update SQL for you. No fancy things - used an older version of it in few web based projects to speed up work a little bit as I mostly need simple SQL insert/update queries for those.

I would appreciate if anybody would comment on it. How could I improve it - what am I doing good/wrong and why ... guess I will improve my Perl skills along the way :)

package DBIx::Handy; use strict; use DBI; sub new { my $class = shift; my %config = @_; my $self = {'_CONFIG' => \%config}; bless ($self,$class); return $self; } sub connect { my $self = shift; # Configs my $c = $self->{_CONFIG}; unless (defined $self->{_DBH}){ $self->{_DBH} = DBI->connect('dbi:' . $c->{driver} . ':database=' . $c->{database} . ';host=' . $c->{host}, $c->{username}, $c->{password}, {'RaiseError' => 1}) or die "Could not connect to + database. Error message: $!"; } return $self->{_DBH}; } sub disconnect { my $self = shift; $self->{_DBH}->disconnect() if (defined $self->{_DBH}); # If it fails, it's already disconnect ... return 1; } sub prepare { my $self = shift; my $sql = shift; $self->connect() if not defined($self->{_DBH}); $self->{_STH} = $self->{_DBH}->prepare($sql); return $self->{_STH}; } sub execute { my $self = shift; my %params = @_; $self->prepare($params{sql}); $self->{_STH}->execute(@{$params{data}}); if(defined $params{method}){ my $method = $params{method}; #if( @{ $params{method_params} } ){ # return $self->{_STH}->$method( @{ $params{method_params} } ); #} else { # return $self->{_STH}->$method(); #} return $self->{_STH}->$method( @{ $params{method_params} } ); } else { return $self->{_STH}; } } sub insert { my $self = shift; my %params = @_; my @fields = $self->_GET_FIELDS($params{table}); my $data = $params{data}; my $sql = 'INSERT INTO ' . $params{table} . ' ('; my ($sql_part1, $sql_part2, @data); foreach (@fields){ if(defined($data->{$_}) && (length($data->{$_}) >= 1) && ($data->{ +$_} ne '')){ $sql_part1 .= "$_,"; $sql_part2 .= '?,'; push (@data,$data->{$_}); } } chop($sql_part1); chop($sql_part2); # to remove last , $sql_part1 .= ')'; $sql_part2 .= ')'; $sql .= $sql_part1 . ' VALUES (' . $sql_part2; return $self->DB_execute(sql => $sql, data => \@data,); } sub update { my $self = shift; my %params = @_; my @fields = $self->_GET_FIELDS($params{table}); my $data = $params{data}; my $sql = 'UPDATE ' . $params{table} . ' SET '; my @data; foreach (@fields){ if(defined($data->{$_}) && (length($data->{$_}) >= 1) && ($data->{$_} ne '') && ($_ ne $params{id_field}) ){ $sql .= $_ . ' = ?,'; push @data,$data->{$_}; } } chop($sql); # to remove last , $sql .= ' WHERE ' . $params{id_field} . ' = ?'; # where id_field = + id_value push @data, $data->{$params{id_field}}; return $self->DB_execute(sql => $sql, data => \@data,); } sub _GET_FIELDS { my $self = shift; my $table = shift; my $results = $self->execute(sql => 'SHOW COLUMNS FROM ' . $table +, method_params => 'Field'); return(keys %{$results}); } 1;
PS. As I'm still working on it - it might have some bugs an such :) O and, currently it's basically MySQL specific(because of the SQL query to find out fields in table - which I believe isn't same for all RDBMS) but I plan to support other RDBMS (possibly).

In reply to Comments on my code, advices how to improve it. by techcode

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.