inteligent DBI

Probably it is impossible, but it would be really usefull.

I'm doing something like:

my $sth = $dbh->prepare('INSERT INTO my_table(field_1) VALUES (?)');
$sth->bind_param( 1, 'pippo', { 'ora_type' => SQLT_BIN } );
$sth->execute;

since I need to pass a parameter in the bind_param

I would really like to do something like:

$dbh->do( 'INSERT INTO my_table(field_1) VALUES (?)', [ 'pippo', {
'ora_type' => SQLT_BIN } ] )

is there a way to do this?

maybe with some DBIx

Best Regards
Marcos Rebelo

--
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
oleber [ Do, 07 April 2011 11:01 ] [ ID #2057777 ]

Re: inteligent DBI

--0016369c8ff022823904a0512306
Content-Type: text/plain; charset=UTF-8

Even if it is not possible at the moment you could always overwrite this
function and make it work this way... I suspect it might be quite a bit of
work, but certainly not impossible. After all you are programming anything
you can think of can be done the only question is if it is worth the effort
to do this.

Personally I would simply create a function for this:
sub insert {
my ( $query, $bind_params ) = [at] _;
my $sth = $dbh->prepare( $query );
$sth->bind_param( [at] {$bind_params} );
$sth->execute
.....
Return the results to the caller
}

Something like that should do the trick (the code above will most likely not
work but I assume you will understand where I am going with this...

Working with Perl in companies you will often see wrappers for DBI that do
things like what you are looking for or that provide other methods of
returning result sets (XML or JSON rather then Perl data structures) stuff
like that. I my self like to use a wrapper that simplifies the DBI interface
a bit so I basically create a DB object that offers me things like insert,
delete, update, execute, select, etc. This wrapper offers log4perl logging
capability and allows me to debug specific statements which means I can by
simply switching on tracing see exactly what query is being send to the
database. That helps a lot certainly if you are dealing with MySQL and their
rather useless way of reporting errors in a query. It also is a great way of
preventing SQL injection issues as you only need to deal with those kinds of
troubles once in the wrapper.

Personally I can say that a good wrapper around a relatively complex module
such as DBI can help a lot in saving time when you end up working with it
over and over again in different projects. The only thing is that if you are
going to be working in a larger team make sure that all team members buy
into the same wrapper and you do not end up with 12 implementations of a DBI
wrapper that all do slightly different things as you all optimized a
slightly different bit of your daily work. ;-)

Regards,

Rob

On Thu, Apr 7, 2011 at 11:01 AM, marcos rebelo <oleber [at] gmail.com> wrote:

> Probably it is impossible, but it would be really usefull.
>
> I'm doing something like:
>
> my $sth = $dbh->prepare('INSERT INTO my_table(field_1) VALUES (?)');
> $sth->bind_param( 1, 'pippo', { 'ora_type' => SQLT_BIN } );
> $sth->execute;
>
> since I need to pass a parameter in the bind_param
>
> I would really like to do something like:
>
> $dbh->do( 'INSERT INTO my_table(field_1) VALUES (?)', [ 'pippo', {
> 'ora_type' => SQLT_BIN } ] )
>
> is there a way to do this?
>
> maybe with some DBIx
>
> Best Regards
> Marcos Rebelo
>
> --
> Marcos Rebelo
> http://www.oleber.com/
> Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
> Webmaster of http://perl5notebook.oleber.com
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>

--0016369c8ff022823904a0512306--
Rob Coops [ Do, 07 April 2011 11:57 ] [ ID #2057779 ]

Re: inteligent DBI

We have a internal wrapper, The problem is that doesn't work with
Oracle ;) I'm implementing it now.

Oracle madness

Best Regards
Marcos Rebelo

On Thu, Apr 7, 2011 at 11:57, Rob Coops <rcoops [at] gmail.com> wrote:
> Even if it is not possible at the moment you could always overwrite this
> function and make it work this way... I suspect it might be quite a bit o=
f
> work, but certainly not impossible. After all you are programming anythin=
g
> you can think of can be done the only question is if it is worth the effo=
rt
> to do this.
> Personally I would simply create a function for this:
> sub insert {
> =A0my ( $query, $bind_params ) =3D [at] _;
> =A0my $sth =3D $dbh->prepare( $query );
> =A0$sth->bind_param( [at] {$bind_params} );
> =A0$sth->execute
> =A0.....
> =A0Return the results to the caller
> }
> Something like that should do the trick (the code above will most likely =
not
> work but I assume you will understand where I am going with this...
> Working with Perl in companies you will often see wrappers for DBI that d=
o
> things like what you are looking for or that provide other methods of
> returning result sets (XML or JSON rather then Perl data structures) stuf=
f
> like that. I my self like to use a wrapper that simplifies the DBI interf=
ace
> a bit so I basically create a DB object that offers me things like insert=
,
> delete, update, execute, select, etc. This wrapper offers log4perl loggin=
g
> capability and allows me to debug specific statements which means I can b=
y
> simply switching on tracing see exactly what query is being send to the
> database. That helps a lot certainly if you are dealing with MySQL and th=
eir
> rather useless way of reporting errors in a query. It also is a great way=
of
> preventing SQL injection issues as you only need to deal with those kinds=
of
> troubles once in the wrapper.
> Personally I can say that a good wrapper around a relatively complex modu=
le
> such as DBI can help a lot in saving time when you end up working with it
> over and over again in different projects. The only thing is that if you =
are
> going to be working in a larger team make sure that all team members buy
> into the same wrapper and you do not end up with 12=A0implementations=A0o=
f a DBI
> wrapper that all do slightly different things as you all=A0optimized=A0a
> slightly different bit of your daily work. ;-)
> Regards,
> Rob
> On Thu, Apr 7, 2011 at 11:01 AM, marcos rebelo <oleber [at] gmail.com> wrote:
>>
>> Probably it is impossible, but it would be really usefull.
>>
>> I'm doing something like:
>>
>> my $sth =3D $dbh->prepare('INSERT INTO my_table(field_1) VALUES (?)');
>> $sth->bind_param( 1, 'pippo', { 'ora_type' =3D> SQLT_BIN } );
>> $sth->execute;
>>
>> since I need to pass a parameter in the bind_param
>>
>> I would really like to do something like:
>>
>> $dbh->do( 'INSERT INTO my_table(field_1) VALUES (?)', [ 'pippo', {
>> 'ora_type' =3D> SQLT_BIN } ] )
>>
>> is there a way to do this?
>>
>> maybe with some DBIx
>>
>> Best Regards
>> Marcos Rebelo
>>
>> --
>> Marcos Rebelo
>> http://www.oleber.com/
>> Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers=
/
>> Webmaster of http://perl5notebook.oleber.com
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
>> For additional commands, e-mail: beginners-help [at] perl.org
>> http://learn.perl.org/
>>
>>
>
>



--
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
oleber [ Do, 07 April 2011 12:17 ] [ ID #2057780 ]
Perl » gmane.comp.lang.perl.beginners » inteligent DBI

Vorheriges Thema: How to recognize apache authentication
Nächstes Thema: Order in which keys are stored in a hash