DBIx::InsertHash(3)   User Contributed Perl Documentation  DBIx::InsertHash(3)



NNAAMMEE
       DBIx::InsertHash - insert/update a database record from a hash

VVEERRSSIIOONN
       This document describes DBIx::InsertHash version 0.010

       AAPPII CCHHAANNGGEE IINN 00..001100:: The order of params in insert was reversed.

SSYYNNOOPPSSIISS
           use DBIx::InsertHash;

           # insert
           DBIx::InsertHash->insert({USERNAME => 'foo',
                                     PASSWORD => 'bar',
                                    }, 'table', $dbh);

           # update
           DBIx::InsertHash->update({PASSWORD => 'foobar'},
                                    [12],'USERID = ?',
                                    'table', $dbh);

           # constructor usage
           my $dbix = DBIx::InsertHash->new(quote => 1,
                                            dbh   => $dbh,
                                            table => 'table',
                                            where => 'USERID = ?',
                                           );
           $dbix->insert($hash);
           $dbix->update($hash, [12]);

DDEESSCCRRIIPPTTIIOONN
       If you have data in a hash (which keys are matching the column names)
       and want to insert it in a database, then this is the right module for
       you.  It frees you from having to construct the SQL statement.

       It really only does a simple insert (or update) from a hash into a sin-
       gle table. For anything beyond I suggest a ORM (object-relational map-
       per), like Rose::DB::Object or DBIx::Class.

IINNTTEERRFFAACCEE
       nneeww

       Constructor (optional). Only needed to store default values or set
       quoting options.

       quote (BOOL)
           Turn quoting of column names on (off by default). This switch
           affects all column names (see quote_func below to only quote spe-
           cific names).

           If you use MySQL quoting is recommended. It is needed when column
           names clash with reserved words.

       quote_char (STRING)
           Quoting character/string (default is backtick).

       qoute_func (CODEREF)
           This function is given the column name as first (and only) parame-
           ter. It has to return a boolean, indicating if the column has to be
           quoted.

           The following example uses SQL::ReservedWords:

               my $quote = sub { SQL::ReservedWords->is_reserved($_[0]) };

               my $dbix = DBIx::InsertHash->new(quote_func => $quote);

               $dbix->insert(...);

       iinnsseerrtt

       Insert hash in database. Returns last_insert_id.

       data (HASHREF)
           Row data. The keys have to match with the column names of your ta-
           ble.  This parameter is mandatory. If an empty hashref is given, no
           record is inserted and a warning is given.

       table (STRING)
           Table name. If this parameter is missing, the object default (see
           new is used). Otherwise it dies.

       dbh (OBJECT)
           DBI database handle (you have to connect yourself). If this parame-
           ter is missing, the object default (see new) is used).  Otherwise
           it dies.

       uuppddaattee

       Update record from hash. Returns do.

       data (HASHREF)
           Row data. The keys have to match with the column names of your ta-
           ble.  This parameter is mandatory. If an empty hashref is given, no
           record is inserted and a warning is given.

       bind_values (ARRAYREF)
           Bind values for the WHERE clause. If you do not use or need them,
           just pass a false value ("undef" or empty string) or an empty
           arrayref. This parameter is optional and has no object defaults.

       where (STRING)
           Where clause (with optional placeholders "?"). If this parameter is
           missing, the object default (see new) is used. Otherwise it dies.

       table (STRING)
           Table name. If this parameter is missing, the object default (see
           new is used). Otherwise it dies.

       dbh (OBJECT)
           DBI database handle (you have to connect yourself). If this parame-
           ter is missing, the object default (see new) is used).  Otherwise
           it dies.

AAUUTTHHOORR
       Uwe Voelker, <uwe.voelker@gmx.de>



perl v5.8.8                       2007-03-09               DBIx::InsertHash(3)
